| | |
| | | package com.sinata.system.service.impl; |
| | | |
| | | import com.sinata.system.domain.MwMicroEquipmentRecord; |
| | | import com.sinata.system.mapper.MwMicroEquipmentRecordMapper; |
| | | import com.sinata.system.service.MwMicroEquipmentRecordService; |
| | | import cn.idev.excel.FastExcel; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.google.common.collect.Lists; |
| | | import com.sinata.common.core.domain.entity.SysDictData; |
| | | import com.sinata.common.entity.PageDTO; |
| | | import com.sinata.common.utils.BeanUtils; |
| | | import com.sinata.common.utils.CollUtils; |
| | | import com.sinata.common.utils.DateUtils; |
| | | import com.sinata.common.utils.SecurityUtils; |
| | | import com.sinata.common.utils.StringUtils; |
| | | import com.sinata.system.domain.MwCollectRecord; |
| | | import com.sinata.system.domain.MwMicroEquipment; |
| | | import com.sinata.system.domain.MwMicroEquipmentRecord; |
| | | import com.sinata.system.domain.MwMicroEquipmentRecordItem; |
| | | import com.sinata.system.domain.SysDepartment; |
| | | import com.sinata.system.domain.dto.MwMicroEquipmentRecordDTO; |
| | | import com.sinata.system.domain.query.MwMicroEquipmentRecordQuery; |
| | | import com.sinata.system.domain.query.MwMicroEquipmentStaticsQuery; |
| | | import com.sinata.system.domain.vo.DepartmentReportItemVO; |
| | | import com.sinata.system.domain.vo.DepartmentReportVO; |
| | | import com.sinata.system.domain.vo.MwCollectRecordVO; |
| | | import com.sinata.system.domain.vo.MwMicroEquipmentRecordVO; |
| | | import com.sinata.system.domain.vo.MwMicroEquipmentStaticsTitleVO; |
| | | import com.sinata.system.domain.vo.MwMicroEquipmentStaticsVO; |
| | | import com.sinata.system.domain.vo.SysDictDataVO; |
| | | import com.sinata.system.enums.MedicalWasteStatusEnum; |
| | | import com.sinata.system.mapper.MwMicroEquipmentRecordMapper; |
| | | import com.sinata.system.service.ISysDictDataService; |
| | | import com.sinata.system.service.MwCollectRecordService; |
| | | import com.sinata.system.service.MwMicroEquipmentRecordItemService; |
| | | import com.sinata.system.service.MwMicroEquipmentRecordService; |
| | | import com.sinata.system.service.MwMicroEquipmentService; |
| | | import com.sinata.system.service.MwStagingRoomService; |
| | | import com.sinata.system.service.SysDepartmentService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2024-12-02 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class MwMicroEquipmentRecordServiceImpl extends ServiceImpl<MwMicroEquipmentRecordMapper, MwMicroEquipmentRecord> implements MwMicroEquipmentRecordService { |
| | | private final SysDepartmentService sysDepartmentService; |
| | | private final MwCollectRecordService mwCollectRecordService; |
| | | private final MwMicroEquipmentRecordItemService mwMicroEquipmentRecordItemService; |
| | | private final ISysDictDataService sysDictDataService; |
| | | private final MwStagingRoomService mwStagingRoomService; |
| | | private final MwMicroEquipmentService mwMicroEquipmentService; |
| | | |
| | | /** |
| | | * 设备使用记录分页列表 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public PageDTO<MwMicroEquipmentRecordVO> pageList(MwMicroEquipmentRecordQuery query) { |
| | | String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId()); |
| | | if (StringUtils.isBlank(treeCode)) { |
| | | return PageDTO.empty(0L, 0L); |
| | | } |
| | | return PageDTO.of(baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode)); |
| | | } |
| | | |
| | | @Override |
| | | public MwMicroEquipmentRecordVO detail(Long id) { |
| | | MwMicroEquipmentRecordVO mwMicroEquipmentRecordVO = BeanUtils.copyBean(this.getById(id), MwMicroEquipmentRecordVO.class); |
| | | if (Objects.nonNull(mwMicroEquipmentRecordVO)) { |
| | | mwMicroEquipmentRecordVO.setMedicalWasteList(mwStagingRoomService.queryMedicalWasteList(mwMicroEquipmentRecordVO.getId())); |
| | | } |
| | | return mwMicroEquipmentRecordVO; |
| | | } |
| | | |
| | | /** |
| | | * 导出设备使用记录 |
| | | * |
| | | * @param query |
| | | * @param response |
| | | */ |
| | | @Override |
| | | public void export(MwMicroEquipmentRecordQuery query, HttpServletResponse response) throws IOException { |
| | | String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId()); |
| | | if (StringUtils.isBlank(treeCode)) { |
| | | return; |
| | | } |
| | | List<MwMicroEquipmentRecordVO> list = baseMapper.getList(query, treeCode); |
| | | // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("微波设备使用记录", "UTF-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | FastExcel.write(response.getOutputStream(), MwCollectRecordVO.class).sheet("微波设备使用记录").doWrite(list); |
| | | } |
| | | |
| | | /** |
| | | * 新增使用记录 |
| | | * |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void add(MwMicroEquipmentRecordDTO dto) { |
| | | List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery().in(MwCollectRecord::getBoxId, dto.getIds()) |
| | | .eq(MwCollectRecord::getStatus, MedicalWasteStatusEnum.TEMPORARILY_STORED.getCode()).list(); |
| | | if (CollUtils.isNotEmpty(collectRecordList)) { |
| | | MwMicroEquipmentRecord record = new MwMicroEquipmentRecord(); |
| | | record.setEquipmentId(dto.getEquipmentId()); |
| | | record.setOperatorId(SecurityUtils.getUserId()); |
| | | record.setRemark(dto.getRemark()); |
| | | record.setUseTime(new Date()); |
| | | record.setBoxNum(dto.getIds().size()); |
| | | record.setBagNum(collectRecordList.size()); |
| | | record.setTotalWeight(collectRecordList.stream().map(MwCollectRecord::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | save(record); |
| | | List<MwMicroEquipmentRecordItem> itemList = new ArrayList<>(); |
| | | collectRecordList.forEach(collectRecord -> { |
| | | //封装设备使用记录项 |
| | | MwMicroEquipmentRecordItem item = new MwMicroEquipmentRecordItem(); |
| | | item.setEquipmentRecordId(record.getId()); |
| | | item.setCollectRecordId(collectRecord.getId()); |
| | | itemList.add(item); |
| | | //修改状态 |
| | | collectRecord.setStatus(MedicalWasteStatusEnum.DISPOSED.getCode()); |
| | | }); |
| | | //更新状态为已处置 |
| | | mwCollectRecordService.updateBatchById(collectRecordList); |
| | | //批量新增设备使用记录项 |
| | | mwMicroEquipmentRecordItemService.saveBatch(itemList); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处置分析表头 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<MwMicroEquipmentStaticsTitleVO> staticsTitle() { |
| | | List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list(); |
| | | |
| | | List<MwMicroEquipmentStaticsTitleVO> titles = new ArrayList<>(); |
| | | medicalWasteTypeList.forEach(item -> { |
| | | MwMicroEquipmentStaticsTitleVO vo = new MwMicroEquipmentStaticsTitleVO(); |
| | | vo.setTitle(item.getDictLabel()); |
| | | MwMicroEquipmentStaticsTitleVO child1 = new MwMicroEquipmentStaticsTitleVO(); |
| | | child1.setTitle("总重量(KG)"); |
| | | MwMicroEquipmentStaticsTitleVO child2 = new MwMicroEquipmentStaticsTitleVO(); |
| | | child2.setTitle("总数量"); |
| | | vo.setChildren(Lists.newArrayList(child1, child2)); |
| | | titles.add(vo); |
| | | }); |
| | | |
| | | MwMicroEquipmentStaticsTitleVO total = new MwMicroEquipmentStaticsTitleVO(); |
| | | total.setTitle("小计"); |
| | | MwMicroEquipmentStaticsTitleVO child1 = new MwMicroEquipmentStaticsTitleVO(); |
| | | child1.setTitle("总重量(KG)"); |
| | | MwMicroEquipmentStaticsTitleVO child2 = new MwMicroEquipmentStaticsTitleVO(); |
| | | child2.setTitle("总数量"); |
| | | total.setChildren(Lists.newArrayList(child1, child2)); |
| | | titles.add(total); |
| | | return titles; |
| | | } |
| | | |
| | | /** |
| | | * 处置分析数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DepartmentReportVO getStaticsData(MwMicroEquipmentStaticsQuery query) { |
| | | DepartmentReportVO vo = new DepartmentReportVO(); |
| | | SysDepartment department = sysDepartmentService.getById(query.getDepartmentId()); |
| | | if (Objects.nonNull(department)) { |
| | | vo.setDepartmentName(department.getDepartmentName()); |
| | | } |
| | | MwMicroEquipment microEquipment = mwMicroEquipmentService.getById(query.getEquipmentId()); |
| | | vo.setEquipmentName(microEquipment.getEquipmentName()); |
| | | List<SysDictDataVO> wasteTypeList = sysDictDataService.medicalWasteTypeList(); |
| | | |
| | | if (CollUtils.isEmpty(wasteTypeList)) { |
| | | return vo; |
| | | } |
| | | vo.setLegend(wasteTypeList.stream().map(SysDictDataVO::getDictLabel).collect(Collectors.toList())); |
| | | vo.getLegend().add("小计"); |
| | | String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId()); |
| | | List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list(); |
| | | |
| | | List<MwMicroEquipmentStaticsVO> staticsData = baseMapper.getStaticsData(query, treeCode); |
| | | SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS); |
| | | switch (query.getDateType()) { |
| | | case 1: |
| | | sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD); |
| | | break; |
| | | case 2: |
| | | sdf = new SimpleDateFormat(DateUtils.YYYY_MM); |
| | | break; |
| | | case 3: |
| | | sdf = new SimpleDateFormat(DateUtils.YYYY); |
| | | break; |
| | | } |
| | | List<String> dateList = DateUtils.getDayBetween(query.getStartTime(), query.getEndTime(), query.getDateType()); |
| | | if (CollUtils.isNotEmpty(staticsData)) { |
| | | List<DepartmentReportItemVO> result = new ArrayList<>(); |
| | | for (String date : dateList) { |
| | | DepartmentReportItemVO departmentReportItemVO = new DepartmentReportItemVO(); |
| | | departmentReportItemVO.setName(date); |
| | | departmentReportItemVO.setData(new ArrayList<>()); |
| | | SimpleDateFormat finalSdf = sdf; |
| | | BigDecimal totalWeight = BigDecimal.ZERO; |
| | | BigDecimal totalCount = BigDecimal.ZERO; |
| | | for (SysDictData sysDictData : medicalWasteTypeList) { |
| | | BigDecimal weight = staticsData.stream().filter(e -> e.getWasteType().equals(sysDictData.getDictCode()) |
| | | && finalSdf.format(e.getUseTime()).equals(date) |
| | | ).map(MwMicroEquipmentStaticsVO::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_UP); |
| | | long count = staticsData.stream().filter(e -> e.getWasteType().equals(sysDictData.getDictCode()) |
| | | && finalSdf.format(e.getUseTime()).equals(date)).count(); |
| | | departmentReportItemVO.getData().add(weight); |
| | | departmentReportItemVO.getData().add(BigDecimal.valueOf(count)); |
| | | totalWeight = totalWeight.add(weight); |
| | | totalCount = totalCount.add(BigDecimal.valueOf(count)); |
| | | } |
| | | departmentReportItemVO.getData().add(totalWeight); |
| | | departmentReportItemVO.getData().add(totalCount); |
| | | result.add(departmentReportItemVO); |
| | | } |
| | | vo.setList(result); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | @Override |
| | | public void staticsExport(MwMicroEquipmentStaticsQuery query, HttpServletResponse response) throws IOException { |
| | | |
| | | List<List<String>> head = head(); |
| | | System.out.println(JSONObject.toJSONString(head)); |
| | | // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("处置分析报表", "UTF-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | // 这里需要设置不关闭流 |
| | | FastExcel.write(response.getOutputStream()) |
| | | .head(head) |
| | | .autoCloseStream(Boolean.TRUE) |
| | | .sheet("处置分析报表") |
| | | .doWrite(getStaticsReportData(query)); |
| | | } |
| | | |
| | | /** |
| | | * 导出报表数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | private List<List<Object>> getStaticsReportData(MwMicroEquipmentStaticsQuery query) { |
| | | DepartmentReportVO vo = getStaticsData(query); |
| | | if (CollUtils.isNotEmpty(vo.getList())) { |
| | | List<List<Object>> result = new ArrayList<>(); |
| | | for (DepartmentReportItemVO departmentReportItemVO : vo.getList()) { |
| | | List<Object> data = new ArrayList<>(); |
| | | data.add(departmentReportItemVO.getName()); |
| | | data.addAll(departmentReportItemVO.getData()); |
| | | result.add(data); |
| | | } |
| | | return result; |
| | | } |
| | | return CollUtils.emptyList(); |
| | | } |
| | | |
| | | /** |
| | | * 导出报表表头 |
| | | * |
| | | * @return |
| | | */ |
| | | private List<List<String>> head() { |
| | | List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list(); |
| | | List<List<String>> headTitles = Lists.newArrayList(); |
| | | headTitles.add(Lists.newArrayList("日期")); |
| | | medicalWasteTypeList.forEach(item -> { |
| | | headTitles.add(Lists.newArrayList(item.getDictLabel(), "总重量(KG)")); |
| | | headTitles.add(Lists.newArrayList(item.getDictLabel(), "总数量")); |
| | | }); |
| | | headTitles.add(Lists.newArrayList("小计", "总重量(KG)")); |
| | | headTitles.add(Lists.newArrayList("小计", "总数量")); |
| | | return headTitles; |
| | | } |
| | | |
| | | } |