PatrolForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 PatrolForm extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->model= new \app\common\model\PatrolForm();
  12. $this->table= $this->model->table;
  13. }
  14. public function index($mode=1){
  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. $cate_id = input('cate_id','','trim');
  33. if($cate_id != ''){
  34. $map[] = ['cate_id','=',$cate_id];
  35. }
  36. $map[] = ['del','=',0];
  37. $map[] = ['patrol_mode','=',$mode];
  38. $map[] = ['org_id','=',$this->orgId];
  39. $map= empty($map) ? true: $map;
  40. //数据查询
  41. $lists = db($this->table)->where($map)->limit($start,$length)
  42. ->order(['sort'=>'asc','id'=>'desc'])
  43. ->select();
  44. foreach ($lists as $k=>$v){
  45. $lists[$k]['type'] = $this->model->type[$v['type']];
  46. $lists[$k]['cateName'] = $v['cate_id']?implode(',',Db::name('patrol_cate')
  47. ->where('id','in',explode(',',$v['cate_id']))
  48. ->column('title')):'';
  49. }
  50. //数据返回
  51. $totalCount = db($this->table)->where($map)->count();
  52. $totalPage = ceil($totalCount/$length);
  53. $result['page'] = $page;
  54. $result['total'] = $totalPage;
  55. $result['records'] = $totalCount;
  56. $result['rows'] = $lists;
  57. return json($result);
  58. }else{
  59. $this->assign('m_name',(new \app\common\model\PatrolAddrForm())->getModeTitle($mode).'检查项');
  60. $this->assign('mode',$mode);
  61. $cate = model('PatrolCate')->getByModeList($mode);
  62. $this->assign('cate',$cate);
  63. return $this->fetch();
  64. }
  65. }
  66. /**
  67. * 新增/编辑
  68. */
  69. public function add($id=0){
  70. $mode =input('mode',1,'trim');
  71. if(request()->isPost()){
  72. $res = $this->model->updates();
  73. if($res){
  74. $this->success('操作成功',url('index',['mode'=>$mode]));
  75. }else{
  76. $this->error($this->model->getError());
  77. }
  78. }else{
  79. if($id){
  80. $info =db($this->table)->where('id',$id)->find();
  81. $info['cate_id'] = $info['cate_id']?explode(',',$info['cate_id']):[];
  82. $this->assign('info',$info);
  83. }
  84. $cate = model('PatrolCate')->getByModeList($mode);
  85. $this->assign('type',$this->model->type);
  86. $this->assign('mode',$mode);
  87. $this->assign('cate',$cate);
  88. return $this->fetch();
  89. }
  90. }
  91. /**
  92. * 删除记录
  93. * @param int $id
  94. */
  95. public function del($id=0){
  96. if(!$id){
  97. $this->error('参数错误');
  98. }
  99. $res = db($this->table)->where('id',$id)->setField('del',1);
  100. if($res){
  101. $this->success('删除成功');
  102. }else{
  103. $this->error('删除失败');
  104. }
  105. }
  106. /**
  107. * 改变字段值
  108. * @param int $fv
  109. * @param string $fn
  110. * @param int $fv
  111. */
  112. public function changeField($id=0,$fn='',$fv=0){
  113. if(!$fn||!$id){
  114. $this->error('参数错误');
  115. }
  116. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  117. if($res){
  118. $this->success('操作成功');
  119. }else{
  120. $this->error('操作失败');
  121. }
  122. }
  123. public function getByCate(){
  124. $cate_id = input('id',[]);
  125. $mode = input('mode',0);
  126. if(empty($cate_id)){
  127. $this->success('操作成功','',[]);
  128. }
  129. $where = '';
  130. foreach ($cate_id as $k=>$v){
  131. //$map[]=['','exp',Db::raw("FIND_IN_SET($v,cate_id)")];
  132. if($k>0){
  133. $where.='or ';
  134. }
  135. $where.='FIND_IN_SET('.$v.',cate_id)';
  136. }
  137. //
  138. // if($mode==4){
  139. // $map[] = ['cate_id','=',$cate_id];
  140. // }
  141. $list = Db::name('patrol_addr_form')
  142. ->where('org_id',$this->orgId)
  143. ->where('del',0)
  144. ->where('enable',1)
  145. ->whereRaw($where)
  146. ->select();
  147. $this->success('操作成功','',$list);
  148. }
  149. public function getByCateFrom(){
  150. $cate_id = input('id',[]);
  151. $mode = input('mode',0);
  152. if(empty($cate_id)){
  153. $this->success('操作成功','',[]);
  154. }
  155. $where = '';
  156. foreach ($cate_id as $k=>$v){
  157. //$map[]=['','exp',Db::raw("FIND_IN_SET($v,cate_id)")];
  158. if($k>0){
  159. $where.='or ';
  160. }
  161. $where.='FIND_IN_SET('.$v.',cate_id)';
  162. }
  163. //
  164. // if($mode==4){
  165. // $map[] = ['cate_id','=',$cate_id];
  166. // }
  167. $list = Db::name('patrol_form')
  168. ->where('org_id',$this->orgId)
  169. ->where('del',0)
  170. ->where('enable',1)
  171. //->where($map)
  172. ->whereRaw($where)
  173. ->order(['sort'=>'asc','id'=>'desc'])
  174. ->select();
  175. $this->success('操作成功','',$list);
  176. }
  177. public function import(){
  178. $this->assign('mode',input('mode'));
  179. return $this->fetch();
  180. }
  181. public function downloadtem(){
  182. set_time_limit(0);
  183. ini_set("memory_limit","512M");
  184. $mode = input('mode');
  185. if($mode == 4){
  186. $header = [
  187. ['title' => '名称', 'name' => 'title','width'=>'20'],
  188. ['title' => '分类(多个用英文,分开)', 'name' => 'cate','width'=>'20'],
  189. ['title' => '类型(0=选择框 1=输入框)', 'name' => 'type','width'=>'20'],
  190. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  191. ];
  192. $lists = [
  193. [
  194. 'title' => '检查项',
  195. 'cate' => '分类1,分类2',
  196. 'type' => '0',
  197. 'remark' => '备注',
  198. ]
  199. ];
  200. }else{
  201. $header = [
  202. ['title' => '名称', 'name' => 'title','width'=>'20'],
  203. ['title' => '类型(0=选择框 1=输入框)', 'name' => 'type','width'=>'20'],
  204. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  205. ];
  206. $lists = [
  207. [
  208. 'title' => '检查项',
  209. 'type' => '0',
  210. 'remark' => '备注',
  211. ]
  212. ];
  213. }
  214. $modeTitle = Db::name('patrol_mode')->where('id',$mode)->value('name');
  215. $filename = $modeTitle.'检查项模板';
  216. ExcelUtil::export($filename,$header,$lists);
  217. }
  218. /**
  219. * 导入
  220. */
  221. public function importexcel(){
  222. set_time_limit(0);
  223. ini_set("memory_limit", -1);
  224. ob_flush();//清空缓存
  225. flush();//刷新缓存
  226. $mode = input('mode');
  227. $curTime = date('Y-m-d H:i:s');
  228. if($mode == 4){
  229. try{
  230. $cols = ['title','cate','type','remark'];
  231. $lists = ExcelUtil::importExcel('file',$cols,2);
  232. if($lists === false){
  233. exit(ExcelUtil::getError());
  234. }
  235. if(empty($lists)){
  236. exit('文件内容为空');
  237. }
  238. foreach ($lists as $k=>$v){
  239. if(!$v['title']){
  240. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  241. continue;
  242. }
  243. $find = Db::name('patrol_form')
  244. ->where('del',0)
  245. ->where('org_id',$this->orgId)
  246. ->where('title',$v['title'])
  247. ->where('patrol_mode',$mode)
  248. ->find();
  249. if($find) {
  250. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第" . ($k + 1) . "行,检查项已存在,未导入</font><br />";
  251. continue;
  252. }
  253. $cateIds = [];
  254. if($v['cate']){
  255. $cates = explode(',',$v['cate']);
  256. foreach ($cates as $kk=>$vv){
  257. $cateId = Db::name('patrol_cate')
  258. ->where('del',0)
  259. ->where('org_id',$this->orgId)
  260. ->where('title',$vv)
  261. ->where('patrol_mode',$mode)
  262. ->value('id');
  263. if(!$cateId){
  264. $cateData = [
  265. 'org_id'=>$this->orgId,
  266. 'title'=>$vv,
  267. 'patrol_mode'=>$mode,
  268. 'create_time'=>date('Y-m-d H:i:s'),
  269. ];
  270. $cateIds[] = Db::name('patrol_cate')->insertGetId($cateData);
  271. }
  272. }
  273. }
  274. $dt = [
  275. 'org_id'=>$this->orgId,
  276. 'title'=>$v['title'],
  277. 'patrol_mode'=>$mode,
  278. 'remark'=>$v['remark'],
  279. 'cate_id'=>$cateIds ? implode(',',$cateIds):'',
  280. 'type'=>!empty($v['type'])?$v['type']:0,
  281. 'create_time'=>date('Y-m-d H:i:s'),
  282. ];
  283. $inset = Db::name('patrol_form')->insert($dt);
  284. if(!$inset){
  285. $msg = "第".($k+2)."行,导入失败";
  286. echo "<font color=\"red\">".$msg."</font><br />";
  287. continue;
  288. }
  289. }
  290. echo "<font color=\"green\">导入完成</font><br />";
  291. }catch (Exception $e){
  292. trace($e->getMessage(),'error');
  293. echo $e->getMessage();
  294. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  295. }
  296. }else{
  297. try{
  298. $cols = ['title','type','remark'];
  299. $lists = ExcelUtil::importExcel('file',$cols,2);
  300. if($lists === false){
  301. exit(ExcelUtil::getError());
  302. }
  303. if(empty($lists)){
  304. exit('文件内容为空');
  305. }
  306. foreach ($lists as $k=>$v){
  307. if(!$v['title']){
  308. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  309. continue;
  310. }
  311. $find = Db::name('patrol_form')
  312. ->where('del',0)
  313. ->where('org_id',$this->orgId)
  314. ->where('title',$v['title'])
  315. ->where('patrol_mode',$mode)
  316. ->find();
  317. if($find) {
  318. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第" . ($k + 1) . "行,检查项已存在,未导入</font><br />";
  319. continue;
  320. }
  321. $dt = [
  322. 'org_id'=>$this->orgId,
  323. 'title'=>$v['title'],
  324. 'patrol_mode'=>$mode,
  325. 'remark'=>$v['remark'],
  326. 'type'=>!empty($v['type'])?$v['type']:0,
  327. 'create_time'=>date('Y-m-d H:i:s'),
  328. ];
  329. $inset = Db::name('patrol_form')->insert($dt);
  330. if(!$inset){
  331. $msg = "第".($k+2)."行,导入失败";
  332. echo "<font color=\"red\">".$msg."</font><br />";
  333. continue;
  334. }
  335. }
  336. echo "<font color=\"green\">导入完成</font><br />";
  337. }catch (Exception $e){
  338. trace($e->getMessage(),'error');
  339. echo $e->getMessage();
  340. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  341. }
  342. }
  343. }
  344. }