OrderType.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class OrderType extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table= 'order_type';
  11. $this->model= new \app\common\model\OrderType();
  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[] = ['parent_id','=',0];
  34. $map= empty($map) ? true: $map;
  35. //数据查询
  36. $lists = db($this->table)->where($map)->limit($start,$length)
  37. ->order([$sortRow=>$sort,'id'=>'asc'])
  38. ->select();
  39. //数据返回
  40. $totalCount = db($this->table)->where($map)->count();
  41. $totalPage = ceil($totalCount/$length);
  42. $result['page'] = $page;
  43. $result['total'] = $totalPage;
  44. $result['records'] = $totalCount;
  45. $result['rows'] = $lists;
  46. return json($result);
  47. }else{
  48. return $this->fetch();
  49. }
  50. }
  51. public function show(){
  52. if(request()->isAjax()){
  53. //分页参数
  54. $length = input('rows',10,'intval'); //每页条数
  55. $page = input('page',1,'intval'); //第几页
  56. $start = ($page - 1) * $length; //分页开始位置
  57. //排序
  58. $sortRow = input('sidx','id','trim'); //排序列
  59. $sort = input('sord','desc','trim'); //排序方式
  60. $order = $sortRow.' '.$sort;
  61. $title = input('title','','trim');
  62. if($title){
  63. $map[] = ['title','like','%'.$title.'%'];
  64. }
  65. $enable = input('enable','','trim');
  66. if($enable != ''){
  67. $map[] = ['enable','=',$enable];
  68. }
  69. $map[] = ['del','=',0];
  70. $map[] = ['org_id','=',$this->orgId];
  71. $map[] = ['parent_id','>',0];
  72. $map= empty($map) ? true: $map;
  73. //数据查询
  74. $lists = db($this->table)->where($map)->limit($start,$length)
  75. ->order([$sortRow=>$sort,'id'=>'asc'])
  76. ->select();
  77. foreach ($lists as &$v){
  78. $v['parent_title'] = db($this->table)->where('id',$v['parent_id'])->value('title');
  79. }
  80. //数据返回
  81. $totalCount = db($this->table)->where($map)->count();
  82. $totalPage = ceil($totalCount/$length);
  83. $result['page'] = $page;
  84. $result['total'] = $totalPage;
  85. $result['records'] = $totalCount;
  86. $result['rows'] = $lists;
  87. return json($result);
  88. }else{
  89. return $this->fetch();
  90. }
  91. }
  92. /**
  93. * 新增/编辑
  94. */
  95. public function add($id=0){
  96. if(request()->isPost()){
  97. $res = $this->model->updates();
  98. if($res){
  99. $this->success('操作成功',url('index'));
  100. }else{
  101. $this->error($this->model->getError());
  102. }
  103. }else{
  104. if($id){
  105. $info = db($this->table)->where('id',$id)->find();
  106. $this->assign('info',$info);
  107. }
  108. $type = input('type','');
  109. if($type==1){
  110. $parent =$this->model->list();
  111. $this->assign('parent_list',$parent);
  112. }
  113. $this->assign('type',$type);
  114. return $this->fetch();
  115. }
  116. }
  117. /**
  118. * 删除记录
  119. * @param int $id
  120. */
  121. public function del($id=0){
  122. if(!$id){
  123. $this->error('参数错误');
  124. }
  125. $info = db($this->table)->where('id',$id)->find();
  126. if($info['parent_id'] == 0){
  127. if(db($this->table)->where('parent_id',$id)->where('del',0)->find()){
  128. $this->error('有报修事项绑定该类型,暂不能删除');
  129. }
  130. }
  131. $res = db($this->table)->where('id',$id)->setField('del',1);
  132. if($res){
  133. $this->success('删除成功');
  134. }else{
  135. $this->error('删除失败');
  136. }
  137. }
  138. /**
  139. * 改变字段值
  140. * @param int $fv
  141. * @param string $fn
  142. * @param int $fv
  143. */
  144. public function changeField($id=0,$fn='',$fv=0){
  145. if(!$fn||!$id){
  146. $this->error('参数错误');
  147. }
  148. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  149. if($res){
  150. $this->success('操作成功');
  151. }else{
  152. $this->error('操作失败');
  153. }
  154. }
  155. public function batchSort(){
  156. $data = input('data','','trim');
  157. if(!$data){
  158. $this->error('参数错误');
  159. }
  160. $data = json_decode($data,true);
  161. if(!$data){
  162. $this->error('参数错误');
  163. }
  164. Db::startTrans();
  165. try{
  166. foreach ($data as $k=>$v){
  167. Db::name('order_type')->where('id',$v['id'])->setField('sort',$v['sort']);
  168. }
  169. Db::commit();
  170. }catch (Exception $e){
  171. Db::rollback();
  172. $this->error('操作失败');
  173. }
  174. $this->success('操作成功');
  175. }
  176. }