p_mansong.model.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_mansongModel extends Model
  12. {
  13. const MANSONG_STATE_NORMAL = 1;
  14. const MANSONG_STATE_CLOSE = 2;
  15. const MANSONG_STATE_CANCEL = 3;
  16. private $mansong_state_array = array(
  17. 0 => '全部',
  18. self::MANSONG_STATE_NORMAL => '正常',
  19. self::MANSONG_STATE_CLOSE => '已结束',
  20. self::MANSONG_STATE_CANCEL => '管理员关闭'
  21. );
  22. public function __construct(){
  23. parent::__construct('p_mansong');
  24. }
  25. /**
  26. * 读取满即送列表
  27. * @param array $condition 查询条件
  28. * @param int $page 分页数
  29. * @param string $order 排序
  30. * @param string $field 所需字段
  31. * @return array 限时折扣列表
  32. *
  33. */
  34. public function getMansongList($condition, $page=null, $order='', $field='*', $limit = 0) {
  35. $mansong_list = $this->field($field)->where($condition)->limit($limit)->page($page)->order($order)->select();
  36. if(!empty($mansong_list)) {
  37. for($i =0, $j = count($mansong_list); $i < $j; $i++) {
  38. $mansong_list[$i] = $this->getMansongExtendInfo($mansong_list[$i]);
  39. }
  40. }
  41. return $mansong_list;
  42. }
  43. /**
  44. * 获取店铺新满即送活动开始时间限制
  45. *
  46. */
  47. public function getMansongNewStartTime($store_id) {
  48. if(empty($store_id)) {
  49. return null;
  50. }
  51. $condition = array();
  52. $condition['store_id'] = $store_id;
  53. $condition['state'] = self::MANSONG_STATE_NORMAL;
  54. $mansong_list = $this->getMansongList($condition, null, 'end_time desc');
  55. return $mansong_list[0]['end_time'];
  56. }
  57. /**
  58. * 根据条件读满即送信息
  59. * @param array $condition 查询条件
  60. * @return array 限时折扣信息
  61. *
  62. */
  63. public function getMansongInfo($condition) {
  64. $mansong_info = $this->where($condition)->find();
  65. $mansong_info = $this->getMansongExtendInfo($mansong_info);
  66. return $mansong_info;
  67. }
  68. /**
  69. * 根据满即送编号读取信息
  70. * @param array $mansong_id 限制折扣活动编号
  71. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  72. * @return array 限时折扣信息
  73. *
  74. */
  75. public function getMansongInfoByID($mansong_id, $store_id = 0) {
  76. if(intval($mansong_id) <= 0) {
  77. return null;
  78. }
  79. $condition = array();
  80. $condition['mansong_id'] = $mansong_id;
  81. $mansong_info = $this->getMansongInfo($condition);
  82. if($store_id > 0 && $mansong_info['store_id'] != $store_id) {
  83. return null;
  84. } else {
  85. return $mansong_info;
  86. }
  87. }
  88. /**
  89. * 获取店铺当前可用满即送活动
  90. * @param array $store_id 店铺编号
  91. * @return array 满即送活动
  92. *
  93. */
  94. public function getMansongInfoByStoreID($store_id) {
  95. if(intval($store_id) <= 0) {
  96. return array();
  97. }
  98. $info = $this->_rGoodsMansongCache($store_id);
  99. if (empty($info)) {
  100. $condition = array();
  101. $condition['state'] = self::MANSONG_STATE_NORMAL;
  102. $condition['store_id'] = $store_id;
  103. $condition['end_time'] = array('gt', time());
  104. $mansong_list = $this->getMansongList($condition, null, 'start_time asc', '*', 1);
  105. $mansong_info = $mansong_list[0];
  106. if(!empty($mansong_info)) {
  107. $model_mansong_rule = Model('p_mansong_rule');
  108. $mansong_info['rules'] = $model_mansong_rule->getMansongRuleListByID($mansong_info['mansong_id']);
  109. if (empty($mansong_info['rules'])) {
  110. $mansong_info = array(); // 如果不存在规则直接返回不记录缓存。
  111. } else {
  112. // 规则数组序列化保存
  113. $mansong_info['rules'] = serialize($mansong_info['rules']);
  114. }
  115. }
  116. $info['info'] = serialize($mansong_info);
  117. $this->_wGoodsMansongCache($store_id, $info);
  118. }
  119. $mansong_info = unserialize($info['info']);
  120. if (!empty($mansong_info) && $mansong_info['start_time'] > time()) {
  121. $mansong_info = array();
  122. }
  123. if (!empty($mansong_info)) {
  124. $mansong_info['rules'] = unserialize($mansong_info['rules']);
  125. }
  126. return $mansong_info;
  127. }
  128. /**
  129. * 获取订单可用满即送规则
  130. * @param array $store_id 店铺编号
  131. * @param array $order_price 订单金额
  132. * @return array 满即送规则
  133. *
  134. */
  135. public function getMansongRuleByStoreID($store_id, $order_price) {
  136. $mansong_info = $this->getMansongInfoByStoreID($store_id);
  137. if(empty($mansong_info)) {
  138. return null;
  139. }
  140. $rule_info = null;
  141. foreach ($mansong_info['rules'] as $value) {
  142. if($order_price >= $value['price']) {
  143. $rule_info = $value;
  144. $rule_info['mansong_name'] = $mansong_info['mansong_name'];
  145. $rule_info['start_time'] = $mansong_info['start_time'];
  146. $rule_info['end_time'] = $mansong_info['end_time'];
  147. break;
  148. }
  149. }
  150. return $rule_info;
  151. }
  152. /**
  153. * 获取满即送状态列表
  154. *
  155. */
  156. public function getMansongStateArray() {
  157. return $this->mansong_state_array;
  158. }
  159. /**
  160. * 获取满即送扩展信息,包括状态文字和是否可编辑状态
  161. * @param array $mansong_info
  162. * @return string
  163. *
  164. */
  165. public function getMansongExtendInfo($mansong_info) {
  166. if($mansong_info['end_time'] > time()) {
  167. $mansong_info['mansong_state_text'] = $this->mansong_state_array[$mansong_info['state']];
  168. } else {
  169. $mansong_info['mansong_state_text'] = '已结束';
  170. }
  171. if($mansong_info['state'] == self::MANSONG_STATE_NORMAL && $mansong_info['end_time'] > time()) {
  172. $mansong_info['editable'] = true;
  173. } else {
  174. $mansong_info['editable'] = false;
  175. }
  176. return $mansong_info;
  177. }
  178. /**
  179. * 增加
  180. * @param array $param
  181. * @return bool
  182. *
  183. */
  184. public function addMansong($param){
  185. $param['state'] = self::MANSONG_STATE_NORMAL;
  186. $result = $this->insert($param);
  187. if ($result) {
  188. $this->_dGoodsMansongCache($param['store_id']);
  189. $this->publish_change();
  190. }
  191. return $result;
  192. }
  193. /**
  194. * 更新
  195. * @param array $update
  196. * @param array $condition
  197. * @return bool
  198. *
  199. */
  200. public function editMansong($update, $condition){
  201. $mansong_list = $this->getMansongList($condition);
  202. if (empty($mansong_list)) {
  203. return true;
  204. }
  205. $result = $this->where($condition)->update($update);
  206. if ($result) {
  207. foreach ($mansong_list as $val) {
  208. $this->_dGoodsMansongCache($val['store_id']);
  209. }
  210. $this->publish_change();
  211. }
  212. return $result;
  213. }
  214. /**
  215. * 删除限时折扣活动,同时删除限时折扣商品
  216. * @param array $condition
  217. * @return bool
  218. *
  219. */
  220. public function delMansong($condition){
  221. $mansong_list = $this->getMansongList($condition);
  222. $mansong_id_string = '';
  223. if(!empty($mansong_list)) {
  224. foreach ($mansong_list as $value) {
  225. $mansong_id_string .= $value['mansong_id'] . ',';
  226. $this->_dGoodsMansongCache($value['store_id']);
  227. }
  228. }
  229. //删除满送规则
  230. $model_mansong_rule = Model('p_mansong_rule');
  231. $model_mansong_rule->delMansongRule($condition);
  232. $ret = $this->where($condition)->delete();
  233. $this->publish_change();
  234. return $ret;
  235. }
  236. /**
  237. * 取消满即送活动
  238. * @param array $condition
  239. * @return bool
  240. *
  241. */
  242. public function cancelMansong($condition){
  243. $update = array();
  244. $update['state'] = self::MANSONG_STATE_CANCEL;
  245. return $this->editMansong($update, $condition);
  246. }
  247. /**
  248. * 过期满送修改状态
  249. */
  250. public function editExpireMansong() {
  251. $update = array();
  252. $update['state'] = self::MANSONG_STATE_CLOSE;
  253. $condition = array();
  254. $condition['end_time'] = array('lt', time());
  255. $condition['state'] = self::MANSONG_STATE_NORMAL;
  256. $this->editMansong($update, $condition);
  257. }
  258. /**
  259. * 读取商品满即送缓存
  260. * @param int $store_id
  261. * @return array
  262. */
  263. private function _rGoodsMansongCache($store_id) {
  264. return rcache($store_id, 'goods_mansong');
  265. }
  266. /**
  267. * 写入商品满即送缓存
  268. * @param int $store_id
  269. * @param array $mansong_info
  270. * @return boolean
  271. */
  272. private function _wGoodsMansongCache($store_id, $mansong_info) {
  273. return wcache($store_id, $mansong_info, 'goods_mansong');
  274. }
  275. /**
  276. * 删除商品满即送缓存
  277. * @param int $store_id
  278. * @return boolean
  279. */
  280. private function _dGoodsMansongCache($store_id) {
  281. return dcache($store_id, 'goods_mansong');
  282. }
  283. private function publish_change()
  284. {
  285. $publisher = new message\publisher();
  286. $publisher->modify_activity_fullsent();
  287. }
  288. }