123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\Db;
- class WastePrintLog extends Auth {
- public function __construct(App $app = null) {
- parent::__construct($app);
- $this->table = 'waste_print_log';
- }
- public function index() {
- if (request()->isAjax()) {
- //分页参数
- $length = input('rows', 10, 'intval'); //每页条数
- $page = input('page', 1, 'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx', 'id', 'trim'); //排序列
- $sort = input('sord', 'desc', 'trim'); //排序方式
- $order = $sortRow . ' ' . $sort;
- $title = input('title', '', 'trim');
- if ($title) {
- $map[] = ['addrss', 'like', '%' . $title . '%'];
- }
- $start_time = input('start', '', 'trim');
- $end_time = input('end', '', 'trim');
- if ($start_time) {
- $start_time = date('Ymd', strtotime($start_time)).' 00:00:00';
- $map[] = ['create_time', '>=', $start_time];
- }
- if ($end_time) {
- $end_time = date('Ymd', strtotime($end_time)).' 00:00:00';
- $map[] = ['create_time', '<=', $end_time];
- }
- $status = input('status', '', 'trim');
- if ($status != '') {
- $map[] = ['status', '=', $status];
- }
- $map[] = ['org_id', '=', $this->orgId];
- $map = empty($map) ? true : $map;
- //数据查询
- $lists = db($this->table)->where($map)
- ->limit($start, $length)
- ->order($order)->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['real_name'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- }
- //数据返回
- $totalCount = db($this->table)->where($map)->count();
- $totalPage = ceil($totalCount / $length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }
- else {
- $this->assign('m_name', '打印记录');
- return $this->fetch();
- }
- }
- /**
- * 详情
- *
- * @author wst
- * @date 2021/9/8 17:29
- * @return array
- */
- public function info($id = 0) {
- $info = Db::name('waste_print_log')
- ->where('id',$id)
- ->find();
- if (!$info) {
- $this->error('记录不存在');
- }
- $info['real_name'] = Db::name('user')
- ->where('id',$info['user_id'])
- ->value('real_name');
- $recordId = explode(',',$info['waste_record_id']);
- $record = Db::name('waste_record')
- ->alias('wr')
- ->where('wr.del',0)
- ->where('wr.id','in',$recordId)
- ->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,wr.num')
- ->order('wr.id desc')
- ->select();
- foreach ($record as $k=>$v){
- $title=Db::name('address')
- ->field('title')
- ->where('id',$v['waste_device_id'])
- ->find();
- $record[$k]['title'] = $title?$title['title']:'';
- $record[$k]['cate_name'] =Db::name('waste_type')
- ->where('id',$v['cateid'])->value('title');
- $record[$k]['weight'] = sprintf("%.2f",$v['weight']/1000);
- $cate = Db::name('waste_type')->where('id',$v['cateid'])->find();
- $tnum = round($v['weight'] / 1000, 2).'kg';
- if($cate['type'] == 1 && $v['num'] > 0){
- $tnum = round($v['weight'] / 1000, 2).'kg'.'('.$cate['unit'].')';
- }
- $record[$k]['tum'] = $tnum;
- }
- $info['record'] = $record;
- $this->assign('info',$info);
- return $this->fetch();
- }
- /**
- * 转运
- *
- * @author wst
- * @date 2021/9/8 17:48
- * @param int $id
- * @return bool
- */
- public function finish($id) {
- $info = $this->model->getOne($id,cur_org_id());
- if (!$info) {
- $this->error('记录不存在');
- }
- if ($info['status'] != 1) {
- $this->error('处于已交接状态的才可转运');
- }
- $ret = $this->model->finish($id, is_login());
- $ret ? $this->success('转运成功') : $this->error($this->model->getError());
- }
- /**
- * 批量转运
- *
- * @author wst
- * @date 2021/9/8 17:48
- * @param array $ids
- * @return bool
- */
- public function batchFinish() {
- $ids = input('ids', [], 'trim');
- if (empty($ids)) {
- $this->error('未选择记录');
- }
- $success = 0;
- $error = '';
- foreach ($ids as $k => $v) {
- $info = $this->model->getOne($v,cur_org_id());
- if (!$info) {
- $error .= 'id='.$v . ':记录不存在';
- continue;
- }
- if ($info['status'] != 1) {
- $error .= 'id='.$v.':当期状态不可转运';
- continue;
- }
- $ret = $this->model->finish($v, is_login());
- if ($ret) {
- $success++;
- }
- else {
- $error .= 'id='.$v.':' . $this->model->getError();
- }
- }
- $success ? $this->success('成功转运' . $success . '条记录') : $this->error($error);
- }
- /**
- * 删除记录
- * @param int $id
- */
- public function del($id = 0) {
- if (!$id) {
- $this->error('参数错误');
- }
- $res = db($this->table)->where('id', $id)->update([
- 'del' => 1,
- 'del_user_id' => is_login(),
- 'del_datetime' => getTime(),
- ]);
- if ($res) {
- $this->success('删除成功');
- }
- else {
- $this->error('删除失败');
- }
- }
- //excel导出
- public function export() {
- $meta_title = '医废记录';
- if (request()->isGet()) {
- $cateid = input('cateid', '', 'trim');
- if ($cateid) {
- $map[] = ['cateid', 'like', '%' . $cateid . '%'];
- }
- $start_time = input('start', '', 'trim');
- $end_time = input('end', '', 'trim');
- if ($start_time !== '' && $end_time !== '') {
- if ($start_time <= $end_time) {
- $start_time = date('Ymd', strtotime($start_time));
- $end_time = date('Ymd', strtotime($end_time));
- $map[] = ['create_yyyymmdd', 'between', [$start_time, $end_time]];
- }
- }
- if ($start_time !== '' && $end_time == '') {
- $start_time = date('Ymd', strtotime($start_time));
- $map[] = ['create_yyyymmdd', '>=', $start_time];
- }
- if ($start_time == '' && $end_time !== '') {
- $end_time = date('Ymd', strtotime($end_time));
- $map[] = ['create_yyyymmdd', '<=', $end_time];
- }
- $status = input('status', '', 'trim');
- if ($status != '') {
- if ($status == 4) {
- $map[] = ['status', 'in', [0, 1]];
- }
- else {
- if (in_array($status, array(0, 1, 2, 3))) {
- $map[] = ['status', '=', $status];
- }
- }
- }
- $map[] = ['del', '=', 0];
- $map[] = ['org_id', '=', $this->orgId];
- $map = empty($map) ? true : $map;
- //数据查询
- $lists = db($this->table)->where($map)->select();
- foreach ($lists as $k => $v) {
- $lists[$k]['waste_device'] = db('address')
- ->where('id', $v['waste_device_id'])
- ->value('title');
- $lists[$k]['real_name'] = db('user')
- ->where('id', $v['user_id'])
- ->value('real_name');
- $lists[$k]['cate'] = db('waste_type')
- ->where('id', $v['cateid'])
- ->value('title');
- $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
- $lists[$k]['serial_number_path'] = url('Qrcode/qrcode') . '?code=' . $v['serial_number'];
- $lists[$k]['status'] = $this->model->status[$v['status']];
- }
- include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
- //实例化PHPExcel类
- $objPHPExcel = new \PHPExcel();
- //激活当前的sheet表
- $objPHPExcel->setActiveSheetIndex(0);
- //设置表格头(即excel表格的第一行)
- $objPHPExcel->setActiveSheetIndex(0)
- ->setCellValue('A1', '编号')
- ->setCellValue('B1', '收取科室')
- ->setCellValue('C1', '收取人')
- ->setCellValue('D1', '类别')
- ->setCellValue('E1', '重量(kg)')
- ->setCellValue('F1', '收取时间')
- ->setCellValue('G1', '状态')
- ->setCellValue('H1', '医废异常信息');
- // 设置表格头水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置列水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置单元格宽度
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
- //循环刚取出来的数组,将数据逐一添加到excel表格。
- for ($i = 0; $i < count($lists); $i++) {
- $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['waste_device']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['real_name']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['cate']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['weight']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['create_time']);
- $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['status']);
- $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['reason']);
- }
- //设置保存的Excel表格名称
- $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
- //设置当前激活的sheet表格名称
- $objPHPExcel->getActiveSheet()->setTitle($meta_title);
- //设置浏览器窗口下载表格
- ob_end_clean();
- header("Content-Type: application/force-download");
- header("Content-Type: application/octet-stream");
- header("Content-Type: application/download");
- header('Content-Disposition:inline;filename="' . $filename);
- //生成excel文件
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- //下载文件在浏览器窗口
- return $objWriter->save('php://output');
- }
- }
- }
|