0
0

OrderType.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class OrderType extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->table= 'order_type';
  12. $this->model= new \app\common\model\OrderType();
  13. }
  14. public function index(){
  15. if(request()->isAjax()){
  16. //分页参数
  17. $length = input('rows',10,'intval'); //每页条数
  18. $page = input('page',1,'intval'); //第几页
  19. $start = ($page - 1) * $length; //分页开始位置
  20. //排序
  21. $sortRow = input('sidx','id','trim'); //排序列
  22. $sort = input('sord','desc','trim'); //排序方式
  23. $order = $sortRow.' '.$sort;
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['title','like','%'.$title.'%'];
  27. }
  28. $enable = input('enable','','trim');
  29. if($enable != ''){
  30. $map[] = ['enable','=',$enable];
  31. }
  32. $map[] = ['del','=',0];
  33. $map[] = ['org_id','=',$this->orgId];
  34. $map[] = ['parent_id','=',0];
  35. $map= empty($map) ? true: $map;
  36. //数据查询
  37. $lists = db($this->table)->where($map)->limit($start,$length)
  38. ->order([$sortRow=>$sort,'id'=>'asc'])
  39. ->select();
  40. //数据返回
  41. $totalCount = db($this->table)->where($map)->count();
  42. $totalPage = ceil($totalCount/$length);
  43. $result['page'] = $page;
  44. $result['total'] = $totalPage;
  45. $result['records'] = $totalCount;
  46. $result['rows'] = $lists;
  47. return json($result);
  48. }else{
  49. return $this->fetch();
  50. }
  51. }
  52. public function show(){
  53. if(request()->isAjax()){
  54. //分页参数
  55. $length = input('rows',10,'intval'); //每页条数
  56. $page = input('page',1,'intval'); //第几页
  57. $start = ($page - 1) * $length; //分页开始位置
  58. //排序
  59. $sortRow = input('sidx','id','trim'); //排序列
  60. $sort = input('sord','desc','trim'); //排序方式
  61. $order = $sortRow.' '.$sort;
  62. $title = input('title','','trim');
  63. if($title){
  64. $map[] = ['title','like','%'.$title.'%'];
  65. }
  66. $enable = input('enable','','trim');
  67. if($enable != ''){
  68. $map[] = ['enable','=',$enable];
  69. }
  70. $map[] = ['del','=',0];
  71. $map[] = ['org_id','=',$this->orgId];
  72. $map[] = ['parent_id','>',0];
  73. $map= empty($map) ? true: $map;
  74. //数据查询
  75. $lists = db($this->table)->where($map)->limit($start,$length)
  76. ->order([$sortRow=>$sort,'id'=>'asc'])
  77. ->select();
  78. foreach ($lists as &$v){
  79. $v['parent_title'] = db($this->table)->where('id',$v['parent_id'])->value('title');
  80. }
  81. //数据返回
  82. $totalCount = db($this->table)->where($map)->count();
  83. $totalPage = ceil($totalCount/$length);
  84. $result['page'] = $page;
  85. $result['total'] = $totalPage;
  86. $result['records'] = $totalCount;
  87. $result['rows'] = $lists;
  88. return json($result);
  89. }else{
  90. return $this->fetch();
  91. }
  92. }
  93. /**
  94. * 新增/编辑
  95. */
  96. public function add($id=0){
  97. if(request()->isPost()){
  98. $res = $this->model->updates();
  99. if($res){
  100. $this->success('操作成功',url('index'));
  101. }else{
  102. $this->error($this->model->getError());
  103. }
  104. }else{
  105. if($id){
  106. $info = db($this->table)->where('id',$id)->find();
  107. $this->assign('info',$info);
  108. }
  109. $type = input('type','');
  110. if($type==1){
  111. $parent =$this->model->list();
  112. $this->assign('parent_list',$parent);
  113. }
  114. $this->assign('type',$type);
  115. return $this->fetch();
  116. }
  117. }
  118. /**
  119. * 删除记录
  120. * @param int $id
  121. */
  122. public function del($id=0){
  123. if(!$id){
  124. $this->error('参数错误');
  125. }
  126. $info = db($this->table)->where('id',$id)->find();
  127. if($info['parent_id'] == 0){
  128. if(db($this->table)->where('parent_id',$id)->where('del',0)->find()){
  129. $this->error('有报修事项绑定该类型,暂不能删除');
  130. }
  131. }
  132. $res = db($this->table)->where('id',$id)->setField('del',1);
  133. if($res){
  134. $this->success('删除成功');
  135. }else{
  136. $this->error('删除失败');
  137. }
  138. }
  139. /**
  140. * 改变字段值
  141. * @param int $fv
  142. * @param string $fn
  143. * @param int $fv
  144. */
  145. public function changeField($id=0,$fn='',$fv=0){
  146. if(!$fn||!$id){
  147. $this->error('参数错误');
  148. }
  149. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  150. if($res){
  151. $this->success('操作成功');
  152. }else{
  153. $this->error('操作失败');
  154. }
  155. }
  156. public function batchSort(){
  157. $data = input('data','','trim');
  158. if(!$data){
  159. $this->error('参数错误');
  160. }
  161. $data = json_decode($data,true);
  162. if(!$data){
  163. $this->error('参数错误');
  164. }
  165. Db::startTrans();
  166. try{
  167. foreach ($data as $k=>$v){
  168. Db::name('order_type')->where('id',$v['id'])->setField('sort',$v['sort']);
  169. }
  170. Db::commit();
  171. }catch (Exception $e){
  172. Db::rollback();
  173. $this->error('操作失败');
  174. }
  175. $this->success('操作成功');
  176. }
  177. public function import(){
  178. return $this->fetch();
  179. }
  180. public function downloadtem(){
  181. set_time_limit(0);
  182. ini_set("memory_limit","512M");
  183. $header = [
  184. ['title' => '报修事项', 'name' => 'parent_title','width'=>'30'],
  185. ['title' => '报修类型', 'name' => 'title','width'=>'30'],
  186. ];
  187. $filename = '报修事项导入';
  188. ExcelUtil::export($filename,$header,[]);
  189. }
  190. /**
  191. * 导入
  192. */
  193. public function importexcel(){
  194. set_time_limit(0);
  195. ini_set("memory_limit", -1);
  196. ob_flush();//清空缓存
  197. flush();//刷新缓存
  198. $curTime = date('Y-m-d H:i:s');
  199. try{
  200. $cols = ['title','val1'];
  201. $lists = ExcelUtil::importExcel('file',$cols,2);
  202. if($lists === false){
  203. exit(ExcelUtil::getError());
  204. }
  205. if(empty($lists)){
  206. exit('文件内容为空');
  207. }
  208. foreach ($lists as $k=>$v){
  209. if(!$v['val1'] || !$v['title']){
  210. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,参数为空,未导入</font><br />";
  211. continue;
  212. }
  213. $pid = Db::name('order_type')
  214. ->where('title',$v['val1'])
  215. ->where('org_id',$this->orgId)
  216. ->where('parent_id',0)
  217. ->value('id');
  218. if(!$pid){
  219. $rd = [
  220. 'org_id'=>$this->orgId,
  221. 'title'=>$v['val1'],
  222. 'parent_id'=>0,
  223. 'sort'=>50,
  224. 'create_time'=>date('Y-m-d H:i:s'),
  225. ];
  226. $pid = Db::name('order_type')->insertGetId($rd);
  227. }
  228. if($pid){
  229. $dt = [
  230. 'org_id'=>$this->orgId,
  231. 'title'=>$v['title'],
  232. 'parent_id'=>$pid,
  233. 'sort'=>50,
  234. 'create_time'=>date('Y-m-d H:i:s'),
  235. ];
  236. $inset = Db::name('order_type')->insert($dt);
  237. if(!$inset){
  238. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  239. continue;
  240. }
  241. }
  242. }
  243. echo "<font color=\"green\">导入完成</font><br />";
  244. }catch (Exception $e){
  245. trace($e->getMessage(),'error');
  246. echo $e->getMessage();
  247. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  248. }
  249. }
  250. }