Pxianshi.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * 秒杀活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  13. * 网站地址: https://www.valimart.net/
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 数据层模型
  18. */
  19. class Pxianshi extends BaseModel {
  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. if($pagesize){
  42. $res = Db::name('pxianshi')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  43. $this->page_info=$res;
  44. $xianshi_list= $res->items();
  45. }else{
  46. $xianshi_list= Db::name('pxianshi')->field($field)->where($condition)->order($order)->select()->toArray();
  47. }
  48. if (!empty($xianshi_list)) {
  49. for ($i = 0, $j = count($xianshi_list); $i < $j; $i++) {
  50. $xianshi_list[$i] = $this->getXianshiExtendInfo($xianshi_list[$i]);
  51. }
  52. }
  53. return $xianshi_list;
  54. }
  55. /**
  56. * 根据条件读取限制折扣信息
  57. * @access public
  58. * @author csdeshang
  59. * @param type $condition 条件
  60. * @return type
  61. */
  62. public function getXianshiInfo($condition) {
  63. $xianshi_info = Db::name('pxianshi')->where($condition)->find();
  64. $xianshi_info = $this->getXianshiExtendInfo($xianshi_info);
  65. return $xianshi_info;
  66. }
  67. /**
  68. * 根据秒杀编号读取限制折扣信息
  69. * @access public
  70. * @author csdeshang
  71. * @param type $xianshi_id 限制折扣活动编号
  72. * @param type $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  73. * @return array
  74. */
  75. public function getXianshiInfoByID($xianshi_id, $store_id = 0) {
  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. return $this->xianshi_state_array;
  96. }
  97. /**
  98. * 增加
  99. * @access public
  100. * @author csdeshang
  101. * @param array $data 数据
  102. * @return bool
  103. */
  104. public function addXianshi($data) {
  105. $data['xianshi_state'] = self::XIANSHI_STATE_NORMAL;
  106. return Db::name('pxianshi')->insertGetId($data);
  107. }
  108. /**
  109. * 更新
  110. * @access public
  111. * @author csdeshang
  112. * @param type $update 数据
  113. * @param type $condition 条件
  114. * @return type
  115. */
  116. public function editXianshi($update, $condition) {
  117. return Db::name('pxianshi')->where($condition)->update($update);
  118. }
  119. /**
  120. * 删除秒杀活动,同时删除秒杀商品
  121. * @access public
  122. * @author csdeshang
  123. * @param type $condition 条件
  124. * @return bool
  125. */
  126. public function delXianshi($condition) {
  127. $xianshi_list = $this->getXianshiList($condition);
  128. $xianshi_id_string = '';
  129. if (!empty($xianshi_list)) {
  130. foreach ($xianshi_list as $value) {
  131. $xianshi_id_string .= $value['xianshi_id'] . ',';
  132. }
  133. }
  134. //删除秒杀商品
  135. if ($xianshi_id_string !== '') {
  136. $xianshigoods_model = model('pxianshigoods');
  137. $xianshigoods_model->delXianshigoods(array(array('xianshi_id','in', $xianshi_id_string)));
  138. }
  139. return Db::name('pxianshi')->where($condition)->delete();
  140. }
  141. /**
  142. * 取消秒杀活动,同时取消秒杀商品
  143. * @access public
  144. * @author csdeshang
  145. * @param type $condition 条件
  146. * @return type
  147. */
  148. public function cancelXianshi($condition) {
  149. $xianshi_list = $this->getXianshiList($condition);
  150. $xianshi_id_string = '';
  151. if (!empty($xianshi_list)) {
  152. foreach ($xianshi_list as $value) {
  153. $xianshi_id_string .= $value['xianshi_id'] . ',';
  154. }
  155. }
  156. $update = array();
  157. $update['xianshi_state'] = self::XIANSHI_STATE_CANCEL;
  158. //删除秒杀商品
  159. if ($xianshi_id_string !== '') {
  160. $xianshigoods_model = model('pxianshigoods');
  161. $condition = array();
  162. $condition[] = array('xianshi_id','in',$xianshi_id_string);
  163. $xianshigoods_model->editXianshigoods(array('xianshigoods_state'=>self::XIANSHI_STATE_CANCEL),$condition);
  164. }
  165. return $this->editXianshi($update, $condition);
  166. }
  167. /**
  168. * 获取秒杀扩展信息,包括状态文字和是否可编辑状态
  169. * @access public
  170. * @author csdeshang
  171. * @param type $xianshi_info 秒杀信息
  172. * @return boolean
  173. */
  174. public function getXianshiExtendInfo($xianshi_info) {
  175. if ($xianshi_info['xianshi_end_time'] > TIMESTAMP) {
  176. $xianshi_info['xianshi_state_text'] = $this->xianshi_state_array[$xianshi_info['xianshi_state']];
  177. } else {
  178. $xianshi_info['xianshi_state_text'] = '已结束';
  179. }
  180. if ($xianshi_info['xianshi_state'] == self::XIANSHI_STATE_NORMAL && $xianshi_info['xianshi_end_time'] > TIMESTAMP) {
  181. $xianshi_info['editable'] = true;
  182. } else {
  183. $xianshi_info['editable'] = false;
  184. }
  185. return $xianshi_info;
  186. }
  187. /**
  188. * 编辑过期修改状态
  189. * @access public
  190. * @author csdeshang
  191. * @param type $condition
  192. * @return boolean
  193. */
  194. public function editExpireXianshi($condition) {
  195. $condition[] = array('xianshi_end_time','<', TIMESTAMP);
  196. // 更新商品促销价格
  197. $xianshigoods_list = model('pxianshigoods')->getXianshigoodsList(array(array('xianshigoods_end_time','<', TIMESTAMP)));
  198. if (!empty($xianshigoods_list)) {
  199. $goodsid_array = array();
  200. foreach ($xianshigoods_list as $val) {
  201. $goodsid_array[] = $val['goods_id'];
  202. }
  203. // 更新商品促销价格,需要考虑抢购是否在进行中
  204. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'updateGoodsPromotionPriceByGoodsId','cron_value'=>serialize($goodsid_array)));
  205. }
  206. $condition[] = array('xianshi_state','=',self::XIANSHI_STATE_NORMAL);
  207. $updata = array();
  208. $update['xianshi_state'] = self::XIANSHI_STATE_CLOSE;
  209. $result = $this->editXianshi($update, $condition);
  210. if ($result) {
  211. foreach ($xianshigoods_list as $value) {
  212. $this->_unlockGoods($value['goods_commonid']);
  213. }
  214. }
  215. return true;
  216. }
  217. /**
  218. * 解锁商品
  219. * @access private
  220. * @author csdeshang
  221. * @param type $goods_commonid 商品编号ID
  222. */
  223. private function _unlockGoods($goods_commonid)
  224. {
  225. $goods_model = model('goods');
  226. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  227. $goods_model->editGoodsUnlock(array('goods_commonid' => $goods_commonid));
  228. // 添加对列 更新商品促销价格
  229. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'updateGoodsPromotionPriceByGoodsCommonId','cron_value'=>serialize($goods_commonid)));
  230. }
  231. }