123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php namespace App\Models\Lottery;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\Traits\Lottery\RewardType;
- /**
- * 积分抽奖奖品模型
- *
- */
- class ScoreReward extends Model
- {
- use HasFactory,RewardType;
- // 与模型关联的表名
- protected $table = 'lottery_score_reward';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 如果操作失败
- if( !$id ) return $id;
- // 更新缓存
- if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 如果操作失败
- if( !$result ) return $result;
- // 更新缓存
- if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
- // 返回结果
- return $result;
- }
- /**
- * 获取列表
- * @param bool $force 是否强制更新
- *
- */
- public function getList($lotteryId, $force = false)
- {
- // 结果数据
- $list = $force ? [] : cache('admin:lottery:score:reward:list:'.$lotteryId);
- // 不存在数据
- if ( !$list ) {
- // 从数据库获取数据
- $data = $this->query()->where([['lottery_id','=',$lotteryId],['status','=',0]])->get(['id','reward_name','reward_thumb','reward_type','reward_all','reward_total','reward_info','probability','lottery_id'])->toArray();
- // 循环处理数据
- $list = [];
- // 进行更新
- foreach ($data as $value) {
- // 处理图片
- $value['reward_thumb'] = $value['reward_thumb'] ? path_compat($value['reward_thumb']) : '';
- // 重组数据
- $list[$value['id']] = $value;
- }
- // 存起来
- cache(['admin:lottery:score:reward:list:'.$lotteryId=>$list]);
- }
- // 返回结果
- return $list;
- }
- /**
- * 获取配置平台对应的应用数据
- *
- * @param array 用户ID
- * @param string 指定字段
- *
- */
- public function getOne($lotteryId,$id,$field='')
- {
- // 获取列表数据
- $list = $this->getList($lotteryId);
- // 获取数据
- $one = isset($list[$id]) ? $list[$id] : [];
- // 返回值
- return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
- }
- /**
- * 获取列表
- * @param int $lotteryId 抽奖活动ID
- *
- */
- public function getListByLottery($lotteryId)
- {
- // 结果数据
- $list = $this->getList($lotteryId);
- // 查询奖品总份数
- $total = $list ? array_sum(array_column($list,'reward_all')) : 0;
- // 循环处理
- foreach ($list as $key=>$value) {
- // 中奖概率,如果不存在的话
- $value['probability'] = $total ? round($value['reward_all'] / $total * 100,2) : 0;
- // 重组
- $list[$key] = $value;
- }
- // 获取列表结构
- $list = array_values($list);
- // 增补一个谢谢参与
- if( $list ) array_unshift($list,['id'=>0,'reward_name'=>'谢谢参与','reward_thumb'=>'','reward_type'=>0,'reward_total'=>0,'reward_all'=>0,'reward_info'=>'','probability'=>0,'lottery_id'=>$lotteryId]);
- // 返回结果
- return $list;
- }
- /**
- * 获取抽奖结果
- * @param int $lotteryId 活动
- *
- */
- public function getRewardResult($lotteryId){
- // 获取奖项
- $reward = $this->getListByLottery($lotteryId);
- // 如果已经没有奖项
- if( !$reward ) return [];
- // 没有任何奖品的话,直接返回
- $total = array_sum(array_column($reward,'reward_all'));
- // 如果已经没有奖项
- if( !$total ) return ['reward_list'=>$reward,'index'=>0];
- // 计算剩余总数
- $total = array_sum(array_column($reward,'reward_total'));
- // 如果已经没有奖项
- if( !$total ) return ['reward_list'=>$reward,'index'=>0];
- // 从0开始, 包含0
- $offset = 0;
- // 随机数,包含起始值,不含结束值
- $randInt = random_int($offset,10000);
- // 中奖下标
- $index = -1;
- // 循环奖品
- foreach ($reward as $key => $value) {
- // 概率为0 或者产品份数为0,不参与
- if( $value['probability'] <= 0 || $value['reward_total']<= 0 ) continue;
- // 开始数值
- $start = $offset;
- // 结束数值
- $end = $value['probability'] ? $start + intval($value['probability'] * 100) : 0;
- // 重新计算开始数值
- $offset = $end ? $end : $offset;
- // 区间内即抽中
- if( $start <= $randInt && $end >= $randInt ) $index = $key;
- }
- // 如果未抽中,继续抽奖
- if( $index < 0 ) return $this->getRewardResult($lotteryId);
- // 是否中奖,以及奖项下标
- return ['reward_list'=>$reward,'index'=>$index];
- }
- }
|