ModeCate.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class ModeCate extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='mode_cate';
  11. $this->model= new \app\common\model\ModeCate();
  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[] = ['title','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)
  36. ->where($map)
  37. ->limit($start,$length)
  38. ->order([$sortRow=>$sort,'id'=>'desc'])
  39. ->select();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['cateTypes'] = Db::name('mode_cate_type')
  42. ->where('cate_id',$v['id'])
  43. ->select();
  44. }
  45. //数据返回
  46. $totalCount = db($this->table)->where($map)->count();
  47. $totalPage = ceil($totalCount/$length);
  48. $result['page'] = $page;
  49. $result['total'] = $totalPage;
  50. $result['records'] = $totalCount;
  51. $result['rows'] = $lists;
  52. return json($result);
  53. }else{
  54. return $this->fetch();
  55. }
  56. }
  57. /**
  58. * 新增/编辑
  59. */
  60. public function add($id=0){
  61. if(request()->isPost()){
  62. $res = $this->model->updates($this->orgId);
  63. if($res){
  64. $this->success('操作成功',url('index'));
  65. }else{
  66. $this->error($this->model->getError());
  67. }
  68. }else{
  69. if($id){
  70. $info =db($this->table)->where('id',$id)->find();
  71. $busIds = Db::name('mode_cate_type')->where('mode',2)->where('cate_id',$info['id'])->column('bus_id');
  72. $busIds1 = Db::name('mode_cate_type')->where('mode',3)->where('cate_id',$info['id'])->column('bus_id');
  73. $info['bus_ids'] = $busIds ? $busIds:[];
  74. $info['bus_ids1'] = $busIds1 ? $busIds1:[];
  75. $this->assign('info',$info);
  76. }
  77. $cleaningType = model('CleaningType')->getList($this->orgId);
  78. $this->assign('cleaningType',$cleaningType);
  79. $conveyType = model('ConveyCate')->getList3($this->orgId);
  80. $this->assign('conveyType',$conveyType);
  81. return $this->fetch();
  82. }
  83. }
  84. /**
  85. * 删除记录
  86. * @param int $id
  87. */
  88. public function del($id=0){
  89. if(!$id){
  90. $this->error('参数错误');
  91. }
  92. $res = db($this->table)->where('id',$id)->setField('del',1);
  93. if($res){
  94. $this->success('删除成功');
  95. }else{
  96. $this->error('删除失败');
  97. }
  98. }
  99. /**
  100. * 改变字段值
  101. * @param int $fv
  102. * @param string $fn
  103. * @param int $fv
  104. */
  105. public function changeField($id=0,$fn='',$fv=0){
  106. if(!$fn||!$id){
  107. $this->error('参数错误');
  108. }
  109. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  110. if($res){
  111. $this->success('操作成功');
  112. }else{
  113. $this->error('操作失败');
  114. }
  115. }
  116. public function changeSort($id=0,$sort=0){
  117. if($id<0||$sort<0){
  118. $this->error('参数错误');
  119. }
  120. $res = db($this->table)->where('id',$id)->setField('sort',$sort);
  121. if($res){
  122. $this->success('操作成功');
  123. }else{
  124. $this->error('操作失败');
  125. }
  126. }
  127. public function cateType($id=0,$mode=2){
  128. if(request()->isAjax()){
  129. //分页参数
  130. $length = input('rows',10,'intval'); //每页条数
  131. $page = input('page',1,'intval'); //第几页
  132. $start = ($page - 1) * $length; //分页开始位置
  133. //排序
  134. $sortRow = input('sidx','id','trim'); //排序列
  135. $sort = input('sord','desc','trim'); //排序方式
  136. $order = $sortRow.' '.$sort;
  137. if($mode == 2){
  138. $atable = 'cleaning_type b';
  139. }else{
  140. $atable = 'convey_cate b';
  141. }
  142. $map[] = ['a.mode','=',$mode];
  143. $map[] = ['a.cate_id','=',$id];
  144. $map= empty($map) ? true: $map;
  145. //数据查询
  146. $lists = db('mode_cate_type')
  147. ->alias('a')
  148. ->field('a.*,b.title')
  149. ->join($atable,'a.bus_id=b.id')
  150. ->where($map)
  151. ->limit($start,$length)
  152. ->order('a.sort desc,a.id desc')
  153. ->select();
  154. //数据返回
  155. $totalCount = db('mode_cate_type')
  156. ->alias('a')
  157. ->join($atable,'a.bus_id=b.id')
  158. ->where($map)
  159. ->count();
  160. $totalPage = ceil($totalCount/$length);
  161. $result['page'] = $page;
  162. $result['total'] = $totalPage;
  163. $result['records'] = $totalCount;
  164. $result['rows'] = $lists;
  165. return json($result);
  166. }else{
  167. $this->assign('id',$id);
  168. $this->assign('mode',$mode);
  169. $m = '';
  170. if($mode == 2){
  171. $m = '保洁';
  172. }elseif ($mode == 3){
  173. $m = '运送';
  174. }
  175. $this->assign('m_name',$m.'类型');
  176. return $this->fetch();
  177. }
  178. }
  179. public function changeSortType($id=0){
  180. $sort = input('sort');
  181. if(!$id){
  182. $this->error('参数错误');
  183. }
  184. $res = db('mode_cate_type')->where('id',$id)->setField('sort',$sort);
  185. if($res){
  186. $this->success('操作成功');
  187. }else{
  188. $this->error('操作失败');
  189. }
  190. }
  191. public function delCateType($id=0){
  192. if(!$id){
  193. $this->error('参数错误');
  194. }
  195. $res = db('mode_cate_type')->where('id',$id)->delete();
  196. if($res){
  197. $this->success('操作成功');
  198. }else{
  199. $this->error('操作失败');
  200. }
  201. }
  202. }