Groupbuy.php 28 KB

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