p_mansong.model.php 9.7 KB

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