Pbundling.php 17 KB

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