|
@@ -0,0 +1,202 @@
|
|
|
+<?php namespace App\Http\Controllers\Admin;
|
|
|
+
|
|
|
+use App\Http\Requests\Admin\LotteryScoreReward as Request;
|
|
|
+use App\Models\Coupon;
|
|
|
+use App\Models\Lottery\ScoreReward as Model;
|
|
|
+use App\Models\Lottery\Score as LotteryScore;
|
|
|
+/**
|
|
|
+ * 抽奖奖品
|
|
|
+ *
|
|
|
+ * @author 刘相欣
|
|
|
+ *
|
|
|
+ */
|
|
|
+class LotteryScoreReward extends Auth{
|
|
|
+
|
|
|
+ protected function _initialize(){
|
|
|
+ parent::_initialize();
|
|
|
+ $this->assign('breadcrumb1','积分抽奖');
|
|
|
+ $this->assign('breadcrumb2','抽奖奖品');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表页
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function index(Model $Model,LotteryScore $LotteryScore){
|
|
|
+ // 查询条件
|
|
|
+ $map = [];
|
|
|
+ // 查询数据
|
|
|
+ $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
|
|
|
+ // 循环处理数据
|
|
|
+ foreach ($list as $key => $value) {
|
|
|
+ // 获取类型名称
|
|
|
+ $value['reward_type'] = $Model->getRewardType($value['id'],'name');
|
|
|
+ // 重组
|
|
|
+ $list[$key] = $value;
|
|
|
+ }
|
|
|
+ // 获取列表
|
|
|
+ $lotteryList = $LotteryScore->query()->get(['id','name'])->toArray();
|
|
|
+ // 分配数据
|
|
|
+ $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
|
|
|
+ $this->assign('list',$list);
|
|
|
+ $this->assign('lotteryList',$lotteryList);
|
|
|
+ // 加载模板
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function add(Request $request,Model $Model,LotteryScore $LotteryScore,Coupon $Coupon){
|
|
|
+ if( request()->isMethod('post') ){
|
|
|
+ // 验证参数
|
|
|
+ $request->scene('add')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $data['reward_name'] = request('reward_name','');
|
|
|
+ $data['reward_thumb'] = request('reward_thumb','');
|
|
|
+ $data['reward_type'] = request('reward_type',0);
|
|
|
+ $data['reward_total'] = request('reward_total',0);
|
|
|
+ $data['reward_info'] = request('reward_info','');
|
|
|
+ $data['probability'] = request('probability',0);
|
|
|
+ $data['lottery_id'] = request('lottery_id',0);
|
|
|
+ $data['status'] = 1;
|
|
|
+ // 计算统计数量
|
|
|
+ $count = $Model->query()->where([['lottery_id','=',$data['lottery_id']]])->count();
|
|
|
+ // 获取统计数量
|
|
|
+ if( $count >= 7 ) return json_send(['code'=>'error','msg'=>'奖项不可超过7个']);
|
|
|
+ // 如果是积分,转整数
|
|
|
+ if( $data['reward_type'] == 1 ) {
|
|
|
+ //
|
|
|
+ $data['reward_info'] = intval($data['reward_info']);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$data['reward_info'] ) return json_send(['code'=>'error','msg'=>'请填写积分信息']);
|
|
|
+ }
|
|
|
+ // 如果是优惠券
|
|
|
+ if( $data['reward_type'] == 2 ) {
|
|
|
+ // 优惠券信息
|
|
|
+ $data['reward_info'] = $Coupon->codeToId($data['reward_info']);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$data['reward_info'] ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
|
|
|
+ }
|
|
|
+ // 如果是红包
|
|
|
+ if( $data['reward_type'] == 3 ) $data['reward_info'] = number_format(floatval($data['reward_info']),2,'.','');
|
|
|
+ // 写入数据表
|
|
|
+ $id = $Model->add($data);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
+ // 记录行为
|
|
|
+ $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
|
|
|
+ // 告知结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
|
|
|
+ }
|
|
|
+ // 抽奖活动ID
|
|
|
+ $lotteryId = request('lottery_id',0);
|
|
|
+ // 通过抽奖活动ID获取已经存在的概率
|
|
|
+ $maxProbability = 100 - (int) $Model->query()->where([['lottery_id','=',$lotteryId]])->sum('probability');
|
|
|
+ // 获取列表
|
|
|
+ $lotteryList = $LotteryScore->query()->get(['id','name'])->toArray();
|
|
|
+ // 奖品类型
|
|
|
+ $typeList = $Model->getRewardTypeList();
|
|
|
+ // 分配数据
|
|
|
+ $this->assign('crumbs','新增');
|
|
|
+ $this->assign('typeList',$typeList);
|
|
|
+ $this->assign('lotteryList',$lotteryList);
|
|
|
+ $this->assign('maxProbability',$maxProbability);
|
|
|
+ // 加载模板
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function edit(Request $request,Model $Model,LotteryScore $LotteryScore,Coupon $Coupon){
|
|
|
+ // 接收参数
|
|
|
+ $id = request('id',0);
|
|
|
+ // 查询用户
|
|
|
+ $oldData = $Model->where(['id'=>$id])->first();
|
|
|
+ // 修改
|
|
|
+ if(request()->isMethod('post')){
|
|
|
+ // 验证参数
|
|
|
+ $request->scene('edit')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $data['reward_name'] = request('reward_name','');
|
|
|
+ $data['reward_thumb'] = request('reward_thumb','');
|
|
|
+ $data['reward_type'] = request('reward_type',0);
|
|
|
+ $data['reward_total'] = request('reward_total',0);
|
|
|
+ $data['reward_info'] = request('reward_info','');
|
|
|
+ $data['probability'] = request('probability',0);
|
|
|
+ $data['lottery_id'] = request('lottery_id',0);
|
|
|
+ // 计算统计数量
|
|
|
+ $count = $Model->query()->where([['lottery_id','=',$data['lottery_id']],['id','<>',$id]])->count();
|
|
|
+ // 获取统计数量
|
|
|
+ if( $count >= 7 ) return json_send(['code'=>'error','msg'=>'奖项不可超过7个']);
|
|
|
+ // 如果是积分,转整数
|
|
|
+ if( $data['reward_type'] == 1 ) {
|
|
|
+ //
|
|
|
+ $data['reward_info'] = intval($data['reward_info']);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$data['reward_info'] ) return json_send(['code'=>'error','msg'=>'请填写积分信息']);
|
|
|
+ }
|
|
|
+ // 如果是优惠券
|
|
|
+ if( $data['reward_type'] == 2 ) {
|
|
|
+ // 优惠券信息
|
|
|
+ $data['reward_info'] = $Coupon->codeToId($data['reward_info']);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$data['reward_info'] ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
|
|
|
+ }
|
|
|
+ // 如果是红包
|
|
|
+ if( $data['reward_type'] == 3 ) $data['reward_info'] = number_format(floatval($data['reward_info']),2,'.','');
|
|
|
+ // 写入数据表
|
|
|
+ $result = $Model->edit($id,$data);
|
|
|
+ // 如果操作失败
|
|
|
+ if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
|
|
|
+ // 记录行为
|
|
|
+ $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
|
|
|
+ // 告知结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
|
|
|
+ }
|
|
|
+ // 错误告知
|
|
|
+ if( !$oldData ) return $this->error('查无数据');
|
|
|
+ // 通过抽奖活动ID获取已经存在的概率
|
|
|
+ $maxProbability = 100 - ((int) $Model->query()->where([['lottery_id','=',$oldData['lottery_id']]])->sum('probability')) + $oldData['probability'];
|
|
|
+ // 获取列表
|
|
|
+ $lotteryList = $LotteryScore->query()->get(['id','name'])->toArray();
|
|
|
+ // 奖品类型
|
|
|
+ $typeList = $Model->getRewardTypeList();
|
|
|
+ // 数据分配
|
|
|
+ $this->assign('crumbs','修改');
|
|
|
+ $this->assign('oldData',$oldData);
|
|
|
+ $this->assign('typeList',$typeList);
|
|
|
+ $this->assign('lotteryList',$lotteryList);
|
|
|
+ $this->assign('maxProbability',$maxProbability);
|
|
|
+ // 加载模板
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改状态
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function set_status(Request $request,Model $Model){
|
|
|
+ // 验证参数
|
|
|
+ $request->scene('set_status')->validate();
|
|
|
+ // 设置状态
|
|
|
+ $id = request('id',0);
|
|
|
+ $status = request('status',0);
|
|
|
+ // 查询用户
|
|
|
+ $oldData = $Model->where(['id'=>$id])->first();
|
|
|
+ // 如果用户不存在
|
|
|
+ if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
|
|
|
+ // 执行修改
|
|
|
+ $result = $Model->edit($id,['status'=>$status]);
|
|
|
+ // 提示新增失败
|
|
|
+ if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
|
|
|
+ // 记录行为
|
|
|
+ $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
|
|
|
+ // 告知结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|