RedpacketActiveReward.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Redpacket\ActiveReward as Request;
  3. use App\Models\Redpacket\ActiveReward as Model;
  4. use App\Models\Redpacket\Active as RedpacketActive;
  5. /**
  6. * 抽奖奖品
  7. *
  8. * @author 刘相欣
  9. *
  10. */
  11. class RedpacketActiveReward extends Auth{
  12. protected function _initialize(){
  13. parent::_initialize();
  14. $this->assign('breadcrumb1','红包活动');
  15. $this->assign('breadcrumb2','活动奖励');
  16. }
  17. /**
  18. * 列表页
  19. *
  20. * */
  21. public function index(Model $Model,RedpacketActive $RedpacketActive){
  22. // 接收参数
  23. $activeId = request('active_id',0);
  24. // 查询条件
  25. $map = [];
  26. // 组合条件
  27. if( $activeId ) $map[] = ['active_id','=',$activeId];
  28. // 查询数据
  29. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
  30. // 获取列表
  31. $activeList = $RedpacketActive->query()->get(['id','name'])->toArray();
  32. // 分配数据
  33. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  34. $this->assign('list',$list);
  35. $this->assign('activeList',$activeList);
  36. // 加载模板
  37. return $this->fetch();
  38. }
  39. /**
  40. * 添加
  41. *
  42. * */
  43. public function add(Request $request,Model $Model,RedpacketActive $RedpacketActive){
  44. if( request()->isMethod('post') ){
  45. // 验证参数
  46. $request->scene('add')->validate();
  47. // 接收数据
  48. $data['reward_total'] = request('reward_total',0);
  49. $data['money'] = (float) request('money',0);
  50. $data['probability'] = request('probability',0);
  51. $data['active_id'] = request('active_id',0);
  52. $data['status'] = 1;
  53. // 写入数据表
  54. $id = $Model->add($data);
  55. // 如果操作失败
  56. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  57. // 记录行为
  58. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  59. // 告知结果
  60. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  61. }
  62. // 抽奖活动ID
  63. $activeId = request('active_id',0);
  64. // 通过抽奖活动ID获取已经存在的概率
  65. $maxProbability = 100 - (int) $Model->query()->where([['active_id','=',$activeId]])->sum('probability');
  66. // 获取列表
  67. $activeList = $RedpacketActive->query()->get(['id','name'])->toArray();
  68. // 分配数据
  69. $this->assign('crumbs','新增');
  70. $this->assign('activeList',$activeList);
  71. $this->assign('maxProbability',$maxProbability);
  72. // 加载模板
  73. return $this->fetch();
  74. }
  75. /**
  76. * 修改
  77. *
  78. * */
  79. public function edit(Request $request,Model $Model,RedpacketActive $RedpacketActive){
  80. // 接收参数
  81. $id = request('id',0);
  82. // 查询用户
  83. $oldData = $Model->where(['id'=>$id])->first();
  84. // 修改
  85. if(request()->isMethod('post')){
  86. // 验证参数
  87. $request->scene('edit')->validate();
  88. // 接收数据
  89. // 接收数据
  90. $data['reward_total'] = request('reward_total',0);
  91. $data['money'] = (float) request('money',0);
  92. $data['probability'] = request('probability',0);
  93. $data['active_id'] = request('active_id',0);
  94. // 写入数据表
  95. $result = $Model->edit($id,$data);
  96. // 如果操作失败
  97. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  98. // 记录行为
  99. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  100. // 告知结果
  101. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  102. }
  103. // 错误告知
  104. if( !$oldData ) return $this->error('查无数据');
  105. // 通过抽奖活动ID获取已经存在的概率
  106. $maxProbability = 100 - ((int) $Model->query()->where([['active_id','=',$oldData['active_id']]])->sum('probability')) + $oldData['probability'];
  107. // 获取列表
  108. $activeList = $RedpacketActive->query()->get(['id','name'])->toArray();
  109. // 数据分配
  110. $this->assign('crumbs','修改');
  111. $this->assign('oldData',$oldData);
  112. $this->assign('activeList',$activeList);
  113. $this->assign('maxProbability',$maxProbability);
  114. // 加载模板
  115. return $this->fetch();
  116. }
  117. /**
  118. * 修改状态
  119. *
  120. * */
  121. public function set_status(Request $request,Model $Model){
  122. // 验证参数
  123. $request->scene('set_status')->validate();
  124. // 设置状态
  125. $id = request('id',0);
  126. $status = request('status',0);
  127. // 查询用户
  128. $oldData = $Model->where(['id'=>$id])->first();
  129. // 如果用户不存在
  130. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  131. // 执行修改
  132. $result = $Model->edit($id,['status'=>$status]);
  133. // 提示新增失败
  134. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  135. // 记录行为
  136. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  137. // 告知结果
  138. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  139. }
  140. }