LotteryScore.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\LotteryScore as Request;
  3. use App\Models\Lottery\Score as Model;
  4. use App\Models\City;
  5. use App\Models\Lottery\ScoreReward;
  6. use App\Models\WeiBan\Tags as WeiBanTags;
  7. /**
  8. * 优惠券自动发放规则
  9. *
  10. * @author 刘相欣
  11. *
  12. */
  13. class LotteryScore extends Auth{
  14. protected function _initialize(){
  15. parent::_initialize();
  16. $this->assign('breadcrumb1','积分管理');
  17. $this->assign('breadcrumb2','积分抽奖');
  18. }
  19. /**
  20. * 列表页
  21. *
  22. * */
  23. public function index(Model $Model){
  24. // 查询条件
  25. $map = [];
  26. // 查询数据
  27. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  28. // 循环处理数据
  29. foreach ($list as $key => $value) {
  30. // 重组
  31. $list[$key] = $value;
  32. }
  33. // 分配数据
  34. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  35. $this->assign('list',$list);
  36. // 加载模板
  37. return $this->fetch();
  38. }
  39. /**
  40. * 添加
  41. *
  42. * */
  43. public function add(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  44. if( request()->isMethod('post') ){
  45. // 验证参数
  46. $request->scene('add')->validate();
  47. // 接收数据
  48. $data['logo'] = request('logo','');
  49. $data['name'] = request('name','');
  50. $data['need_score'] = request('need_score',0);
  51. $data['need_old_score'] = request('need_old_score',0);
  52. $data['rule'] = request('rule','');
  53. $data['start_time'] = request('start_time','');
  54. $data['end_time'] = request('end_time','');
  55. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  56. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  57. $cityIds = request('city_ids',[]);
  58. $tagScope = request('tag_scope',[]);
  59. $data['city_ids'] = implode(',',$cityIds);
  60. $data['tag_scope'] = implode(',',$tagScope);
  61. $data['status'] = 1;
  62. // 写入数据表
  63. $id = $Model->add($data);
  64. // 如果操作失败
  65. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  66. // 记录行为
  67. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  68. // 告知结果
  69. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  70. }
  71. // 获取列表
  72. $cityList = $City->getCityList();
  73. // 标签列表
  74. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  75. // 标签列表
  76. $tagList = [];
  77. // 循环数据
  78. foreach ($tagData as $value) {
  79. $tagList[$value['group']][] = $value['name'];
  80. }
  81. // 分配数据
  82. $this->assign('cityList',$cityList);
  83. $this->assign('tagList',$tagList);
  84. $this->assign('crumbs','新增');
  85. // 加载模板
  86. return $this->fetch();
  87. }
  88. /**
  89. * 修改
  90. *
  91. * */
  92. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  93. // 接收参数
  94. $id = request('id',0);
  95. // 查询用户
  96. $oldData = $Model->where(['id'=>$id])->first();
  97. // 修改
  98. if(request()->isMethod('post')){
  99. // 验证参数
  100. $request->scene('edit')->validate();
  101. // 接收数据
  102. $data['logo'] = request('logo','');
  103. $data['name'] = request('name','');
  104. $data['need_score'] = request('need_score',0);
  105. $data['need_old_score'] = request('need_old_score',0);
  106. $data['rule'] = request('rule','');
  107. $data['start_time'] = request('start_time','');
  108. $data['end_time'] = request('end_time','');
  109. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  110. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  111. $cityIds = request('city_ids',[]);
  112. $tagScope = request('tag_scope',[]);
  113. $data['city_ids'] = implode(',',$cityIds);
  114. $data['tag_scope'] = implode(',',$tagScope);
  115. // 写入数据表
  116. $result = $Model->edit($id,$data);
  117. // 如果操作失败
  118. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  119. // 记录行为
  120. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  121. // 告知结果
  122. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  123. }
  124. // 错误告知
  125. if( !$oldData ) return $this->error('查无数据');
  126. // 获取城市ID
  127. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  128. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  129. // 获取列表
  130. $cityList = $City->getCityList();
  131. // 标签列表
  132. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  133. // 标签列表
  134. $tagList = [];
  135. // 循环数据
  136. foreach ($tagData as $value) {
  137. $tagList[$value['group']][] = $value['name'];
  138. }
  139. // 分配数据
  140. $this->assign('cityList',$cityList);
  141. $this->assign('tagList',$tagList);
  142. $this->assign('oldData',$oldData);
  143. $this->assign('crumbs','修改');
  144. // 加载模板
  145. return $this->fetch();
  146. }
  147. /**
  148. * 修改状态
  149. *
  150. * */
  151. public function set_status(Request $request,Model $Model,ScoreReward $ScoreReward){
  152. // 验证参数
  153. $request->scene('set_status')->validate();
  154. // 设置状态
  155. $id = request('id',0);
  156. $status = request('status',0);
  157. // 查询用户
  158. $oldData = $Model->where(['id'=>$id])->first();
  159. // 如果用户不存在
  160. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  161. // 如果设置上架
  162. if( !$status ) {
  163. // 判断奖品个数
  164. $count = $ScoreReward->query()->where([['lottery_id','=',$id]])->count();
  165. // 获取统计数量
  166. if( $count < 3 || $count > 7 ) return json_send(['code'=>'error','msg'=>'奖项请设置3~7个再启用']);
  167. }
  168. // 执行修改
  169. $result = $Model->edit($id,['status'=>$status]);
  170. // 提示新增失败
  171. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  172. // 记录行为
  173. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  174. // 告知结果
  175. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  176. }
  177. }