WastePrintLog.php 14 KB

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