Groupbuy.php 28 KB

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