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