0
0

WasteRecord.php 21 KB

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