Pmansong.php 11 KB

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