LotteryOrderProduct.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Lottery\OrderProduct as Request;
  3. use App\Models\Product;
  4. use App\Models\Lottery\OrderProduct as Model;
  5. use App\Models\Lottery\Order as LotteryOrder;
  6. /**
  7. * 下单抽奖商品范围模型
  8. *
  9. * @author 刘相欣
  10. *
  11. */
  12. class LotteryOrderProduct extends Auth{
  13. protected function _initialize(){
  14. parent::_initialize();
  15. $this->assign('breadcrumb1','下点抽奖');
  16. $this->assign('breadcrumb2','商品范围');
  17. }
  18. /**
  19. * 列表页
  20. *
  21. * */
  22. public function index(Model $Model,LotteryOrder $LotteryOrder,Product $Product){
  23. // 接收参数
  24. $lotteryId = request('lottery_id',0);
  25. $productCode = request('product_code','');
  26. $productId = $Product->codeToId($productCode);
  27. // 查询条件
  28. $map = [];
  29. // 组合条件
  30. if( $lotteryId ) $map[] = ['lottery_id','=',$lotteryId];
  31. if( $productId ) $map[] = ['product_id','=',$productId];
  32. // 查询数据
  33. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  34. // 循环处理数据
  35. foreach ($list as $key => $value) {
  36. // 产品编码
  37. $value['lottery_name'] = $LotteryOrder->getOne($value['lottery_id'],'name');
  38. // 产品编码
  39. $value['product_code'] = $Product->idToCode($value['product_id']);
  40. // 产品编码
  41. $value['product_name'] = $Product->query()->where([['id','=',$value['product_id']]])->value('name');
  42. // 重组
  43. $list[$key] = $value;
  44. }
  45. // 获取列表
  46. $lotteryList = $LotteryOrder->query()->get(['id','name'])->toArray();
  47. // 分配数据
  48. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  49. $this->assign('lotteryList',$lotteryList);
  50. $this->assign('list',$list);
  51. // 加载模板
  52. return $this->fetch();
  53. }
  54. /**
  55. * 添加
  56. *
  57. * */
  58. public function add(Request $request,Model $Model,Product $Product,LotteryOrder $LotteryOrder){
  59. if( request()->isMethod('post') ){
  60. // 验证参数
  61. $request->scene('add')->validate();
  62. // 接收数据
  63. $productCode = request('product_code','');
  64. $data['product_id'] = $Product->codeToId($productCode);
  65. $data['lottery_id'] = request('lottery_id',0);
  66. // 如果操作失败
  67. if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
  68. // 查询产品ID是否存在
  69. $oldId = $Model->query()->where([['lottery_id','=',$data['lottery_id']],['product_id','=',$data['product_id']]])->value('id');
  70. // 重复提醒
  71. if( $oldId ) return json_send(['code'=>'error','msg'=>'产品编码已存在']);
  72. // 写入数据表
  73. $id = $Model->add($data);
  74. // 如果操作失败
  75. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  76. // 记录行为
  77. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  78. // 告知结果
  79. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  80. }
  81. // 获取列表
  82. $lotteryList = $LotteryOrder->query()->get(['id','name'])->toArray();
  83. // 分配数据
  84. $this->assign('lotteryList',$lotteryList);
  85. $this->assign('crumbs','新增');
  86. // 加载模板
  87. return $this->fetch();
  88. }
  89. /**
  90. * 修改
  91. *
  92. * */
  93. public function edit(Request $request,Model $Model,Product $Product,LotteryOrder $LotteryOrder){
  94. // 接收参数
  95. $id = request('id',0);
  96. // 查询用户
  97. $oldData = $Model->where(['id'=>$id])->first();
  98. // 修改
  99. if(request()->isMethod('post')){
  100. // 验证参数
  101. $request->scene('edit')->validate();
  102. // 接收数据
  103. $productCode = request('product_code','');
  104. $data['product_id'] = $Product->codeToId($productCode);
  105. $data['lottery_id'] = request('lottery_id',0);
  106. // 如果操作失败
  107. if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
  108. // 查询产品ID是否存在
  109. $oldId = $Model->query()->where([['lottery_id','=',$data['lottery_id']],['product_id','=',$data['product_id']]])->value('id');
  110. // 重复提醒
  111. if( $oldId && $oldId != $id ) return json_send(['code'=>'error','msg'=>'产品编码已存在']);
  112. // 写入数据表
  113. $result = $Model->edit($id,$data);
  114. // 如果操作失败
  115. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  116. // 记录行为
  117. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  118. // 告知结果
  119. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  120. }
  121. // 错误告知
  122. if( !$oldData ) return $this->error('查无数据');
  123. // 产品编码
  124. $oldData['product_code'] = $Product->idToCode($oldData['product_id']);
  125. // 获取列表
  126. $lotteryList = $LotteryOrder->query()->get(['id','name'])->toArray();
  127. // 分配数据
  128. $this->assign('lotteryList',$lotteryList);
  129. $this->assign('oldData',$oldData);
  130. $this->assign('crumbs','修改');
  131. // 加载模板
  132. return $this->fetch();
  133. }
  134. /**
  135. * 修改状态
  136. *
  137. * */
  138. public function set_status(Request $request,Model $Model){
  139. // 验证参数
  140. $request->scene('set_status')->validate();
  141. // 设置状态
  142. $id = request('id',0);
  143. $status = request('status',0);
  144. // 查询用户
  145. $oldData = $Model->where(['id'=>$id])->first();
  146. // 如果用户不存在
  147. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  148. // 执行修改
  149. $result = $Model->edit($id,['status'=>$status]);
  150. // 提示新增失败
  151. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  152. // 记录行为
  153. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  154. // 告知结果
  155. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  156. }
  157. }