LuckPrizeServices.php 7.3 KB

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