Pxianshi.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * 秒杀活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 数据层模型
  17. */
  18. class Pxianshi extends BaseModel
  19. {
  20. public $page_info;
  21. const XIANSHI_STATE_NORMAL = 1;
  22. const XIANSHI_STATE_CLOSE = 2;
  23. const XIANSHI_STATE_CANCEL = 3;
  24. private $xianshi_state_array = array(
  25. 0 => '全部',
  26. self::XIANSHI_STATE_NORMAL => '正常',
  27. self::XIANSHI_STATE_CLOSE => '已结束',
  28. self::XIANSHI_STATE_CANCEL => '管理员关闭'
  29. );
  30. /**
  31. * 读取秒杀列表
  32. * @access public
  33. * @author csdeshang
  34. * @param type $condition 条件
  35. * @param type $pagesize 分页
  36. * @param type $order 排序
  37. * @param type $field 字段
  38. * @return type
  39. */
  40. public function getXianshiList($condition, $pagesize = null, $order = '', $field = '*')
  41. {
  42. if ($pagesize) {
  43. $res = Db::name('pxianshi')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  44. $this->page_info = $res;
  45. $xianshi_list = $res->items();
  46. } else {
  47. $xianshi_list = Db::name('pxianshi')->field($field)->where($condition)->order($order)->select()->toArray();
  48. }
  49. if (!empty($xianshi_list)) {
  50. for ($i = 0, $j = count($xianshi_list); $i < $j; $i++) {
  51. $xianshi_list[$i] = $this->getXianshiExtendInfo($xianshi_list[$i]);
  52. }
  53. }
  54. return $xianshi_list;
  55. }
  56. /**
  57. * 根据条件读取限制折扣信息
  58. * @access public
  59. * @author csdeshang
  60. * @param type $condition 条件
  61. * @return type
  62. */
  63. public function getXianshiInfo($condition)
  64. {
  65. $xianshi_info = Db::name('pxianshi')->where($condition)->find();
  66. $xianshi_info = $this->getXianshiExtendInfo($xianshi_info);
  67. return $xianshi_info;
  68. }
  69. /**
  70. * 根据秒杀编号读取限制折扣信息
  71. * @access public
  72. * @author csdeshang
  73. * @param type $xianshi_id 限制折扣活动编号
  74. * @param type $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  75. * @return array
  76. */
  77. public function getXianshiInfoByID($xianshi_id, $store_id = 0)
  78. {
  79. if (intval($xianshi_id) <= 0) {
  80. return null;
  81. }
  82. $condition = array();
  83. $condition[] = array('xianshi_id', '=', $xianshi_id);
  84. $xianshi_info = $this->getXianshiInfo($condition);
  85. if ($store_id > 0 && $xianshi_info['store_id'] != $store_id) {
  86. return null;
  87. } else {
  88. return $xianshi_info;
  89. }
  90. }
  91. /**
  92. * 秒杀状态数组
  93. * @access public
  94. * @author csdeshang
  95. * @return type
  96. */
  97. public function getXianshiStateArray()
  98. {
  99. return $this->xianshi_state_array;
  100. }
  101. /**
  102. * 增加
  103. * @access public
  104. * @author csdeshang
  105. * @param array $data 数据
  106. * @return bool
  107. */
  108. public function addXianshi($data)
  109. {
  110. $data['xianshi_state'] = self::XIANSHI_STATE_NORMAL;
  111. return Db::name('pxianshi')->insertGetId($data);
  112. }
  113. /**
  114. * 更新
  115. * @access public
  116. * @author csdeshang
  117. * @param type $update 数据
  118. * @param type $condition 条件
  119. * @return type
  120. */
  121. public function editXianshi($update, $condition)
  122. {
  123. return Db::name('pxianshi')->where($condition)->update($update);
  124. }
  125. /**
  126. * 删除秒杀活动,同时删除秒杀商品
  127. * @access public
  128. * @author csdeshang
  129. * @param type $condition 条件
  130. * @return bool
  131. */
  132. public function delXianshi($condition)
  133. {
  134. $xianshi_list = $this->getXianshiList($condition);
  135. $xianshi_id_string = '';
  136. if (!empty($xianshi_list)) {
  137. foreach ($xianshi_list as $value) {
  138. $xianshi_id_string .= $value['xianshi_id'] . ',';
  139. }
  140. }
  141. //删除秒杀商品
  142. if ($xianshi_id_string !== '') {
  143. $xianshigoods_model = model('pxianshigoods');
  144. $xianshigoods_model->delXianshigoods(array(array('xianshi_id', 'in', $xianshi_id_string)));
  145. }
  146. return Db::name('pxianshi')->where($condition)->delete();
  147. }
  148. /**
  149. * 取消秒杀活动,同时取消秒杀商品
  150. * @access public
  151. * @author csdeshang
  152. * @param type $condition 条件
  153. * @return type
  154. */
  155. public function cancelXianshi($condition)
  156. {
  157. $xianshi_list = $this->getXianshiList($condition);
  158. $xianshi_id_string = '';
  159. if (!empty($xianshi_list)) {
  160. foreach ($xianshi_list as $value) {
  161. $xianshi_id_string .= $value['xianshi_id'] . ',';
  162. }
  163. }
  164. $update = array();
  165. $update['xianshi_state'] = self::XIANSHI_STATE_CANCEL;
  166. //删除秒杀商品
  167. if ($xianshi_id_string !== '') {
  168. $xianshigoods_model = model('pxianshigoods');
  169. $condition = array();
  170. $condition[] = array('xianshi_id', 'in', $xianshi_id_string);
  171. $xianshigoods_model->editXianshigoods(array('xianshigoods_state' => self::XIANSHI_STATE_CANCEL), $condition);
  172. }
  173. return $this->editXianshi($update, $condition);
  174. }
  175. /**
  176. * 获取秒杀扩展信息,包括状态文字和是否可编辑状态
  177. * @access public
  178. * @author csdeshang
  179. * @param type $xianshi_info 秒杀信息
  180. * @return boolean
  181. */
  182. public function getXianshiExtendInfo($xianshi_info)
  183. {
  184. if ($xianshi_info['xianshi_end_time'] > TIMESTAMP) {
  185. $xianshi_info['xianshi_state_text'] = $this->xianshi_state_array[$xianshi_info['xianshi_state']];
  186. } else {
  187. $xianshi_info['xianshi_state_text'] = '已结束';
  188. }
  189. if ($xianshi_info['xianshi_state'] == self::XIANSHI_STATE_NORMAL && $xianshi_info['xianshi_end_time'] > TIMESTAMP) {
  190. $xianshi_info['editable'] = true;
  191. } else {
  192. $xianshi_info['editable'] = false;
  193. }
  194. return $xianshi_info;
  195. }
  196. /**
  197. * 编辑过期修改状态
  198. * @access public
  199. * @author csdeshang
  200. * @param type $condition
  201. * @return boolean
  202. */
  203. public function editExpireXianshi($condition)
  204. {
  205. $condition[] = array('xianshi_end_time', '<', TIMESTAMP);
  206. // 更新商品促销价格
  207. $xianshigoods_list = model('pxianshigoods')->getXianshigoodsList(array(array('xianshigoods_end_time', '<', TIMESTAMP)));
  208. if (!empty($xianshigoods_list)) {
  209. $goodsid_array = array();
  210. foreach ($xianshigoods_list as $val) {
  211. $goodsid_array[] = $val['goods_id'];
  212. }
  213. // 更新商品促销价格,需要考虑抢购是否在进行中
  214. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'updateGoodsPromotionPriceByGoodsId', 'cron_value' => serialize($goodsid_array)));
  215. }
  216. $condition[] = array('xianshi_state', '=', self::XIANSHI_STATE_NORMAL);
  217. $updata = array();
  218. $update['xianshi_state'] = self::XIANSHI_STATE_CLOSE;
  219. $result = $this->editXianshi($update, $condition);
  220. if ($result) {
  221. foreach ($xianshigoods_list as $value) {
  222. $this->_unlockGoods($value['goods_commonid']);
  223. }
  224. }
  225. return true;
  226. }
  227. /**
  228. * 解锁商品
  229. * @access private
  230. * @author csdeshang
  231. * @param type $goods_commonid 商品编号ID
  232. */
  233. private function _unlockGoods($goods_commonid)
  234. {
  235. $goods_model = model('goods');
  236. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  237. $goods_model->editGoodsUnlock(array('goods_commonid' => $goods_commonid));
  238. // 添加对列 更新商品促销价格
  239. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'updateGoodsPromotionPriceByGoodsCommonId', 'cron_value' => serialize($goods_commonid)));
  240. }
  241. }