LuckLotteryServices.php 25 KB

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