LuckPrizeServices.php 5.8 KB

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