Presell.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * 预售活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 数据层模型
  19. */
  20. class Presell extends BaseModel {
  21. public $page_info;
  22. const PRESELL_STATE_CANCEL = 0;
  23. const PRESELL_STATE_TO_BEGIN = 1;
  24. const PRESELL_STATE_NORMAL = 2;
  25. const PRESELL_STATE_END = 3;
  26. private $presell_state_array = array(
  27. self::PRESELL_STATE_CANCEL => '已取消',
  28. self::PRESELL_STATE_TO_BEGIN => '待开始',
  29. self::PRESELL_STATE_NORMAL => '进行中',
  30. self::PRESELL_STATE_END => '已结束'
  31. );
  32. /**
  33. * 读取预售列表
  34. * @access public
  35. * @author csdeshang
  36. * @param array $condition 查询条件
  37. * @param int $pagesize 分页数
  38. * @param string $order 排序
  39. * @param string $field 所需字段
  40. * @return array 预售列表
  41. */
  42. public function getPresellList($condition, $pagesize = null, $order = 'presell_id desc', $field = '*') {
  43. if($pagesize){
  44. $res = Db::name('presell')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  45. $presell_list = $res->items();
  46. $this->page_info = $res;
  47. return $presell_list;
  48. }else{
  49. return Db::name('presell')->field($field)->where($condition)->order($order)->select()->toArray();
  50. }
  51. }
  52. /**
  53. * 读取预售列表
  54. * @access public
  55. * @author csdeshang
  56. * @param array $condition 查询条件
  57. * @param int $pagesize 分页数
  58. * @param string $order 排序
  59. * @param string $field 所需字段
  60. * @return array 预售列表
  61. */
  62. public function getOnlinePresellList($condition, $pagesize = null, $order = 'presell_id desc', $field = '*') {
  63. $condition[]=array('presell_state','=',self::PRESELL_STATE_NORMAL);
  64. $condition[]=array('presell_end_time','>',TIMESTAMP);
  65. $presell_list = $this->getPresellList($condition, $pagesize, $order, $field);
  66. return $presell_list;
  67. }
  68. /**
  69. * 根据条件读取预售信息
  70. * @access public
  71. * @author csdeshang
  72. * @param array $condition 查询条件
  73. * @return array 预售信息
  74. */
  75. public function getPresellInfo($condition) {
  76. $presell_info = Db::name('presell')->where($condition)->find();
  77. return $presell_info;
  78. }
  79. /**
  80. * 根据预售编号读取预售信息
  81. * @access public
  82. * @author csdeshang
  83. * @param array $presell_id 预售活动编号
  84. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  85. * @return array 预售信息
  86. */
  87. public function getPresellInfoByID($presell_id, $store_id = 0) {
  88. if (intval($presell_id) <= 0) {
  89. return null;
  90. }
  91. $condition = array();
  92. $condition[] = array('presell_id','=',$presell_id);
  93. $presell_info = $this->getPresellInfo($condition);
  94. if ($store_id > 0 && $presell_info['store_id'] != $store_id) {
  95. return null;
  96. } else {
  97. return $presell_info;
  98. }
  99. }
  100. public function getOnlinePresellInfoByID($presell_id){
  101. if (intval($presell_id) <= 0) {
  102. return null;
  103. }
  104. $condition = array();
  105. $condition[] = array('presell_id','=',$presell_id);
  106. $condition[] = array('presell_state','=',self::PRESELL_STATE_NORMAL);
  107. $condition[] = array('presell_end_time','>',TIMESTAMP);
  108. $presell_info = $this->getPresellInfo($condition);
  109. return $presell_info;
  110. }
  111. /**
  112. * 预售状态数组
  113. * @access public
  114. * @author csdeshang
  115. * @return type
  116. */
  117. public function getPresellStateArray() {
  118. return $this->presell_state_array;
  119. }
  120. /**
  121. * 增加
  122. * @access public
  123. * @author csdeshang
  124. * @param array $data 数据
  125. * @return type
  126. */
  127. public function addPresell($data) {
  128. $flag= Db::name('presell')->insertGetId($data);
  129. if($flag){
  130. // 发布预售锁定商品
  131. $this->_lockGoods($data['goods_commonid'],$data['goods_id']);
  132. }
  133. return $flag;
  134. }
  135. /**
  136. * 编辑更新
  137. * @param type $update 更新数据
  138. * @param type $condition 条件
  139. * @return type
  140. */
  141. public function editPresell($update, $condition) {
  142. $goods_ids=Db::name('presell')->where($condition)->column('goods_id');
  143. foreach($goods_ids as $goods_id){
  144. $this->_dGoodsPresellCache($goods_id);
  145. }
  146. return Db::name('presell')->where($condition)->update($update);
  147. }
  148. /**
  149. * 指定预售活动结束,参团成功的继续参团,不成功的保持默认.
  150. * @access public
  151. * @author csdeshang
  152. * @param type $condition
  153. * @return type
  154. */
  155. public function endPresell($condition=array()) {
  156. $condition[]=array('presell_state','=',self::PRESELL_STATE_NORMAL);
  157. $goods_commonid=Db::name('presell')->where($condition)->column('goods_commonid');
  158. $goods_id=Db::name('presell')->where($condition)->column('goods_id');
  159. $data['presell_state'] = self::PRESELL_STATE_END;
  160. $flag= Db::name('presell')->where($condition)->update($data);
  161. if($flag){
  162. if(!empty($goods_commonid)){
  163. $this->_unlockGoods($goods_commonid,$goods_id);
  164. }
  165. }
  166. return $flag;
  167. }
  168. /**
  169. * 取消预售活动
  170. * @access public
  171. * @author csdeshang
  172. * @param type $condition 条件
  173. * @return type
  174. */
  175. public function cancelPresell($condition) {
  176. $goods_commonid = Db::name('presell')->where($condition)->column('goods_commonid');
  177. $goods_id = Db::name('presell')->where($condition)->column('goods_id');
  178. $update = array();
  179. $update['presell_state'] = self::PRESELL_STATE_CANCEL;
  180. $flag= $this->editPresell($update, $condition);
  181. if($flag){
  182. if(!empty($goods_commonid)){
  183. $this->_unlockGoods($goods_commonid,$goods_id);
  184. }
  185. }
  186. return $flag;
  187. }
  188. /**
  189. * 删除预售活动
  190. * @access public
  191. * @author csdeshang
  192. * @param array $condition 检索条件
  193. * @return boolean
  194. */
  195. public function delPresell($condition) {
  196. return Db::name('presell')->where($condition)->delete();
  197. }
  198. /**
  199. * 锁定商品
  200. * @access private
  201. * @author csdeshang
  202. * @param type $goods_commonid 商品编号
  203. */
  204. private function _lockGoods($goods_commonid,$goods_id)
  205. {
  206. $condition = array();
  207. $condition[] = array('goods_commonid','=',$goods_commonid);
  208. $goods_model = model('goods');
  209. $goods_model->editGoodsCommonLock($condition);
  210. $condition = array();
  211. $condition[] = array('goods_id','=',$goods_id);
  212. $goods_model->editGoodsLock($condition);
  213. }
  214. /**
  215. * 解锁商品
  216. * @access private
  217. * @author csdeshang
  218. * @param type $goods_commonid 商品编号ID
  219. */
  220. private function _unlockGoods($goods_commonid,$goods_id)
  221. {
  222. $goods_model = model('goods');
  223. $goods_model->editGoodsUnlock(array(array('goods_id' ,'in', $goods_id)));
  224. $temp=Db::name('goods')->where(array(array('goods_id','in',$goods_id),array('goods_lock','=',1)))->column('goods_commonid');
  225. if(!empty($temp)){
  226. $goods_commonid=array_diff($goods_commonid,$temp);
  227. }
  228. if(!empty($goods_commonid)){
  229. $goods_model->editGoodsCommonUnlock(array(array('goods_commonid' ,'in', $goods_commonid)));
  230. }
  231. }
  232. /**
  233. * 获取预售是否可编辑状态
  234. * @access public
  235. * @author csdeshang
  236. * @param type $presell_info 预售信息
  237. * @return boolean
  238. */
  239. public function getPresellBtn($presell_info) {
  240. if (!$presell_info) {
  241. return false;
  242. }
  243. if ($presell_info['presell_state'] == self::PRESELL_STATE_TO_BEGIN && $presell_info['presell_start_time'] > TIMESTAMP) {
  244. $presell_info['editable'] = true;
  245. } else {
  246. $presell_info['editable'] = false;
  247. }
  248. return $presell_info;
  249. }
  250. /**
  251. * 获取状态文字
  252. * @access public
  253. * @author csdeshang
  254. * @param type $presell_info 预售信息
  255. * @return boolean
  256. */
  257. public function getPresellStateText($presell_info) {
  258. if (!$presell_info) {
  259. return false;
  260. }
  261. $presell_state_text = $this->presell_state_array[$presell_info['presell_state']];
  262. return $presell_state_text;
  263. }
  264. /**
  265. * 根据商品编号查询是否有可用预售活动,如果有返回预售信息,没有返回null
  266. * @param type $goods_id 商品id
  267. * @return array
  268. */
  269. public function getPresellInfoByGoodsID($goods_id) {
  270. $info = $this->_rGoodsPresellCache($goods_id);
  271. if (empty($info)) {
  272. $condition = array();
  273. $condition[] = array('goods_id','=',$goods_id);
  274. $condition[] = array('presell_state','=',self::PRESELL_STATE_NORMAL);
  275. $condition[] = array('presell_end_time','>',TIMESTAMP);
  276. $presell_info = $this->getPresellInfo($condition);
  277. //序列化存储到缓存
  278. $info['info'] = serialize($presell_info);
  279. $this->_wGoodsPresellCache($goods_id, $info);
  280. }
  281. $presell_goods_info = unserialize($info['info']);
  282. if (!empty($presell_goods_info) && ($presell_goods_info['presell_state']!=2 || $presell_goods_info['presell_start_time'] > TIMESTAMP || $presell_goods_info['presell_end_time'] < TIMESTAMP)) {
  283. $presell_goods_info = array();
  284. }
  285. return $presell_goods_info;
  286. }
  287. /**
  288. * 读取商品预售缓存
  289. * @access public
  290. * @author csdeshang
  291. * @param type $goods_id 商品id
  292. * @return type
  293. */
  294. private function _rGoodsPresellCache($goods_id) {
  295. return rcache($goods_id, 'goods_presell');
  296. }
  297. /**
  298. * 写入商品预售缓存
  299. * @access public
  300. * @author csdeshang
  301. * @param type $goods_id ID
  302. * @param type $info 信息
  303. * @return type
  304. */
  305. private function _wGoodsPresellCache($goods_id, $info) {
  306. return wcache($goods_id, $info, 'goods_presell');
  307. }
  308. /**
  309. * 删除商品预售缓存
  310. * @access public
  311. * @author csdeshang
  312. * @param int $goods_id 商品ID
  313. * @return boolean
  314. */
  315. public function _dGoodsPresellCache($goods_id) {
  316. return dcache($goods_id, 'goods_presell');
  317. }
  318. }