package com.sinata.system.service.biz;
|
|
import cn.hutool.core.date.DateUtil;
|
import com.sinata.common.exception.ServiceException;
|
import com.sinata.common.utils.BeanUtils;
|
import com.sinata.common.utils.CollUtils;
|
import com.sinata.common.utils.DateUtils;
|
import com.sinata.system.domain.MedicalWasteStaticsVO;
|
import com.sinata.system.domain.MwCollectRecord;
|
import com.sinata.system.domain.SysDepartment;
|
import com.sinata.system.domain.vo.ScreenDepartmentVO;
|
import com.sinata.system.domain.vo.SysDictDataVO;
|
import com.sinata.system.domain.vo.TodayMedicalWastePieVO;
|
import com.sinata.system.domain.vo.TotalCollectWeightByTypeVO;
|
import com.sinata.system.enums.DepartmentEnum;
|
import com.sinata.system.service.ISysDictDataService;
|
import com.sinata.system.service.MwCollectRecordService;
|
import com.sinata.system.service.SysDepartmentService;
|
import lombok.RequiredArgsConstructor;
|
import org.jetbrains.annotations.NotNull;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author mitao
|
* @date 2025/1/1
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class ScreenService {
|
private final SysDepartmentService sysDepartmentService;
|
private final MwCollectRecordService mwCollectRecordService;
|
private final ISysDictDataService sysDictDataService;
|
|
/**
|
* 机构分布-获取机构列表
|
*
|
* @return
|
*/
|
public List<ScreenDepartmentVO> departmentList() {
|
SysDepartment department = getNanNingDepartment();
|
List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, department.getTreeCode()).in(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode()).list();
|
return BeanUtils.copyToList(list, ScreenDepartmentVO.class);
|
}
|
|
/**
|
* 预警数据统计
|
*
|
* @return
|
*/
|
public MedicalWasteStaticsVO medicalWasteStatics() {
|
SysDepartment department = getNanNingDepartment();
|
return mwCollectRecordService.queryMedicalWasteStatics(department.getTreeCode());
|
}
|
|
/**
|
* 查询南宁市
|
*
|
* @return
|
*/
|
@NotNull
|
private SysDepartment getNanNingDepartment() {
|
SysDepartment department = sysDepartmentService.lambdaQuery().eq(SysDepartment::getDepartmentName, "南宁市").one();
|
if (Objects.isNull(department)) {
|
throw new ServiceException("南宁市不存在!");
|
}
|
return department;
|
}
|
|
/**
|
* 今日医废类型统计占比
|
*
|
* @return
|
*/
|
public List<TodayMedicalWastePieVO> todayMedicalWastePie() {
|
SysDepartment nanNingDepartment = getNanNingDepartment();
|
//查询南宁市下面的医疗机构
|
List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode()).eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode()).list();
|
List<Long> departmentIdList = list.stream().map(SysDepartment::getId).collect(Collectors.toList());
|
List<TodayMedicalWastePieVO> todayMedicalWastePieVOList = new ArrayList<>();
|
if (CollUtils.isEmpty(departmentIdList)) {
|
return todayMedicalWastePieVOList;
|
}
|
//查询收集记录
|
List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery()
|
.in(MwCollectRecord::getDepartmentId, departmentIdList).list();
|
if (CollUtils.isEmpty(collectRecordList)) {
|
return todayMedicalWastePieVOList;
|
}
|
int total = collectRecordList.size();
|
// 分组并统计每个 wasteTypeStr 对应的记录数
|
Map<String, Long> resultMap = collectRecordList.stream()
|
.collect(Collectors.groupingBy(MwCollectRecord::getWasteTypeStr, Collectors.counting()));
|
|
todayMedicalWastePieVOList = resultMap.entrySet().stream().map(entry -> {
|
TodayMedicalWastePieVO todayMedicalWastePieVO = new TodayMedicalWastePieVO();
|
todayMedicalWastePieVO.setMedicalWasteStr(entry.getKey());
|
todayMedicalWastePieVO.setCount(entry.getValue().intValue());
|
todayMedicalWastePieVO.setProportion(BigDecimal.valueOf(entry.getValue()).divide(BigDecimal.valueOf(total), RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
|
return todayMedicalWastePieVO;
|
}).collect(Collectors.toList());
|
|
return todayMedicalWastePieVOList;
|
}
|
|
/**
|
* 各类型医废收集总量
|
*
|
* @param type
|
* @return
|
*/
|
public TotalCollectWeightByTypeVO totalCollectWeightByType(Integer type) {
|
SysDepartment nanNingDepartment = getNanNingDepartment();
|
TotalCollectWeightByTypeVO vo = new TotalCollectWeightByTypeVO();
|
Date startTime = DateUtils.getNowDate();
|
Date endTime = DateUtils.getNowDate();
|
if (type.equals(1)) {
|
//获取最本周的日期
|
vo.setDateList(DateUtils.getAllDatesOfCurrentWeek("MM-dd"));
|
startTime = DateUtils.getFirstDayOfCurrentWeekAtMidnight();
|
} else {
|
//最近一个月的日期
|
vo.setDateList(DateUtils.getAllDatesOfCurrentMonth("MM-dd"));
|
startTime = DateUtils.getFirstDayOfCurrentMonthAtMidnight();
|
}
|
//查询医废类型
|
List<SysDictDataVO> medicalWasteTypeList = sysDictDataService.medicalWasteTypeList();
|
|
//查询南宁市下面的医疗机构
|
List<SysDepartment> list = sysDepartmentService.lambdaQuery()
|
.likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode())
|
.eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode()).list();
|
List<Long> departmentIdList = list.stream().map(SysDepartment::getId).collect(Collectors.toList());
|
if (CollUtils.isEmpty(departmentIdList)) {
|
return vo;
|
}
|
//查询医废收集记录
|
List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery()
|
.in(MwCollectRecord::getDepartmentId, departmentIdList).between(MwCollectRecord::getCollectTime, startTime, endTime).list();
|
if (CollUtils.isEmpty(collectRecordList)) {
|
return vo;
|
}
|
//封装数据
|
for (SysDictDataVO sysDictDataVO : medicalWasteTypeList) {
|
List<BigDecimal> totalCollectWeightList = new ArrayList<>();
|
for (String date : vo.getDateList()) {
|
BigDecimal totalWeight = collectRecordList.stream().filter(record -> DateUtil.format(record.getCollectTime(), "MM-dd").equals(date)
|
&& record.getWasteType().equals(sysDictDataVO.getDictCode())).map(MwCollectRecord::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
|
totalCollectWeightList.add(totalWeight);
|
}
|
vo.getTotalCollectWeightList().add(totalCollectWeightList);
|
}
|
return vo;
|
}
|
}
|