p_bundling.model.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * 优惠套装
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
  11. class p_bundlingModel extends Model {
  12. const STATE1 = 1; // 开启
  13. const STATE0 = 0; // 关闭
  14. public function __construct() {
  15. parent::__construct();
  16. }
  17. /**
  18. * 组合活动数量
  19. *
  20. * @param array $condition
  21. * @return array
  22. */
  23. public function getBundlingCount($condition) {
  24. return $this->table('p_bundling')->where($condition)->count();
  25. }
  26. /**
  27. * 活动列表
  28. *
  29. * @param array $condition
  30. * @param string $field
  31. * @param string $order
  32. * @param int $page
  33. * @param int $limit
  34. * @param int $count
  35. * @return array
  36. */
  37. public function getBundlingList($condition, $field = '*', $order = 'bl_id desc', $page = 10, $limit = 0, $count = 0) {
  38. return $this->table('p_bundling')->where($condition)->order($order)->limit($limit)->page($page, $count)->select();
  39. }
  40. /**
  41. * 开启的活动列表
  42. * @param array $condition
  43. * @param string $field
  44. * @param string $order
  45. * @param int $limit
  46. * @return array
  47. */
  48. public function getBundlingOpenList($condition, $field = '*', $order = 'bl_id desc', $limit = 0) {
  49. $condition['bl_state'] = self::STATE1;
  50. return $this->getBundlingList($condition, $field, $order, 0, $limit);
  51. }
  52. /**
  53. * 获得获得详细信息
  54. */
  55. public function getBundlingInfo($condition) {
  56. return $this->table('p_bundling')->where($condition)->find();
  57. }
  58. /**
  59. * 保存活动
  60. *
  61. * @param array $insert
  62. * @param string $replace
  63. * @return boolean
  64. */
  65. public function addBundling($insert, $replace = false) {
  66. $ret = $this->table('p_bundling')->insert($insert, $replace);
  67. if($ret != false) {
  68. $this->publis_message();
  69. }
  70. return $ret;
  71. }
  72. /**
  73. * 更新活动
  74. *
  75. * @param array $update
  76. * @param array $condition
  77. * @return boolean
  78. */
  79. public function editBundling($update, $condition) {
  80. $ret = $this->table('p_bundling')->where($condition)->update($update);
  81. if($ret != false) {
  82. $this->publis_message();
  83. }
  84. return $ret;
  85. }
  86. /**
  87. * 更新活动关闭
  88. *
  89. * @param array $update
  90. * @param array $condition
  91. * @return boolean
  92. */
  93. public function editBundlingCloseByGoodsIds($condition) {
  94. $bundlinggoods_list = $this->getBundlingGoodsList($condition, 'bl_id');
  95. if (!empty($bundlinggoods_list)) {
  96. $blid_array = array();
  97. foreach ($bundlinggoods_list as $val) {
  98. $blid_array[] = $val['bl_id'];
  99. }
  100. $update = array('bl_state' => self::STATE0);
  101. $ret = $this->table('p_bundling')->where(array('bl_id' => array('in', $blid_array)))->update($update);
  102. if($ret != false) {
  103. $this->publis_message();
  104. }
  105. return $ret;
  106. }
  107. return true;
  108. }
  109. /**
  110. * 删除套餐活动
  111. * @param array $blids
  112. * @param int $store_id
  113. * @return boolean
  114. */
  115. public function delBundling($blids, $store_id) {
  116. $blid_array = explode(',', $blids);
  117. foreach ($blid_array as $val) {
  118. if (!is_numeric($val)) {
  119. return false;
  120. }
  121. }
  122. $where = array();
  123. $where['bl_id'] = array('in', $blid_array);
  124. $where['store_id'] = $store_id;
  125. $bl_list = $this->getBundlingList($where, 'bl_id');
  126. $bl_list = array_under_reset($bl_list, 'bl_id');
  127. $blid_array = array_keys($bl_list);
  128. $where = array();
  129. $where['bl_id'] = array('in', $blid_array);
  130. $rs = $this->table('p_bundling')->where($where)->delete();
  131. if ($rs) {
  132. return $this->delBundlingGoods($where);
  133. } else {
  134. return false;
  135. }
  136. }
  137. /**
  138. * 删除套餐活动(平台后台使用)
  139. * @param array $condition
  140. * @return boolean
  141. */
  142. public function delBundlingForAdmin($condition) {
  143. $rs = $this->table('p_bundling')->where($condition)->delete();
  144. if ($rs) {
  145. return $this->delBundlingGoods($condition);
  146. } else {
  147. return false;
  148. }
  149. }
  150. /**
  151. * 单条组合套餐
  152. *
  153. * @param array $condition
  154. * @return array
  155. */
  156. public function getBundlingQuotaInfo($condition) {
  157. return $this->table('p_bundling_quota')->where($condition)->find();
  158. }
  159. /**
  160. * 单条组合套餐
  161. *
  162. * @param int $store_id
  163. * @return array
  164. */
  165. public function getBundlingQuotaInfoCurrent($store_id) {
  166. $condition['store_id'] = $_SESSION['store_id'];
  167. $condition['bl_quota_endtime'] = array('gt', time());
  168. $condition['bl_state'] = 1;
  169. return $this->getBundlingQuotaInfo($condition);
  170. }
  171. /**
  172. * 组合套餐列表
  173. *
  174. * @param array $condition
  175. * @param int $page
  176. * @param int $limit
  177. * @return array
  178. */
  179. public function getBundlingQuotaList($condition, $page = 10, $limit = 0) {
  180. return $this->table('p_bundling_quota')->where($condition)->order('bl_quota_id desc')->limit($limit)->page($page)->select();
  181. }
  182. /**
  183. * 开启的组合套餐列表
  184. *
  185. * @param array $condition
  186. * @param int $page
  187. * @param int $limit
  188. * @return array
  189. */
  190. public function getBundlingQuotaOpenList($condition, $page = 10, $limit = 0) {
  191. $condition['bl_state'] = self::STATE1;
  192. return $this->getBundlingQuotaList($condition, $page, $limit);
  193. }
  194. /**
  195. * 保存组合套餐
  196. *
  197. * @param array $insert
  198. * @return boolean
  199. */
  200. public function addBundlingQuota($insert) {
  201. return $this->table('p_bundling_quota')->insert($insert);
  202. }
  203. /**
  204. * 更新组合套餐
  205. *
  206. * @param array $update
  207. * @param array $condition
  208. * @return boolean
  209. */
  210. public function editBundlingQuota($update, $condition) {
  211. return $this->table('p_bundling_quota')->where($condition)->update($update);
  212. }
  213. /**
  214. * 更新组合套餐
  215. *
  216. * @param array $update
  217. * @param array $condition
  218. * @return boolean
  219. */
  220. public function editBundlingQuotaOpen($update, $condition) {
  221. $update['bl_state'] = self::STATE1;
  222. return $this->table('p_bundling_quota')->where($condition)->update($update);
  223. }
  224. /**
  225. * 更新套餐为关闭状态
  226. * @param array $condition
  227. * @return boolean
  228. */
  229. public function editBundlingQuotaClose($condition) {
  230. $quota_list = $this->getBundlingQuotaList($condition);
  231. if (empty($quota_list)) {
  232. return true;
  233. }
  234. $storeid_array = array();
  235. foreach ($quota_list as $val) {
  236. $storeid_array[] = $val['store_id'];
  237. }
  238. $where = array('store_id' => array('in', $storeid_array));
  239. $update = array('bl_state' => self::STATE0);
  240. $this->editBundlingQuota($update, $where);
  241. $this->editBundling($update, $where);
  242. return true;
  243. }
  244. /**
  245. * 更新超时的套餐为关闭状态
  246. * @param array $condition
  247. * @return boolean
  248. */
  249. public function editBundlingTimeout($condition) {
  250. $condition['bl_quota_endtime'] = array('lt', time());
  251. $quota_list = $this->getBundlingQuotaList($condition);
  252. if (!empty($quota_list)) {
  253. $quotaid_array = array();
  254. foreach ($quota_list as $val) {
  255. $quotaid_array[] = $val['bl_quota_id'];
  256. }
  257. return $this->editBundlingQuotaClose(array('bl_quota_id' => array('in', $quotaid_array)));
  258. } else {
  259. return true;
  260. }
  261. }
  262. /**
  263. * 套餐商品列表
  264. *
  265. * @param array $condition
  266. * @param string $field
  267. * @param string $order
  268. * @param string $group
  269. * @return array
  270. */
  271. public function getBundlingGoodsList($condition, $field = '*', $order = 'bl_goods_id asc', $group = '') {
  272. return $this->table('p_bundling_goods')->field($field)->where($condition)->group($group)->order($order)->select();
  273. }
  274. /**
  275. * 保存套餐商品
  276. *
  277. * @param unknown $insert
  278. * @return boolean
  279. */
  280. public function addBundlingGoodsAll($insert) {
  281. $result = $this->table('p_bundling_goods')->insertAll($insert);
  282. if ($result) {
  283. foreach ((array)$insert as $v) {
  284. if (isset($v['goods_id'])) $this->_dGoodsBundlingCache($v['goods_id']);
  285. }
  286. }
  287. return $result;
  288. }
  289. /**
  290. * 删除套餐商品
  291. *
  292. * @param array $condition
  293. * @return boolean
  294. */
  295. public function delBundlingGoods($condition) {
  296. $list = $this->getBundlingGoodsList($condition, 'goods_id');
  297. if (empty($list)) {
  298. return true;
  299. }
  300. $result = $this->table('p_bundling_goods')->where($condition)->delete();
  301. if ($result)
  302. {
  303. foreach ($list as $v) {
  304. $this->_dGoodsBundlingCache($v['goods_id']);
  305. }
  306. $this->publis_message();
  307. }
  308. return $result;
  309. }
  310. /**
  311. * 根据商品id查询套餐数据
  312. * @param unknown $goods_id
  313. */
  314. public function getBundlingCacheByGoodsId($goods_id) {
  315. $array = $this->_rGoodsBundlingCache($goods_id);
  316. if (empty($array)) {
  317. $bundling_array = array();
  318. $b_goods_array = array();
  319. // 根据商品id查询bl_id
  320. $b_g_list = $this->getBundlingGoodsList(array('goods_id' => $goods_id, 'bl_appoint' => 1), 'bl_id');
  321. if (!empty($b_g_list)) {
  322. $b_id_array = array();
  323. foreach ($b_g_list as $val) {
  324. $b_id_array[] = $val['bl_id'];
  325. }
  326. // 查询套餐列表
  327. $bundling_list = $this->getBundlingOpenList(array('bl_id' => array('in', $b_id_array)));
  328. // 整理
  329. if (!empty($bundling_list)) {
  330. foreach ($bundling_list as $val) {
  331. $bundling_array[$val['bl_id']]['id'] = $val['bl_id'];
  332. $bundling_array[$val['bl_id']]['name'] = $val['bl_name'];
  333. $bundling_array[$val['bl_id']]['cost_price'] = 0;
  334. $bundling_array[$val['bl_id']]['price'] = $val['bl_discount_price'];
  335. $bundling_array[$val['bl_id']]['freight'] = $val['bl_freight'];
  336. }
  337. $blid_array = array_keys($bundling_array);
  338. $b_goods_list = $this->getBundlingGoodsList(array('bl_id' => array('in', $blid_array)));
  339. if (!empty($b_goods_list) && count($b_goods_list) > 1) {
  340. $goodsid_array = array();
  341. foreach ($b_goods_list as $val) {
  342. $goodsid_array[] = $val['goods_id'];
  343. }
  344. $goods_list = Model('goods')->getGoodsList(array('goods_id' => array('in', $goodsid_array)), 'goods_id,goods_name,goods_price,goods_image');
  345. $goods_list = array_under_reset($goods_list, 'goods_id');
  346. foreach ( $b_goods_list as $val ) {
  347. if (isset($goods_list[$val['goods_id']])) {
  348. $k = (intval($val['goods_id']) == $goods_id) ? 0 : $val['goods_id']; // 排序当前商品放到最前面
  349. $b_goods_array[$val['bl_id']][$k]['id'] = $val['goods_id'];
  350. $b_goods_array[$val['bl_id']][$k]['image'] = thumb($goods_list[$val['goods_id']], 240);
  351. $b_goods_array[$val['bl_id']][$k]['name'] = $goods_list[$val['goods_id']]['goods_name'];
  352. $b_goods_array[$val['bl_id']][$k]['shop_price'] = ncPriceFormat($goods_list[$val['goods_id']]['goods_price']);
  353. $b_goods_array[$val['bl_id']][$k]['price'] = ncPriceFormat($val['bl_goods_price']);
  354. $bundling_array[$val['bl_id']]['cost_price'] += $goods_list[$val['goods_id']]['goods_price'];
  355. }
  356. }
  357. }
  358. }
  359. }
  360. $array = array('bundling_array' => serialize($bundling_array), 'b_goods_array' => serialize($b_goods_array));
  361. $this->_wGoodsBundlingCache($goods_id, $array);
  362. }
  363. return $array;
  364. }
  365. /**
  366. * 读取商品优惠套装缓存
  367. * @param int $goods_id
  368. * @return array
  369. */
  370. private function _rGoodsBundlingCache($goods_id) {
  371. return rcache($goods_id, 'goods_bundling');
  372. }
  373. /**
  374. * 写入商品优惠套装缓存
  375. * @param int $goods_id
  376. * @param array $array
  377. * @return boolean
  378. */
  379. private function _wGoodsBundlingCache($goods_id, $array) {
  380. return wcache($goods_id, $array, 'goods_bundling');
  381. }
  382. /**
  383. * 删除商品优惠套装缓存
  384. * @param int $goods_id
  385. * @return boolean
  386. */
  387. private function _dGoodsBundlingCache($goods_id) {
  388. return dcache($goods_id, 'goods_bundling');
  389. }
  390. private function publis_message()
  391. {
  392. $publisher = new message\publisher();
  393. $publisher->modify_activity_bundling();
  394. }
  395. }