Producer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Producer as Request;
  3. use App\Models\Producer as Model;
  4. use Illuminate\Support\Carbon;
  5. /**
  6. * 商业公司管理
  7. *
  8. * @author 刘相欣
  9. *
  10. */
  11. class Producer 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){
  22. // 接受参数
  23. $name = request('name','');
  24. $status = request('status');
  25. $startTime = request('start_time','');
  26. // 查询条件
  27. $map = [];
  28. // 编码ID
  29. if( $name ) $map[] = ['name','=',$name];
  30. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  31. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  32. if( !is_null($status) ) $map[] = ['status','=',$status];
  33. // 查询数据
  34. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  35. // 循环处理数据
  36. foreach ($list as $key => $value) {
  37. // 重组
  38. $list[$key] = $value;
  39. }
  40. // 分配数据
  41. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  42. $this->assign('list',$list);
  43. // 加载模板
  44. return $this->fetch();
  45. }
  46. /**
  47. * 添加
  48. *
  49. * */
  50. public function add(Request $request,Model $Model){
  51. if( request()->isMethod('post') ){
  52. // 验证参数
  53. $request->scene('add')->validate();
  54. // 接收数据
  55. $data['name'] = request('name','');
  56. // 写入数据表
  57. $id = $Model->add($data);
  58. // 如果操作失败
  59. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  60. // 记录行为
  61. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  62. // 告知结果
  63. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  64. }
  65. // 分配数据
  66. $this->assign('crumbs','新增');
  67. // 加载模板
  68. return $this->fetch();
  69. }
  70. /**
  71. * 修改
  72. *
  73. * */
  74. public function edit(Request $request,Model $Model){
  75. // 接收参数
  76. $id = request('id',0);
  77. // 查询用户
  78. $oldData = $Model->where(['id'=>$id])->first();
  79. // 修改
  80. if(request()->isMethod('post')){
  81. // 验证参数
  82. $request->scene('edit')->validate();
  83. // 接收数据
  84. $data['name'] = request('name','');
  85. // 写入数据表
  86. $result = $Model->edit($id,$data);
  87. // 如果操作失败
  88. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  89. // 记录行为
  90. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  91. // 告知结果
  92. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  93. }
  94. // 错误告知
  95. if( !$oldData ) return $this->error('查无数据');
  96. // 分配数据
  97. $this->assign('oldData',$oldData);
  98. $this->assign('crumbs','修改');
  99. // 加载模板
  100. return $this->fetch();
  101. }
  102. /**
  103. * 修改状态
  104. *
  105. * */
  106. public function set_status(Request $request,Model $Model){
  107. // 验证参数
  108. $request->scene('set_status')->validate();
  109. // 设置状态
  110. $id = request('id',0);
  111. $status = request('status',0);
  112. // 查询用户
  113. $oldData = $Model->where(['id'=>$id])->first();
  114. // 如果用户不存在
  115. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  116. // 执行修改
  117. $result = $Model->edit($id,['status'=>$status]);
  118. // 提示新增失败
  119. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  120. // 记录行为
  121. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  122. // 告知结果
  123. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  124. }
  125. }