Update Luckdraw.php

This commit is contained in:
Anyon 2020-10-12 14:14:23 +08:00
parent f029bba269
commit a3ef52b9a5

View File

@ -114,55 +114,54 @@ class Luckdraw extends Controller
*/
public function prize()
{
if ($this->record) $this->error('已经参与抽奖,不能再抽奖了!');
if ($this->record) {
$this->error('已经参与抽奖,不能再抽奖了!');
}
/* 统计已经发出的奖品 */
$query = $this->app->db->name('ActivityLuckdrawRecord')->group('prize_code');
$useds = $query->where(['code' => $this->code])->column('count(1) count', 'prize_code');
$map = ['code' => $this->code];
[$check, $useds, $rateNumber, $rateRand] = [[], [], 0, rand(1, 1000000) / 10000];
$query = $this->app->db->name('ActivityLuckdrawRecord')->where($map)->group('prize_code,prize_level');
$query->field('prize_code,prize_level,count(1) prize_used')->select()->map(function ($item) use (&$useds) {
$useds["{$item['prize_code']}_{$item['prize_level']}"] = $item['prize_used'];
});
/* 统计活动的奖品数据 */
$query = $this->app->db->name('ActivityLuckdrawConfigRecord');
$query->field('prize_num,prize_rate,prize_level,prize_code');
$prizes = $query->where(['code' => $this->code])->select()->toArray();
/* 统计奖品信息 */
[$prize, $prizesNum, $tempRate, $tempNumber] = [[], [], 0, rand(1, 1000000) / 10000];
foreach ($prizes as $item) $prizesNum[$item['prize_code']] = ($prizesNum[$item['prize_code']] ?? 0) + $item['prize_num'];
$query = $this->app->db->name('ActivityLuckdrawConfigRecord')->field('prize_num,prize_rate,prize_level,prize_code');
/* 计算抽奖的中奖数据 */
foreach ($prizes as $key => $item) {
if (isset($useds[$item['prize_code']]) && $useds[$item['prize_code']] >= $prizesNum[$item['prize_code']]) {
unset($prizes[$key]);
foreach ($query->where($map)->select()->toArray() as $key => $item) {
$item['prize_used'] = $useds["{$item['prize_code']}_{$item['prize_level']}"] ?? 0;
if (empty($item['prize_num']) || $item['prize_used'] >= $item['prize_num']) {
continue;
}
if ($tempNumber <= ($tempRate += $item['prize_rate'])) {
$prize = $item;
} elseif ($rateRand <= ($rateNumber += $item['prize_rate'])) {
$check = $item;
break;
}
}
if (empty($prize)) {
$data = [
'mid' => $this->member['id'],
'username' => $this->member['username'],
'code' => $this->vo['code'],
'prize_level' => '未中奖',
'prize_code' => '',
];
/* 组装活动中奖记录 */
$data = ['code' => $this->code];
$data['mid'] = $this->member['id'];
$data['phone'] = $this->member['phone'];
$data['username'] = $this->member['username'];
if (empty($check)) {
$data['prize_code'] = '';
$data['prize_level'] = '未中奖';
if ($this->app->db->name('ActivityLuckdrawRecord')->insert($data) !== false) {
$this->success('抱歉没有抽到奖品哦!');
}
} else {
$_prize = $this->app->db->name('ActivityLuckdrawPrize')->where(['code' => $prize['prize_code']])->find();
$data = [
'mid' => $this->member['id'],
'phone' => $this->member['phone'],
'username' => $this->member['username'],
'code' => $this->code,
'prize_level' => $prize['prize_level'],
'prize_code' => $_prize['code'],
'prize_name' => $_prize['name'],
'prize_cover' => $_prize['cover'],
'prize_remark' => $_prize['remark'],
'prize_express' => $_prize['express'],
];
if ($this->app->db->name('ActivityLuckdrawRecord')->insert($data) !== false) {
$this->success('奖品抽取成功!', $data);
$map = ['code' => $check['prize_code'], 'deleted' => 0, 'status' => 1];
$prize = $this->app->db->name('ActivityLuckdrawPrize')->where($map)->find();
if (empty($prize)) {
$this->error('奖品已下架或被禁用!');
} else {
$data['prize_code'] = $prize['code'];
$data['prize_name'] = $prize['name'];
$data['prize_cover'] = $prize['cover'];
$data['prize_remark'] = $prize['remark'];
$data['prize_express'] = $prize['express'];
$data['prize_level'] = $check['prize_level'];
if ($this->app->db->name('ActivityLuckdrawRecord')->insert($data) !== false) {
$this->success('奖品抽取成功!', $data);
}
}
}
$this->error('抽奖失败,请稍候再试!');
@ -189,10 +188,10 @@ class Luckdraw extends Controller
public function express()
{
$data = $this->_vali([
'express_province.require#province' => '收货省份不能为空!',
'express_city.require#city' => '收货城市不能为空!',
'express_area.require#area' => '收货区域不能为空!',
'express_address.require#address' => '详细地址不能为空!',
'express_province.require#province' => '收货省份不能为空!',
]);
$result = $this->app->db->name('ActivityLuckdrawRecord')->where(['id' => $this->record['id']])->update($data);
$result !== false ? $this->success('提交收货地址成功!') : $this->error('提交收货地址失败,请稍候再试!');