Groupbuy.php 28 KB

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