WasteType.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. class WasteType extends Auth
  5. {
  6. public function __construct(App $app = null) {
  7. parent::__construct($app);
  8. $this->model= new \app\common\model\WasteType();
  9. $this->table= $this->model->table;
  10. }
  11. public function index(){
  12. if(request()->isAjax()){
  13. //分页参数
  14. $length = input('rows',10,'intval'); //每页条数
  15. $page = input('page',1,'intval'); //第几页
  16. $start = ($page - 1) * $length; //分页开始位置
  17. //排序
  18. $sortRow = input('sidx','id','trim'); //排序列
  19. $sort = input('sord','desc','trim'); //排序方式
  20. $order = $sortRow.' '.$sort;
  21. $title = input('title','','trim');
  22. if($title){
  23. $map[] = ['title','like','%'.$title.'%'];
  24. }
  25. $enable = input('enable','','trim');
  26. if($enable != ''){
  27. $map[] = ['enable','=',$enable];
  28. }
  29. $map[] = ['del','=',0];
  30. $map[] = ['org_id','in',[0,$this->orgId]];
  31. $map= empty($map) ? true: $map;
  32. //数据查询
  33. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  34. //数据返回
  35. $totalCount = db($this->table)->where($map)->count();
  36. $totalPage = ceil($totalCount/$length);
  37. $result['page'] = $page;
  38. $result['total'] = $totalPage;
  39. $result['records'] = $totalCount;
  40. $result['rows'] = $lists;
  41. return json($result);
  42. }else{
  43. $this->assign('m_name','医废类型');
  44. return $this->fetch();
  45. }
  46. }
  47. /**
  48. * 新增/编辑
  49. */
  50. public function add($id=0){
  51. if(request()->isPost()){
  52. if($id > 0){
  53. $info = db($this->table)->where('id',$id)->find();
  54. if(!$info){
  55. $this->error('参数错误');
  56. }
  57. if($info['org_id'] <= 0){
  58. $this->error('共有类型无法修改');
  59. }
  60. }
  61. $res = $this->model->updates();
  62. if($res){
  63. $this->success('操作成功',url('index'));
  64. }else{
  65. $this->error($this->model->getError());
  66. }
  67. }else{
  68. if($id){
  69. $info =db($this->table)->where('id',$id)->find();
  70. $this->assign('info',$info);
  71. }
  72. return $this->fetch();
  73. }
  74. }
  75. /**
  76. * 删除记录
  77. * @param int $id
  78. */
  79. public function del($id=0){
  80. if(!$id){
  81. $this->error('参数错误');
  82. }
  83. $info = db($this->table)->where('id',$id)->find();
  84. if(!$info){
  85. $this->error('参数错误');
  86. }
  87. if($info['org_id'] <= 0){
  88. $this->error('共有类型无法删除');
  89. }
  90. if(db('waste_record')->where('cateid',$id)->find()){
  91. $this->error('有医废记录绑定该类型,暂不能删除');
  92. }
  93. $res = db($this->table)->where('id',$id)->setField('del',1);
  94. if($res){
  95. $this->success('删除成功');
  96. }else{
  97. $this->error('删除失败');
  98. }
  99. }
  100. /**
  101. * 改变字段值
  102. * @param int $fv
  103. * @param string $fn
  104. * @param int $fv
  105. */
  106. public function changeField($id=0,$fn='',$fv=0){
  107. if(!$fn||!$id){
  108. $this->error('参数错误');
  109. }
  110. $info = db($this->table)->where('id',$id)->find();
  111. if(!$info){
  112. $this->error('参数错误');
  113. }
  114. if($info['org_id'] <= 0){
  115. $this->error('共有类型无法修改');
  116. }
  117. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  118. if($res){
  119. $this->success('操作成功');
  120. }else{
  121. $this->error('操作失败');
  122. }
  123. }
  124. }