Pmansong.php 12 KB

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