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