LuckLotteryServices.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. if ($where['time'] == '') {
  64. $where['time'] = [];
  65. } else {
  66. $time = explode('-', $where['time']);
  67. $where['time'] = [strtotime($time[0]), strtotime($time[1]) + 86399];
  68. }
  69. $data = $this->dao->getList($where, '*', 'id desc', $page, $limit);
  70. foreach ($data['list'] as &$item) {
  71. $item['lottery_type'] = $this->lottery_factor[$item['factor']] ?? '未知';
  72. if ($item['start_time'] > time()) {
  73. $item['status_name'] = '未开始';
  74. $item['lottery_status'] = 0;
  75. } else if ($item['end_time'] < time()) {
  76. $item['status_name'] = '已结束';
  77. $item['lottery_status'] = 2;
  78. } else if ($item['end_time'] > time() && $item['start_time'] < time()) {
  79. $item['status_name'] = '进行中';
  80. $item['lottery_status'] = 1;
  81. }
  82. $item['start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '';
  83. $item['end_time'] = $item['end_time'] ? date('Y-m-d 23:59:59', $item['end_time']) : '';
  84. if (!count($item['records'])) {
  85. $item['records_total_user'] = 0;
  86. $item['records_wins_user'] = 0;
  87. $item['records_total_num'] = 0;
  88. $item['records_wins_num'] = 0;
  89. } else {
  90. $item['records_total_user'] = $item['records'][0]['total_user'];
  91. $item['records_wins_user'] = $item['records'][0]['wins_user'];
  92. $item['records_total_num'] = $item['records'][0]['total_num'];
  93. $item['records_wins_num'] = $item['records'][0]['wins_num'];
  94. }
  95. }
  96. return $data;
  97. }
  98. /**
  99. * 获取抽奖详情
  100. * @param int $id
  101. * @return array|\think\Model
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function getLotteryInfo(int $id)
  107. {
  108. $lottery = $this->dao->getLottery($id, '*', ['prize']);
  109. if (!$lottery) {
  110. throw new ApiException(410057);
  111. }
  112. $lottery = $lottery->toArray();
  113. if (isset($lottery['prize']) && $lottery['prize']) {
  114. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  115. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  116. /** @var StoreProductServices $productServices */
  117. $productServices = app()->make(StoreProductServices::class);
  118. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  119. /** @var StoreCouponIssueServices $couponServices */
  120. $couponServices = app()->make(StoreCouponIssueServices::class);
  121. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  122. foreach ($lottery['prize'] as &$prize) {
  123. $prize['coupon_title'] = $prize['goods_image'] = '';
  124. if ($prize['type'] == 6) {
  125. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  126. }
  127. if ($prize['type'] == 5) {
  128. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  129. }
  130. }
  131. }
  132. return $lottery;
  133. }
  134. /**
  135. * 根据类型获取数据
  136. * @param int $factor
  137. * @return array
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function getlotteryFactorInfo(int $factor)
  143. {
  144. $lottery = $this->dao->getFactorLottery($factor, '*', ['prize']);
  145. if (!$lottery) {
  146. return [];
  147. }
  148. $lottery = $lottery->toArray();
  149. if (isset($lottery['prize']) && $lottery['prize']) {
  150. $product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
  151. $coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
  152. /** @var StoreProductServices $productServices */
  153. $productServices = app()->make(StoreProductServices::class);
  154. $products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
  155. /** @var StoreCouponIssueServices $couponServices */
  156. $couponServices = app()->make(StoreCouponIssueServices::class);
  157. $coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
  158. foreach ($lottery['prize'] as &$prize) {
  159. $prize['coupon_title'] = $prize['goods_image'] = '';
  160. if ($prize['type'] == 6) {
  161. $prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
  162. }
  163. if ($prize['type'] == 5) {
  164. $prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
  165. }
  166. }
  167. }
  168. foreach ($lottery['user_level'] as &$item) {
  169. $item = (int)$item;
  170. }
  171. /** @var UserLabelServices $userLabelServices */
  172. $userLabelServices = app()->make(UserLabelServices::class);
  173. $lottery['user_label'] = !empty($lottery['user_label']) ? $userLabelServices->getLabelList(['ids' => $lottery['user_label']], ['id', 'label_name']) : [];
  174. return $lottery;
  175. }
  176. /**
  177. * 添加抽奖活动以及奖品
  178. * @param array $data
  179. * @return mixed
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function add(array $data)
  185. {
  186. $prizes = $data['prize'];
  187. $total = array_sum(array_column($prizes, 'percent'));
  188. if ($total != 100) {
  189. throw new AdminException('奖品概率之和不是100%,请检查!');
  190. }
  191. $prize_num = $this->lottery_type[1];
  192. if (count($prizes) != $prize_num) {
  193. throw new AdminException(400535);
  194. }
  195. unset($data['prize']);
  196. return $this->transaction(function () use ($data, $prizes) {
  197. $time = time();
  198. $data['add_time'] = $time;
  199. if (!$lottery = $this->dao->save($data)) {
  200. throw new AdminException(400536);
  201. }
  202. if ($data['status']) {
  203. $this->setStatus((int)$lottery->id, $data['status']);
  204. }
  205. /** @var LuckPrizeServices $luckPrizeServices */
  206. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  207. $data = [];
  208. $sort = 1;
  209. $prizeStatus = false;
  210. foreach ($prizes as $prize) {
  211. if (isset($prize['type']) && $prize['type'] == 1) $prizeStatus = true;
  212. $prize = $luckPrizeServices->checkPrizeData($prize);
  213. $prize['lottery_id'] = $lottery->id;
  214. unset($prize['id']);
  215. $prize['add_time'] = $time;
  216. $prize['sort'] = $sort;
  217. $data[] = $prize;
  218. $sort++;
  219. }
  220. if (!$prizeStatus) {
  221. throw new AdminException('必须设置至少一个未中奖');
  222. }
  223. if (!$luckPrizeServices->saveAll($data)) {
  224. throw new AdminException(400536);
  225. }
  226. return true;
  227. });
  228. }
  229. /**
  230. * 修改抽奖活动以及奖品
  231. * @param int $id
  232. * @param array $data
  233. * @return mixed
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\DbException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. */
  238. public function edit(int $id, array $data)
  239. {
  240. $lottery = $this->dao->getLottery($id);
  241. if (!$lottery) {
  242. throw new AdminException(400537);
  243. }
  244. $newPrizes = $data['prize'];
  245. $percentArr = array_column($newPrizes, 'percent');
  246. $allPercent = 0;
  247. foreach ($percentArr as $k => $v) {
  248. $allPercent = bcadd((string)$allPercent, (string)$v, 2);
  249. }
  250. if ($allPercent != 100) {
  251. throw new AdminException('奖品概率之和不是100%,请检查!');
  252. }
  253. unset($data['prize'], $data['id']);
  254. $prize_num = $this->lottery_type[1];
  255. if (count($newPrizes) != $prize_num) {
  256. throw new AdminException(400535);
  257. }
  258. if ($data['attends_user'] == 1) {
  259. $data['user_label'] = $data['user_level'] = [];
  260. $data['is_svip'] = -1;
  261. }
  262. /** @var LuckPrizeServices $luckPrizeServices */
  263. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  264. $prizes = $luckPrizeServices->getLotteryPrizeList($id);
  265. return $this->transaction(function () use ($id, $lottery, $data, $newPrizes, $prizes, $luckPrizeServices) {
  266. $updateIds = array_column($newPrizes, 'id');
  267. $oldIds = array_column($prizes, 'id');
  268. $delIds = array_merge(array_diff($oldIds, $updateIds));
  269. $insert = [];
  270. $time = time();
  271. $sort = 1;
  272. $prizeStatus = false;
  273. foreach ($newPrizes as $prize) {
  274. if (isset($prize['type']) && $prize['type'] == 1) $prizeStatus = true;
  275. $prize = $luckPrizeServices->checkPrizeData($prize);
  276. $prize['sort'] = $sort;
  277. if (isset($prize['id']) && $prize['id']) {
  278. if (!$prize['lottery_id']) {
  279. throw new AdminException(100100);
  280. }
  281. if (!$luckPrizeServices->update($prize['id'], $prize, 'id')) {
  282. throw new AdminException(100007);
  283. }
  284. } else {
  285. unset($prize['id']);
  286. $prize['lottery_id'] = $id;
  287. $prize['add_time'] = $time;
  288. $prize['sort'] = $sort;
  289. $insert[] = $prize;
  290. }
  291. $sort++;
  292. }
  293. if (!$prizeStatus) {
  294. throw new AdminException('必须设置至少一个未中奖');
  295. }
  296. if ($insert) {
  297. if (!$luckPrizeServices->saveAll($insert)) {
  298. throw new AdminException(100022);
  299. }
  300. }
  301. if ($delIds) {
  302. if (!$luckPrizeServices->update([['id', 'in', $delIds]], ['is_del' => 1])) {
  303. throw new AdminException(100008);
  304. }
  305. }
  306. if (!$this->dao->update($id, $data)) {
  307. throw new AdminException(100007);
  308. }
  309. //上架
  310. if (!$lottery['status'] && $data['status']) {
  311. $this->setStatus($id, $data['status']);
  312. }
  313. return true;
  314. });
  315. }
  316. /**
  317. * 获取用户某个抽奖活动剩余抽奖次数
  318. * @param int $uid
  319. * @param int $lottery_id
  320. * @param array $userInfo
  321. * @param array $lottery
  322. * @return false|float|int|mixed
  323. * @throws \Psr\SimpleCache\InvalidArgumentException
  324. * @throws \think\db\exception\DataNotFoundException
  325. * @throws \think\db\exception\DbException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. */
  328. public function getLotteryNum(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  329. {
  330. /** @var UserServices $userServices */
  331. $userServices = app()->make(UserServices::class);
  332. if (!$userInfo) {
  333. $userInfo = $userServices->getUserInfo($uid);
  334. }
  335. if (!$userInfo) {
  336. throw new ApiException(410032);
  337. }
  338. if (!$lottery) {
  339. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  340. }
  341. if (!$lottery) {
  342. throw new ApiException(410057);
  343. }
  344. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  345. switch ($lottery['factor']) {
  346. case 1:
  347. /** @var UserBillServices $userBillServices */
  348. $userBillServices = app()->make(UserBillServices::class);
  349. $usable_integral = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  350. return $usable_integral > 0 && $lottery['factor_num'] > 0 ? floor($usable_integral / $lottery['factor_num']) : 0;
  351. case 2:
  352. return $userInfo['now_money'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['now_money'] / $lottery['factor_num']) : 0;
  353. case 3:
  354. return $this->getCacheLotteryNum($uid, 'order');
  355. case 4:
  356. return $this->getCacheLotteryNum($uid, 'comment');
  357. case 5:
  358. return $userInfo['spread_lottery'] ?? 0;
  359. default:
  360. throw new ApiException(410058);
  361. }
  362. }
  363. /**
  364. * 验证用户抽奖资格(用户等级、付费会员、用户标签)
  365. * @param int $uid
  366. * @param int $lottery_id
  367. * @param array $userInfo
  368. * @param array $lottery
  369. * @return bool
  370. * @throws \think\db\exception\DataNotFoundException
  371. * @throws \think\db\exception\DbException
  372. * @throws \think\db\exception\ModelNotFoundException
  373. */
  374. public function checkoutUserAuth(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
  375. {
  376. if (!$userInfo) {
  377. /** @var UserServices $userServices */
  378. $userServices = app()->make(UserServices::class);
  379. $userInfo = $userServices->getUserInfo($uid);
  380. }
  381. if (!$userInfo) {
  382. throw new ApiException(410032);
  383. }
  384. if (!$lottery) {
  385. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  386. }
  387. if (!$lottery) {
  388. throw new ApiException(410057);
  389. }
  390. //部分用户参与
  391. if ($lottery['attends_user'] == 2) {
  392. //用户等级
  393. if ($lottery['user_level'] && !in_array($userInfo['level'], $lottery['user_level'])) {
  394. throw new ApiException(410059);
  395. }
  396. //用户标签
  397. if ($lottery['user_label']) {
  398. /** @var UserLabelRelationServices $userlableRelation */
  399. $userlableRelation = app()->make(UserLabelRelationServices::class);
  400. $user_labels = $userlableRelation->getUserLabels($uid);
  401. if (!array_intersect($lottery['user_label'], $user_labels)) {
  402. throw new ApiException(410059);
  403. }
  404. }
  405. //是否是付费会员
  406. if ($lottery['is_svip'] != -1) {
  407. if (($lottery['is_svip'] == 1 && $userInfo['is_money_level'] <= 0) || ($lottery['is_svip'] == 0 && $userInfo['is_money_level'] > 0)) {
  408. throw new ApiException(410059);
  409. }
  410. }
  411. }
  412. return true;
  413. }
  414. /**
  415. * 抽奖
  416. * @param int $uid
  417. * @param int $lottery_id
  418. * @return mixed
  419. * @throws \Psr\SimpleCache\InvalidArgumentException
  420. * @throws \think\db\exception\DataNotFoundException
  421. * @throws \think\db\exception\DbException
  422. * @throws \think\db\exception\ModelNotFoundException
  423. */
  424. public function luckLottery(int $uid, int $lottery_id, $channel_type)
  425. {
  426. /** @var UserServices $userServices */
  427. $userServices = app()->make(UserServices::class);
  428. $userInfo = $userServices->getUserInfo($uid);
  429. if (!$userInfo) {
  430. throw new ApiException(410032);
  431. }
  432. $lottery = $this->dao->getLottery($lottery_id, '*', [], true);
  433. if (!$lottery) {
  434. throw new ApiException(410057);
  435. }
  436. $userInfo = $userInfo->toArray();
  437. $lottery = $lottery->toArray();
  438. //验证用户身份
  439. $this->checkoutUserAuth($uid, $lottery_id, $userInfo, $lottery);
  440. /** @var LuckPrizeServices $lotteryPrizeServices */
  441. $lotteryPrizeServices = app()->make(LuckPrizeServices::class);
  442. $lotteryPrize = $lotteryPrizeServices->getPrizeList($lottery_id);
  443. if (!$lotteryPrize) {
  444. throw new ApiException(410060);
  445. }
  446. if ($this->getLotteryNum($uid, $lottery_id, $userInfo, $lottery) < 1) {
  447. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  448. switch ($lottery['factor']) {
  449. case 1:
  450. throw new ApiException(410061);
  451. case 2:
  452. throw new ApiException(410062);
  453. case 3:
  454. throw new ApiException(410063);
  455. case 4:
  456. throw new ApiException(410064);
  457. case 5:
  458. throw new ApiException(410065);
  459. default:
  460. throw new ApiException(410058);
  461. }
  462. }
  463. return $this->transaction(function () use ($uid, $lotteryPrize, $userInfo, $lottery, $channel_type) {
  464. /** @var LuckPrizeServices $luckPrizeServices */
  465. $luckPrizeServices = app()->make(LuckPrizeServices::class);
  466. //随机抽奖
  467. $prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
  468. if (!$prize) {
  469. throw new ApiException(410060);
  470. }
  471. //中奖扣除积分、余额
  472. $this->lotteryFactor($uid, $userInfo, $lottery);
  473. //中奖减少奖品数量
  474. $luckPrizeServices->decPrizeNum($prize['id'], $prize);
  475. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  476. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  477. //中奖写入记录
  478. $record = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo, $channel_type);
  479. //不是站内商品直接领奖
  480. if ($prize['type'] != 6) {
  481. $lotteryRecordServices->receivePrize($uid, (int)$record->id);
  482. }
  483. $prize['lottery_record_id'] = $record->id;
  484. //自定义事件-用户抽奖
  485. event('CustomEventListener', ['user_lottery', [
  486. 'uid' => $uid,
  487. 'lottery_id' => $prize['lottery_id'],
  488. 'prize_id' => $prize['id'],
  489. 'record_id' => $record['id'],
  490. 'lottery_time' => date('Y-m-d H:i:s'),
  491. ]]);
  492. return $prize;
  493. });
  494. }
  495. /**
  496. * 抽奖消耗扣除用户积分、余额等
  497. * @param int $uid
  498. * @param array $userInfo
  499. * @param array $lottery
  500. * @return bool
  501. * @throws \Psr\SimpleCache\InvalidArgumentException
  502. */
  503. public function lotteryFactor(int $uid, array $userInfo, array $lottery)
  504. {
  505. if (!$userInfo || !$lottery) {
  506. return true;
  507. }
  508. //抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
  509. switch ($lottery['factor']) {
  510. case 1:
  511. if ($userInfo['integral'] > $lottery['factor_num']) {
  512. $integral = bcsub((string)$userInfo['integral'], (string)$lottery['factor_num'], 0);
  513. } else {
  514. $integral = 0;
  515. }
  516. /** @var UserServices $userServices */
  517. $userServices = app()->make(UserServices::class);
  518. /** @var UserBillServices $userBillServices */
  519. $userBillServices = app()->make(UserBillServices::class);
  520. $userBillServices->income('lottery_use_integral', $uid, $lottery['factor_num'], $integral, $lottery['id']);
  521. if (!$userServices->update($uid, ['integral' => $integral], 'uid')) {
  522. throw new ApiException(410066);
  523. }
  524. break;
  525. case 2:
  526. if ($userInfo['now_money'] >= $lottery['factor_num']) {
  527. $now_money = bcsub((string)$userInfo['now_money'], (string)$lottery['factor_num'], 2);
  528. } else {
  529. throw new ApiException(410067);
  530. }
  531. /** @var UserServices $userServices */
  532. $userServices = app()->make(UserServices::class);
  533. /** @var UserMoneyServices $userMoneyServices */
  534. $userMoneyServices = app()->make(UserMoneyServices::class);
  535. $userMoneyServices->income('lottery_use_money', $uid, $lottery['factor_num'], $now_money, $lottery['id']);
  536. if (!$userServices->update($uid, ['now_money' => $now_money], 'uid')) {
  537. throw new ApiException(410068);
  538. }
  539. break;
  540. case 3:
  541. case 4:
  542. //销毁抽奖次数缓存
  543. $this->delCacheLotteryNum($uid, $lottery['factor'] == 3 ? 'order' : 'comment');
  544. break;
  545. case 5:
  546. /** @var UserServices $userServices */
  547. $userServices = app()->make(UserServices::class);
  548. $spread_lottery = 0;
  549. if ($userInfo['spread_lottery'] > 1) {
  550. $spread_lottery = $userInfo['spread_lottery'] - 1;
  551. }
  552. if (!$userServices->update($uid, ['spread_lottery' => $spread_lottery], 'uid')) {
  553. throw new ApiException(410069);
  554. }
  555. break;
  556. default:
  557. throw new ApiException(410058);
  558. }
  559. return true;
  560. }
  561. /**
  562. * 删除
  563. * @param int $id
  564. * @return bool
  565. * @throws \think\db\exception\DataNotFoundException
  566. * @throws \think\db\exception\DbException
  567. * @throws \think\db\exception\ModelNotFoundException
  568. */
  569. public function delLottery(int $id)
  570. {
  571. $lottery = $this->dao->getLottery($id);
  572. if ($lottery) {
  573. $res = $this->dao->update(['id' => $id], ['is_del' => 1]);
  574. if (!$res) {
  575. throw new AdminException(100008);
  576. }
  577. }
  578. return true;
  579. }
  580. /**
  581. * 设置抽奖活动状态
  582. * @param int $id
  583. * @param $status
  584. * @return false|mixed
  585. * @throws \think\db\exception\DataNotFoundException
  586. * @throws \think\db\exception\DbException
  587. * @throws \think\db\exception\ModelNotFoundException
  588. */
  589. public function setStatus(int $id, $status)
  590. {
  591. if (!$id) return false;
  592. $lottery = $this->dao->getLottery($id, 'id,factor');
  593. if (!$lottery) return false;
  594. return $this->dao->update($id, ['status' => $status], 'id');
  595. }
  596. /**
  597. * 下单支付、评论缓存抽奖次数
  598. * @param int $uid
  599. * @param string $type
  600. * @return bool
  601. * @throws \Psr\SimpleCache\InvalidArgumentException
  602. */
  603. public function setCacheLotteryNum(int $uid, string $type = 'order')
  604. {
  605. $factor = $type == 'order' ? 3 : 4;
  606. $lottery = $this->dao->getFactorLottery($factor, 'id,factor_num', ['prize'], true);
  607. if (!$lottery || !$lottery['factor_num']) {
  608. return true;
  609. }
  610. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  611. return CacheService::set($key, $lottery['factor_num'], 120);
  612. }
  613. /**
  614. * 取出下单支付、评论得到的抽奖次数
  615. * @param int $uid
  616. * @param string $type
  617. * @return int|mixed
  618. * @throws \Psr\SimpleCache\InvalidArgumentException
  619. */
  620. public function getCacheLotteryNum(int $uid, string $type = 'order')
  621. {
  622. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  623. $num = CacheService::get($key);
  624. return empty($num) ? 0 : $num;
  625. }
  626. /**
  627. * 抽奖之后销毁缓存
  628. * @param int $uid
  629. * @param string $type
  630. * @return bool
  631. * @throws \Psr\SimpleCache\InvalidArgumentException
  632. */
  633. public function delCacheLotteryNum(int $uid, string $type = 'order')
  634. {
  635. $key = 'user_' . $type . '_luck_lottery_' . $uid;
  636. $num = $this->getCacheLotteryNum($uid, $type);
  637. if ($num > 1) {
  638. CacheService::set($key, $num - 1, 120);
  639. } else {
  640. CacheService::delete($key);
  641. }
  642. return true;
  643. }
  644. public function factorList()
  645. {
  646. $list = $this->dao->selectList(['status' => 1, 'is_del' => 0], 'id,name,factor,is_use')->toArray();
  647. $data = [
  648. 'info' => [
  649. 'point' => '',
  650. 'pay' => '',
  651. 'evaluate' => ''
  652. ],
  653. 'point' => [],
  654. 'pay' => [],
  655. 'evaluate' => []
  656. ];
  657. foreach ($list as $item) {
  658. if ($item['factor'] == 1) {
  659. $data['point'][] = $item;
  660. if ($data['info']['point'] == '') {
  661. $data['info']['point'] = $item['is_use'] ? $item['id'] : '';
  662. }
  663. } elseif ($item['factor'] == 3) {
  664. $data['pay'][] = $item;
  665. if ($data['info']['pay'] == '') {
  666. $data['info']['pay'] = $item['is_use'] ? $item['id'] : '';
  667. }
  668. } else {
  669. $data['evaluate'][] = $item;
  670. if ($data['info']['evaluate'] == '') {
  671. $data['info']['evaluate'] = $item['is_use'] ? $item['id'] : '';
  672. }
  673. }
  674. }
  675. return $data;
  676. }
  677. public function factorUse($data)
  678. {
  679. $this->dao->update(['is_del' => 0], ['is_use' => 0]);
  680. if ($data['point']) {
  681. $this->dao->update(['factor' => 1, 'id' => $data['point']], ['is_use' => 1]);
  682. }
  683. if ($data['pay']) {
  684. $this->dao->update(['factor' => 3, 'id' => $data['pay']], ['is_use' => 1]);
  685. }
  686. if ($data['evaluate']) {
  687. $this->dao->update(['factor' => 4, 'id' => $data['evaluate']], ['is_use' => 1]);
  688. }
  689. return true;
  690. }
  691. }