package cn.besteam.modules.medical.service.impl;
|
|
import cn.besteam.common.constant.DepartmentEnum;
|
import cn.besteam.common.constant.ErrorCode;
|
import cn.besteam.common.exception.BusinessException;
|
import cn.besteam.common.util.DateUtil;
|
import cn.besteam.common.util.RedisUtil;
|
import cn.besteam.common.util.oConvertUtils;
|
import cn.besteam.modules.medical.dto.MwWasteWaterReportDto;
|
import cn.besteam.modules.medical.dto.MwWasteWaterSeacherDto;
|
import cn.besteam.modules.medical.entity.MwDetectionIndex;
|
import cn.besteam.modules.medical.entity.MwEquipment;
|
import cn.besteam.modules.medical.entity.MwWasteWater;
|
import cn.besteam.modules.medical.mapper.MwWasteWaterMapper;
|
import cn.besteam.modules.medical.service.IMwDetectionIndexService;
|
import cn.besteam.modules.medical.service.IMwEquipmentService;
|
import cn.besteam.modules.medical.service.IMwWasteWaterService;
|
import cn.besteam.modules.medical.vo.DetectionIndexVo;
|
import cn.besteam.modules.medical.vo.MwCollectRecordVO;
|
import cn.besteam.modules.medical.vo.WasteWaterVo;
|
import cn.besteam.modules.report.dto.ReportDTO;
|
import cn.besteam.modules.report.vo.*;
|
import cn.besteam.modules.shiro.authc.util.EnvUtils;
|
import cn.besteam.modules.system.entity.SysDepartment;
|
import cn.besteam.modules.system.entity.SysDictItem;
|
import cn.besteam.modules.system.service.ISysDepartmentService;
|
import cn.hutool.poi.excel.ExcelWriter;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
import org.apache.poi.ss.util.CellRangeAddress;
|
import org.apache.poi.xssf.usermodel.*;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.net.URLEncoder;
|
import java.text.DateFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* @Description: 废水
|
* @author: besteam
|
* @date: 2022-09-04
|
* @version: V1.0
|
*/
|
@Service
|
public class MwWasteWaterServiceImpl extends ServiceImpl<MwWasteWaterMapper, MwWasteWater> implements IMwWasteWaterService {
|
|
|
|
@Autowired
|
private ISysDepartmentService sysDepartmentService;
|
|
@Autowired
|
private IMwEquipmentService mwEquipmentService;
|
|
@Autowired
|
private IMwDetectionIndexService detectionIndexService;
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
@Override
|
public WasteWaterReportVo getWasteWaterReportList(MwWasteWaterReportDto dto) {
|
WasteWaterReportVo vo = new WasteWaterReportVo();
|
List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
|
if (CollectionUtils.isEmpty(hospitalIdList)) {
|
return vo;
|
}
|
dto.setHospitalIdList(hospitalIdList);
|
List<MwDetectionIndex> wasteTypeList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().eq(MwDetectionIndex::getDelFlag,0));
|
if (CollectionUtils.isEmpty(wasteTypeList)) {
|
return vo;
|
}
|
vo.setLegend(wasteTypeList.stream().map(MwDetectionIndex::getName).collect(Collectors.toList()));
|
List<WasteWateItemVo> list = this.baseMapper.getTime(dto);
|
for(WasteWateItemVo wasteWateItemVo:list){
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setTime(wasteWateItemVo.getName());
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getList(dto);
|
if(dto.getNumType()==1){
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
}else if(dto.getNumType()==2){
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
}else{
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
}
|
|
}
|
}
|
vo.setList(list);
|
return vo;
|
}
|
|
@Override
|
public WasteWaterReportVo getWasteWaterList(MwWasteWaterReportDto dto) {
|
WasteWaterReportVo vo = new WasteWaterReportVo();
|
|
List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
|
if (CollectionUtils.isEmpty(hospitalIdList)) {
|
return vo;
|
}
|
dto.setHospitalIdList(hospitalIdList);
|
List<MwDetectionIndex> wasteTypeList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().eq(MwDetectionIndex::getDelFlag,0));
|
if (CollectionUtils.isEmpty(wasteTypeList)) {
|
return vo;
|
}
|
vo.setLegend(wasteTypeList.stream().map(MwDetectionIndex::getName).collect(Collectors.toList()));
|
|
List<WasteWateItemVo> list = this.baseMapper.getTime(dto);
|
for(WasteWateItemVo wasteWateItemVo:list){
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setTime(wasteWateItemVo.getName());
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getList(dto);
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
|
|
}
|
}
|
WasteWateItemVo wasteWateItemVo = new WasteWateItemVo();
|
wasteWateItemVo.setName("最小值");
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getMINList(dto);
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
|
|
}
|
list.add(wasteWateItemVo);
|
wasteWateItemVo = new WasteWateItemVo();
|
wasteWateItemVo.setName("最大值");
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getMAXList(dto);
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
|
|
}
|
list.add(wasteWateItemVo);
|
wasteWateItemVo = new WasteWateItemVo();
|
wasteWateItemVo.setName("平均值");
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getAVGList(dto);
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
|
|
}
|
list.add(wasteWateItemVo);
|
wasteWateItemVo = new WasteWateItemVo();
|
wasteWateItemVo.setName("累计值");
|
wasteWateItemVo.setData(new ArrayList<>());
|
for (MwDetectionIndex sysDictItem : wasteTypeList) {
|
dto.setCode(sysDictItem.getCode());
|
WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getSUMList(dto);
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
|
wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
|
|
}
|
list.add(wasteWateItemVo);
|
vo.setList(list);
|
return vo;
|
}
|
|
@Override
|
public IPage<WasteWaterVo> queryPageList(Integer pageNo, Integer pageSize, MwWasteWaterSeacherDto dto) {
|
|
Page<WasteWaterVo> page = new Page<>(pageNo, pageSize);
|
List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
|
if (CollectionUtils.isEmpty(hospitalIdList)) {
|
return page;
|
}
|
dto.setHospitalIdList(hospitalIdList);
|
page =this.baseMapper.queryPageList(page,dto);
|
List<WasteWaterVo> list = page.getRecords();
|
for(WasteWaterVo wasteWaterVo:list){
|
String[] ids = wasteWaterVo.getDetectionIndexId().toString().split(",");
|
List<Integer> idList = new ArrayList<>();
|
for(String id :ids){
|
idList.add(Integer.parseInt(id));
|
}
|
List<MwDetectionIndex> detectionIndexList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().in(MwDetectionIndex::getId,idList));
|
List<DetectionIndexVo> detectionIndexVoList = new ArrayList<>();
|
for(MwDetectionIndex mwDetectionIndex:detectionIndexList){
|
DetectionIndexVo detectionIndexVo = new DetectionIndexVo();
|
BeanUtils.copyProperties(mwDetectionIndex, detectionIndexVo);
|
List<MwWasteWater> wasteWaterList = this.baseMapper.selectList(new QueryWrapper<MwWasteWater>().lambda().eq(MwWasteWater::getEquipmentnumber,wasteWaterVo.getCode()).eq(MwWasteWater::getCode,detectionIndexVo.getCode()).orderByDesc(MwWasteWater::getCreateTime));
|
if(wasteWaterList!=null && wasteWaterList.size()>0){
|
MwWasteWater wasteWater = wasteWaterList.get(0);
|
if(Double.parseDouble(wasteWater.getAvgnum())>=Double.parseDouble(mwDetectionIndex.getMinNum()) && Double.parseDouble(wasteWater.getAvgnum())<=Double.parseDouble(mwDetectionIndex.getMaxNum())){
|
detectionIndexVo.setStatus(1);
|
}else{
|
detectionIndexVo.setStatus(2);
|
}
|
detectionIndexVo.setMwWasteWater(wasteWater);
|
}
|
detectionIndexVoList.add(detectionIndexVo);
|
}
|
wasteWaterVo.setDetectionIndexVoList(detectionIndexVoList);
|
}
|
page.setRecords(list);
|
return page;
|
}
|
|
private List<Integer> getSearchHospitalIdList(Integer hospitalId) {
|
|
if (hospitalId == null) {
|
return sysDepartmentService.listMyHospital(EnvUtils.getUser().getId()).stream().map(SysDepartment::getId).collect(Collectors.toList());
|
} else {
|
SysDepartment sysDepartment = sysDepartmentService.getById(hospitalId);
|
if (sysDepartment == null) {
|
return new ArrayList<>();
|
}
|
List<SysDepartment> childrenList = sysDepartmentService.list(new QueryWrapper<SysDepartment>().lambda().likeRight(SysDepartment::getTreeCode, sysDepartment.getTreeCode()).eq(SysDepartment::getOrgType, DepartmentEnum.OrgTypeEnum.HOSPITAL.getType()));
|
if (CollectionUtils.isEmpty(childrenList)) {
|
return new ArrayList<>();
|
}
|
return childrenList.stream().map(SysDepartment::getId).collect(Collectors.toList());
|
}
|
}
|
|
@Override
|
public void exportReport(HttpServletResponse response, MwWasteWaterReportDto dto) throws Exception {
|
String fileName = "";
|
ExcelWriter writer = null;
|
fileName = "汇总报表";
|
writer = new ExcelWriter(exportDepartmentReport(dto, fileName), fileName);
|
if (writer != null) {
|
response.setContentType("application/octet-stream;charset=utf-8");
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
|
writer.flush(response.getOutputStream(), true);
|
writer.close();
|
}
|
}
|
|
|
private XSSFWorkbook exportDepartmentReport(MwWasteWaterReportDto dto, String sheetName) throws Exception {
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
WasteWaterReportVo vo = null;
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
String cellName = "", title = "";
|
vo = getWasteWaterList(dto);
|
SysDepartment hospital = sysDepartmentService.getById(dto.getHospitalId());
|
if (hospital == null) {
|
throw new BusinessException(ErrorCode.ERR_500, "医院不存在");
|
}
|
cellName = "时间";
|
title = hospital.getDepartName() + "废水汇总报表";
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
XSSFCellStyle cellStyle = createDefaultCellStyle(workbook);
|
XSSFCellStyle rightCellStyle = createDefaultRightCellStyle(workbook);
|
//
|
int maxCellSize = 1 + vo.getLegend().size() * 4;
|
XSSFRow titleRow = sheet.createRow(0);
|
XSSFRow dateRow = sheet.createRow(1);
|
XSSFRow firstRow = sheet.createRow(2);
|
XSSFRow secondRow = sheet.createRow(3);
|
int legendIndex = 0;
|
for (int i = 0; i < maxCellSize; i++) {
|
XSSFCell titleRowCell = titleRow.createCell(i);
|
titleRowCell.setCellStyle(cellStyle);
|
XSSFCell firstRowCell = firstRow.createCell(i);
|
firstRowCell.setCellStyle(cellStyle);
|
XSSFCell secondRowCell = secondRow.createCell(i);
|
secondRowCell.setCellStyle(cellStyle);
|
XSSFCell dateRowCell = dateRow.createCell(i);
|
dateRowCell.setCellStyle(rightCellStyle);
|
if (i == 0) {
|
titleRowCell.setCellValue(title);
|
firstRowCell.setCellValue(cellName);
|
} else {
|
if (i % 4 == 1) {
|
firstRowCell.setCellValue(vo.getLegend().get(legendIndex));
|
legendIndex++;
|
secondRowCell.setCellValue("最小值(mg/l)");
|
} else if (i % 4 == 2) {
|
secondRowCell.setCellValue("平均值(mg/l)");
|
} else if (i % 4 == 3) {
|
secondRowCell.setCellValue("最大值(mg/l)");
|
} else {
|
secondRowCell.setCellValue("累计值(mg/l)");
|
}
|
}
|
if (i == maxCellSize - 1) {
|
dateRowCell.setCellValue("时间:" + DateUtil.format(df.parse(dto.getStartTime()), "yyyy年MM月dd日") + "至" + DateUtil.format(df.parse(dto.getEndTime()), "yyyy年MM月dd日"));
|
}
|
}
|
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxCellSize - 1));
|
sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
|
for (int i = 0; i < vo.getLegend().size(); i++) {
|
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1 + i * 2, 2 + i * 2));
|
}
|
//
|
if (CollectionUtils.isNotEmpty(vo.getList())) {
|
for (int i = 0; i < vo.getList().size(); i++) {
|
WasteWateItemVo itemVO = vo.getList().get(i);
|
XSSFRow dataRow = sheet.createRow(i + 4);
|
int dataIndex = 0;
|
for (int k = 0; k < maxCellSize; k++) {
|
XSSFCell dataRowCell = dataRow.createCell(k);
|
dataRowCell.setCellStyle(cellStyle);
|
if (k == 0) {
|
dataRowCell.setCellValue(itemVO.getName());
|
} else {
|
if(itemVO.getName().equals("最小值")){
|
if(k%4==0){
|
dataRowCell.setCellValue("-");
|
dataIndex++;
|
}else{
|
dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
|
dataIndex++;
|
}
|
}else if(itemVO.getName().equals("最大值")){
|
if(k%4==0){
|
dataRowCell.setCellValue("-");
|
dataIndex++;
|
}else{
|
dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
|
dataIndex++;
|
}
|
}else if(itemVO.getName().equals("平均值")){
|
if(k%4==0){
|
dataRowCell.setCellValue("-");
|
dataIndex++;
|
}else{
|
dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
|
dataIndex++;
|
}
|
}else if(itemVO.getName().equals("累计值")){
|
if(k%4==0){
|
dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
|
dataIndex++;
|
}else{
|
dataRowCell.setCellValue("-");
|
dataIndex++;
|
}
|
}else {
|
dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
|
dataIndex++;
|
}
|
|
}
|
}
|
}
|
}
|
sheet.setColumnWidth(0, 6065);
|
|
return workbook;
|
}
|
|
private XSSFCellStyle createDefaultCellStyle(XSSFWorkbook workbook) {
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
XSSFFont font = workbook.createFont();
|
font.setFontName("宋体");
|
font.setFontHeightInPoints((short) 12);
|
cellStyle.setFont(font);
|
return cellStyle;
|
}
|
|
private XSSFCellStyle createDefaultLeftCellStyle(XSSFWorkbook workbook) {
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
cellStyle.setAlignment(HorizontalAlignment.LEFT);
|
XSSFFont font = workbook.createFont();
|
font.setFontName("宋体");
|
font.setFontHeightInPoints((short) 12);
|
cellStyle.setFont(font);
|
return cellStyle;
|
}
|
|
private XSSFCellStyle createDefaultRightCellStyle(XSSFWorkbook workbook) {
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
cellStyle.setAlignment(HorizontalAlignment.RIGHT);
|
XSSFFont font = workbook.createFont();
|
font.setFontName("宋体");
|
font.setFontHeightInPoints((short) 12);
|
cellStyle.setFont(font);
|
return cellStyle;
|
}
|
|
}
|