LotteryScore.php 6.1 KB

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