Pxianshi.php 8.7 KB

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