0
0

WastePackage.php 14 KB

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