groupbuy.model.php 24 KB

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