QualityCate.php 4.1 KB

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