WastePackage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class WastePackage extends Auth {
  6. public function __construct(App $app = null) {
  7. parent::__construct($app);
  8. $this->table = 'waste_package';
  9. }
  10. public function index() {
  11. if (request()->isAjax()) {
  12. //分页参数
  13. $length = input('rows', 10, 'intval'); //每页条数
  14. $page = input('page', 1, 'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. //排序
  17. $sortRow = input('sidx', 'id', 'trim'); //排序列
  18. $sort = input('sord', 'desc', 'trim'); //排序方式
  19. $order = $sortRow . ' ' . $sort;
  20. $cateid = input('cateid', '', 'trim');
  21. if ($cateid) {
  22. $map[] = ['cateid', 'like', '%' . $cateid . '%'];
  23. }
  24. $start_time = input('start', '', 'trim');
  25. $end_time = input('end', '', 'trim');
  26. if ($start_time) {
  27. $start_time = date('Ymd', strtotime($start_time)).' 00:00:00';
  28. $map[] = ['create_time', '>=', $start_time];
  29. }
  30. if ($end_time) {
  31. $end_time = date('Ymd', strtotime($end_time)).' 00:00:00';
  32. $map[] = ['create_time', '<=', $end_time];
  33. }
  34. $status = input('status', '', 'trim');
  35. if ($status != '') {
  36. $map[] = ['status', '=', $status];
  37. }
  38. $map[] = ['del', '=', 0];
  39. $map[] = ['org_id', '=', $this->orgId];
  40. $map = empty($map) ? true : $map;
  41. //数据查询
  42. $lists = db($this->table)->where($map)
  43. ->limit($start, $length)
  44. ->order($order)->select();
  45. foreach ($lists as $k => $v) {
  46. $lists[$k]['real_name'] = db('user')
  47. ->where('id', $v['user_id'])
  48. ->value('real_name');
  49. $ids = explode(',',$v['record']);
  50. $record= Db::name('waste_record')
  51. ->where('id','in',$ids)
  52. ->where('del',0)
  53. ->where('org_id',$this->orgId)
  54. ->count();
  55. $weight= Db::name('waste_record')
  56. ->where('id','in',$ids)
  57. ->where('del',0)
  58. ->where('org_id',$this->orgId)
  59. ->sum('weight');
  60. $num = Db::name('waste_record')
  61. ->where('id','in',$ids)
  62. ->where('del',0)
  63. ->where('org_id',$this->orgId)
  64. ->sum('num');
  65. $kg = round($weight / 1000, 2);
  66. $lists[$k]['weight'] = $kg;
  67. $lists[$k]['count'] = $record?$record:0;
  68. $lists[$k]['qrcode'] = url('Qrcode/qrcode') . '?code=' . $v['sn'];
  69. }
  70. //数据返回
  71. $totalCount = db($this->table)->where($map)->count();
  72. $totalPage = ceil($totalCount / $length);
  73. $result['page'] = $page;
  74. $result['total'] = $totalPage;
  75. $result['records'] = $totalCount;
  76. $result['rows'] = $lists;
  77. return json($result);
  78. }
  79. else {
  80. $this->assign('m_name', '打包记录');
  81. $this->assign('status', [
  82. 0=>"待交接",
  83. 1=>"待转运",
  84. 2=>"已转运",
  85. ]);
  86. return $this->fetch();
  87. }
  88. }
  89. /**
  90. * 详情
  91. *
  92. * @author wst
  93. * @date 2021/9/8 17:29
  94. * @return array
  95. */
  96. public function info($id = 0) {
  97. $info = Db::name('waste_package')
  98. ->where('id',$id)
  99. ->find();
  100. if (!$info) {
  101. $this->error('记录不存在');
  102. }
  103. $data = (new \app\common\model\WasteRecord())->packInfo($info['sn'],cur_org_id());
  104. $info = $data['pinfo'];
  105. $record = $data['record'];
  106. $this->assign('info', $info);
  107. $this->assign('record', $record);
  108. return $this->fetch();
  109. }
  110. /**
  111. * 转运
  112. *
  113. * @author wst
  114. * @date 2021/9/8 17:48
  115. * @param int $id
  116. * @return bool
  117. */
  118. public function finish($id) {
  119. $info = $this->model->getOne($id,cur_org_id());
  120. if (!$info) {
  121. $this->error('记录不存在');
  122. }
  123. if ($info['status'] != 1) {
  124. $this->error('处于已交接状态的才可转运');
  125. }
  126. $ret = $this->model->finish($id, is_login());
  127. $ret ? $this->success('转运成功') : $this->error($this->model->getError());
  128. }
  129. /**
  130. * 批量转运
  131. *
  132. * @author wst
  133. * @date 2021/9/8 17:48
  134. * @param array $ids
  135. * @return bool
  136. */
  137. public function batchFinish() {
  138. $ids = input('ids', [], 'trim');
  139. if (empty($ids)) {
  140. $this->error('未选择记录');
  141. }
  142. $success = 0;
  143. $error = '';
  144. foreach ($ids as $k => $v) {
  145. $info = $this->model->getOne($v,cur_org_id());
  146. if (!$info) {
  147. $error .= 'id='.$v . ':记录不存在';
  148. continue;
  149. }
  150. if ($info['status'] != 1) {
  151. $error .= 'id='.$v.':当期状态不可转运';
  152. continue;
  153. }
  154. $ret = $this->model->finish($v, is_login());
  155. if ($ret) {
  156. $success++;
  157. }
  158. else {
  159. $error .= 'id='.$v.':' . $this->model->getError();
  160. }
  161. }
  162. $success ? $this->success('成功转运' . $success . '条记录') : $this->error($error);
  163. }
  164. /**
  165. * 删除记录
  166. * @param int $id
  167. */
  168. public function del($id = 0) {
  169. if (!$id) {
  170. $this->error('参数错误');
  171. }
  172. $res = db($this->table)->where('id', $id)->update([
  173. 'del' => 1,
  174. 'del_user_id' => is_login(),
  175. 'del_datetime' => getTime(),
  176. ]);
  177. if ($res) {
  178. $this->success('删除成功');
  179. }
  180. else {
  181. $this->error('删除失败');
  182. }
  183. }
  184. //excel导出
  185. public function export() {
  186. $meta_title = '医废记录';
  187. if (request()->isGet()) {
  188. $cateid = input('cateid', '', 'trim');
  189. if ($cateid) {
  190. $map[] = ['cateid', 'like', '%' . $cateid . '%'];
  191. }
  192. $start_time = input('start', '', 'trim');
  193. $end_time = input('end', '', 'trim');
  194. if ($start_time !== '' && $end_time !== '') {
  195. if ($start_time <= $end_time) {
  196. $start_time = date('Ymd', strtotime($start_time));
  197. $end_time = date('Ymd', strtotime($end_time));
  198. $map[] = ['create_yyyymmdd', 'between', [$start_time, $end_time]];
  199. }
  200. }
  201. if ($start_time !== '' && $end_time == '') {
  202. $start_time = date('Ymd', strtotime($start_time));
  203. $map[] = ['create_yyyymmdd', '>=', $start_time];
  204. }
  205. if ($start_time == '' && $end_time !== '') {
  206. $end_time = date('Ymd', strtotime($end_time));
  207. $map[] = ['create_yyyymmdd', '<=', $end_time];
  208. }
  209. $status = input('status', '', 'trim');
  210. if ($status != '') {
  211. if ($status == 4) {
  212. $map[] = ['status', 'in', [0, 1]];
  213. }
  214. else {
  215. if (in_array($status, array(0, 1, 2, 3))) {
  216. $map[] = ['status', '=', $status];
  217. }
  218. }
  219. }
  220. $map[] = ['del', '=', 0];
  221. $map[] = ['org_id', '=', $this->orgId];
  222. $map = empty($map) ? true : $map;
  223. //数据查询
  224. $lists = db($this->table)->where($map)->select();
  225. foreach ($lists as $k => $v) {
  226. $lists[$k]['waste_device'] = db('address')
  227. ->where('id', $v['waste_device_id'])
  228. ->value('title');
  229. $lists[$k]['real_name'] = db('user')
  230. ->where('id', $v['user_id'])
  231. ->value('real_name');
  232. $lists[$k]['cate'] = db('waste_type')
  233. ->where('id', $v['cateid'])
  234. ->value('title');
  235. $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
  236. $lists[$k]['serial_number_path'] = url('Qrcode/qrcode') . '?code=' . $v['serial_number'];
  237. $lists[$k]['status'] = $this->model->status[$v['status']];
  238. }
  239. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  240. //实例化PHPExcel类
  241. $objPHPExcel = new \PHPExcel();
  242. //激活当前的sheet表
  243. $objPHPExcel->setActiveSheetIndex(0);
  244. //设置表格头(即excel表格的第一行)
  245. $objPHPExcel->setActiveSheetIndex(0)
  246. ->setCellValue('A1', '编号')
  247. ->setCellValue('B1', '收取科室')
  248. ->setCellValue('C1', '收取人')
  249. ->setCellValue('D1', '类别')
  250. ->setCellValue('E1', '重量(kg)')
  251. ->setCellValue('F1', '收取时间')
  252. ->setCellValue('G1', '状态')
  253. ->setCellValue('H1', '医废异常信息');
  254. // 设置表格头水平居中
  255. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  256. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  257. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  258. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  259. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  260. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  261. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  262. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  263. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  264. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  265. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  266. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  267. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  268. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  269. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  270. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  271. //设置列水平居中
  272. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  273. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  274. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  275. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  276. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  277. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  278. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  279. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  280. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  281. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  282. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  283. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  284. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  285. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  286. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  287. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  288. //设置单元格宽度
  289. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  290. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  291. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  292. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  293. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  294. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  295. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  296. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  297. //循环刚取出来的数组,将数据逐一添加到excel表格。
  298. for ($i = 0; $i < count($lists); $i++) {
  299. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
  300. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['waste_device']);
  301. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['real_name']);
  302. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['cate']);
  303. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['weight']);
  304. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['create_time']);
  305. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['status']);
  306. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['reason']);
  307. }
  308. //设置保存的Excel表格名称
  309. $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
  310. //设置当前激活的sheet表格名称
  311. $objPHPExcel->getActiveSheet()->setTitle($meta_title);
  312. //设置浏览器窗口下载表格
  313. ob_end_clean();
  314. header("Content-Type: application/force-download");
  315. header("Content-Type: application/octet-stream");
  316. header("Content-Type: application/download");
  317. header('Content-Disposition:inline;filename="' . $filename);
  318. //生成excel文件
  319. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  320. //下载文件在浏览器窗口
  321. return $objWriter->save('php://output');
  322. }
  323. }
  324. }