Daily.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 Daily extends Auth {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model = new \app\common\model\Daily();
  11. }
  12. public function index() {
  13. if (request()->isAjax()) {
  14. //分页参数
  15. $length = input('rows', 10, 'intval'); //每页条数
  16. $page = input('page', 1, 'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx', 'sort', 'trim'); //排序列
  20. $sort = input('sord', 'asc', 'trim'); //排序方式
  21. $order = $sortRow . ' ' . $sort . ' ,id desc';
  22. $title = input('title', '', 'trim');
  23. if ($title) {
  24. $map[] = ['title', 'like', '%' . $title . '%'];
  25. }
  26. $enable = input('enable', '', 'trim');
  27. if ($enable != '') {
  28. $map[] = ['enable', '=', $enable];
  29. }
  30. $map[] = ['org_id', '=', $this->orgId];
  31. $map[] = ['del', '=', 0];
  32. $map = empty($map) ? true : $map;
  33. //数据查询
  34. $lists = Db::name('daily')->where($map)->limit($start, $length)->order($order)->select();
  35. foreach ($lists as $k => $v) {
  36. // $rolesName = $this->model->getRoles($v['roles']);
  37. // $lists[$k]['user_name'] = implode(',', $rolesName);
  38. $lists[$k]['title'] = '';
  39. if($v['address_id'] >0){
  40. $lists[$k]['title'] = Db::name('address')
  41. ->where('id',$v['address_id'])
  42. ->value('title');
  43. }
  44. }
  45. //数据返回
  46. $totalCount = Db::name('daily')->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. }
  54. else {
  55. $this->assign('meta_title', '任务列表');
  56. return $this->fetch();
  57. }
  58. }
  59. /**
  60. * 新增/编辑
  61. */
  62. public function add($id = 0) {
  63. if (request()->isPost()) {
  64. $model = $this->model;
  65. $res = $model->updates();
  66. if ($res) {
  67. $this->success('操作成功', url('index'));
  68. }
  69. else {
  70. $this->error($model->getError());
  71. }
  72. }
  73. else {
  74. $meta_title = '新增工作地点';
  75. $formModel = new \app\common\model\DailyForm();
  76. if ($id) {
  77. $info = Db::name('daily')->where('id', $id)->find();
  78. $info['daily_form'] = $formModel->getByIdList($info['daily_form']);
  79. // $info['roles'] = explode(',', $info['roles']);
  80. $this->assign('info', $info);
  81. $daily_user = Db::name('daily_user')
  82. ->where('daily_id',$info['id'])->column('user_id');
  83. $info['daily_user'] = isset($daily_user)?implode(',',$daily_user):'';
  84. $this->assign('info',$info);
  85. $meta_title = '编辑工作地点';
  86. }
  87. //获取检查项
  88. $dailyForm = $formModel->list();
  89. //角色
  90. $roles = $this->model->getRolesList($this->orgId);
  91. $dailyUser = model('WorkTypeMode')->getRolesUser(9,cur_org_id());
  92. $this->assign('daily_form', $dailyForm);
  93. $this->assign('dailyUser',$dailyUser);
  94. $this->assign('meta_title', $meta_title);
  95. $this->assign('send_user_num',0);
  96. $address = model('Address')->getListByType(10);
  97. $this->assign('address',$address);
  98. return $this->fetch();
  99. }
  100. }
  101. /**
  102. * 删除记录
  103. * @param int $id
  104. */
  105. public function del($id = 0) {
  106. if (!$id) {
  107. $this->error('参数错误');
  108. }
  109. $res = Db::name('daily')->where('id', $id)->update(['del' => 1]);
  110. if ($res) {
  111. $this->success('删除成功');
  112. }
  113. else {
  114. $this->error('删除失败');
  115. }
  116. }
  117. /**
  118. * 改变字段值
  119. * @param int $fv
  120. * @param string $fn
  121. * @param int $fv
  122. */
  123. public function changeField($id = 0, $fn = '', $fv = 0) {
  124. if (!$fn || !$id) {
  125. $this->error('参数错误');
  126. }
  127. $res = Db::name('daily')->where('id', $id)->update([$fn => $fv]);
  128. if ($res) {
  129. $this->success('操作成功');
  130. }
  131. else {
  132. $this->error('操作失败');
  133. }
  134. }
  135. //二维码
  136. public function qrcode($id = 0) {
  137. $info = Db::name('daily')->where('id', $id)->find();
  138. if (!$info) {
  139. exit('数据不存在');
  140. }
  141. $code = get_qrcode_str('daily', $id);
  142. $this->assign('code', $code);
  143. $this->assign('info', $info);
  144. return $this->fetch();
  145. }
  146. public function allPrint(){
  147. $ids = input('ids');
  148. if(!$ids){
  149. $list = Db::name('daily')
  150. ->where('org_id',cur_org_id())
  151. ->where('enable',1)
  152. ->where('del',0)
  153. ->select();
  154. }else{
  155. $ids = explode(',',$ids);
  156. $list = Db::name('daily')
  157. ->where('org_id',cur_org_id())
  158. ->where('enable',1)
  159. ->where('id','in',$ids)
  160. ->where('del',0)
  161. ->select();
  162. }
  163. foreach ($list as $k=>$v){
  164. $list[$k]['qCode'] = get_qrcode_str('daily',$v['id']);
  165. }
  166. $this->assign('list',$list);
  167. return $this->fetch();
  168. }
  169. public function group() {
  170. $title = input('title', '', 'trim');
  171. if ($title) {
  172. $map[] = ['title', 'like', '%' . $title . '%'];
  173. }
  174. $map[] = ['enable', '=', 1];
  175. $map[] = ['org_id', '=', $this->orgId];
  176. $map[] = ['del', '=', 0];
  177. $lists = Db::name('daily')->where($map)->field('id,title,address_id')->order('id desc')->select();
  178. $hours = model('Daily')->getTimeDaily($this->orgId);
  179. foreach ($lists as $k=>$v){
  180. $lists[$k]['title'] = '';
  181. if($v['address_id'] >0){
  182. $lists[$k]['title'] = Db::name('address')
  183. ->where('id',$v['address_id'])
  184. ->value('title');
  185. }
  186. $users = Db::name('daily_user')
  187. ->alias('du')
  188. ->join('user u','u.id = du.user_id')
  189. ->where('du.daily_id',$v['id'])
  190. ->column('real_name');
  191. $users = $users?$users:[];
  192. $lists[$k]['users'] = $users?implode(',',$users):'';
  193. $m = [];
  194. $m[] = ['org_id','=',$this->orgId];
  195. $m[] = ['daily_id','=',$v['id']];
  196. $m[] = ['create_time','>=',$hours['start']];
  197. $m[] = ['create_time','<',$hours['end']];
  198. $count = Db::name('daily_record')->where($m)->count();
  199. $lists[$k]['count'] = $count;
  200. $lists[$k]['zc'] = 0;
  201. if($count > 0){
  202. $lists[$k]['zc'] = 1;
  203. }
  204. $m[] = ['order_id','>',0];
  205. $orderIds = Db::name('daily_record')->where($m)->column('order_id');
  206. if($orderIds){
  207. $res = Db::name('orders')->where('id','in',$orderIds)->where('del',0)->where('order_mode','in',[1,4])->find();
  208. if($res){
  209. $lists[$k]['zc'] = 2;
  210. }
  211. }
  212. }
  213. $this->assign('hours',$hours);
  214. $this->assign('lists',$lists);
  215. return $this->fetch();
  216. }
  217. public function record($id=0,$s='',$e='') {
  218. if (request()->isAjax()) {
  219. //分页参数
  220. $length = input('rows', 10, 'intval'); //每页条数
  221. $page = input('page', 1, 'intval'); //第几页
  222. $start = ($page - 1) * $length; //分页开始位置
  223. $order = 'id desc';
  224. $map[] = ['dr.daily_id','=',$id];
  225. $map[] = ['dr.create_time','>=',$s];
  226. $map[] = ['dr.create_time','<',$e];
  227. $map[] = ['dr.org_id', '=', $this->orgId];
  228. $map = empty($map) ? true : $map;
  229. //数据查询
  230. $lists = Db::name('daily_record')
  231. ->alias('dr')
  232. ->join('daily d', 'd.id=dr.daily_id')
  233. ->field('dr.*,d.title,d.content as daily_details')
  234. ->where($map)
  235. ->limit($start, $length)
  236. ->order($order)
  237. ->select();
  238. foreach ($lists as $k => $v) {
  239. $lists[$k]['user_name'] = Db::name('user')
  240. ->where('id', $v['user_id'])
  241. ->value('real_name');
  242. $lists[$k]['zc'] = 0;
  243. if($v['order_id'] > 0){
  244. $lists[$k]['zc'] = 1;
  245. $res = Db::name('orders')->where('id',$v['order_id'])->where('del',0)->where('order_mode','in',[1,4])->find();
  246. if($res){
  247. $lists[$k]['zc'] = 2;
  248. }
  249. }
  250. }
  251. //数据返回
  252. $totalCount = Db::name('daily_record')
  253. ->alias('dr')
  254. ->field('dr.*,d.title,d.content as daily_details')
  255. ->join('daily d', 'd.id=dr.daily_id')
  256. ->where($map)->count();
  257. $totalPage = ceil($totalCount / $length);
  258. $result['page'] = $page;
  259. $result['total'] = $totalPage;
  260. $result['records'] = $totalCount;
  261. $result['rows'] = $lists;
  262. return json($result);
  263. } else {
  264. $this->assign('id',$id);
  265. $this->assign('s',$s);
  266. $this->assign('e',$e);
  267. $this->assign('meta_title', '检查记录列表');
  268. return $this->fetch();
  269. }
  270. }
  271. public function addrdown(){
  272. $orgid = $this->orgId;
  273. $map[] = ['del','=',0];
  274. $map[] = ['org_id','=',$this->orgId];
  275. $map= empty($map) ? true: $map;
  276. //数据查询
  277. $lists = db('daily')->where($map)->select();
  278. foreach ($lists as $k=>$v){
  279. $lists[$k]['code'] = get_qrcode_str('daily',$v['id']);
  280. }
  281. $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
  282. $dir = './uploads/qrcodeimg/'.$path.'/';
  283. foreach ($lists as $v1){
  284. $v1['title'] = '';
  285. if($v['address_id'] >0){
  286. $v1['title'] = Db::name('address')
  287. ->where('id',$v['address_id'])
  288. ->value('title');
  289. }
  290. $name = $v1['title'].'('.$v1['id'].')'.'.jpg';
  291. $filepath = $dir.'/'.$name;
  292. create_qrcode($v1['code'],$filepath);
  293. }
  294. $zippath = './uploads/qrcodeimg/'.$path.'.zip';
  295. $spath = $dir;
  296. @unlink($zippath);
  297. $zip = new \ZipArchive();
  298. if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
  299. add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  300. $zip->close(); //关闭处理的zip文件
  301. if(!file_exists($zippath)){
  302. $this->error("打包失败"); //即使创建,仍有可能失败。。。。
  303. }
  304. deldir($dir); // 删除生成的目录
  305. $downname = session('orgName').'日常工作地点二维码.zip';
  306. header("Cache-Control: public");
  307. header("Content-Description: File Transfer");
  308. header('Content-disposition: attachment; filename='.$downname); //文件名
  309. header("Content-Type: application/zip"); //zip格式的
  310. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  311. header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
  312. @readfile($zippath);
  313. }else{
  314. $this->error("打包失败");
  315. }
  316. }
  317. public function import(){
  318. return $this->fetch();
  319. }
  320. /**
  321. * 导入
  322. */
  323. public function importexcel(){
  324. set_time_limit(0);
  325. ini_set("memory_limit", -1);
  326. ob_flush();//清空缓存
  327. flush();//刷新缓存
  328. $curTime = date('Y-m-d H:i:s');
  329. try{
  330. $cols = ['','addr','remark','content','user_name'];
  331. $lists = ExcelUtil::importExcel('file',$cols,2);
  332. if($lists === false){
  333. exit(ExcelUtil::getError());
  334. }
  335. if(empty($lists)){
  336. exit('文件内容为空');
  337. }
  338. foreach ($lists as $k=>$v){
  339. if($k > 0){
  340. if(!$v['addr']){
  341. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地点为空,未导入</font><br />";
  342. continue;
  343. }
  344. if(!$v['content']){
  345. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,工作内容为空,未导入</font><br />";
  346. continue;
  347. }
  348. $addrInfo = Db::name('address')
  349. ->where('org_id',$this->orgId)
  350. ->where('del',0)
  351. ->where('title',$v['addr'])
  352. ->find();
  353. if($addrInfo){
  354. $types = $addrInfo ? explode(',',$addrInfo['types']):[];
  355. if(!in_array(10,$types)){
  356. if($addrInfo['types']){
  357. $newtypes = $addrInfo['types'].',10';
  358. }else{
  359. $newtypes = 10;
  360. }
  361. Db::name('address')->where('id',$addrInfo['id'])
  362. ->update([
  363. 'types'=>$newtypes
  364. ]);
  365. }
  366. $addrId = $addrInfo['id'];
  367. }else{
  368. $addrId = Db::name('address')->insertGetId([
  369. 'org_id'=>$this->orgId,
  370. 'title'=>$v['addr'],
  371. 'sn'=>'',
  372. 'remark'=>'',
  373. 'types'=>10,
  374. 'create_time'=>getTime()
  375. ]);
  376. }
  377. $contents = explode('、',$v['content']);
  378. $daily_form = [];
  379. foreach ($contents as $k1=>$v1){
  380. if(!$v1){
  381. continue;
  382. }
  383. $formId = Db::name('daily_form')
  384. ->where('org_id',$this->orgId)
  385. ->where('del',0)
  386. ->where('content',$v1)
  387. ->value('id');
  388. if(!$formId){
  389. $formId = Db::name('daily_form')
  390. ->insertGetId([
  391. 'org_id'=>$this->orgId,
  392. 'content'=>$v1,
  393. 'create_time'=>date('Y-m-d H:i:s')
  394. ]);
  395. }
  396. $daily_form[] = $formId;
  397. }
  398. $user_names = $v['user_name']? explode('/',$v['user_name']):[];
  399. $userIds = Db::name('user')
  400. ->alias('u')
  401. ->join('user_org uo','uo.user_id=u.id')
  402. ->where('uo.org_id',$this->orgId)
  403. ->where('u.del',0)
  404. ->where('u.real_name','in',$user_names)
  405. ->column('u.id');
  406. $dforms = $daily_form?implode(',',$daily_form):'';
  407. $dt = [
  408. 'org_id'=>$this->orgId,
  409. 'address_id'=>$addrId,
  410. 'content'=>$v['remark'],
  411. 'daily_form'=>$dforms,
  412. 'create_time'=>date('Y-m-d H:i:s'),
  413. ];
  414. $dailyfind = Db::name('daily')
  415. ->where('org_id',$this->orgId)
  416. ->where('address_id',$dt['address_id'])
  417. ->where('daily_form',$dforms)
  418. ->find();
  419. if(!$dailyfind){
  420. $insetId = Db::name('daily')->insertGetId($dt);
  421. if($userIds){
  422. foreach ($userIds as $kk=>$vv){
  423. Db::name('daily_user')->insertGetId(['daily_id'=>$insetId,'user_id'=>$vv]);
  424. }
  425. }
  426. if(!$insetId){
  427. $msg = "第".($k+2)."行,导入失败";
  428. echo "<font color=\"red\">".$msg."</font><br />";
  429. continue;
  430. }
  431. }
  432. }
  433. }
  434. echo "<font color=\"green\">导入完成</font><br />";
  435. }catch (Exception $e){
  436. trace($e->getMessage(),'error');
  437. echo $e->getMessage();
  438. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  439. }
  440. }
  441. }