LuckLotteryServices.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\lottery;
  13. use app\services\BaseServices;
  14. use app\dao\activity\lottery\LuckLotteryDao;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\user\UserBillServices;
  18. use app\services\user\UserLabelRelationServices;
  19. use app\services\user\UserLabelServices;
  20. use app\services\user\UserMoneyServices;
  21. use app\services\user\UserServices;
  22. use crmeb\exceptions\AdminException;
  23. use crmeb\exceptions\ApiException;
  24. use crmeb\services\CacheService;
  25. /**
  26. *
  27. * Class LuckLotteryServices
  28. * @package app\services\activity\lottery
  29. * @method getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = true)
  30. */
  31. class LuckLotteryServices extends BaseServices
  32. {
  33. /**
  34. * 抽奖形式,奖品数量
  35. * @var int[]
  36. */
  37. protected $lottery_type = [
  38. '1' => 8 //九宫格
  39. ];
  40. /**
  41. * 抽奖类型
  42. * @var string[]
  43. */
  44. protected $lottery_factor = [
  45. '1' => '积分',
  46. '2' => '余额',
  47. '3' => '下单支付成功',
  48. '4' => '订单评价',
  49. '5' => '关注公众号'
  50. ];
  51. /**
  52. * LuckLotteryServices constructor.
  53. * @param LuckLotteryDao $dao
  54. */
  55. public function __construct(LuckLotteryDao $dao)
  56. {
  57. $this->dao = $dao;
  58. }
  59. public function getList(array $where)
  60. {
  61. [$page, $limit] = $this->getPageValue();
  62. $where['is_del'] = 0;
  63. $list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
  64. $lottery_factor = $this->lottery_factor;
  65. /** @var LuckLotteryRecordServices $luckLotteryRecordServices */
  66. $luckLotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  67. foreach ($list as &$item) {
  68. $item['lottery_type'] = $lottery_factor[$item['factor']] ?? '';
  69. $data = $luckLotteryRecordServices->getLotteryRecordData((int)$item['id']);
  70. $item['lottery_all'] = $data['all'] ?? 0;
  71. $item['lottery_people'] = $data['people'] ?? 0;
  72. $item['lottery_win'] = $data['win'] ?? 0;
  73. if ($item['start_time'] == 0 && $item['end_time'] == 0) {
  74. $status_name = '进行中';
  75. $item['lottery_status'] = 1;
  76. } else {
  77. if ($item['start_time'] > time()) {
  78. $status_name = '未开始';
  79. $item['lottery_status'] = 0;
  80. } else if ($item['end_time'] < time()) {
  81. $status_name = '已结束';
  82. $item['lottery_status'] = 2;
  83. } else if ($item['end_time'] > time() && $item['start_time'] < time()) {
  84. $status_name = '进行中';
  85. $item['lottery_status'] = 1;
  86. }
  87. }
  88. $item['status_name'] = $status_name;
  89. $item['start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '';
  90. $item['end_time'] = $item['end_time'] ? date('Y-m-d H:i:s', $item['end_time']) : '';
  91. }
  92. $count = $this->dao->count($where);
  93. return compact('list', 'count');
  94. }
  95. /**
  96. * 获取抽奖详情
  97. * @param int $id
  98. * @return array|\think\Model
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function getlotteryInfo(int $id)
  104. {
  105. $lottery = $this->dao->getLottery($id, '*', ['prize']);
  106. if (!$lottery) {
  107. throw new ApiException(410057);
  108. }
  109. $lottery = $lottery->toArray();
  110. if (isset($lottery['prize']) && $lottery['prize']) {
  111. $products = $coupons = [];
  112. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  113. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  114. /** @var StoreProductServices $productServices */
  115. $productServices = app()->make(StoreProductServices::class);
  116. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  117. /** @var StoreCouponIssueServices $couponServices */
  118. $couponServices = app()->make(StoreCouponIssueServices::class);
  119. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  120. foreach ($lottery['prize'] as &$prize) {
  121. $prize['coupon_title'] = $prize['goods_image'] = '';
  122. if ($prize['type'] == 6) {
  123. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  124. }
  125. if ($prize['type'] == 5) {
  126. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  127. }
  128. }
  129. }
  130. return $lottery;
  131. }
  132. /**
  133. * 根据类型获取数据
  134. * @param int $factor
  135. * @return array
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function getlotteryFactorInfo(int $factor)
  141. {
  142. $lottery = $this->dao->getFactorLottery($factor, '*', ['prize']);
  143. if (!$lottery) {
  144. return [];
  145. }
  146. $lottery = $lottery->toArray();
  147. if (isset($lottery['prize']) && $lottery['prize']) {
  148. $products = $coupons = [];
  149. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  150. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  151. /** @var StoreProductServices $productServices */
  152. $productServices = app()->make(StoreProductServices::class);
  153. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  154. /** @var StoreCouponIssueServices $couponServices */
  155. $couponServices = app()->make(StoreCouponIssueServices::class);
  156. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  157. foreach ($lottery['prize'] as &$prize) {
  158. $prize['coupon_title'] = $prize['goods_image'] = '';
  159. if ($prize['type'] == 6) {
  160. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  161. }
  162. if ($prize['type'] == 5) {
  163. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  164. }
  165. }
  166. }
  167. /** @var UserLabelServices $userLabelServices */
  168. $userLabelServices = app()->make(UserLabelServices::class);
  169. $lottery['user_label'] = !empty($lottery['user_label']) ? $userLabelServices->getLabelList(['ids' => $lottery['user_label']], ['id', 'label_name']) : [];
  170. return $lottery;
  171. }
  172. /**
  173. * 添加抽奖活动以及奖品
  174. * @param array $data
  175. * @return mixed
  176. */
  177. public function add(array $data)
  178. {
  179. $prizes = $data['prize'];
  180. $prize_num = $this->lottery_type[1];
  181. if (count($prizes) != $prize_num) {
  182. throw new AdminException(400535);
  183. }
  184. unset($data['prize']);
  185. return $this->transaction(function () use ($data, $prizes) {
  186. $time = time();
  187. $data['add_time'] = $time;
  188. if (!$lottery = $this->dao->save($data)) {
  189. throw new AdminException(400536);
  190. }
  191. if ($data['status']) {
  192. $this->setStatus((int)$lottery->id, $data['status']);
  193. }
  194. /** @var LuckPrizeServices $luckPrizeServices */
  195. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  196. $data = [];
  197. $sort = 1;
  198. foreach ($prizes as $prize) {
  199. $prize = $luckPrizeServices->checkPrizeData($prize);
  200. $prize['lottery_id'] = $lottery->id;
  201. unset($prize['id']);
  202. $prize['add_time'] = $time;
  203. $prize['sort'] = $sort;
  204. $data[] = $prize;
  205. $sort++;
  206. }
  207. if (!$luckPrizeServices->saveAll($data)) {
  208. throw new AdminException(400536);
  209. }
  210. return true;
  211. });
  212. }
  213. /**
  214. * 修改抽奖活动以及奖品
  215. * @param int $id
  216. * @param array $data
  217. * @return mixed
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function edit(int $id, array $data)
  223. {
  224. $lottery = $this->dao->getLottery($id);
  225. if (!$lottery) {
  226. throw new AdminException(400537);
  227. }
  228. $newPrizes = $data['prize'];
  229. unset($data['prize'], $data['id']);
  230. $prize_num = $this->lottery_type[1];
  231. if (count($newPrizes) != $prize_num) {
  232. throw new AdminException(400535);
  233. }
  234. if ($data['attends_user'] == 1) {
  235. $data['user_label'] = $data['user_level'] = [];
  236. $data['is_svip'] = -1;
  237. }
  238. /** @var LuckPrizeServices $luckPrizeServices */
  239. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  240. $prizes = $luckPrizeServices->getLotteryPrizeList($id);
  241. return $this->transaction(function () use ($id, $lottery, $data, $newPrizes, $prizes, $luckPrizeServices) {
  242. $updateIds = array_column($newPrizes, 'id');
  243. $oldIds = array_column($prizes, 'id');
  244. $delIds = array_merge(array_diff($oldIds, $updateIds));
  245. $insert = [];
  246. $time = time();
  247. $sort = 1;
  248. foreach ($newPrizes as $prize) {
  249. $prize = $luckPrizeServices->checkPrizeData($prize);
  250. $prize['sort'] = $sort;
  251. if (isset($prize['id']) && $prize['id']) {
  252. if (!$prize['lottery_id']) {
  253. throw new AdminException(100100);
  254. }
  255. if (!$luckPrizeServices->update($prize['id'], $prize, 'id')) {
  256. throw new AdminException(100007);
  257. }
  258. } else {
  259. unset($prize['id']);
  260. $prize['lottery_id'] = $id;
  261. $prize['add_time'] = $time;
  262. $prize['sort'] = $sort;
  263. $insert[] = $prize;
  264. }
  265. $sort++;
  266. }
  267. if ($insert) {
  268. if (!$luckPrizeServices->saveAll($insert)) {
  269. throw new AdminException(100022);
  270. }
  271. }
  272. if ($delIds) {
  273. if (!$luckPrizeServices->update([['id', 'in', $delIds]], ['is_del' => 1])) {
  274. throw new AdminException(100008);
  275. }
  276. }
  277. if (!$this->dao->update($id, $data)) {
  278. throw new AdminException(100007);
  279. }
  280. //上架
  281. if (!$lottery['status'] && $data['status']) {
  282. $this->setStatus($id, $data['status']);
  283. }
  284. return true;
  285. });
  286. }
  287. /**
  288. * 获取用户某个抽奖活动剩余抽奖次数
  289. * @param int $uid
  290. * @param int $lottery_id
  291. * @param array $userInfo
  292. * @param array $lottery
  293. * @return false|float|int|mixed
  294. * @throws \Psr\SimpleCache\InvalidArgumentException
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function getLotteryNum(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  300. {
  301. /** @var UserServices $userServices */
  302. $userServices = app()->make(UserServices::class);
  303. if (!$userInfo) {
  304. $userInfo = $userServices->getUserInfo($uid);
  305. }
  306. if (!$userInfo) {
  307. throw new ApiException(410032);
  308. }
  309. if (!$lottery) {
  310. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  311. }
  312. if (!$lottery) {
  313. throw new ApiException(410057);
  314. }
  315. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  316. switch ($lottery['factor']) {
  317. case 1:
  318. /** @var UserBillServices $userBillServices */
  319. $userBillServices = app()->make(UserBillServices::class);
  320. $usable_integral = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  321. return $usable_integral > 0 && $lottery['factor_num'] > 0 ? floor($usable_integral / $lottery['factor_num']) : 0;
  322. case 2:
  323. return $userInfo['now_money'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['now_money'] / $lottery['factor_num']) : 0;
  324. case 3:
  325. return $this->getCacheLotteryNum($uid, 'order');
  326. case 4:
  327. return $this->getCacheLotteryNum($uid, 'comment');
  328. case 5:
  329. return $userInfo['spread_lottery'] ?? 0;
  330. default:
  331. throw new ApiException(410058);
  332. }
  333. }
  334. /**
  335. * 验证用户抽奖资格(用户等级、付费会员、用户标签)
  336. * @param int $uid
  337. * @param int $lottery_id
  338. * @param array $userInfo
  339. * @param array $lottery
  340. * @return bool
  341. * @throws \think\db\exception\DataNotFoundException
  342. * @throws \think\db\exception\DbException
  343. * @throws \think\db\exception\ModelNotFoundException
  344. */
  345. public function checkoutUserAuth(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  346. {
  347. if (!$userInfo) {
  348. /** @var UserServices $userServices */
  349. $userServices = app()->make(UserServices::class);
  350. $userInfo = $userServices->getUserInfo($uid);
  351. }
  352. if (!$userInfo) {
  353. throw new ApiException(410032);
  354. }
  355. if (!$lottery) {
  356. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  357. }
  358. if (!$lottery) {
  359. throw new ApiException(410057);
  360. }
  361. //部分用户参与
  362. if ($lottery['attends_user'] == 2) {
  363. //用户等级
  364. if ($lottery['user_level'] && !in_array($userInfo['level'], $lottery['user_level'])) {
  365. throw new ApiException(410059);
  366. }
  367. //用户标签
  368. if ($lottery['user_label']) {
  369. /** @var UserLabelRelationServices $userlableRelation */
  370. $userlableRelation = app()->make(UserLabelRelationServices::class);
  371. $user_labels = $userlableRelation->getUserLabels($uid);
  372. if (!array_intersect($lottery['user_label'], $user_labels)) {
  373. throw new ApiException(410059);
  374. }
  375. }
  376. //是否是付费会员
  377. if ($lottery['is_svip'] != -1) {
  378. if (($lottery['is_svip'] == 1 && $userInfo['is_money_level'] <= 0) || ($lottery['is_svip'] == 0 && $userInfo['is_money_level'] > 0)) {
  379. throw new ApiException(410059);
  380. }
  381. }
  382. }
  383. return true;
  384. }
  385. /**
  386. * 抽奖
  387. * @param int $uid
  388. * @param int $lottery_id
  389. */
  390. public function luckLottery(int $uid, int $lottery_id)
  391. {
  392. /** @var UserServices $userServices */
  393. $userServices = app()->make(UserServices::class);
  394. $userInfo = $userServices->getUserInfo($uid);
  395. if (!$userInfo) {
  396. throw new ApiException(410032);
  397. }
  398. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  399. if (!$lottery) {
  400. throw new ApiException(410057);
  401. }
  402. $userInfo = $userInfo->toArray();
  403. $lottery = $lottery->toArray();
  404. //验证用户身份
  405. $this->checkoutUserAuth($uid, $lottery_id, $userInfo, $lottery);
  406. /** @var LuckPrizeServices $lotteryPrizeServices */
  407. $lotteryPrizeServices = app()->make(LuckPrizeServices::class);
  408. $lotteryPrize = $lotteryPrizeServices->getPrizeList($lottery_id);
  409. if (!$lotteryPrize) {
  410. throw new ApiException(410060);
  411. }
  412. if ($this->getLotteryNum($uid, $lottery_id, $userInfo, $lottery) < 1) {
  413. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  414. switch ($lottery['factor']) {
  415. case 1:
  416. throw new ApiException(410061);
  417. case 2:
  418. throw new ApiException(410062);
  419. case 3:
  420. throw new ApiException(410063);
  421. case 4:
  422. throw new ApiException(410064);
  423. case 5:
  424. throw new ApiException(410065);
  425. default:
  426. throw new ApiException(410058);
  427. }
  428. }
  429. return $this->transaction(function () use ($uid, $lotteryPrize, $userInfo, $lottery) {
  430. /** @var LuckPrizeServices $luckPrizeServices */
  431. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  432. //随机抽奖
  433. $prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
  434. if (!$prize) {
  435. throw new ApiException(410060);
  436. }
  437. //中奖扣除积分、余额
  438. $this->lotteryFactor($uid, $userInfo, $lottery);
  439. //中奖减少奖品数量
  440. $luckPrizeServices->decPrizeNum($prize['id'], $prize);
  441. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  442. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  443. //中奖写入记录
  444. $record = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo);
  445. //不是站内商品直接领奖
  446. if ($prize['type'] != 6) {
  447. $lotteryRecordServices->receivePrize($uid, (int)$record->id);
  448. }
  449. $prize['lottery_record_id'] = $record->id;
  450. return $prize;
  451. });
  452. }
  453. /**
  454. * 抽奖消耗扣除用户积分、余额等
  455. * @param int $uid
  456. * @param array $userInfo
  457. * @param array $lottery
  458. * @return bool
  459. * @throws \Psr\SimpleCache\InvalidArgumentException
  460. */
  461. public function lotteryFactor(int $uid, array $userInfo, array $lottery)
  462. {
  463. if (!$userInfo || !$lottery) {
  464. return true;
  465. }
  466. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  467. switch ($lottery['factor']) {
  468. case 1:
  469. if ($userInfo['integral'] > $lottery['factor_num']) {
  470. $integral = bcsub((string)$userInfo['integral'], (string)$lottery['factor_num'], 0);
  471. } else {
  472. $integral = 0;
  473. }
  474. /** @var UserServices $userServices */
  475. $userServices = app()->make(UserServices::class);
  476. /** @var UserBillServices $userBillServices */
  477. $userBillServices = app()->make(UserBillServices::class);
  478. $userBillServices->income('lottery_use_integral', $uid, $lottery['factor_num'], $integral, $lottery['id']);
  479. if (!$userServices->update($uid, ['integral' => $integral], 'uid')) {
  480. throw new ApiException(410066);
  481. }
  482. break;
  483. case 2:
  484. if ($userInfo['now_money'] >= $lottery['factor_num']) {
  485. $now_money = bcsub((string)$userInfo['now_money'], (string)$lottery['factor_num'], 2);
  486. } else {
  487. throw new ApiException(410067);
  488. }
  489. /** @var UserServices $userServices */
  490. $userServices = app()->make(UserServices::class);
  491. /** @var UserMoneyServices $userMoneyServices */
  492. $userMoneyServices = app()->make(UserMoneyServices::class);
  493. $userMoneyServices->income('lottery_use_money', $uid, $lottery['factor_num'], $now_money, $lottery['id']);
  494. if (!$userServices->update($uid, ['now_money' => $now_money], 'uid')) {
  495. throw new ApiException(410068);
  496. }
  497. break;
  498. case 3:
  499. case 4:
  500. //销毁抽奖次数缓存
  501. $this->delCacheLotteryNum($uid, $lottery['factor'] == 3 ? 'order' : 'comment');
  502. break;
  503. case 5:
  504. /** @var UserServices $userServices */
  505. $userServices = app()->make(UserServices::class);
  506. $spread_lottery = 0;
  507. if ($userInfo['spread_lottery'] > 1) {
  508. $spread_lottery = $userInfo['spread_lottery'] - 1;
  509. }
  510. if (!$userServices->update($uid, ['spread_lottery' => $spread_lottery], 'uid')) {
  511. throw new ApiException(410069);
  512. }
  513. break;
  514. default:
  515. throw new ApiException(410058);
  516. }
  517. return true;
  518. }
  519. /**
  520. * 删除
  521. * @param int $id
  522. * @return bool
  523. * @throws \think\db\exception\DataNotFoundException
  524. * @throws \think\db\exception\DbException
  525. * @throws \think\db\exception\ModelNotFoundException
  526. */
  527. public function delLottery(int $id)
  528. {
  529. if ($lottery = $this->dao->getLottery($id)) {
  530. if (!$this->dao->update(['id' => $id], ['is_del' => 1])) {
  531. throw new AdminException(100008);
  532. }
  533. }
  534. return true;
  535. }
  536. /**
  537. * 设置抽奖活动状态
  538. * @param int $id
  539. * @param $status
  540. * @return false|mixed
  541. * @throws \think\db\exception\DataNotFoundException
  542. * @throws \think\db\exception\DbException
  543. * @throws \think\db\exception\ModelNotFoundException
  544. */
  545. public function setStatus(int $id, $status)
  546. {
  547. if (!$id) return false;
  548. $lottery = $this->dao->getLottery($id, 'id,factor');
  549. if (!$lottery) {
  550. return false;
  551. }
  552. //每一种抽奖类型只有一个上架
  553. if ($status) {
  554. $this->dao->update(['factor' => $lottery['factor']], ['status' => 0]);
  555. }
  556. return $this->dao->update($id, ['status' => $status], 'id');
  557. }
  558. /**
  559. * 下单支付、评论缓存抽奖次数
  560. * @param int $uid
  561. * @param string $type
  562. * @return bool
  563. * @throws \Psr\SimpleCache\InvalidArgumentException
  564. */
  565. public function setCacheLotteryNum(int $uid, string $type = 'order')
  566. {
  567. $factor = $type == 'order' ? 3 : 4;
  568. $lottery = $this->dao->getFactorLottery($factor, 'id,factor_num', ['prize'], true);
  569. if (!$lottery || !$lottery['factor_num']) {
  570. return true;
  571. }
  572. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  573. return CacheService::set($key, $lottery['factor_num'], 120);
  574. }
  575. /**
  576. * 取出下单支付、评论得到的抽奖此处
  577. * @param int $uid
  578. * @param string $type
  579. * @return int|mixed
  580. * @throws \Psr\SimpleCache\InvalidArgumentException
  581. */
  582. public function getCacheLotteryNum(int $uid, string $type = 'order')
  583. {
  584. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  585. $num = CacheService::get($key);
  586. return empty($num) ? 0 : $num;
  587. }
  588. /**
  589. * 抽奖之后销毁缓存
  590. * @param int $uid
  591. * @param string $type
  592. * @return bool
  593. * @throws \Psr\SimpleCache\InvalidArgumentException
  594. */
  595. public function delCacheLotteryNum(int $uid, string $type = 'order')
  596. {
  597. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  598. $num = $this->getCacheLotteryNum($uid, $type);
  599. if ($num > 1) {
  600. CacheService::set($key, $num - 1, 120);
  601. } else {
  602. CacheService::delete($key);
  603. }
  604. return true;
  605. }
  606. }