Groupbuy.php 28 KB

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