model = new \app\common\model\WasteRecord(); $this->table = $this->model->table; } 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; $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]; } } } $post = request()->post(); if (!isset($post['status'])) { $map[] = ['status', 'in', [0, 1]]; } $map[] = ['del', '=', 0]; $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]['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']]; } //数据返回 $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 { $type = (new \app\common\model\WasteType())->getList(); $this->assign('m_name', '医废类型'); $this->assign('type', $type); $this->assign('status', $this->model->status); return $this->fetch(); } } /** * 详情 * * @author wst * @date 2021/9/8 17:29 * @return array */ public function info($id = 0) { $info = $this->model->info($id,cur_org_id()); if (!$info) { $this->error('记录不存在'); } $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 .= '当前状态不可转运'; 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表 $objActSheet = $objPHPExcel->setActiveSheetIndex(0); //设置表格头(即excel表格的第一行) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '编号') ->setCellValue('B1', '收取地点') ->setCellValue('C1', '收取人') ->setCellValue('D1', '类别') ->setCellValue('E1', '重量(kg)') ->setCellValue('F1', '收取时间') ->setCellValue('G1', '状态') ->setCellValue('H1', '医废异常信息') ->setCellValue('I1', '是否签字') ->setCellValue('J1', '签名图'); // 设置表格头水平居中 $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']); $sign_text = '否'; if($lists[$i]['sign'] == 1){ $sign_text = '是'; } $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $sign_text); $signList = explode(".",$lists[$i]['sign_path']); if($signList){ $ext = end($signList); $img =''; if ($ext == 'jpg' || $ext == 'jpeg'){ $img = @imagecreatefromjpeg($lists[$i]['sign_path']); } if ($ext == 'png'){ $img = @imagecreatefrompng($lists[$i]['sign_path']); } if($img){ //实例化图片操作类 $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing(); $objDrawing->setImageResource($img); $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);//渲染方法 $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT); $objDrawing->setHeight(40);//照片高度 $objDrawing->setWidth(40); //照片宽度 $objDrawing->setCoordinates('J'.($i+2));//图片要插入的单元格 $objDrawing->setOffsetX(8);//图片X轴偏移 $objDrawing->setOffsetY(8);//图片Y轴偏移 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //设置每一行高度 }else{ $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), ''); } }else{ $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), ''); } $objActSheet->getRowDimension($i + 2)->setRowHeight(40); // 行高 } //设置保存的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'); } } public function allZy(){ $ids = Db::name('waste_record') ->where('org_id',$this->orgId) ->where('del',0) ->where('status',1) ->column('id'); if($ids){ // 启动事务 Db::startTrans(); try { $ret = Db::name('waste_record') ->whereIn('id', $ids) ->update(['status'=>2]); if(!$ret){ throw new Exception('更新状态失败'); } foreach ($ids as $k=>$v){ $ret2 = Db::name('waste_record_user')->insert(array( 'waste_record_id' => $v, 'user_id' => $this->userId, 'status' => 2, 'create_time' => getTime() )); if(!$ret2){ throw new exception('保存操作记录失败'); } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } $this->success('成功转运' . count($ids) . '条记录'); }else{ $this->success('成功转运'); } } public function apply() { $id= input('id', ''); if (empty($id)) { $this->error('未选择记录'); } $info = $this->model->getOne($id,cur_org_id()); if (!$info) { $this->error('医废记录不存在'); } if (request()->isPost()) { $data = request()->post(); $check = Db::name('waste_apply') ->where('record_id',$id) ->where('del',0) ->where('status',0) ->find(); if ($check) { $this->error('当前记录正在申请中'); } $old = Db::name('waste_record') ->where('id',$info['id']) ->find(); $key = ['user_id','cateid','weight','waste_device_id']; $su = 0; foreach ($key as $kk=>$vv){ if($data[$vv]!=$old[$vv]){ $su++; } } if($su==0){ $this->error('当前数据未做修改'); } $data['record_id'] = $data['id']; $data['org_id'] =$this->orgId; $data['create_time'] = getTime(); unset($data['id']); $result = validate('WasteApply')->check($data,[],''); if(true !== $result){ $this->error(validate('WasteApply')->getError()); } $data['old_data'] = json_encode($old); $res = Db::name('waste_apply')->insertGetId($data); $res?$this->success('操作成功'):$this->error('操作失败'); }else{ $scan = Db::name('waste_scale') ->where('enable',1) ->where('del',0) ->where('org_id',cur_org_id()) ->select(); $uList = []; if(!empty($scan)){ $uList = Db::name('user') ->where('del',0) ->where('id','in',array_column($scan,'user_id')) ->field('id,real_name as title') ->select(); } $type = model('WasteType')->getList(); $addr = model('Address')->getListByType(7,$this->orgId); $this->assign('addr', $addr); $this->assign('type', $type); $this->assign('uList',$uList); $this->assign('info',$info); $uInfo = Db::name('user') ->where('id',$this->userId) ->find(); $this->assign('uInfo',$uInfo); return $this->fetch(); } } }