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