123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace app\services\lottery;
- use app\admin\model\lottery\Prize;
- /**
- *
- * Class LuckPrizeServices
- *
- */
- class LuckPrizeServices
- {
- /**
- * @var array 1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
- */
- public $prize_type = [
- '1' => '未中奖',
- '2' => '积分',
- '3' => '余额',
- '4' => '红包',
- '5' => '优惠券',
- '6' => '站内商品',
- '7' => '等级经验',
- '8' => '用户等级',
- '9' => 'svip天数'
- ];
- /**
- * 奖品数据字段
- * @var array
- */
- public $prize = [
- 'id' => 0,
- 'type' => 1,
- 'lottery_id' => 0,
- 'name' => '',
- 'prompt' => '',
- 'image' => '',
- 'chance' => 0,
- 'total' => 0,
- 'coupon_id' => 0,
- 'product_id' => 0,
- 'unique' => '',
- 'num' => 1,
- 'sort' => 0,
- 'status' => 1,
- 'is_del' => 0,
- 'add_time' => 0,
- ];
- /**
- * 奖品数据验证
- * @param array $data
- */
- public function checkPrizeData(array $data)
- {
- $data = array_merge($this->prize, array_intersect_key($data, $this->prize));
- if (!isset($data['name']) || !$data['name']) {
- return [1,'奖品不存在'];
- }
- if (!isset($data['image']) || !$data['image']) {
- return [1,'奖品不存在'];
- }
- if (!isset($data['chance']) || !$data['chance']) {
- return [1,'奖品不存在'];
- }
- if (!isset($data['type']) || !isset($this->prize_type[$data['type']])) {
- return [1,'奖品不存在'];
- }
- if (in_array($data['type'], [2, 3, 4]) && (!isset($data['num']) || !$data['num'])) {
- $msg = '';
- switch ($data['type']) {
- case 2:
- $msg = '积分';
- break;
- case 3:
- $msg = '余额';
- break;
- case 4:
- $msg = '红包';
- break;
- }
- return [1,'type' => $msg];
- }
- return [0,$data];
- }
- /**
- * 获取某个抽奖活动的所有奖品
- * @param int $lottery_id
- * @param string $field
- */
- public function getLotteryPrizeList(int $lottery_id, string $field = '*')
- {
- $where = ['status' => 1,'lottery_id' => $lottery_id];
- return Prize::where($where)->field($field)->order('sort desc,id desc')->select();
- }
- /**
- * 随机奖品
- * @param array $data
- * @return array|mixed
- */
- function getLuckPrize(array $data)
- {
- $prize = [];
- if (!$data) return $prize;
- $totalChance = array_sum(array_column($data, 'chance'));
- if (!$totalChance) return $prize;
- $startChance = 0;
- mt_srand();
- $prizeChance = rand(0, $totalChance);
- $newPrize = array_combine(array_column($data, 'type'), $data);
- foreach ($data as $item) {
- $newStartChance = $item['chance'] + $startChance;
- //随机数在这个基数端内 且该商品数量大于0 中奖
- if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
- //随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
- if (($item['type'] != 1 && $item['total'] != -1 && $item['total'] <= 0)
- ) {
- $prize = $newPrize[1] ?? [];
- } else {
- $prize = $item;
- }
- break;
- }
- $startChance = $newStartChance;
- }
- return $prize;
- }
- /**
- * 中奖后减少奖品数量
- * @param int $id
- * @param array $prize
- * @return array
- */
- public function decPrizeNum(int $id, array $prize = [])
- {
- if (!$id) return [1,'请上传奖品'];
- if (!$prize) {
- $prize = Prize::find($id);
- }
- if (!$prize) {
- return [1,'奖品不存在'];
- }
- //不是未中奖奖品 减少奖品数量
- if ($prize['type'] != 1 && $prize['total'] >= 1) {
- $total = $prize['total'] - 1;
- if (! Prize::where('id',$id)->update(['total' => $total] )) {
- return [1,'修改奖品数量失败'];
- }
- }
- return [0,'减少奖品数量成功'];
- }
- }
|