Pbundling.php 18 KB

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