groupbuy.model.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 groupbuyModel extends Model
  12. {
  13. const GROUPBUY_STATE_REVIEW = 10;
  14. const GROUPBUY_STATE_NORMAL = 20;
  15. const GROUPBUY_STATE_REVIEW_FAIL = 30;
  16. const GROUPBUY_STATE_CANCEL = 31;
  17. const GROUPBUY_STATE_CLOSE = 32;
  18. private $groupbuy_state_array = array(
  19. 0 => '全部',
  20. self::GROUPBUY_STATE_REVIEW => '审核中',
  21. self::GROUPBUY_STATE_NORMAL => '正常',
  22. self::GROUPBUY_STATE_CLOSE => '已结束',
  23. self::GROUPBUY_STATE_REVIEW_FAIL => '审核失败',
  24. self::GROUPBUY_STATE_CANCEL => '管理员关闭',
  25. );
  26. public function __construct() {
  27. parent::__construct('groupbuy');
  28. }
  29. /**
  30. * 读取抢购列表
  31. * @param array $condition 查询条件
  32. * @param int $page 分页数
  33. * @param string $order 排序
  34. * @param string $field 所需字段
  35. * @return array 抢购列表
  36. *
  37. */
  38. public function getGroupbuyList($condition, $page = null, $order = 'state asc', $field = '*', $limit = 0) {
  39. return $this->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
  40. }
  41. /**
  42. * 读取抢购列表
  43. * @param array $condition 查询条件
  44. * @param int $page 分页数
  45. * @param string $order 排序
  46. * @param string $field 所需字段
  47. * @return array 抢购列表
  48. *
  49. */
  50. public function getGroupbuyExtendList($condition, $page = null, $order = 'state asc', $field = '*', $limit = 0) {
  51. $groupbuy_list = $this->getGroupbuyList($condition, $page, $order, $field, $limit);
  52. if(!empty($groupbuy_list)) {
  53. for($i =0, $j = count($groupbuy_list); $i < $j; $i++) {
  54. $groupbuy_list[$i] = $this->getGroupbuyExtendInfo($groupbuy_list[$i]);
  55. }
  56. }
  57. return $groupbuy_list;
  58. }
  59. /**
  60. * 读取可用抢购列表
  61. */
  62. public function getGroupbuyAvailableList($condition) {
  63. $condition['state'] = array('in', array(self::GROUPBUY_STATE_REVIEW, self::GROUPBUY_STATE_NORMAL));
  64. return $this->getGroupbuyExtendList($condition);
  65. }
  66. /**
  67. * 查询抢购数量
  68. * @param array $condition
  69. * @return int
  70. */
  71. public function getGroupbuyCount($condition) {
  72. return $this->where($condition)->count();
  73. }
  74. /**
  75. * 读取当前可用的抢购列表
  76. * @param array $condition 查询条件
  77. * @param int $page 分页数
  78. * @param string $order 排序
  79. * @param string $field 所需字段
  80. * @return array 抢购列表
  81. *
  82. */
  83. public function getGroupbuyOnlineList($condition, $page = null, $order = 'state asc', $field = '*') {
  84. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  85. $condition['start_time'] = array('lt', time());
  86. $condition['end_time'] = array('gt', time());
  87. return $this->getGroupbuyExtendList($condition, $page, $order, $field);
  88. }
  89. /**
  90. * 读取即将开始的抢购列表
  91. * @param array $condition 查询条件
  92. * @param int $page 分页数
  93. * @param string $order 排序
  94. * @param string $field 所需字段
  95. * @return array 抢购列表
  96. *
  97. */
  98. public function getGroupbuySoonList($condition, $page = null, $order = 'state asc', $field = '*') {
  99. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  100. $condition['start_time'] = array('gt', time());
  101. return $this->getGroupbuyExtendList($condition, $page, $order, $field);
  102. }
  103. /**
  104. * 读取已经结束的抢购列表
  105. * @param array $condition 查询条件
  106. * @param int $page 分页数
  107. * @param string $order 排序
  108. * @param string $field 所需字段
  109. * @return array 抢购列表
  110. *
  111. */
  112. public function getGroupbuyHistoryList($condition, $page = null, $order = 'state asc', $field = '*') {
  113. $condition['state'] = self::GROUPBUY_STATE_CLOSE;
  114. return $this->getGroupbuyExtendList($condition, $page, $order, $field);
  115. }
  116. /**
  117. * 读取推荐抢购列表
  118. * @param int $limit 要读取的数量
  119. */
  120. public function getGroupbuyCommendedList($limit = 4) {
  121. $condition = array();
  122. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  123. $condition['start_time'] = array('lt', time());
  124. $condition['end_time'] = array('gt', time());
  125. return $this->getGroupbuyExtendList($condition, null, 'recommended desc', '*', $limit);
  126. }
  127. /**
  128. * 根据条件读取抢购信息
  129. * @param array $condition 查询条件
  130. * @return array 抢购信息
  131. *
  132. */
  133. public function getGroupbuyInfo($condition) {
  134. $groupbuy_info = $this->where($condition)->find();
  135. if (empty($groupbuy_info)) return array();
  136. $groupbuy_info = $this->getGroupbuyExtendInfo($groupbuy_info);
  137. return $groupbuy_info;
  138. }
  139. /**
  140. * 根据条件读取抢购信息
  141. * @param array $condition 查询条件
  142. * @param int $page 分页数
  143. * @param string $order 排序
  144. * @param string $field 所需字段
  145. * @return array 抢购列表
  146. *
  147. */
  148. public function getGroupbuyOnlineInfo($condition) {
  149. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  150. $condition['start_time'] = array('lt', time());
  151. $condition['end_time'] = array('gt', time());
  152. $groupbuy_info = $this->where($condition)->find();
  153. return $groupbuy_info;
  154. }
  155. /**
  156. * 根据抢购编号读取抢购信息
  157. * @param array $groupbuy_id 抢购活动编号
  158. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  159. * @return array 抢购信息
  160. *
  161. */
  162. public function getGroupbuyInfoByID($groupbuy_id, $store_id = 0) {
  163. if(intval($groupbuy_id) <= 0) {
  164. return null;
  165. }
  166. $condition = array();
  167. $condition['groupbuy_id'] = $groupbuy_id;
  168. $groupbuy_info = $this->getGroupbuyInfo($condition);
  169. if($store_id > 0 && $groupbuy_info['store_id'] != $store_id) {
  170. return null;
  171. } else {
  172. return $groupbuy_info;
  173. }
  174. }
  175. /**
  176. * 根据商品编号查询是否有可用抢购活动,如果有返回抢购信息,没有返回null
  177. * @param int $goods_id
  178. * @return array $groupbuy_info
  179. *
  180. */
  181. public function getGroupbuyInfoByGoodsCommonID($goods_commonid) {
  182. $info = $this->_rGoodsGroupbuyCache($goods_commonid);
  183. if (empty($info)) {
  184. $condition = array();
  185. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  186. $condition['end_time'] = array('gt', time());
  187. $condition['goods_commonid'] = $goods_commonid;
  188. $groupbuy_goods_list = $this->getGroupbuyExtendList($condition, null, 'start_time asc', '*', 1);
  189. $info['info'] = serialize($groupbuy_goods_list[0]);
  190. $this->_wGoodsGroupbuyCache($goods_commonid, $info);
  191. }
  192. $groupbuy_goods_info = unserialize($info['info']);
  193. if (!empty($groupbuy_goods_info) && ($groupbuy_goods_info['start_time'] > time() || $groupbuy_goods_info['end_time'] < time())) {
  194. $groupbuy_goods_info = array();
  195. }
  196. return $groupbuy_goods_info;
  197. }
  198. /**
  199. * 根据商品编号查询是否有可用抢购活动,如果有返回抢购活动,没有返回null
  200. * @param string $goods_string 商品编号字符串,例:'1,22,33'
  201. * @return array $groupbuy_list
  202. *
  203. */
  204. public function getGroupbuyListByGoodsCommonIDString($goods_commonid_string) {
  205. $groupbuy_list = $this->_getGroupbuyListByGoodsCommon($goods_commonid_string);
  206. $groupbuy_list = array_under_reset($groupbuy_list, 'goods_commonid');
  207. return $groupbuy_list;
  208. }
  209. /**
  210. * 根据商品编号查询是否有可用抢购活动,如果有返回抢购活动,没有返回null
  211. * @param string $goods_id_string
  212. * @return array $groupbuy_list
  213. *
  214. */
  215. private function _getGroupbuyListByGoodsCommon($goods_commonid_string) {
  216. $condition = array();
  217. $condition['state'] = self::GROUPBUY_STATE_NORMAL;
  218. $condition['start_time'] = array('lt', time());
  219. $condition['end_time'] = array('gt', time());
  220. $condition['goods_commonid'] = array('in', $goods_commonid_string);
  221. $xianshi_goods_list = $this->getGroupbuyExtendList($condition, null, 'groupbuy_id desc', '*');
  222. return $xianshi_goods_list;
  223. }
  224. /**
  225. * 抢购状态数组
  226. */
  227. public function getGroupbuyStateArray() {
  228. return $this->groupbuy_state_array;
  229. }
  230. /*
  231. * 增加
  232. * @param array $param
  233. * @return bool
  234. *
  235. */
  236. public function addGroupbuy($param){
  237. // 发布抢购锁定商品
  238. $this->_lockGoods($param['goods_commonid']);
  239. $param['state'] = self::GROUPBUY_STATE_REVIEW;
  240. $param['recommended'] = 0;
  241. $result = $this->insert($param);
  242. if ($result) {
  243. // 更新商品抢购缓存
  244. $this->_dGoodsGroupbuyCache($param['goods_commonid']);
  245. //$this->publish_message(); 此处无需发布变更通知
  246. return $result;
  247. } else {
  248. return false;
  249. }
  250. }
  251. /**
  252. * 锁定商品
  253. */
  254. private function _lockGoods($goods_commonid) {
  255. $condition = array();
  256. $condition['goods_commonid'] = $goods_commonid;
  257. $model_goods = Model('goods');
  258. $model_goods->editGoodsCommonLock($condition);
  259. }
  260. /**
  261. * 解锁商品
  262. */
  263. private function _unlockGoods($goods_commonid) {
  264. $model_goods = Model('goods');
  265. $model_goods->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  266. // 添加对列 更新商品促销价格
  267. QueueClient::push('updateGoodsPromotionPriceByGoodsCommonId', $goods_commonid);
  268. }
  269. /**
  270. * 更新
  271. * @param array $update
  272. * @param array $condition
  273. * @return bool
  274. *
  275. */
  276. public function editGroupbuy($update, $condition)
  277. {
  278. $groupbuy_list = $this->getGroupbuyList($condition, null, '', 'goods_commonid');
  279. $result = $this->where($condition)->update($update);
  280. if ($result) {
  281. if (!empty($groupbuy_list)) {
  282. foreach ($groupbuy_list as $val) {
  283. // 更新商品抢购缓存
  284. $this->_dGoodsGroupbuyCache($val['goods_commonid']);
  285. }
  286. $this->publish_message();
  287. }
  288. }
  289. return $result;
  290. }
  291. /*
  292. * 审核成功
  293. * @param int $groupbuy_id
  294. * @return bool
  295. *
  296. */
  297. public function reviewPassGroupbuy($groupbuy_id) {
  298. $condition = array();
  299. $condition['groupbuy_id'] = $groupbuy_id;
  300. $update = array();
  301. $update['state'] = self::GROUPBUY_STATE_NORMAL;
  302. return $this->editGroupbuy($update, $condition);
  303. }
  304. /*
  305. * 审核失败
  306. * @param int $groupbuy_id
  307. * @return bool
  308. *
  309. */
  310. public function reviewFailGroupbuy($groupbuy_id) {
  311. // 商品解锁
  312. $groupbuy_info = $this->getGroupbuyInfoByID($groupbuy_id);
  313. $condition = array();
  314. $condition['groupbuy_id'] = $groupbuy_id;
  315. $update = array();
  316. $update['state'] = self::GROUPBUY_STATE_REVIEW_FAIL;
  317. $return = $this->editGroupbuy($update, $condition);
  318. if ($return) {
  319. $this->_unlockGoods($groupbuy_info['goods_commonid']);
  320. }
  321. return $return;
  322. }
  323. /*
  324. * 取消
  325. * @param int $groupbuy_id
  326. * @return bool
  327. *
  328. */
  329. public function cancelGroupbuy($groupbuy_id) {
  330. // 商品解锁
  331. $groupbuy_info = $this->getGroupbuyInfoByID($groupbuy_id);
  332. $condition = array();
  333. $condition['groupbuy_id'] = $groupbuy_id;
  334. $update = array();
  335. $update['state'] = self::GROUPBUY_STATE_CANCEL;
  336. $return = $this->editGroupbuy($update, $condition);
  337. if ($return) {
  338. $this->_unlockGoods($groupbuy_info['goods_commonid']);
  339. }
  340. return $return;
  341. }
  342. /**
  343. * 过期抢购修改状态,解锁对应商品
  344. */
  345. public function editExpireGroupbuy($condition) {
  346. $condition['end_time'] = array('lt', time());
  347. $condition['state'] = array('in', array(self::GROUPBUY_STATE_REVIEW, self::GROUPBUY_STATE_NORMAL));
  348. $expire_groupbuy_list = $this->getGroupbuyExtendList($condition, null);
  349. if (!empty($expire_groupbuy_list)) {
  350. $goodscommonid_array = array();
  351. foreach ($expire_groupbuy_list as $val) {
  352. $goodscommonid_array[] = $val['goods_commonid'];
  353. }
  354. // 更新商品促销价格,需要考虑抢购是否在进行中
  355. QueueClient::push('updateGoodsPromotionPriceByGoodsCommonId', $goodscommonid_array);
  356. }
  357. $groupbuy_id_string = '';
  358. if(!empty($expire_groupbuy_list)) {
  359. foreach ($expire_groupbuy_list as $value) {
  360. $groupbuy_id_string .= $value['groupbuy_id'].',';
  361. }
  362. }
  363. if($groupbuy_id_string != '') {
  364. $update = array();
  365. $update['state'] = self::GROUPBUY_STATE_CLOSE;
  366. $condition = array();
  367. $condition['groupbuy_id'] = array('in', rtrim($groupbuy_id_string, ','));
  368. $result = $this->editGroupbuy($update, $condition);
  369. if ($result) {
  370. foreach ($expire_groupbuy_list as $value) {
  371. $this->_unlockGoods($value['goods_commonid']);
  372. }
  373. }
  374. }
  375. return true;
  376. }
  377. /*
  378. * 删除抢购活动
  379. * @param array $condition
  380. * @return bool
  381. *
  382. */
  383. public function delGroupbuy($condition){
  384. $groupbuy_list = $this->getGroupbuyExtendList($condition);
  385. $result = $this->where($condition)->delete();
  386. if(!empty($groupbuy_list) && $result)
  387. {
  388. foreach ($groupbuy_list as $value) {
  389. // 商品解锁
  390. $this->_unlockGoods($value['goods_commonid']);
  391. // 更新商品抢购缓存
  392. $this->_dGoodsGroupbuyCache($value['goods_commonid']);
  393. list($base_name, $ext) = explode('.', $value['groupbuy_image']);
  394. list($store_id) = explode('_', $base_name);
  395. $path = BASE_UPLOAD_PATH.DS.ATTACH_GROUPBUY.DS.$store_id.DS;
  396. @unlink($path.$base_name.'.'.$ext);
  397. @unlink($path.$base_name.'_small.'.$ext);
  398. @unlink($path.$base_name.'_mid.'.$ext);
  399. @unlink($path.$base_name.'_max.'.$ext);
  400. if(!empty($value['groupbuy_image1'])) {
  401. list($base_name, $ext) = explode('.', $value['groupbuy_image1']);
  402. @unlink($path.$base_name.'.'.$ext);
  403. @unlink($path.$base_name.'_small.'.$ext);
  404. @unlink($path.$base_name.'_mid.'.$ext);
  405. @unlink($path.$base_name.'_max.'.$ext);
  406. }
  407. }
  408. $this->publish_message();
  409. }
  410. return true;
  411. }
  412. /**
  413. * 获取抢购扩展信息
  414. */
  415. public function getGroupbuyExtendInfo($groupbuy_info) {
  416. $groupbuy_info['groupbuy_url'] = urlShop('show_groupbuy', 'groupbuy_detail', array('group_id' => $groupbuy_info['groupbuy_id']));
  417. $groupbuy_info['goods_url'] = urlShop('goods', 'index', array('goods_id' => $groupbuy_info['goods_id']));
  418. $groupbuy_info['start_time_text'] = date('Y-m-d H:i', $groupbuy_info['start_time']);
  419. $groupbuy_info['end_time_text'] = date('Y-m-d H:i', $groupbuy_info['end_time']);
  420. if(empty($groupbuy_info['groupbuy_image1'])) {
  421. $groupbuy_info['groupbuy_image1'] = $groupbuy_info['groupbuy_image'];
  422. }
  423. if($groupbuy_info['start_time'] > time() && $groupbuy_info['state'] == self::GROUPBUY_STATE_NORMAL) {
  424. $groupbuy_info['groupbuy_state_text'] = '正常(未开始)';
  425. } elseif ($groupbuy_info['end_time'] < time() && $groupbuy_info['state'] == self::GROUPBUY_STATE_NORMAL) {
  426. $groupbuy_info['groupbuy_state_text'] = '已结束';
  427. } else {
  428. $groupbuy_info['groupbuy_state_text'] = $this->groupbuy_state_array[$groupbuy_info['state']];
  429. }
  430. if($groupbuy_info['state'] == self::GROUPBUY_STATE_REVIEW) {
  431. $groupbuy_info['reviewable'] = 1;
  432. } else {
  433. $groupbuy_info['reviewable'] = 0;
  434. }
  435. if($groupbuy_info['state'] == self::GROUPBUY_STATE_NORMAL) {
  436. $groupbuy_info['cancelable'] = 1;
  437. } else {
  438. $groupbuy_info['cancelable'] = 0;
  439. }
  440. switch ($groupbuy_info['state']) {
  441. case self::GROUPBUY_STATE_REVIEW:
  442. $groupbuy_info['state_flag'] = 'not-verify';
  443. $groupbuy_info['button_text'] = '未审核';
  444. break;
  445. case self::GROUPBUY_STATE_REVIEW_FAIL:
  446. case self::GROUPBUY_STATE_CANCEL:
  447. case self::GROUPBUY_STATE_CLOSE:
  448. $groupbuy_info['state_flag'] = 'close';
  449. $groupbuy_info['button_text'] = '已结束';
  450. break;
  451. case self::GROUPBUY_STATE_NORMAL:
  452. if($groupbuy_info['start_time'] > time()) {
  453. $groupbuy_info['state_flag'] = 'not-start';
  454. $groupbuy_info['button_text'] = '未开始';
  455. $groupbuy_info['count_down_text'] = '距抢购开始';
  456. $groupbuy_info['count_down'] = $groupbuy_info['start_time'] - time();
  457. } elseif ($groupbuy_info['end_time'] < time()) {
  458. $groupbuy_info['state_flag'] = 'close';
  459. $groupbuy_info['button_text'] = '已结束';
  460. } else {
  461. $groupbuy_info['state_flag'] = 'buy-now';
  462. $groupbuy_info['button_text'] = '我要抢';
  463. $groupbuy_info['count_down_text'] = '距抢购结束';
  464. $groupbuy_info['count_down'] = $groupbuy_info['end_time'] - time();
  465. }
  466. break;
  467. }
  468. return $groupbuy_info;
  469. }
  470. /**
  471. * 读取商品抢购缓存
  472. * @param int $goods_commonid
  473. * @return array/bool
  474. */
  475. private function _rGoodsGroupbuyCache($goods_commonid) {
  476. return rcache($goods_commonid, 'goods_groupbuy');
  477. }
  478. /**
  479. * 写入商品抢购缓存
  480. * @param int $goods_commonid
  481. * @param array $info
  482. * @return boolean
  483. */
  484. private function _wGoodsGroupbuyCache($goods_commonid, $info) {
  485. return wcache($goods_commonid, $info, 'goods_groupbuy');
  486. }
  487. /**
  488. * 删除商品抢购缓存
  489. * @param int $goods_commonid
  490. * @return boolean
  491. */
  492. private function _dGoodsGroupbuyCache($goods_commonid) {
  493. return dcache($goods_commonid, 'goods_groupbuy');
  494. }
  495. private function publish_message()
  496. {
  497. $publisher = new message\publisher();
  498. $publisher->modify_activity_groupbuy();
  499. }
  500. /**
  501. * 读取抢购分类
  502. *
  503. * @return array
  504. */
  505. public function getGroupbuyClasses()
  506. {
  507. return $this->getCachedData('groupbuy_classes');
  508. }
  509. /**
  510. * 读取虚拟抢购分类
  511. *
  512. * @return array
  513. */
  514. public function getGroupbuyVrClasses()
  515. {
  516. return $this->getCachedData('groupbuy_vr_classes');
  517. }
  518. /**
  519. * 读取虚拟抢购地区
  520. *
  521. * @return array
  522. */
  523. public function getGroupbuyVrCities()
  524. {
  525. return $this->getCachedData('groupbuy_vr_cities');
  526. }
  527. /**
  528. * 删除缓存
  529. *
  530. * @param string $key 缓存键
  531. */
  532. public function dropCachedData($key) {
  533. unset($this->cachedData[$key]);
  534. dkcache($key);
  535. }
  536. /**
  537. * 获取缓存
  538. *
  539. * @param string $key 缓存键
  540. * @return array 缓存数据
  541. */
  542. protected function getCachedData($key) {
  543. $data = $this->cachedData[$key];
  544. // 属性中存在则返回
  545. if ($data || is_array($data)) {
  546. return $data;
  547. }
  548. $data = rkcache($key);
  549. // 缓存中存在则返回
  550. if ($data || is_array($data)) {
  551. // 写入属性
  552. $this->cachedData[$key] = $data;
  553. return $data;
  554. }
  555. $data = $this->getCachingDataByQuery($key);
  556. // 写入缓存
  557. wkcache($key, $data);
  558. // 写入属性
  559. $this->cachedData[$key] = $data;
  560. return $data;
  561. }
  562. protected function getCachingDataByQuery($key) {
  563. $data = array();
  564. switch ($key) {
  565. case 'groupbuy_classes': // 抢购分类
  566. $classes = Model()->table('groupbuy_class')->order('sort asc')->limit(false)->select();
  567. foreach ((array) $classes as $v) {
  568. $id = $v['class_id'];
  569. $pid = $v['class_parent_id'];
  570. $data['name'][$id] = $v['class_name'];
  571. $data['parent'][$id] = $pid;
  572. $data['children'][$pid][] = $id;
  573. }
  574. break;
  575. case 'groupbuy_vr_classes': // 虚拟抢购分类
  576. $classes = Model()->table('vr_groupbuy_class')->order('class_sort asc')->limit(false)->select();
  577. foreach ((array) $classes as $v) {
  578. $id = $v['class_id'];
  579. $pid = $v['parent_class_id'];
  580. $data['name'][$id] = $v['class_name'];
  581. $data['parent'][$id] = $pid;
  582. $data['children'][$pid][] = $id;
  583. }
  584. break;
  585. case 'groupbuy_vr_cities': // 虚拟抢购地区
  586. // 一级地区 城市
  587. $arr = (array) Model()->table('vr_groupbuy_area')->where(array(
  588. 'hot_city' => 1,
  589. 'parent_area_id' => 0,
  590. ))->order('area_id asc')->limit(false)->key('area_id')->select();
  591. foreach ($arr as $v) {
  592. $id = $v['area_id'];
  593. $pid = $v['parent_area_id'];
  594. $data['name'][$id] = $v['area_name'];
  595. $data['parent'][$id] = $pid;
  596. $data['children'][$pid][] = $id;
  597. }
  598. if ($pids = array_keys($arr)) {
  599. // 二级地区 区域
  600. $arr = (array) Model()->table('vr_groupbuy_area')->where(array(
  601. 'parent_area_id' => array('in', $pids),
  602. ))->order('area_id asc')->limit(false)->key('area_id')->select();
  603. foreach ($arr as $v) {
  604. $id = $v['area_id'];
  605. $pid = $v['parent_area_id'];
  606. $data['name'][$id] = $v['area_name'];
  607. $data['parent'][$id] = $pid;
  608. $data['children'][$pid][] = $id;
  609. }
  610. if ($pids = array_keys($arr)) {
  611. // 三级地区 街区
  612. $arr = (array) Model()->table('vr_groupbuy_area')->where(array(
  613. 'parent_area_id' => array('in', $pids),
  614. ))->order('area_id asc')->limit(false)->key('area_id')->select();
  615. $pids = array_keys($arr);
  616. foreach ($arr as $v) {
  617. $id = $v['area_id'];
  618. $pid = $v['parent_area_id'];
  619. $data['name'][$id] = $v['area_name'];
  620. $data['parent'][$id] = $pid;
  621. $data['children'][$pid][] = $id;
  622. }
  623. }
  624. }
  625. break;
  626. default:
  627. throw new Exception("Invalid data key: {$key}");
  628. }
  629. return $data;
  630. }
  631. /**
  632. * 缓存数据(抢购分类、虚拟抢购分类、虚拟抢购地区)
  633. * 数组键为缓存名称 值为缓存数据
  634. *
  635. * 例 抢购分类缓存数据格式如下
  636. * array(
  637. * 'name' => array(
  638. * '分类id' => '分类名称',
  639. * // ..
  640. * ),
  641. * 'parent' => array(
  642. * '子分类id' => '父分类id',
  643. * // ..
  644. * ),
  645. * 'children' => array(
  646. * '父分类id' => array(
  647. * '子分类id 1',
  648. * '子分类id 2',
  649. * // ..
  650. * ),
  651. * // ..
  652. * ),
  653. * )
  654. *
  655. * @return array
  656. */
  657. protected $cachedData;
  658. }