LuckLotteryServices.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  149. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  150. /** @var StoreProductServices $productServices */
  151. $productServices = app()->make(StoreProductServices::class);
  152. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  153. /** @var StoreCouponIssueServices $couponServices */
  154. $couponServices = app()->make(StoreCouponIssueServices::class);
  155. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  156. foreach ($lottery['prize'] as &$prize) {
  157. $prize['coupon_title'] = $prize['goods_image'] = '';
  158. if ($prize['type'] == 6) {
  159. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  160. }
  161. if ($prize['type'] == 5) {
  162. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  163. }
  164. }
  165. }
  166. foreach ($lottery['user_level'] as &$item) {
  167. $item = (int)$item;
  168. }
  169. /** @var UserLabelServices $userLabelServices */
  170. $userLabelServices = app()->make(UserLabelServices::class);
  171. $lottery['user_label'] = !empty($lottery['user_label']) ? $userLabelServices->getLabelList(['ids' => $lottery['user_label']], ['id', 'label_name']) : [];
  172. return $lottery;
  173. }
  174. /**
  175. * 添加抽奖活动以及奖品
  176. * @param array $data
  177. * @return mixed
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function add(array $data)
  183. {
  184. $prizes = $data['prize'];
  185. $prize_num = $this->lottery_type[1];
  186. if (count($prizes) != $prize_num) {
  187. throw new AdminException(400535);
  188. }
  189. unset($data['prize']);
  190. return $this->transaction(function () use ($data, $prizes) {
  191. $time = time();
  192. $data['add_time'] = $time;
  193. if (!$lottery = $this->dao->save($data)) {
  194. throw new AdminException(400536);
  195. }
  196. if ($data['status']) {
  197. $this->setStatus((int)$lottery->id, $data['status']);
  198. }
  199. /** @var LuckPrizeServices $luckPrizeServices */
  200. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  201. $data = [];
  202. $sort = 1;
  203. foreach ($prizes as $prize) {
  204. $prize = $luckPrizeServices->checkPrizeData($prize);
  205. $prize['lottery_id'] = $lottery->id;
  206. unset($prize['id']);
  207. $prize['add_time'] = $time;
  208. $prize['sort'] = $sort;
  209. $data[] = $prize;
  210. $sort++;
  211. }
  212. if (!$luckPrizeServices->saveAll($data)) {
  213. throw new AdminException(400536);
  214. }
  215. return true;
  216. });
  217. }
  218. /**
  219. * 修改抽奖活动以及奖品
  220. * @param int $id
  221. * @param array $data
  222. * @return mixed
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\DbException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. */
  227. public function edit(int $id, array $data)
  228. {
  229. $lottery = $this->dao->getLottery($id);
  230. if (!$lottery) {
  231. throw new AdminException(400537);
  232. }
  233. $newPrizes = $data['prize'];
  234. unset($data['prize'], $data['id']);
  235. $prize_num = $this->lottery_type[1];
  236. if (count($newPrizes) != $prize_num) {
  237. throw new AdminException(400535);
  238. }
  239. if ($data['attends_user'] == 1) {
  240. $data['user_label'] = $data['user_level'] = [];
  241. $data['is_svip'] = -1;
  242. }
  243. /** @var LuckPrizeServices $luckPrizeServices */
  244. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  245. $prizes = $luckPrizeServices->getLotteryPrizeList($id);
  246. return $this->transaction(function () use ($id, $lottery, $data, $newPrizes, $prizes, $luckPrizeServices) {
  247. $updateIds = array_column($newPrizes, 'id');
  248. $oldIds = array_column($prizes, 'id');
  249. $delIds = array_merge(array_diff($oldIds, $updateIds));
  250. $insert = [];
  251. $time = time();
  252. $sort = 1;
  253. foreach ($newPrizes as $prize) {
  254. $prize = $luckPrizeServices->checkPrizeData($prize);
  255. $prize['sort'] = $sort;
  256. if (isset($prize['id']) && $prize['id']) {
  257. if (!$prize['lottery_id']) {
  258. throw new AdminException(100100);
  259. }
  260. if (!$luckPrizeServices->update($prize['id'], $prize, 'id')) {
  261. throw new AdminException(100007);
  262. }
  263. } else {
  264. unset($prize['id']);
  265. $prize['lottery_id'] = $id;
  266. $prize['add_time'] = $time;
  267. $prize['sort'] = $sort;
  268. $insert[] = $prize;
  269. }
  270. $sort++;
  271. }
  272. if ($insert) {
  273. if (!$luckPrizeServices->saveAll($insert)) {
  274. throw new AdminException(100022);
  275. }
  276. }
  277. if ($delIds) {
  278. if (!$luckPrizeServices->update([['id', 'in', $delIds]], ['is_del' => 1])) {
  279. throw new AdminException(100008);
  280. }
  281. }
  282. if (!$this->dao->update($id, $data)) {
  283. throw new AdminException(100007);
  284. }
  285. //上架
  286. if (!$lottery['status'] && $data['status']) {
  287. $this->setStatus($id, $data['status']);
  288. }
  289. return true;
  290. });
  291. }
  292. /**
  293. * 获取用户某个抽奖活动剩余抽奖次数
  294. * @param int $uid
  295. * @param int $lottery_id
  296. * @param array $userInfo
  297. * @param array $lottery
  298. * @return false|float|int|mixed
  299. * @throws \Psr\SimpleCache\InvalidArgumentException
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\DbException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. */
  304. public function getLotteryNum(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  305. {
  306. /** @var UserServices $userServices */
  307. $userServices = app()->make(UserServices::class);
  308. if (!$userInfo) {
  309. $userInfo = $userServices->getUserInfo($uid);
  310. }
  311. if (!$userInfo) {
  312. throw new ApiException(410032);
  313. }
  314. if (!$lottery) {
  315. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  316. }
  317. if (!$lottery) {
  318. throw new ApiException(410057);
  319. }
  320. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  321. switch ($lottery['factor']) {
  322. case 1:
  323. /** @var UserBillServices $userBillServices */
  324. $userBillServices = app()->make(UserBillServices::class);
  325. $usable_integral = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  326. return $usable_integral > 0 && $lottery['factor_num'] > 0 ? floor($usable_integral / $lottery['factor_num']) : 0;
  327. case 2:
  328. return $userInfo['now_money'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['now_money'] / $lottery['factor_num']) : 0;
  329. case 3:
  330. return $this->getCacheLotteryNum($uid, 'order');
  331. case 4:
  332. return $this->getCacheLotteryNum($uid, 'comment');
  333. case 5:
  334. return $userInfo['spread_lottery'] ?? 0;
  335. default:
  336. throw new ApiException(410058);
  337. }
  338. }
  339. /**
  340. * 验证用户抽奖资格(用户等级、付费会员、用户标签)
  341. * @param int $uid
  342. * @param int $lottery_id
  343. * @param array $userInfo
  344. * @param array $lottery
  345. * @return bool
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function checkoutUserAuth(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  351. {
  352. if (!$userInfo) {
  353. /** @var UserServices $userServices */
  354. $userServices = app()->make(UserServices::class);
  355. $userInfo = $userServices->getUserInfo($uid);
  356. }
  357. if (!$userInfo) {
  358. throw new ApiException(410032);
  359. }
  360. if (!$lottery) {
  361. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  362. }
  363. if (!$lottery) {
  364. throw new ApiException(410057);
  365. }
  366. //部分用户参与
  367. if ($lottery['attends_user'] == 2) {
  368. //用户等级
  369. if ($lottery['user_level'] && !in_array($userInfo['level'], $lottery['user_level'])) {
  370. throw new ApiException(410059);
  371. }
  372. //用户标签
  373. if ($lottery['user_label']) {
  374. /** @var UserLabelRelationServices $userlableRelation */
  375. $userlableRelation = app()->make(UserLabelRelationServices::class);
  376. $user_labels = $userlableRelation->getUserLabels($uid);
  377. if (!array_intersect($lottery['user_label'], $user_labels)) {
  378. throw new ApiException(410059);
  379. }
  380. }
  381. //是否是付费会员
  382. if ($lottery['is_svip'] != -1) {
  383. if (($lottery['is_svip'] == 1 && $userInfo['is_money_level'] <= 0) || ($lottery['is_svip'] == 0 && $userInfo['is_money_level'] > 0)) {
  384. throw new ApiException(410059);
  385. }
  386. }
  387. }
  388. return true;
  389. }
  390. /**
  391. * 抽奖
  392. * @param int $uid
  393. * @param int $lottery_id
  394. * @return mixed
  395. * @throws \Psr\SimpleCache\InvalidArgumentException
  396. * @throws \think\db\exception\DataNotFoundException
  397. * @throws \think\db\exception\DbException
  398. * @throws \think\db\exception\ModelNotFoundException
  399. */
  400. public function luckLottery(int $uid, int $lottery_id)
  401. {
  402. /** @var UserServices $userServices */
  403. $userServices = app()->make(UserServices::class);
  404. $userInfo = $userServices->getUserInfo($uid);
  405. if (!$userInfo) {
  406. throw new ApiException(410032);
  407. }
  408. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  409. if (!$lottery) {
  410. throw new ApiException(410057);
  411. }
  412. $userInfo = $userInfo->toArray();
  413. $lottery = $lottery->toArray();
  414. //验证用户身份
  415. $this->checkoutUserAuth($uid, $lottery_id, $userInfo, $lottery);
  416. /** @var LuckPrizeServices $lotteryPrizeServices */
  417. $lotteryPrizeServices = app()->make(LuckPrizeServices::class);
  418. $lotteryPrize = $lotteryPrizeServices->getPrizeList($lottery_id);
  419. if (!$lotteryPrize) {
  420. throw new ApiException(410060);
  421. }
  422. if ($this->getLotteryNum($uid, $lottery_id, $userInfo, $lottery) < 1) {
  423. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  424. switch ($lottery['factor']) {
  425. case 1:
  426. throw new ApiException(410061);
  427. case 2:
  428. throw new ApiException(410062);
  429. case 3:
  430. throw new ApiException(410063);
  431. case 4:
  432. throw new ApiException(410064);
  433. case 5:
  434. throw new ApiException(410065);
  435. default:
  436. throw new ApiException(410058);
  437. }
  438. }
  439. return $this->transaction(function () use ($uid, $lotteryPrize, $userInfo, $lottery) {
  440. /** @var LuckPrizeServices $luckPrizeServices */
  441. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  442. //随机抽奖
  443. $prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
  444. if (!$prize) {
  445. throw new ApiException(410060);
  446. }
  447. //中奖扣除积分、余额
  448. $this->lotteryFactor($uid, $userInfo, $lottery);
  449. //中奖减少奖品数量
  450. $luckPrizeServices->decPrizeNum($prize['id'], $prize);
  451. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  452. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  453. //中奖写入记录
  454. $record = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo);
  455. //不是站内商品直接领奖
  456. if ($prize['type'] != 6) {
  457. $lotteryRecordServices->receivePrize($uid, (int)$record->id);
  458. }
  459. $prize['lottery_record_id'] = $record->id;
  460. return $prize;
  461. });
  462. }
  463. /**
  464. * 抽奖消耗扣除用户积分、余额等
  465. * @param int $uid
  466. * @param array $userInfo
  467. * @param array $lottery
  468. * @return bool
  469. * @throws \Psr\SimpleCache\InvalidArgumentException
  470. */
  471. public function lotteryFactor(int $uid, array $userInfo, array $lottery)
  472. {
  473. if (!$userInfo || !$lottery) {
  474. return true;
  475. }
  476. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  477. switch ($lottery['factor']) {
  478. case 1:
  479. if ($userInfo['integral'] > $lottery['factor_num']) {
  480. $integral = bcsub((string)$userInfo['integral'], (string)$lottery['factor_num'], 0);
  481. } else {
  482. $integral = 0;
  483. }
  484. /** @var UserServices $userServices */
  485. $userServices = app()->make(UserServices::class);
  486. /** @var UserBillServices $userBillServices */
  487. $userBillServices = app()->make(UserBillServices::class);
  488. $userBillServices->income('lottery_use_integral', $uid, $lottery['factor_num'], $integral, $lottery['id']);
  489. if (!$userServices->update($uid, ['integral' => $integral], 'uid')) {
  490. throw new ApiException(410066);
  491. }
  492. break;
  493. case 2:
  494. if ($userInfo['now_money'] >= $lottery['factor_num']) {
  495. $now_money = bcsub((string)$userInfo['now_money'], (string)$lottery['factor_num'], 2);
  496. } else {
  497. throw new ApiException(410067);
  498. }
  499. /** @var UserServices $userServices */
  500. $userServices = app()->make(UserServices::class);
  501. /** @var UserMoneyServices $userMoneyServices */
  502. $userMoneyServices = app()->make(UserMoneyServices::class);
  503. $userMoneyServices->income('lottery_use_money', $uid, $lottery['factor_num'], $now_money, $lottery['id']);
  504. if (!$userServices->update($uid, ['now_money' => $now_money], 'uid')) {
  505. throw new ApiException(410068);
  506. }
  507. break;
  508. case 3:
  509. case 4:
  510. //销毁抽奖次数缓存
  511. $this->delCacheLotteryNum($uid, $lottery['factor'] == 3 ? 'order' : 'comment');
  512. break;
  513. case 5:
  514. /** @var UserServices $userServices */
  515. $userServices = app()->make(UserServices::class);
  516. $spread_lottery = 0;
  517. if ($userInfo['spread_lottery'] > 1) {
  518. $spread_lottery = $userInfo['spread_lottery'] - 1;
  519. }
  520. if (!$userServices->update($uid, ['spread_lottery' => $spread_lottery], 'uid')) {
  521. throw new ApiException(410069);
  522. }
  523. break;
  524. default:
  525. throw new ApiException(410058);
  526. }
  527. return true;
  528. }
  529. /**
  530. * 删除
  531. * @param int $id
  532. * @return bool
  533. * @throws \think\db\exception\DataNotFoundException
  534. * @throws \think\db\exception\DbException
  535. * @throws \think\db\exception\ModelNotFoundException
  536. */
  537. public function delLottery(int $id)
  538. {
  539. if ($lottery = $this->dao->getLottery($id)) {
  540. if (!$this->dao->update(['id' => $id], ['is_del' => 1])) {
  541. throw new AdminException(100008);
  542. }
  543. }
  544. return true;
  545. }
  546. /**
  547. * 设置抽奖活动状态
  548. * @param int $id
  549. * @param $status
  550. * @return false|mixed
  551. * @throws \think\db\exception\DataNotFoundException
  552. * @throws \think\db\exception\DbException
  553. * @throws \think\db\exception\ModelNotFoundException
  554. */
  555. public function setStatus(int $id, $status)
  556. {
  557. if (!$id) return false;
  558. $lottery = $this->dao->getLottery($id, 'id,factor');
  559. if (!$lottery) {
  560. return false;
  561. }
  562. //每一种抽奖类型只有一个上架
  563. if ($status) {
  564. $this->dao->update(['factor' => $lottery['factor']], ['status' => 0]);
  565. }
  566. return $this->dao->update($id, ['status' => $status], 'id');
  567. }
  568. /**
  569. * 下单支付、评论缓存抽奖次数
  570. * @param int $uid
  571. * @param string $type
  572. * @return bool
  573. * @throws \Psr\SimpleCache\InvalidArgumentException
  574. */
  575. public function setCacheLotteryNum(int $uid, string $type = 'order')
  576. {
  577. $factor = $type == 'order' ? 3 : 4;
  578. $lottery = $this->dao->getFactorLottery($factor, 'id,factor_num', ['prize'], true);
  579. if (!$lottery || !$lottery['factor_num']) {
  580. return true;
  581. }
  582. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  583. return CacheService::set($key, $lottery['factor_num'], 120);
  584. }
  585. /**
  586. * 取出下单支付、评论得到的抽奖此处
  587. * @param int $uid
  588. * @param string $type
  589. * @return int|mixed
  590. * @throws \Psr\SimpleCache\InvalidArgumentException
  591. */
  592. public function getCacheLotteryNum(int $uid, string $type = 'order')
  593. {
  594. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  595. $num = CacheService::get($key);
  596. return empty($num) ? 0 : $num;
  597. }
  598. /**
  599. * 抽奖之后销毁缓存
  600. * @param int $uid
  601. * @param string $type
  602. * @return bool
  603. * @throws \Psr\SimpleCache\InvalidArgumentException
  604. */
  605. public function delCacheLotteryNum(int $uid, string $type = 'order')
  606. {
  607. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  608. $num = $this->getCacheLotteryNum($uid, $type);
  609. if ($num > 1) {
  610. CacheService::set($key, $num - 1, 120);
  611. } else {
  612. CacheService::delete($key);
  613. }
  614. return true;
  615. }
  616. }