WasteApplyOption.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class WasteApplyOption extends Auth {
  7. public function __construct(App $app = null) {
  8. parent::__construct($app);
  9. $this->table = 'waste_apply';
  10. }
  11. public $s = [
  12. '申请中',
  13. '审核通过',
  14. '审核拒绝',
  15. '撤回',
  16. ];
  17. public function index() {
  18. if (request()->isAjax()) {
  19. //分页参数
  20. $length = input('rows', 10, 'intval'); //每页条数
  21. $page = input('page', 1, 'intval'); //第几页
  22. $start = ($page - 1) * $length; //分页开始位置
  23. //排序
  24. $sortRow = input('sidx', 'id', 'trim'); //排序列
  25. $sort = input('sord', 'desc', 'trim'); //排序方式
  26. $order = $sortRow . ' ' . $sort;
  27. $cateid = input('cateid', '', 'trim');
  28. if ($cateid) {
  29. $map[] = ['cateid', 'like', '%' . $cateid . '%'];
  30. }
  31. $addrId= input('addrId','');
  32. if ($addrId) {
  33. $map[] = ['waste_device_id','=',$addrId];
  34. }
  35. $start_time = input('start', '', 'trim');
  36. $end_time = input('end', '', 'trim');
  37. if ($start_time !== '' && $end_time !== '') {
  38. if ($start_time <= $end_time) {
  39. $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
  40. $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
  41. $map[] = ['create_time', 'between', [$start_time, $end_time]];
  42. }
  43. }
  44. if ($start_time !== '' && $end_time == '') {
  45. $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
  46. $map[] = ['create_time', '>=', $start_time];
  47. }
  48. if ($start_time == '' && $end_time !== '') {
  49. $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
  50. $map[] = ['create_time', '<=', $end_time];
  51. }
  52. $status = input('status', '', 'trim');
  53. // $post = request()->post();
  54. // if (!isset($post['status'])) {
  55. // $map[] = ['status', 'in', [0, 1]];
  56. // }
  57. if($status != ''){
  58. $map[] = ['status', '=', $status];
  59. }
  60. $map[] = ['del', '=', 0];
  61. //$map[] = ['org_id', '=', $this->orgId];
  62. $map = empty($map) ? true : $map;
  63. //数据查询
  64. $lists = db($this->table)
  65. ->where($map)
  66. ->limit($start, $length)
  67. ->order('id desc')->select();
  68. foreach ($lists as $k => $v) {
  69. $lists[$k]['waste_device'] = db('address')
  70. ->where('id', $v['waste_device_id'])
  71. ->value('title');
  72. $lists[$k]['real_name'] = db('user')
  73. ->where('id', $v['user_id'])
  74. ->value('real_name');
  75. $lists[$k]['cate'] = db('waste_type')
  76. ->where('id', $v['cateid'])
  77. ->value('title');
  78. $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
  79. $lists[$k]['statusName'] = isset($this->s[$v['status']])?$this->s[$v['status']]:'';
  80. $lists[$k]['deal_real_name'] = db('user')
  81. ->where('id', $v['deal_user_id'])
  82. ->value('real_name');
  83. $lists[$k]['orgName'] = db('org')
  84. ->where('id', $v['org_id'])
  85. ->value('name');
  86. }
  87. //数据返回
  88. $totalCount = db($this->table)->where($map)->count();
  89. $totalPage = ceil($totalCount / $length);
  90. $result['page'] = $page;
  91. $result['total'] = $totalPage;
  92. $result['records'] = $totalCount;
  93. $result['rows'] = $lists;
  94. return json($result);
  95. }
  96. else {
  97. $type = (new \app\common\model\WasteType())->getList();
  98. $addr = model('Address')->getListByType(7,$this->orgId);
  99. $this->assign('addr', $addr);
  100. $this->assign('m_name', '修改申请审核');
  101. $this->assign('type', $type);
  102. $this->assign('status', $this->s);
  103. return $this->fetch();
  104. }
  105. }
  106. public function edit($id) {
  107. if (empty($id)) {
  108. $this->error('未选择记录');
  109. }
  110. $info = Db::name($this->table)->where('id',$id)->find();
  111. if (!$info) {
  112. $this->error('申请记录不存在');
  113. }
  114. if (request()->isPost()) {
  115. $data = request()->post();
  116. $check = Db::name('waste_apply')
  117. ->where('record_id',$id)
  118. ->where('del',0)
  119. ->where('status',0)
  120. ->find();
  121. if ($check) {
  122. $this->error('当前记录正在申请中');
  123. }
  124. $data['record_id'] = $data['id'];
  125. $data['org_id'] =$this->orgId;
  126. $data['create_time'] = getTime();
  127. unset($data['id']);
  128. $result = validate('WasteApply')->check($data,[],'');
  129. if(true !== $result){
  130. $this->error(validate('WasteApply')->getError());
  131. }
  132. $res = Db::name('waste_apply')
  133. ->where('id',$id)
  134. ->update($data);
  135. $res?$this->success('操作成功'):$this->error('操作失败');
  136. }else{
  137. $scan = Db::name('waste_scale')
  138. ->where('enable',1)
  139. ->where('del',0)
  140. ->where('org_id',cur_org_id())
  141. ->select();
  142. $uList = [];
  143. if(!empty($scan)){
  144. $uList = Db::name('user')
  145. ->where('del',0)
  146. ->where('id','in',array_column($scan,'user_id'))
  147. ->field('id,real_name as title')
  148. ->select();
  149. }
  150. $type = (new \app\common\model\WasteType())->getList();
  151. $addr = model('Address')->getListByType(7,$this->orgId);
  152. $this->assign('addr', $addr);
  153. $this->assign('type', $type);
  154. $this->assign('uList',$uList);
  155. $this->assign('info',$info);
  156. $uInfo = Db::name('user')
  157. ->where('id',$this->userId)
  158. ->find();
  159. $this->assign('uInfo',$uInfo);
  160. return $this->fetch();
  161. }
  162. }
  163. public function withdraw($id = 0) {
  164. if (!$id) {
  165. $this->error('参数错误');
  166. }
  167. $info = db($this->table)->where('id', $id)
  168. ->find();
  169. if(empty($info)){
  170. $this->error('记录不存在');
  171. }
  172. if($info['status']!=0){
  173. $this->error('当前记录状态不可撤回');
  174. }
  175. $res = db($this->table)->where('id', $id)->update([
  176. 'status' => 3,
  177. 'update_time' => getTime(),
  178. ]);
  179. if ($res) {
  180. $this->success('操作成功');
  181. }
  182. else {
  183. $this->error('操作失败');
  184. }
  185. }
  186. public function option($id = 0) {
  187. if (!$id) {
  188. $this->error('参数错误');
  189. }
  190. $info = db($this->table)->where('id', $id)
  191. ->find();
  192. if(empty($info)){
  193. $this->error('记录不存在');
  194. }
  195. if($info['status']!=0){
  196. $this->error('当前记录状态不可审核');
  197. }
  198. $old = Db::name('waste_record')
  199. ->where('id',$info['record_id'])
  200. ->find();
  201. if (request()->isPost()) {
  202. $data = request()->post();
  203. $key = ['user_id','cateid','weight','waste_device_id'];
  204. $su = 0;
  205. $aa = [];
  206. foreach ($key as $kk=>$vv){
  207. if($info[$vv]!=$old[$vv]){
  208. $aa[$vv] = $info[$vv];
  209. }
  210. }
  211. if(empty($aa)){
  212. $this->error('没有要修改的内容');
  213. }
  214. Db::startTrans();
  215. try{
  216. $rr = Db::name('waste_record')
  217. ->where('id',$info['record_id'])
  218. ->update($aa);
  219. if(!$rr){
  220. \exception('操作失败');
  221. }
  222. $res = Db::name($this->table)->where('id', $id)->update([
  223. 'status' => $data['status'],
  224. 'remark' => $data['remark'],
  225. 'update_time' => getTime(),
  226. 'deal_user_id' => is_login(),
  227. 'deal_time' => getTime(),
  228. ]);
  229. if ($res) {
  230. Db::commit();
  231. $this->success('操作成功');
  232. }
  233. else {
  234. \exception('操作失败');
  235. }
  236. }catch (Exception $e){
  237. Db::rollback();
  238. $this->error($e->getMessage());
  239. }
  240. }else{
  241. $scan = Db::name('waste_scale')
  242. ->where('enable',1)
  243. ->where('del',0)
  244. ->where('org_id',cur_org_id())
  245. ->select();
  246. $uList = [];
  247. if(!empty($scan)){
  248. $uList = Db::name('user')
  249. ->where('del',0)
  250. ->where('id','in',array_column($scan,'user_id'))
  251. ->field('id,real_name as title')
  252. ->select();
  253. }
  254. $type = (new \app\common\model\WasteType())->getList();
  255. $addr = model('Address')->getListByType(7,$this->orgId);
  256. $this->assign('addr', $addr);
  257. $this->assign('type', $type);
  258. $this->assign('uList',$uList);
  259. $this->assign('old',$old);
  260. $this->assign('info',$info);
  261. $org = Db::name('org')
  262. ->where('id',$info['org_id'])
  263. ->value('name');
  264. $this->assign('org',$org);
  265. return $this->fetch();
  266. }
  267. }
  268. public function info($id){
  269. if (!$id) {
  270. $this->error('参数错误');
  271. }
  272. $info = db($this->table)->where('id', $id)
  273. ->find();
  274. if(empty($info)){
  275. $this->error('记录不存在');
  276. }
  277. $old = $info['old_data']?json_decode($info['old_data'],true):[];
  278. $scan = Db::name('waste_scale')
  279. ->where('enable',1)
  280. ->where('del',0)
  281. ->where('org_id',cur_org_id())
  282. ->select();
  283. $uList = [];
  284. if(!empty($scan)){
  285. $uList = Db::name('user')
  286. ->where('del',0)
  287. ->where('id','in',array_column($scan,'user_id'))
  288. ->field('id,real_name as title')
  289. ->select();
  290. }
  291. $type = (new \app\common\model\WasteType())->getList();
  292. $addr = model('Address')->getListByType(7,$this->orgId);
  293. $this->assign('addr', $addr);
  294. $this->assign('type', $type);
  295. $this->assign('uList',$uList);
  296. $this->assign('old',$old);
  297. $this->assign('info',$info);
  298. $org = Db::name('org')
  299. ->where('id',$info['org_id'])
  300. ->value('name');
  301. $this->assign('org',$org);
  302. return $this->fetch();
  303. }
  304. //excel导出
  305. public function export() {
  306. $meta_title = '医废修改审核';
  307. if (request()->isGet()) {
  308. $cateid = input('cateid', '', 'trim');
  309. if ($cateid) {
  310. $map[] = ['cateid', 'like', '%' . $cateid . '%'];
  311. }
  312. $addrId= input('addrId','');
  313. if ($addrId) {
  314. $map[] = ['waste_device_id','=',$addrId];
  315. }
  316. $start_time = input('start', '', 'trim');
  317. $end_time = input('end', '', 'trim');
  318. if ($start_time !== '' && $end_time !== '') {
  319. if ($start_time <= $end_time) {
  320. $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
  321. $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
  322. $map[] = ['create_time', 'between', [$start_time, $end_time]];
  323. }
  324. }
  325. if ($start_time !== '' && $end_time == '') {
  326. $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
  327. $map[] = ['create_time', '>=', $start_time];
  328. }
  329. if ($start_time == '' && $end_time !== '') {
  330. $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
  331. $map[] = ['create_time', '<=', $end_time];
  332. }
  333. $status = input('status', '', 'trim');
  334. // $post = request()->post();
  335. // if (!isset($post['status'])) {
  336. // $map[] = ['status', 'in', [0, 1]];
  337. // }
  338. if($status != ''){
  339. $map[] = ['status', '=', $status];
  340. }
  341. $map[] = ['del', '=', 0];
  342. // $map[] = ['org_id', '=', $this->orgId];
  343. $map = empty($map) ? true : $map;
  344. //数据查询
  345. $lists = db($this->table)
  346. ->where($map)
  347. ->order('id desc')->select();
  348. foreach ($lists as $k => $v) {
  349. $lists[$k]['waste_device'] = db('address')
  350. ->where('id', $v['waste_device_id'])
  351. ->value('title');
  352. $lists[$k]['real_name'] = db('user')
  353. ->where('id', $v['user_id'])
  354. ->value('real_name');
  355. $lists[$k]['cate'] = db('waste_type')
  356. ->where('id', $v['cateid'])
  357. ->value('title');
  358. $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
  359. $lists[$k]['statusName'] = isset($this->s[$v['status']])?$this->s[$v['status']]:'';
  360. $lists[$k]['deal_real_name'] = db('user')
  361. ->where('id', $v['deal_user_id'])
  362. ->value('real_name');
  363. $lists[$k]['orgName'] = db('org')
  364. ->where('id', $v['org_id'])
  365. ->value('name');
  366. }
  367. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  368. //实例化PHPExcel类
  369. $objPHPExcel = new \PHPExcel();
  370. //激活当前的sheet表
  371. $objPHPExcel->setActiveSheetIndex(0);
  372. //设置表格头(即excel表格的第一行)
  373. $objPHPExcel->setActiveSheetIndex(0)
  374. ->setCellValue('A1', '编号')
  375. ->setCellValue('B1', '收取地点')
  376. ->setCellValue('C1', '收取人')
  377. ->setCellValue('D1', '类别')
  378. ->setCellValue('E1', '重量(kg)')
  379. ->setCellValue('F1', '时间')
  380. ->setCellValue('G1', '状态')
  381. ->setCellValue('H1', '审核人')
  382. ->setCellValue('I1', '审核时间')
  383. ->setCellValue('J1', '医废id')
  384. ->setCellValue('K1', '所属项目');
  385. // 设置表格头水平居中
  386. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  387. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  388. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  389. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  390. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  391. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  392. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  393. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  394. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  395. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  396. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  397. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  398. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  399. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  400. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  401. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  402. //设置列水平居中
  403. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  404. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  405. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  406. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  407. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  408. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  409. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  410. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  411. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  412. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  413. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  414. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  415. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  416. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  417. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  418. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  419. //设置单元格宽度
  420. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  421. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  422. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  423. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  424. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  425. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  426. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  427. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  428. //循环刚取出来的数组,将数据逐一添加到excel表格。
  429. for ($i = 0; $i < count($lists); $i++) {
  430. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
  431. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['waste_device']);
  432. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['real_name']);
  433. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['cate']);
  434. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['weight']);
  435. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['create_time']);
  436. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['statusName']);
  437. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['deal_real_name']);
  438. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['deal_time']);
  439. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['record_id']);
  440. $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $lists[$i]['orgName']);
  441. }
  442. //设置保存的Excel表格名称
  443. $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
  444. //设置当前激活的sheet表格名称
  445. $objPHPExcel->getActiveSheet()->setTitle($meta_title);
  446. //设置浏览器窗口下载表格
  447. ob_end_clean();
  448. header("Content-Type: application/force-download");
  449. header("Content-Type: application/octet-stream");
  450. header("Content-Type: application/download");
  451. header('Content-Disposition:inline;filename="' . $filename);
  452. //生成excel文件
  453. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  454. //下载文件在浏览器窗口
  455. return $objWriter->save('php://output');
  456. }
  457. }
  458. }