WastePackage.php 14 KB

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