|
@@ -34,7 +34,7 @@ class ScoreReward extends Model
|
|
|
// 如果操作失败
|
|
|
if( !$id ) return $id;
|
|
|
// 更新缓存
|
|
|
- $this->getList(true);
|
|
|
+ if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
|
|
|
// 返回结果
|
|
|
return $id;
|
|
|
}
|
|
@@ -52,7 +52,7 @@ class ScoreReward extends Model
|
|
|
// 如果操作失败
|
|
|
if( !$result ) return $result;
|
|
|
// 更新缓存
|
|
|
- $this->getList(true);
|
|
|
+ if( isset($data['lottery_id'])) $this->getList($data['lottery_id'],true);
|
|
|
// 返回结果
|
|
|
return $result;
|
|
|
}
|
|
@@ -62,16 +62,14 @@ class ScoreReward extends Model
|
|
|
* @param bool $force 是否强制更新
|
|
|
*
|
|
|
*/
|
|
|
- public function getList($force = false)
|
|
|
+ public function getList($lotteryId, $force = false)
|
|
|
{
|
|
|
// 结果数据
|
|
|
- $list = $force ? [] : cache('admin:lottery:score:reward:list');
|
|
|
+ $list = $force ? [] : cache('admin:lottery:score:reward:list:'.$lotteryId);
|
|
|
// 不存在数据
|
|
|
if ( !$list ) {
|
|
|
// 从数据库获取数据
|
|
|
- $data = $this->query()->where([['status','=',0]])->get(['id','reward_name','reward_thumb','reward_type','reward_total','reward_info','probability','lottery_id']);
|
|
|
- // 是否有数据
|
|
|
- $data = $data ? $data->toArray() : [];
|
|
|
+ $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 = [];
|
|
|
// 进行更新
|
|
@@ -82,7 +80,7 @@ class ScoreReward extends Model
|
|
|
$list[$value['id']] = $value;
|
|
|
}
|
|
|
// 存起来
|
|
|
- cache(['admin:lottery:score:reward:list'=>$list]);
|
|
|
+ cache(['admin:lottery:score:reward:list:'.$lotteryId=>$list]);
|
|
|
}
|
|
|
// 返回结果
|
|
|
return $list;
|
|
@@ -95,10 +93,10 @@ class ScoreReward extends Model
|
|
|
* @param string 指定字段
|
|
|
*
|
|
|
*/
|
|
|
- public function getOne($id,$field='')
|
|
|
+ public function getOne($lotteryId,$id,$field='')
|
|
|
{
|
|
|
// 获取列表数据
|
|
|
- $list = $this->getList();
|
|
|
+ $list = $this->getList($lotteryId);
|
|
|
// 获取数据
|
|
|
$one = isset($list[$id]) ? $list[$id] : [];
|
|
|
// 返回值
|
|
@@ -113,32 +111,48 @@ class ScoreReward extends Model
|
|
|
public function getListByLottery($lotteryId)
|
|
|
{
|
|
|
// 结果数据
|
|
|
- $data = $this->getList();
|
|
|
- // 返回结果
|
|
|
- $list = [];
|
|
|
+ $list = $this->getList($lotteryId);
|
|
|
+ // 查询奖品总份数
|
|
|
+ $total = $list ? array_sum(array_column($list,'reward_all')) : 0;
|
|
|
// 循环处理
|
|
|
- foreach ($data as $value) {
|
|
|
- // 获取数据
|
|
|
- if( $lotteryId == $value['lottery_id'] ) $list[] = $value;
|
|
|
+ foreach ($list as $key=>$value) {
|
|
|
+ // 中奖概率,如果不存在的话
|
|
|
+ $value['probability'] = $total ? round($value['reward_all'] / $total * 100,2) : 0;
|
|
|
+ // 重组
|
|
|
+ $list[$key] = $value;
|
|
|
}
|
|
|
- // 追加一个谢谢参与
|
|
|
- if( $list ) array_unshift($list,['id'=>0,'reward_name'=>'谢谢参与','reward_thumb'=>'','reward_type'=>0,'reward_total'=>1,'reward_info'=>'','probability'=>0,'lottery_id'=>$lotteryId]);
|
|
|
+ // 获取列表结构
|
|
|
+ $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;
|
|
|
+ return $list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取抽奖结果
|
|
|
- * @param array $reward 奖品列表
|
|
|
+ * @param int $lotteryId 活动
|
|
|
*
|
|
|
*/
|
|
|
- public function getRewardResult($reward){
|
|
|
- // 从0开始
|
|
|
+ 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 = 0;
|
|
|
+ $index = -1;
|
|
|
// 循环奖品
|
|
|
foreach ($reward as $key => $value) {
|
|
|
// 概率为0 或者产品份数为0,不参与
|
|
@@ -152,8 +166,10 @@ class ScoreReward extends Model
|
|
|
// 区间内即抽中
|
|
|
if( $start <= $randInt && $end >= $randInt ) $index = $key;
|
|
|
}
|
|
|
+ // 如果未抽中,继续抽奖
|
|
|
+ if( $index < 0 ) return $this->getRewardResult($lotteryId);
|
|
|
// 是否中奖,以及奖项下标
|
|
|
- return $index;
|
|
|
+ return ['reward_list'=>$reward,'index'=>$index];
|
|
|
}
|
|
|
|
|
|
}
|