LuckPrizeServices.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\LuckPrizeDao;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. *
  20. * Class LuckPrizeServices
  21. * @package app\services\activity\lottery
  22. */
  23. class LuckPrizeServices extends BaseServices
  24. {
  25. /**
  26. * @var array 1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
  27. */
  28. public $prize_type = [
  29. '1' => '未中奖',
  30. '2' => '积分',
  31. '3' => '余额',
  32. '4' => '红包',
  33. '5' => '优惠券',
  34. '6' => '站内商品',
  35. '7' => '等级经验',
  36. '8' => '用户等级',
  37. '9' => 'svip天数'
  38. ];
  39. /**
  40. * 奖品数据字段
  41. * @var array
  42. */
  43. public $prize = [
  44. 'id' => 0,
  45. 'type' => 1,
  46. 'lottery_id' => 0,
  47. 'name' => '',
  48. 'prompt' => '',
  49. 'image' => '',
  50. 'chance' => 0,
  51. 'total' => 0,
  52. 'coupon_id' => 0,
  53. 'product_id' => 0,
  54. 'unique' => '',
  55. 'num' => 1,
  56. 'sort' => 0,
  57. 'status' => 1,
  58. 'is_del' => 0,
  59. 'add_time' => 0,
  60. ];
  61. /**
  62. * LuckPrizeServices constructor.
  63. * @param LuckPrizeDao $dao
  64. */
  65. public function __construct(LuckPrizeDao $dao)
  66. {
  67. $this->dao = $dao;
  68. }
  69. /**
  70. * 奖品数据验证
  71. * @param array $data
  72. * @return bool
  73. */
  74. public function checkPrizeData(array $data)
  75. {
  76. $data = array_merge($this->prize, array_intersect_key($data, $this->prize));
  77. if (!isset($data['name']) || !$data['name']) {
  78. throw new AdminException(400538);
  79. }
  80. if (!isset($data['image']) || !$data['image']) {
  81. throw new AdminException(400539);
  82. }
  83. if (!isset($data['chance']) || !$data['chance']) {
  84. throw new AdminException(400540);
  85. }
  86. if (!isset($data['type']) || !isset($this->prize_type[$data['type']])) {
  87. throw new AdminException(400541);
  88. }
  89. if (in_array($data['type'], [2, 3, 4]) && (!isset($data['num']) || !$data['num'])) {
  90. $msg = '';
  91. switch ($data['type']) {
  92. case 2:
  93. $msg = '积分';
  94. break;
  95. case 3:
  96. $msg = '余额';
  97. break;
  98. case 4:
  99. $msg = '红包';
  100. break;
  101. }
  102. throw new AdminException(400542, ['type' => $msg]);
  103. }
  104. if ($data['type'] == 5 && (!isset($data['coupon_id']) || !$data['coupon_id'])) {
  105. throw new AdminException(400543);
  106. }
  107. if ($data['type'] == 6 && (!isset($data['product_id']) || !$data['product_id'])) {
  108. throw new AdminException(400337);
  109. }
  110. return $data;
  111. }
  112. /**
  113. * 获取某个抽奖活动的所有奖品
  114. * @param int $lottery_id
  115. * @param string $field
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getLotteryPrizeList(int $lottery_id, string $field = '*')
  122. {
  123. return $this->dao->getPrizeList($lottery_id, $field);
  124. }
  125. /**
  126. * 随机奖品
  127. * @param array $data
  128. * @return array|mixed
  129. */
  130. function getLuckPrize(array $data)
  131. {
  132. $prize = [];
  133. if (!$data) return $prize;
  134. $coupon = [];
  135. $coupon_ids = array_unique(array_column($data, 'coupon_id'));
  136. if ($coupon_ids) {
  137. /** @var StoreCouponIssueServices $couponServices */
  138. $couponServices = app()->make(StoreCouponIssueServices::class);
  139. $coupon = $couponServices->getGiveCoupon([['id', 'IN', $coupon_ids]]);
  140. if ($coupon) $coupon = array_combine(array_column($coupon, 'id'), $coupon);
  141. }
  142. $totalChance = array_sum(array_column($data, 'chance'));
  143. if (!$totalChance) return $prize;
  144. $startChance = 0;
  145. mt_srand();
  146. $prizeChance = rand(0, $totalChance);
  147. $newPrize = array_combine(array_column($data, 'type'), $data);
  148. foreach ($data as $item) {
  149. $newStartChance = $item['chance'] + $startChance;
  150. //随机数在这个基数端内 且该商品数量大于0 中奖
  151. if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
  152. //随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
  153. if (($item['type'] != 1 && $item['total'] != -1 && $item['total'] <= 0) || ($item['coupon_id'] && $coupon && !isset($coupon[$item['coupon_id']]))) {
  154. $prize = $newPrize[1] ?? [];
  155. } else {
  156. $prize = $item;
  157. }
  158. break;
  159. }
  160. $startChance = $newStartChance;
  161. }
  162. return $prize;
  163. }
  164. /**
  165. * 中奖后减少奖品数量
  166. * @param int $id
  167. * @param array $prize
  168. * @return bool
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\DbException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. */
  173. public function decPrizeNum(int $id, array $prize = [])
  174. {
  175. if (!$id) return false;
  176. if (!$prize) {
  177. $prize = $this->dao->get($id);
  178. }
  179. if (!$prize) {
  180. throw new ApiException(410048);
  181. }
  182. //不是未中奖奖品 减少奖品数量
  183. if ($prize['type'] != 1 && $prize['total'] >= 1) {
  184. $total = $prize['total'] - 1;
  185. if (!$this->dao->update($id, ['total' => $total], 'id')) {
  186. throw new ApiException(410070);
  187. }
  188. }
  189. return true;
  190. }
  191. }