p_xianshi.model.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * 限时折扣活动模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
  11. class p_xianshiModel extends Model{
  12. const XIANSHI_STATE_NORMAL = 1;
  13. const XIANSHI_STATE_CLOSE = 2;
  14. const XIANSHI_STATE_CANCEL = 3;
  15. private $xianshi_state_array = array(
  16. 0 => '全部',
  17. self::XIANSHI_STATE_NORMAL => '正常',
  18. self::XIANSHI_STATE_CLOSE => '已结束',
  19. self::XIANSHI_STATE_CANCEL => '管理员关闭'
  20. );
  21. public function __construct(){
  22. parent::__construct('p_xianshi');
  23. }
  24. /**
  25. * 读取限时折扣列表
  26. * @param array $condition 查询条件
  27. * @param int $page 分页数
  28. * @param string $order 排序
  29. * @param string $field 所需字段
  30. * @return array 限时折扣列表
  31. *
  32. */
  33. public function getXianshiList($condition, $page=null, $order='', $field='*') {
  34. $xianshi_list = $this->field($field)->where($condition)->page($page)->order($order)->select();
  35. if(!empty($xianshi_list)) {
  36. for($i =0, $j = count($xianshi_list); $i < $j; $i++) {
  37. $xianshi_list[$i] = $this->getXianshiExtendInfo($xianshi_list[$i]);
  38. }
  39. }
  40. return $xianshi_list;
  41. }
  42. /**
  43. * 根据条件读取限制折扣信息
  44. * @param array $condition 查询条件
  45. * @return array 限时折扣信息
  46. *
  47. */
  48. public function getXianshiInfo($condition) {
  49. $xianshi_info = $this->where($condition)->find();
  50. $xianshi_info = $this->getXianshiExtendInfo($xianshi_info);
  51. return $xianshi_info;
  52. }
  53. /**
  54. * 根据限时折扣编号读取限制折扣信息
  55. * @param array $xianshi_id 限制折扣活动编号
  56. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  57. * @return array 限时折扣信息
  58. *
  59. */
  60. public function getXianshiInfoByID($xianshi_id, $store_id = 0) {
  61. if(intval($xianshi_id) <= 0) {
  62. return null;
  63. }
  64. $condition = array();
  65. $condition['xianshi_id'] = $xianshi_id;
  66. $xianshi_info = $this->getXianshiInfo($condition);
  67. if($store_id > 0 && $xianshi_info['store_id'] != $store_id) {
  68. return null;
  69. } else {
  70. return $xianshi_info;
  71. }
  72. }
  73. /**
  74. * 限时折扣状态数组
  75. *
  76. */
  77. public function getXianshiStateArray() {
  78. return $this->xianshi_state_array;
  79. }
  80. /*
  81. * 增加
  82. * @param array $param
  83. * @return bool
  84. *
  85. */
  86. public function addXianshi($param){
  87. $param['state'] = self::XIANSHI_STATE_NORMAL;
  88. $ret = $this->insert($param);
  89. $this->_wVersionCache();
  90. return $ret;
  91. }
  92. /*
  93. * 更新
  94. * @param array $update
  95. * @param array $condition
  96. * @return bool
  97. *
  98. */
  99. public function editXianshi($update, $condition){
  100. $ret = $this->where($condition)->update($update);
  101. $this->_wVersionCache();
  102. return $ret;
  103. }
  104. /*
  105. * 删除限时折扣活动,同时删除限时折扣商品
  106. * @param array $condition
  107. * @return bool
  108. *
  109. */
  110. public function delXianshi($condition){
  111. $xianshi_list = $this->getXianshiList($condition);
  112. $xianshi_id_string = '';
  113. if(!empty($xianshi_list)) {
  114. foreach ($xianshi_list as $value) {
  115. $xianshi_id_string .= $value['xianshi_id'] . ',';
  116. }
  117. }
  118. //删除限时折扣商品
  119. if($xianshi_id_string !== '') {
  120. $model_xianshi_goods = Model('p_xianshi_goods');
  121. $model_xianshi_goods->delXianshiGoods(array('xianshi_id'=>array('in', $xianshi_id_string)));
  122. }
  123. $ret = $this->where($condition)->delete();
  124. $this->_wVersionCache();
  125. return $ret;
  126. }
  127. /*
  128. * 取消限时折扣活动,同时取消限时折扣商品
  129. * @param array $condition
  130. * @return bool
  131. *
  132. */
  133. public function cancelXianshi($condition){
  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. $update = array();
  142. $update['state'] = self::XIANSHI_STATE_CANCEL;
  143. //删除限时折扣商品
  144. if($xianshi_id_string !== '') {
  145. $model_xianshi_goods = Model('p_xianshi_goods');
  146. $model_xianshi_goods->editXianshiGoods($update, array('xianshi_id'=>array('in', $xianshi_id_string)));
  147. }
  148. return $this->editXianshi($update, $condition);
  149. }
  150. /**
  151. * 获取限时折扣扩展信息,包括状态文字和是否可编辑状态
  152. * @param array $xianshi_info
  153. * @return string
  154. *
  155. */
  156. public function getXianshiExtendInfo($xianshi_info) {
  157. if($xianshi_info['end_time'] > time()) {
  158. $xianshi_info['xianshi_state_text'] = $this->xianshi_state_array[$xianshi_info['state']];
  159. } else {
  160. $xianshi_info['xianshi_state_text'] = '已结束';
  161. }
  162. if($xianshi_info['state'] == self::XIANSHI_STATE_NORMAL && $xianshi_info['end_time'] > time()) {
  163. $xianshi_info['editable'] = true;
  164. } else {
  165. $xianshi_info['editable'] = false;
  166. }
  167. return $xianshi_info;
  168. }
  169. /**
  170. * 过期修改状态
  171. */
  172. public function editExpireXianshi($condition) {
  173. $condition['end_time'] = array('lt', time());
  174. // 更新商品促销价格
  175. $xianshigoods_list = Model('p_xianshi_goods')->getXianshiGoodsList($condition);
  176. if (!empty($xianshigoods_list)) {
  177. $goodsid_array = array();
  178. foreach ($xianshigoods_list as $val) {
  179. $goodsid_array[] = $val['goods_id'];
  180. }
  181. // 更新商品促销价格,需要考虑抢购是否在进行中
  182. QueueClient::push('updateGoodsPromotionPriceByGoodsId', $goodsid_array);
  183. }
  184. $condition['state'] = self::XIANSHI_STATE_NORMAL;
  185. $updata = array();
  186. $updata['state'] = self::XIANSHI_STATE_CLOSE;
  187. $this->editXianshi($updata, $condition);
  188. return true;
  189. }
  190. private function _wVersionCache()
  191. {
  192. $publisher = new message\publisher();
  193. $publisher->modify_activity_limit();
  194. }
  195. }