Pmansong.php 11 KB

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