WasteScale.php 3.8 KB

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