LuckLotteryServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\label\UserLabelServices;
  18. use app\services\user\UserBillServices;
  19. use app\services\user\label\UserLabelRelationServices;
  20. use app\services\user\UserMoneyServices;
  21. use app\services\user\UserServices;
  22. use crmeb\exceptions\AdminException;
  23. use crmeb\services\CacheService;
  24. use think\exception\ValidateException;
  25. /**
  26. *
  27. * Class LuckLotteryServices
  28. * @package app\services\activity\lottery
  29. * @mixin LuckLotteryDao
  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. * 抽奖次数缓存时间
  53. * @var int
  54. */
  55. public $luck_cache_time = 120;
  56. /**
  57. * LuckLotteryServices constructor.
  58. * @param LuckLotteryDao $dao
  59. */
  60. public function __construct(LuckLotteryDao $dao)
  61. {
  62. $this->dao = $dao;
  63. }
  64. /**
  65. * 获取抽奖缓存
  66. * @param int $factor
  67. * @return mixed
  68. * @author 等风来
  69. * @email 136327134@qq.com
  70. * @date 2022/11/16
  71. */
  72. public function getFactorLotteryCache(int $factor)
  73. {
  74. return $this->dao->cacheTag()->remember('' . $factor, function () use ($factor) {
  75. $lottery = $this->dao->getFactorLottery($factor);
  76. return $lottery ? $lottery->toArray() : null;
  77. });
  78. }
  79. /**
  80. * 获取列表
  81. * @param array $where
  82. * @return array
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function getList(array $where)
  88. {
  89. [$page, $limit] = $this->getPageValue();
  90. $where['is_del'] = 0;
  91. $list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
  92. $lottery_factor = $this->lottery_factor;
  93. /** @var LuckLotteryRecordServices $luckLotteryRecordServices */
  94. $luckLotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  95. foreach ($list as &$item) {
  96. $item['lottery_type'] = $lottery_factor[$item['factor']] ?? '';
  97. $data = $luckLotteryRecordServices->getLotteryRecordData((int)$item['id']);
  98. $item['lottery_all'] = $data['all'] ?? 0;
  99. $item['lottery_people'] = $data['people'] ?? 0;
  100. $item['lottery_win'] = $data['win'] ?? 0;
  101. if ($item['status']) {
  102. if ($item['start_time'] == 0 && $item['end_time'] == 0) {
  103. $item['lottery_status'] = '进行中';
  104. } else {
  105. if ($item['start_time'] > time())
  106. $item['lottery_status'] = '未开始';
  107. else if ($item['end_time'] < time())
  108. $item['lottery_status'] = '已结束';
  109. else if ($item['end_time'] > time() && $item['start_time'] < time()) {
  110. $item['lottery_status'] = '进行中';
  111. }
  112. }
  113. } else $item['lottery_status'] = '已结束';
  114. $item['start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '';
  115. $item['end_time'] = $item['end_time'] ? date('Y-m-d H:i:s', $item['end_time']) : '';
  116. }
  117. $count = $this->dao->count($where);
  118. return compact('list', 'count');
  119. }
  120. /**
  121. * 获取抽奖详情
  122. * @param int $id
  123. * @return array|\think\Model
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function getlotteryInfo(int $id)
  129. {
  130. $lottery = $this->dao->getLottery($id, '*', ['prize']);
  131. if (!$lottery) {
  132. throw new ValidateException('活动不存在或已删除');
  133. }
  134. $lottery = $lottery->toArray();
  135. if (isset($lottery['prize']) && $lottery['prize']) {
  136. $products = $coupons = [];
  137. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  138. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  139. /** @var StoreProductServices $productServices */
  140. $productServices = app()->make(StoreProductServices::class);
  141. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  142. /** @var StoreCouponIssueServices $couponServices */
  143. $couponServices = app()->make(StoreCouponIssueServices::class);
  144. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  145. foreach ($lottery['prize'] as &$prize) {
  146. $prize['coupon_title'] = $prize['goods_image'] = '';
  147. if ($prize['type'] == 6) {
  148. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  149. }
  150. if ($prize['type'] == 5) {
  151. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  152. }
  153. }
  154. }
  155. return $lottery;
  156. }
  157. /**
  158. * 根据类型获取数据
  159. * @param int $factor
  160. * @return array
  161. * @throws \think\db\exception\DataNotFoundException
  162. * @throws \think\db\exception\DbException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. */
  165. public function getlotteryFactorInfo(int $factor)
  166. {
  167. $lottery = $this->dao->getFactorLottery($factor, '*', ['prize'], false);
  168. if (!$lottery) {
  169. return [];
  170. }
  171. $lottery = $lottery->toArray();
  172. if (isset($lottery['prize']) && $lottery['prize']) {
  173. $products = $coupons = [];
  174. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  175. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  176. /** @var StoreProductServices $productServices */
  177. $productServices = app()->make(StoreProductServices::class);
  178. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  179. /** @var StoreCouponIssueServices $couponServices */
  180. $couponServices = app()->make(StoreCouponIssueServices::class);
  181. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  182. foreach ($lottery['prize'] as &$prize) {
  183. $prize['coupon_title'] = $prize['goods_image'] = '';
  184. if ($prize['type'] == 6) {
  185. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  186. }
  187. if ($prize['type'] == 5) {
  188. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  189. }
  190. }
  191. /** @var UserLabelServices $labelService */
  192. $labelService = app()->make(UserLabelServices::class);
  193. $lottery['user_label'] = $lottery['user_label'] ? $labelService->getColumn([
  194. ['id', 'in', $lottery['user_label']],
  195. ], 'id,label_name') : [];
  196. }
  197. return $lottery;
  198. }
  199. /**
  200. * 添加抽奖活动以及奖品
  201. * @param array $data
  202. * @return mixed
  203. */
  204. public function add(array $data)
  205. {
  206. $prizes = $data['prize'];
  207. $prize_num = $this->lottery_type[1];
  208. if (count($prizes) != $prize_num) {
  209. throw new ValidateException('请选择' . $prize_num . '个奖品');
  210. }
  211. unset($data['prize']);
  212. $this->transaction(function () use ($data, $prizes) {
  213. $time = time();
  214. $data['add_time'] = $time;
  215. if (!$lottery = $this->dao->save($data)) {
  216. throw new ValidateException('添加抽奖活动失败');
  217. }
  218. if ($data['status']) {
  219. $this->setStatus((int)$lottery->id, $data['status']);
  220. }
  221. /** @var LuckPrizeServices $luckPrizeServices */
  222. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  223. $sort = 1;
  224. $typeTrue = false;
  225. foreach ($prizes as $prize) {
  226. $prize = $luckPrizeServices->checkPrizeData($prize);
  227. if ($prize['type'] == 1) {
  228. $typeTrue = true;
  229. }
  230. $prize['lottery_id'] = $lottery->id;
  231. unset($prize['id']);
  232. $prize['add_time'] = $time;
  233. $prize['sort'] = $sort;
  234. $res = $luckPrizeServices->save($prize);
  235. if (!$res) {
  236. throw new ValidateException('添加抽奖奖品失败');
  237. }
  238. $sort++;
  239. //抽奖商品库存加入redis
  240. CacheService::setStock((string)$res->id, (int)$prize['total'], 6);
  241. }
  242. if (!$typeTrue) {
  243. throw new ValidateException('添加抽奖奖品至少需要一个未中奖奖项');
  244. }
  245. return true;
  246. });
  247. $this->dao->cacheTag()->clear();
  248. return true;
  249. }
  250. /**
  251. * 修改抽奖活动以及奖品
  252. * @param int $id
  253. * @param array $data
  254. * @return mixed
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function edit(int $id, array $data)
  260. {
  261. $lottery = $this->dao->getLottery($id);
  262. if (!$lottery) {
  263. throw new ValidateException('抽奖活动不存在');
  264. }
  265. $newPrizes = $data['prize'];
  266. unset($data['prize'], $data['id']);
  267. $prize_num = $this->lottery_type[1];
  268. if (count($newPrizes) != $prize_num) {
  269. throw new ValidateException('请选择' . $prize_num . '个奖品');
  270. }
  271. /** @var LuckPrizeServices $luckPrizeServices */
  272. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  273. $prizes = $luckPrizeServices->getLotteryPrizeList($id);
  274. $this->transaction(function () use ($id, $lottery, $data, $newPrizes, $prizes, $luckPrizeServices) {
  275. $updateIds = array_column($newPrizes, 'id');
  276. $oldIds = array_column($prizes, 'id');
  277. $delIds = array_merge(array_diff($oldIds, $updateIds));
  278. $insert = [];
  279. $time = time();
  280. $sort = 1;
  281. foreach ($newPrizes as $prize) {
  282. $prize = $luckPrizeServices->checkPrizeData($prize);
  283. $prize['sort'] = $sort;
  284. if (isset($prize['id']) && $prize['id']) {
  285. $prizeId = $prize['id'];
  286. if (!$prize['lottery_id']) {
  287. throw new ValidateException('缺少活动ID');
  288. }
  289. if (!$luckPrizeServices->update($prize['id'], $prize, 'id')) {
  290. throw new ValidateException('修改奖品失败');
  291. }
  292. } else {
  293. unset($prize['id']);
  294. $prize['lottery_id'] = $id;
  295. $prize['add_time'] = $time;
  296. $prize['sort'] = $sort;
  297. $res = $luckPrizeServices->save($prize);
  298. if (!$res) {
  299. throw new ValidateException('新增奖品失败');
  300. }
  301. $prizeId = $res->id;
  302. }
  303. $sort++;
  304. //抽奖商品库存加入redis
  305. CacheService::setStock((string)$prizeId, (int)$prize['total'], 6);
  306. }
  307. if ($delIds) {
  308. if (!$luckPrizeServices->update([['id', 'in', $delIds]], ['is_del' => 1])) {
  309. throw new ValidateException('删除奖品失败');
  310. }
  311. }
  312. if (!$this->dao->update($id, $data)) {
  313. throw new ValidateException('修改失败');
  314. }
  315. //上架
  316. if (!$lottery['status'] && $data['status']) {
  317. $this->setStatus($id, $data['status']);
  318. }
  319. return true;
  320. });
  321. $this->dao->cacheTag()->clear();
  322. return true;
  323. }
  324. /**
  325. * 获取用户某个抽奖活动剩余抽奖次数
  326. * @param int $uid
  327. * @param int $lottery_id
  328. * @param array $userInfo
  329. * @param array $lottery
  330. * @return false|float|int|mixed
  331. * @throws \Psr\SimpleCache\InvalidArgumentException
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\DbException
  334. * @throws \think\db\exception\ModelNotFoundException
  335. */
  336. public function getLotteryNum(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  337. {
  338. /** @var UserServices $userServices */
  339. $userServices = app()->make(UserServices::class);
  340. if (!$userInfo) {
  341. $userInfo = $userServices->getUserInfo($uid);
  342. }
  343. if (!$userInfo) {
  344. throw new ValidateException('用户不存在');
  345. }
  346. if (!$lottery) {
  347. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  348. }
  349. if (!$lottery) {
  350. throw new ValidateException('该活动已经下架,请持续关注');
  351. }
  352. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  353. switch ($lottery['factor']) {
  354. case 1:
  355. return $userInfo['integral'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['integral'] / $lottery['factor_num']) : 0;
  356. break;
  357. case 2:
  358. return $userInfo['now_money'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['now_money'] / $lottery['factor_num']) : 0;
  359. break;
  360. case 3:
  361. return $this->getCacheLotteryNum($uid, 'order');
  362. break;
  363. case 4:
  364. return $this->getCacheLotteryNum($uid, 'comment');
  365. break;
  366. case 5:
  367. return $userInfo['spread_lottery'] ?? 0;
  368. break;
  369. default:
  370. throw new ValidateException('暂未有该类型活动');
  371. break;
  372. }
  373. }
  374. /**
  375. * 验证用户抽奖资格(用户等级、付费会员、用户标签)
  376. * @param int $uid
  377. * @param int $lottery_id
  378. * @param array $userInfo
  379. * @param array $lottery
  380. * @return bool
  381. * @throws \think\db\exception\DataNotFoundException
  382. * @throws \think\db\exception\DbException
  383. * @throws \think\db\exception\ModelNotFoundException
  384. */
  385. public function checkoutUserAuth(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  386. {
  387. if (!$userInfo) {
  388. /** @var UserServices $userServices */
  389. $userServices = app()->make(UserServices::class);
  390. $userInfo = $userServices->getUserInfo($uid);
  391. }
  392. if (!$userInfo) {
  393. throw new ValidateException('用户不存在');
  394. }
  395. if (!$lottery) {
  396. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  397. }
  398. if (!$lottery) {
  399. throw new ValidateException('该活动已经下架,请持续关注');
  400. }
  401. //部分用户参与
  402. if ($lottery['attends_user'] == 2) {
  403. //用户等级
  404. if ($lottery['user_level'] && !in_array($userInfo['level'], $lottery['user_level'])) {
  405. throw new ValidateException('您暂时无法参与该活动');
  406. }
  407. //用户标签
  408. if ($lottery['user_label']) {
  409. /** @var UserLabelRelationServices $userlableRelation */
  410. $userlableRelation = app()->make(UserLabelRelationServices::class);
  411. $user_labels = $userlableRelation->getUserLabels($uid);
  412. if (!array_intersect($lottery['user_label'], $user_labels)) {
  413. throw new ValidateException('您暂时无法参与该活动');
  414. }
  415. }
  416. //是否是付费会员
  417. if ($lottery['is_svip'] != -1) {
  418. if (($lottery['is_svip'] == 1 && $userInfo['is_money_level'] <= 0) || ($lottery['is_svip'] == 0 && $userInfo['is_money_level'] > 0)) {
  419. throw new ValidateException('您暂时无法参与该活动');
  420. }
  421. }
  422. }
  423. return true;
  424. }
  425. /**
  426. * 抽奖
  427. * @param int $uid
  428. * @param int $lottery_id
  429. * @return mixed
  430. * @throws \Psr\SimpleCache\InvalidArgumentException
  431. * @throws \think\db\exception\DataNotFoundException
  432. * @throws \think\db\exception\DbException
  433. * @throws \think\db\exception\ModelNotFoundException
  434. */
  435. public function luckLottery(int $uid, int $lottery_id)
  436. {
  437. /** @var UserServices $userServices */
  438. $userServices = app()->make(UserServices::class);
  439. $userInfo = $userServices->getUserInfo($uid);
  440. if (!$userInfo) {
  441. throw new ValidateException('用户不存在');
  442. }
  443. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  444. if (!$lottery) {
  445. throw new ValidateException('该活动已经下架,请持续关注');
  446. }
  447. $userInfo = $userInfo->toArray();
  448. $lottery = $lottery->toArray();
  449. //验证用户身份
  450. $this->checkoutUserAuth($uid, $lottery_id, $userInfo, $lottery);
  451. /** @var LuckPrizeServices $lotteryPrizeServices */
  452. $lotteryPrizeServices = app()->make(LuckPrizeServices::class);
  453. $lotteryPrize = $lotteryPrizeServices->getPrizeList($lottery_id);
  454. if (!$lotteryPrize) {
  455. throw new ValidateException('该活动状态有误,请联系管理员');
  456. }
  457. if ($this->getLotteryNum($uid, $lottery_id, $userInfo, $lottery) < 1) {
  458. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  459. switch ($lottery['factor']) {
  460. case 1:
  461. throw new ValidateException('积分不足,没有更多抽奖次数');
  462. break;
  463. case 2:
  464. throw new ValidateException('余额不足,没有更多抽奖次数');
  465. break;
  466. case 3:
  467. throw new ValidateException('购买商品之后获得更多抽奖次数');
  468. break;
  469. case 4:
  470. throw new ValidateException('订单完成评价之后获得更多抽奖次数');
  471. break;
  472. case 5:
  473. throw new ValidateException('邀请更多好友获取抽奖次数');
  474. break;
  475. default:
  476. throw new ValidateException('暂未有该类型活动');
  477. break;
  478. }
  479. }
  480. if ($lottery['factor'] == 1) {//积分抽奖
  481. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  482. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  483. $data = $lotteryRecordServices->getLotteryNum($uid, (int)$lottery['id']);
  484. $lotteryData['todayCount'] = (int)max(bcsub((string)$lottery['lottery_num'], (string)$data['todayCount'], 0), 0);
  485. $lotteryData['totalCount'] = (int)max(bcsub((string)$lottery['total_lottery_num'], (string)$data['totalCount'], 0), 0);
  486. if ($lotteryData['todayCount'] <= 0) {
  487. throw new ValidateException('本次活动当天抽奖次数已用完!');
  488. }
  489. if ($lotteryData['totalCount'] <= 0) {
  490. throw new ValidateException('本次活动总抽奖次数已用完!');
  491. }
  492. }
  493. /** @var LuckPrizeServices $luckPrizeServices */
  494. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  495. //随机抽奖
  496. $prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
  497. if (!$prize) {
  498. throw new ValidateException('该活动状态有误,请联系管理员');
  499. }
  500. try {
  501. $res = $this->transaction(function () use ($prize, $luckPrizeServices, $uid, $lotteryPrize, $userInfo, $lottery) {
  502. //中奖扣除积分、余额
  503. $this->lotteryFactor($uid, $userInfo, $lottery);
  504. //中奖减少奖品数量
  505. $luckPrizeServices->decPrizeNum($prize['id'], $prize);
  506. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  507. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  508. //中奖写入记录
  509. $record = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo);
  510. //不是站内商品直接领奖
  511. if ($prize['type'] != 6) {
  512. $lotteryRecordServices->receivePrize($uid, (int)$record->id);
  513. }
  514. $prize['lottery_record_id'] = $record->id;
  515. return $prize;
  516. });
  517. } catch (\Throwable $e) {
  518. throw new ValidateException($e->getMessage());
  519. //发生错误回退库存
  520. CacheService::setStock((string)$prize['id'], 1, 6, false);
  521. }
  522. return $res;
  523. }
  524. /**
  525. * 抽奖消耗扣除用户积分、余额等
  526. * @param int $uid
  527. * @param array $userInfo
  528. * @param array $lottery
  529. * @return bool
  530. * @throws \Psr\SimpleCache\InvalidArgumentException
  531. */
  532. public function lotteryFactor(int $uid, array $userInfo, array $lottery)
  533. {
  534. if (!$userInfo || !$lottery) {
  535. return true;
  536. }
  537. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  538. switch ($lottery['factor']) {
  539. case 1:
  540. if ($userInfo['integral'] > $lottery['factor_num']) {
  541. $integral = bcsub((string)$userInfo['integral'], (string)$lottery['factor_num'], 0);
  542. } else {
  543. $integral = 0;
  544. }
  545. /** @var UserServices $userServices */
  546. $userServices = app()->make(UserServices::class);
  547. /** @var UserBillServices $userBillServices */
  548. $userBillServices = app()->make(UserBillServices::class);
  549. $userBillServices->income('lottery_use_integral', $uid, (int)$lottery['factor_num'], (int)$integral, $lottery['id']);
  550. if (!$userServices->update($uid, ['integral' => $integral], 'uid')) {
  551. throw new ValidateException('抽奖扣除用户积分失败');
  552. }
  553. break;
  554. case 2:
  555. if ($userInfo['now_money'] >= $lottery['factor_num']) {
  556. $now_money = bcsub((string)$userInfo['now_money'], (string)$lottery['factor_num'], 2);
  557. } else {
  558. throw new ValidateException('抽奖失败,余额不足!');
  559. }
  560. /** @var UserServices $userServices */
  561. $userServices = app()->make(UserServices::class);
  562. /** @var UserMoneyServices $userMoneyServices */
  563. $userMoneyServices = app()->make(UserMoneyServices::class);
  564. $userMoneyServices->income('lottery_use_money', $uid, $lottery['factor_num'], $now_money, $lottery['id']);
  565. if (!$userServices->update($uid, ['now_money' => $now_money], 'uid')) {
  566. throw new ValidateException('抽奖扣除用户余额失败');
  567. }
  568. break;
  569. case 3:
  570. case 4:
  571. //销毁抽奖次数缓存
  572. $this->delCacheLotteryNum($uid, $lottery['factor'] == 3 ? 'order' : 'comment');
  573. break;
  574. case 5:
  575. /** @var UserServices $userServices */
  576. $userServices = app()->make(UserServices::class);
  577. $spread_lottery = 0;
  578. if ($userInfo['spread_lottery'] > 1) {
  579. $spread_lottery = $userInfo['spread_lottery'] - 1;
  580. }
  581. if (!$userServices->update($uid, ['spread_lottery' => $spread_lottery], 'uid')) {
  582. throw new ValidateException('抽奖扣除用户推广获取抽奖次数失败');
  583. }
  584. break;
  585. default:
  586. throw new ValidateException('暂未有该类型活动');
  587. break;
  588. }
  589. return true;
  590. }
  591. /**
  592. * 删除
  593. * @param int $id
  594. * @return bool
  595. * @throws \think\db\exception\DataNotFoundException
  596. * @throws \think\db\exception\DbException
  597. * @throws \think\db\exception\ModelNotFoundException
  598. */
  599. public function delLottery(int $id)
  600. {
  601. if ($lottery = $this->dao->getLottery($id)) {
  602. if (!$this->dao->update(['id' => $id], ['is_del' => 1])) {
  603. throw new AdminException('删除失败,请稍候重试');
  604. }
  605. }
  606. return true;
  607. }
  608. /**
  609. * 设置抽奖活动状态
  610. * @param int $id
  611. * @param $status
  612. * @return false|mixed
  613. * @throws \think\db\exception\DataNotFoundException
  614. * @throws \think\db\exception\DbException
  615. * @throws \think\db\exception\ModelNotFoundException
  616. */
  617. public function setStatus(int $id, $status)
  618. {
  619. if (!$id) return false;
  620. $lottery = $this->dao->getLottery($id, 'id,factor');
  621. if (!$lottery) {
  622. return false;
  623. }
  624. //每一种抽奖类型只有一个上架
  625. if ($status) {
  626. $this->dao->update(['factor' => $lottery['factor']], ['status' => 0]);
  627. }
  628. return $this->dao->update($id, ['status' => $status], 'id');
  629. }
  630. /**
  631. * 下单支付、评论缓存抽奖次数
  632. * @param int $uid
  633. * @param string $type
  634. * @return bool
  635. * @throws \Psr\SimpleCache\InvalidArgumentException
  636. */
  637. public function setCacheLotteryNum(int $uid, string $type = 'order')
  638. {
  639. $factor = $type == 'order' ? 3 : 4;
  640. $lottery = $this->dao->getFactorLottery($factor, 'id,factor_num');
  641. if (!$lottery || !$lottery['factor_num']) {
  642. return true;
  643. }
  644. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  645. $timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
  646. $cache = CacheService::redisHandler();
  647. // $num = (int)$this->getCacheLotteryNum($uid, $type);
  648. $cache->set($timeKey, bcadd((string)time(), (string)$this->luck_cache_time), bcadd('10', (string)$this->luck_cache_time));
  649. return $cache->set($key, $lottery['factor_num'], $this->luck_cache_time);
  650. }
  651. /**
  652. * 获取抽奖到期时间
  653. * @param int $uid
  654. * @param string $type
  655. * @return int|mixed
  656. * @throws \Psr\SimpleCache\InvalidArgumentException
  657. */
  658. public function getCacheLotteryExpireTime(int $uid, string $type = 'order')
  659. {
  660. $timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
  661. $time = CacheService::redisHandler()->get($timeKey);
  662. return empty($time) ? 0 : $time;
  663. }
  664. /**
  665. * 取出下单支付、评论得到的抽奖此处
  666. * @param int $uid
  667. * @param string $type
  668. * @return int|mixed
  669. * @throws \Psr\SimpleCache\InvalidArgumentException
  670. */
  671. public function getCacheLotteryNum(int $uid, string $type = 'order')
  672. {
  673. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  674. $num = CacheService::redisHandler()->get($key);
  675. return empty($num) ? 0 : $num;
  676. }
  677. /**
  678. * 抽奖之后销毁缓存
  679. * @param int $uid
  680. * @param string $type
  681. * @return bool
  682. * @throws \Psr\SimpleCache\InvalidArgumentException
  683. */
  684. public function delCacheLotteryNum(int $uid, string $type = 'order')
  685. {
  686. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  687. $timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
  688. $num = $this->getCacheLotteryNum($uid, $type);
  689. $cache = CacheService::redisHandler();
  690. if ($num > 1) {
  691. $cache->set($timeKey, bcadd((string)time(), (string)$this->luck_cache_time), bcadd('10', (string)$this->luck_cache_time));
  692. $cache->set($key, $num - 1, $this->luck_cache_time);
  693. } else {
  694. $cache->delete($timeKey);
  695. $cache->delete($key);
  696. }
  697. return true;
  698. }
  699. }