0
0

PatrolForm.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // $str = '镜面是否擦拭
  179. //台面是否有水渍
  180. //地面是否拖地
  181. //便池是否清理
  182. //床和床头橱是否擦拭与消毒
  183. // 设备带 窗台 窗槽是否擦拭 ';
  184. //
  185. // $a = explode('
  186. //',$str);
  187. //
  188. // halt($a);
  189. return $this->fetch();
  190. }
  191. /**
  192. * 导入
  193. */
  194. public function importexcel(){
  195. set_time_limit(0);
  196. ini_set("memory_limit", -1);
  197. ob_flush();//清空缓存
  198. flush();//刷新缓存
  199. $curTime = date('Y-m-d H:i:s');
  200. try{
  201. $cols = ['','addr','remark','content'];
  202. $lists = ExcelUtil::importExcel('file',$cols,2);
  203. if($lists === false){
  204. exit(ExcelUtil::getError());
  205. }
  206. if(empty($lists)){
  207. exit('文件内容为空');
  208. }
  209. foreach ($lists as $k=>$v){
  210. if($k > 0){
  211. // if(!$v['addr']){
  212. // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地点为空,未导入</font><br />";
  213. // continue;
  214. // }
  215. if(!$v['content']){
  216. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,内容为空,未导入</font><br />";
  217. continue;
  218. }
  219. if($v['addr']){
  220. $addrInfo = Db::name('address')
  221. ->where('org_id',$this->orgId)
  222. ->where('del',0)
  223. ->where('title',$v['addr'])
  224. ->find();
  225. if($addrInfo){
  226. $types = $addrInfo ? explode(',',$addrInfo['types']):[];
  227. if(!in_array(4,$types)){
  228. $newtypes = $addrInfo['types'].',4';
  229. Db::name('address')->where('id',$addrInfo['id'])
  230. ->update([
  231. 'types'=>$newtypes
  232. ]);
  233. }
  234. $addrId = $addrInfo['id'];
  235. }else{
  236. $addrId = Db::name('address')->insertGetId([
  237. 'org_id'=>$this->orgId,
  238. 'title'=>$v['addr'],
  239. 'sn'=>'',
  240. 'remark'=>'',
  241. 'types'=>4,
  242. 'create_time'=>getTime()
  243. ]);
  244. }
  245. }
  246. $contents = explode(',',$v['content']);
  247. $patrol_form = [];
  248. foreach ($contents as $k1=>$v1){
  249. if(!$v1){
  250. continue;
  251. }
  252. $formId = Db::name('patrol_form')
  253. ->where('org_id',$this->orgId)
  254. ->where('del',0)
  255. ->where('patrol_mode',2)
  256. ->where('title',trim($v1))
  257. ->value('id');
  258. if(!$formId){
  259. $formId = Db::name('patrol_form')
  260. ->insertGetId([
  261. 'org_id'=>$this->orgId,
  262. 'title'=>trim($v1),
  263. 'create_time'=>date('Y-m-d H:i:s'),
  264. 'patrol_mode'=>2
  265. ]);
  266. }
  267. $patrol_form[] = $formId;
  268. }
  269. }
  270. echo "<font color=\"green\">导入完成</font><br />";
  271. }
  272. }catch (Exception $e){
  273. trace($e->getMessage(),'error');
  274. echo $e->getMessage();
  275. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  276. }
  277. }
  278. }