Pbundling.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 Pbundling extends BaseModel
  19. {
  20. const STATE1 = 1; // 开启
  21. const STATE0 = 0; // 关闭
  22. public $page_info;
  23. /**
  24. * 组合活动数量
  25. * @access public
  26. * @author csdeshang
  27. * @param array $condition 检索条件
  28. * @return array
  29. */
  30. public function getBundlingCount($condition)
  31. {
  32. return Db::name('pbundling')->where($condition)->count();
  33. }
  34. /**
  35. * 活动列表
  36. * @access public
  37. * @author csdeshang
  38. * @param array $condition 查询条件
  39. * @param string $field 查询字段
  40. * @param string $order 排序信息
  41. * @param int $pagesize 分页信息
  42. * @param int $limit 限制数量
  43. * @param int $count 计数
  44. * @return array
  45. */
  46. public function getBundlingList($condition, $field = '*', $order = 'bl_id desc', $pagesize = 0, $limit = 0, $count = 0)
  47. {
  48. if ($pagesize) {
  49. $res = Db::name('pbundling')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  50. $this->page_info = $res;
  51. return $res->items();
  52. } else {
  53. return Db::name('pbundling')->where($condition)->order($order)->limit($limit, $count)->select()->toArray();
  54. }
  55. }
  56. /**
  57. * 开启的活动列表
  58. * @access public
  59. * @author csdeshang
  60. * @param array $condition 条件
  61. * @param string $field 字段
  62. * @param string $order 排序
  63. * @param int $limit 限制
  64. * @return array
  65. */
  66. public function getBundlingOpenList($condition, $field = '*', $order = 'bl_id desc', $limit = 0)
  67. {
  68. $condition[] = array('bl_state', '=', self::STATE1);
  69. return $this->getBundlingList($condition, $field, $order, 0, $limit);
  70. }
  71. /**
  72. * 获取活动详细信息
  73. * @access public
  74. * @author csdeshang
  75. * @param type $condition 条件
  76. * @return type
  77. */
  78. public function getBundlingInfo($condition)
  79. {
  80. return Db::name('pbundling')->where($condition)->find();
  81. }
  82. /**
  83. * 保存活动
  84. * @access public
  85. * @author csdeshang
  86. * @param array $data 参数内容
  87. * @return boolean
  88. */
  89. public function addBundling($data)
  90. {
  91. return Db::name('pbundling')->insertGetId($data);
  92. }
  93. /**
  94. * 更新活动
  95. * @access public
  96. * @author csdeshang
  97. * @param array $update 更新数据
  98. * @param array $condition 条件
  99. * @return boolean
  100. */
  101. public function editBundling($update, $condition)
  102. {
  103. return Db::name('pbundling')->where($condition)->update($update);
  104. }
  105. /**
  106. * 更新活动关闭
  107. * @access public
  108. * @author csdeshang
  109. * @param array $condition 检索条件
  110. * @return boolean
  111. */
  112. public function editBundlingCloseByGoodsIds($condition)
  113. {
  114. $bundlinggoods_list = $this->getBundlingGoodsList($condition, 'bl_id');
  115. if (!empty($bundlinggoods_list)) {
  116. $blid_array = array();
  117. foreach ($bundlinggoods_list as $val) {
  118. $blid_array[] = $val['bl_id'];
  119. }
  120. $update = array('bl_state' => self::STATE0);
  121. return Db::name('pbundling')->where('bl_id', 'in', $blid_array)->update($update);
  122. }
  123. return true;
  124. }
  125. /**
  126. * 删除套餐活动
  127. * @access public
  128. * @author csdeshang
  129. * @param array $blids 活动id
  130. * @param int $store_id 店铺id
  131. * @return boolean
  132. */
  133. public function delBundling($blids, $store_id)
  134. {
  135. $blid_array = explode(',', $blids);
  136. foreach ($blid_array as $val) {
  137. if (!is_numeric($val)) {
  138. return false;
  139. }
  140. }
  141. $where = array();
  142. $where[] = array('bl_id', 'in', $blid_array);
  143. $where[] = array('store_id', '=', $store_id);
  144. $bl_list = $this->getBundlingList($where, 'bl_id');
  145. $bl_list = array_under_reset($bl_list, 'bl_id');
  146. $blid_array = array_keys($bl_list);
  147. $where = array();
  148. $where[] = array('bl_id', 'in', $blid_array);
  149. $rs = Db::name('pbundling')->where($where)->delete();
  150. if ($rs) {
  151. $res = $this->delBundlingGoods($where);
  152. if ($res)
  153. return true;
  154. return false;
  155. } else {
  156. return false;
  157. }
  158. }
  159. /**
  160. * 删除套餐活动(平台后台使用)
  161. * @access public
  162. * @author csdeshang
  163. * @param array $condition 条件
  164. * @return boolean
  165. */
  166. public function delBundlingForAdmin($condition)
  167. {
  168. $rs = Db::name('pbundling')->where($condition)->delete();
  169. if ($rs) {
  170. return $this->delBundlingGoods($condition);
  171. } else {
  172. return false;
  173. }
  174. }
  175. /**
  176. * 单条组合套餐
  177. * @access public
  178. * @author csdeshang
  179. * @param array $condition 条件
  180. * @return array
  181. */
  182. public function getBundlingQuotaInfo($condition)
  183. {
  184. return Db::name('pbundlingquota')->where($condition)->find();
  185. }
  186. /**
  187. * 单条组合套餐
  188. * @access public
  189. * @author csdeshang
  190. * @param int $store_id 店铺ID
  191. * @return array
  192. */
  193. public function getBundlingQuotaInfoCurrent($store_id)
  194. {
  195. $condition = array();
  196. $condition[] = array('store_id', '=', $store_id);
  197. $condition[] = array('blquota_endtime', '>', TIMESTAMP);
  198. $condition[] = array('blquota_state', '=', 1);
  199. return $this->getBundlingQuotaInfo($condition);
  200. }
  201. /**
  202. * 组合套餐列表
  203. * @access public
  204. * @author csdeshang
  205. * @param array $condition 条件
  206. * @param int $pagesize 分页
  207. * @param int $limit 限制
  208. * @return array
  209. */
  210. public function getBundlingQuotaList($condition, $pagesize = 0, $limit = 0)
  211. {
  212. if ($pagesize) {
  213. $res = Db::name('pbundlingquota')->where($condition)->order('blquota_id desc')->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  214. $this->page_info = $res;
  215. return $res->items();
  216. } else {
  217. return Db::name('pbundlingquota')->where($condition)->order('blquota_id desc')->limit($limit)->select()->toArray();
  218. }
  219. }
  220. /**
  221. * 开启的组合套餐列表
  222. * @access public
  223. * @author csdeshang
  224. * @param array $condition 条件
  225. * @param int $pagesize 分页
  226. * @param int $limit 限制
  227. * @return array
  228. */
  229. public function getBundlingQuotaOpenList($condition, $pagesize = 0, $limit = 0)
  230. {
  231. $condition[] = array('blquota_state', '=', self::STATE1);
  232. return $this->getBundlingQuotaList($condition, $pagesize, $limit);
  233. }
  234. /**
  235. * 保存组合套餐
  236. * @access public
  237. * @author csdeshang
  238. * @param array $data 参数内容
  239. * @return boolean
  240. */
  241. public function addBundlingQuota($data)
  242. {
  243. return Db::name('pbundlingquota')->insertGetId($data);
  244. }
  245. /**
  246. * 更新组合套餐
  247. * @access public
  248. * @author csdeshang
  249. * @param array $update 更新数据
  250. * @param array $condition 查询条件
  251. * @return boolean
  252. */
  253. public function editBundlingQuota($update, $condition)
  254. {
  255. return Db::name('pbundlingquota')->where($condition)->update($update);
  256. }
  257. /**
  258. * 更新组合套餐
  259. * @access public
  260. * @author csdeshang
  261. * @param array $update 更新数据
  262. * @param array $condition 条件
  263. * @return boolean
  264. */
  265. public function editBundlingQuotaOpen($update, $condition)
  266. {
  267. $update[] = array('blquota_state', '=', self::STATE1);
  268. return Db::name('pbundlingquota')->where($condition)->update($update);
  269. }
  270. /**
  271. * 更新套餐为关闭状态
  272. * @access public
  273. * @author csdeshang
  274. * @param array $condition 条件
  275. * @return boolean
  276. */
  277. public function editBundlingQuotaClose($condition)
  278. {
  279. $quota_list = $this->getBundlingQuotaList($condition);
  280. if (empty($quota_list)) {
  281. return true;
  282. }
  283. $storeid_array = array();
  284. foreach ($quota_list as $val) {
  285. $storeid_array[] = $val['store_id'];
  286. }
  287. $where = array(array('store_id', 'in', $storeid_array));
  288. $update = array('blquota_state' => self::STATE0);
  289. $this->editBundlingQuota($update, $where);
  290. $update = array('bl_state' => self::STATE0);
  291. $this->editBundling($update, $where);
  292. return true;
  293. }
  294. /**
  295. * 更新超时的套餐为关闭状态
  296. * @access public
  297. * @author csdeshang
  298. * @param array $condition 条件
  299. * @return boolean
  300. */
  301. public function editBundlingTimeout($condition)
  302. {
  303. $condition[] = array('blquota_endtime', '<', TIMESTAMP);
  304. $quota_list = $this->getBundlingQuotaList($condition);
  305. if (!empty($quota_list)) {
  306. $quotaid_array = array();
  307. foreach ($quota_list as $val) {
  308. $quotaid_array[] = $val['blquota_id'];
  309. }
  310. return $this->editBundlingQuotaClose(array(array('blquota_id', 'in', $quotaid_array)));
  311. } else {
  312. return true;
  313. }
  314. }
  315. /**
  316. * 套餐商品列表
  317. * @access public
  318. * @author csdeshang
  319. * @param array $condition 条件
  320. * @param string $field 字段
  321. * @param string $order 排序
  322. * @param string $group 分组
  323. * @return array
  324. */
  325. public function getBundlingGoodsList($condition, $field = '*', $order = 'blgoods_id asc', $group = '')
  326. {
  327. return Db::name('pbundlinggoods')->field($field)->where($condition)->group($group)->order($order)->select()->toArray();
  328. }
  329. /**
  330. * 保存套餐商品
  331. * @access public
  332. * @author csdeshang
  333. * @param unknown $data 参数内容
  334. * @return boolean
  335. */
  336. public function addBundlingGoodsAll($data)
  337. {
  338. $result = Db::name('pbundlinggoods')->insertAll($data);
  339. if ($result) {
  340. foreach ((array)$data as $v) {
  341. // 发布套餐锁定商品
  342. $this->_lockGoods($v['goods_commonid'], $v['goods_id']);
  343. $this->_dGoodsBundlingCache($v['goods_id']);
  344. }
  345. }
  346. return $result;
  347. }
  348. /**
  349. * 删除套餐商品
  350. * @access public
  351. * @author csdeshang
  352. * @param array $condition 条件
  353. * @return boolean
  354. */
  355. public function delBundlingGoods($condition)
  356. {
  357. $list = $this->getBundlingGoodsList($condition, 'goods_id,goods_commonid');
  358. if (empty($list)) {
  359. return true;
  360. }
  361. //halt($condition);
  362. $result = Db::name('pbundlinggoods')->where($condition)->delete();
  363. if ($result) {
  364. foreach ($list as $v) {
  365. $this->_unlockGoods($v['goods_commonid'], $v['goods_id']);
  366. $this->_dGoodsBundlingCache($v['goods_id']);
  367. }
  368. return true;
  369. }
  370. return $result;
  371. }
  372. /**
  373. * 锁定商品
  374. * @access private
  375. * @author csdeshang
  376. * @param type $goods_commonid 商品编号
  377. */
  378. private function _lockGoods($goods_commonid, $goods_id)
  379. {
  380. $condition = array();
  381. $condition[] = array('goods_commonid', '=', $goods_commonid);
  382. $goods_model = model('goods');
  383. $goods_model->editGoodsCommonLock($condition);
  384. $condition = array();
  385. $condition[] = array('goods_id', '=', $goods_id);
  386. $goods_model->editGoodsLock($condition);
  387. }
  388. /**
  389. * 解锁商品
  390. * @access private
  391. * @author csdeshang
  392. * @param type $goods_commonid 商品编号ID
  393. */
  394. private function _unlockGoods($goods_commonid, $goods_id)
  395. {
  396. $goods_model = model('goods');
  397. $goods_model->editGoodsUnlock(array('goods_id' => $goods_id));
  398. if (!$goods_model->getGoodsCount(array('goods_commonid' => $goods_commonid, 'goods_lock' => 1))) {
  399. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  400. }
  401. }
  402. /**
  403. * 根据商品id查询套餐数据
  404. * @access public
  405. * @author csdeshang
  406. * @param unknown $goods_id 商品ID
  407. * @return array
  408. */
  409. public function getBundlingCacheByGoodsId($goods_id)
  410. {
  411. $array = $this->_rGoodsBundlingCache($goods_id);
  412. if (empty($array)) {
  413. $bundling_array = array();
  414. $b_goods_array = array();
  415. // 根据商品id查询bl_id
  416. $b_g_list = $this->getBundlingGoodsList(array('goods_id' => $goods_id, 'blgoods_appoint' => 1), 'bl_id');
  417. if (!empty($b_g_list)) {
  418. $b_id_array = array();
  419. foreach ($b_g_list as $val) {
  420. $b_id_array[] = $val['bl_id'];
  421. }
  422. // 查询套餐列表
  423. $bundling_list = $this->getBundlingOpenList(array(array('bl_id', 'in', $b_id_array)));
  424. // 整理
  425. if (!empty($bundling_list)) {
  426. foreach ($bundling_list as $val) {
  427. $bundling_array[$val['bl_id']]['id'] = $val['bl_id'];
  428. $bundling_array[$val['bl_id']]['name'] = $val['bl_name'];
  429. $bundling_array[$val['bl_id']]['storecost_price'] = 0;
  430. $bundling_array[$val['bl_id']]['price'] = $val['bl_discount_price'];
  431. $bundling_array[$val['bl_id']]['freight'] = $val['bl_freight'];
  432. }
  433. $blid_array = array_keys($bundling_array);
  434. $b_goods_list = $this->getBundlingGoodsList(array(array('bl_id', 'in', $blid_array)));
  435. if (!empty($b_goods_list) && count($b_goods_list) > 1) {
  436. $goodsid_array = array();
  437. foreach ($b_goods_list as $val) {
  438. $goodsid_array[] = $val['goods_id'];
  439. }
  440. $goods_list = model('goods')->getGoodsList(array(
  441. array(
  442. 'goods_id', 'in', $goodsid_array
  443. )
  444. ), 'goods_id,goods_name,goods_price,goods_image');
  445. $goods_list = array_under_reset($goods_list, 'goods_id');
  446. foreach ($b_goods_list as $val) {
  447. if (isset($goods_list[$val['goods_id']])) {
  448. $k = (intval($val['goods_id']) == $goods_id) ? 0 : $val['goods_id']; // 排序当前商品放到最前面
  449. $b_goods_array[$val['bl_id']][$k]['id'] = $val['goods_id'];
  450. $b_goods_array[$val['bl_id']][$k]['image'] = goods_thumb($goods_list[$val['goods_id']], 240);
  451. $b_goods_array[$val['bl_id']][$k]['name'] = $goods_list[$val['goods_id']]['goods_name'];
  452. $b_goods_array[$val['bl_id']][$k]['shop_price'] = ds_price_format($goods_list[$val['goods_id']]['goods_price']);
  453. $b_goods_array[$val['bl_id']][$k]['price'] = ds_price_format($val['blgoods_price']);
  454. $bundling_array[$val['bl_id']]['storecost_price'] += $goods_list[$val['goods_id']]['goods_price'];
  455. }
  456. }
  457. }
  458. }
  459. }
  460. $array = array(
  461. 'bundling_array' => serialize($bundling_array), 'b_goods_array' => serialize($b_goods_array)
  462. );
  463. $this->_wGoodsBundlingCache($goods_id, $array);
  464. }
  465. return $array;
  466. }
  467. /**
  468. * 读取商品优惠套装缓存
  469. * @access public
  470. * @author csdeshang
  471. * @param int $goods_id 商品ID
  472. * @return array
  473. */
  474. private function _rGoodsBundlingCache($goods_id)
  475. {
  476. return rcache($goods_id, 'goods_bundling');
  477. }
  478. /**
  479. * 写入商品优惠套装缓存
  480. * @access public
  481. * @author csdeshang
  482. * @param int $goods_id 商品ID
  483. * @param array $array 缓存数组
  484. * @return boolean
  485. */
  486. private function _wGoodsBundlingCache($goods_id, $array)
  487. {
  488. return wcache($goods_id, $array, 'goods_bundling');
  489. }
  490. /**
  491. * @access public
  492. * @author csdeshang
  493. * 删除商品优惠套装缓存
  494. * @param int $goods_id 商品ID
  495. * @return boolean
  496. */
  497. private function _dGoodsBundlingCache($goods_id)
  498. {
  499. return dcache($goods_id, 'goods_bundling');
  500. }
  501. }