0
0

WasteApply.php 18 KB

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