|
@@ -0,0 +1,278 @@
|
|
|
+package com.zhentao.prescription.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.zhentao.prescription.dto.PrescriptionDTO;
|
|
|
+import com.zhentao.prescription.dto.PrescriptionDetailDTO;
|
|
|
+import com.zhentao.prescription.mapper.PrescriptionDetailMapper;
|
|
|
+import com.zhentao.prescription.mapper.PrescriptionMapper;
|
|
|
+import com.zhentao.prescription.model.*;
|
|
|
+import com.zhentao.prescription.service.PrescriptionService;
|
|
|
+
|
|
|
+import org.modelmapper.ModelMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Transactional
|
|
|
+public class PrescriptionServiceImpl implements PrescriptionService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrescriptionMapper prescriptionMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrescriptionDetailMapper prescriptionDetailMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ModelMapper modelMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findAll() {
|
|
|
+ return prescriptionMapper.selectList(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<Prescription> findAll(Page<Prescription> page) {
|
|
|
+ return prescriptionMapper.selectPage(page, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Prescription findById(Long id) {
|
|
|
+ return prescriptionMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Prescription findByPrescriptionNo(String prescriptionNo) {
|
|
|
+ return prescriptionMapper.selectFullDetailByNo(prescriptionNo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findByPatientId(Long patientId) {
|
|
|
+ QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("patient_id", patientId);
|
|
|
+ return prescriptionMapper.selectList(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findByDoctorId(Long doctorId) {
|
|
|
+ QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("doctor_id", doctorId);
|
|
|
+ return prescriptionMapper.selectList(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findByStatus(String status) {
|
|
|
+ QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("status", status);
|
|
|
+ return prescriptionMapper.selectList(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findByVisitDate(LocalDate visitDate) {
|
|
|
+ QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("visit_date", visitDate);
|
|
|
+ return prescriptionMapper.selectList(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Prescription> findByPatientNameContaining(String name) {
|
|
|
+ return prescriptionMapper.selectByPatientName(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PrescriptionDTO getPrescriptionDetails(Long id) {
|
|
|
+ Prescription prescription = prescriptionMapper.selectById(id);
|
|
|
+ if (prescription == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ PrescriptionDTO dto = modelMapper.map(prescription, PrescriptionDTO.class);
|
|
|
+
|
|
|
+ // 设置处方明细
|
|
|
+ List<PrescriptionDetail> details = prescriptionDetailMapper.selectList(
|
|
|
+ new QueryWrapper<PrescriptionDetail>().eq("prescription_id", id));
|
|
|
+
|
|
|
+ List<PrescriptionDetailDTO> detailDTOs = details.stream()
|
|
|
+ .map(detail -> {
|
|
|
+ PrescriptionDetailDTO detailDTO = modelMapper.map(detail, PrescriptionDetailDTO.class);
|
|
|
+ // 实际项目中应该查询Medicine表获取药品名称和规格
|
|
|
+ detailDTO.setMedicineName("药品名称" + detail.getMedicineId());
|
|
|
+ detailDTO.setSpecification("规格" + detail.getMedicineId());
|
|
|
+ return detailDTO;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ dto.setDetails(detailDTOs);
|
|
|
+
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<PrescriptionDTO> searchPrescriptions(Page<PrescriptionDTO> page,
|
|
|
+ String patientName,
|
|
|
+ String prescriptionNo,
|
|
|
+ LocalDate startDate,
|
|
|
+ LocalDate endDate,
|
|
|
+ String status) {
|
|
|
+ // 先查询Prescription列表
|
|
|
+ Page<Prescription> prescriptionPage = new Page<>(page.getCurrent(), page.getSize());
|
|
|
+ prescriptionMapper.selectPageWithCondition(
|
|
|
+ prescriptionPage, patientName, prescriptionNo, startDate, endDate, status);
|
|
|
+
|
|
|
+ // 转换为DTO
|
|
|
+ List<PrescriptionDTO> dtoList = prescriptionPage.getRecords().stream()
|
|
|
+ .map(this::convertToDTO)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 构建结果
|
|
|
+ Page<PrescriptionDTO> resultPage = new Page<>(
|
|
|
+ prescriptionPage.getCurrent(),
|
|
|
+ prescriptionPage.getSize(),
|
|
|
+ prescriptionPage.getTotal());
|
|
|
+ resultPage.setRecords(dtoList);
|
|
|
+
|
|
|
+ return resultPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PrescriptionDTO convertToDTO(Prescription prescription) {
|
|
|
+ PrescriptionDTO dto = modelMapper.map(prescription, PrescriptionDTO.class);
|
|
|
+
|
|
|
+ // 设置处方明细
|
|
|
+ List<PrescriptionDetail> details = prescriptionDetailMapper.selectList(
|
|
|
+ new QueryWrapper<PrescriptionDetail>().eq("prescription_id", prescription.getPrescriptionId()));
|
|
|
+
|
|
|
+ List<PrescriptionDetailDTO> detailDTOs = details.stream()
|
|
|
+ .map(detail -> {
|
|
|
+ PrescriptionDetailDTO detailDTO = modelMapper.map(detail, PrescriptionDetailDTO.class);
|
|
|
+ // 实际项目中应该查询Medicine表获取药品名称和规格
|
|
|
+ detailDTO.setMedicineName("药品名称" + detail.getMedicineId());
|
|
|
+ detailDTO.setSpecification("规格" + detail.getMedicineId());
|
|
|
+ return detailDTO;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ dto.setDetails(detailDTOs);
|
|
|
+
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] exportToExcel(List<PrescriptionDTO> prescriptions) {
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ Sheet sheet = workbook.createSheet("处方信息");
|
|
|
+
|
|
|
+ // 创建标题行
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
+ String[] headers = {
|
|
|
+ "处方编号", "患者姓名", "性别", "年龄", "医生", "科室",
|
|
|
+ "就诊日期", "诊断结果", "总金额", "状态",
|
|
|
+ "药品名称", "规格", "数量", "用法", "剂量",
|
|
|
+ "频次", "疗程", "注意事项", "单价", "金额"
|
|
|
+ };
|
|
|
+
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ Cell cell = headerRow.createCell(i);
|
|
|
+ cell.setCellValue(headers[i]);
|
|
|
+
|
|
|
+ // 设置标题样式
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setBold(true);
|
|
|
+ style.setFont(font);
|
|
|
+ style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
|
|
|
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充数据
|
|
|
+ int rowNum = 1;
|
|
|
+ for (PrescriptionDTO prescription : prescriptions) {
|
|
|
+ // 为每个处方的每个明细创建一行
|
|
|
+ for (PrescriptionDetailDTO detail : prescription.getDetails()) {
|
|
|
+ Row row = sheet.createRow(rowNum++);
|
|
|
+
|
|
|
+ // 处方信息
|
|
|
+ row.createCell(0).setCellValue(prescription.getPrescriptionNo());
|
|
|
+ row.createCell(1).setCellValue(prescription.getPatientName());
|
|
|
+ row.createCell(2).setCellValue(prescription.getPatientGender());
|
|
|
+
|
|
|
+ if (prescription.getPatientAge() != null) {
|
|
|
+ row.createCell(3).setCellValue(prescription.getPatientAge());
|
|
|
+ }
|
|
|
+
|
|
|
+ row.createCell(4).setCellValue(prescription.getDoctorName());
|
|
|
+ row.createCell(5).setCellValue(prescription.getDepartment());
|
|
|
+ row.createCell(6).setCellValue(prescription.getVisitDate().toString());
|
|
|
+ row.createCell(7).setCellValue(prescription.getDiagnosis());
|
|
|
+
|
|
|
+ if (prescription.getTotalAmount() != null) {
|
|
|
+ row.createCell(8).setCellValue(prescription.getTotalAmount());
|
|
|
+ }
|
|
|
+
|
|
|
+ row.createCell(9).setCellValue(prescription.getStatus());
|
|
|
+
|
|
|
+ // 药品明细信息
|
|
|
+ row.createCell(10).setCellValue(detail.getMedicineName());
|
|
|
+ row.createCell(11).setCellValue(detail.getSpecification());
|
|
|
+ row.createCell(12).setCellValue(detail.getQuantity());
|
|
|
+ row.createCell(13).setCellValue(detail.getUsageMethod());
|
|
|
+ row.createCell(14).setCellValue(detail.getDosage());
|
|
|
+ row.createCell(15).setCellValue(detail.getFrequency());
|
|
|
+ row.createCell(16).setCellValue(detail.getDuration());
|
|
|
+ row.createCell(17).setCellValue(detail.getNotes());
|
|
|
+
|
|
|
+ if (detail.getPrice() != null) {
|
|
|
+ row.createCell(18).setCellValue(detail.getPrice());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (detail.getAmount() != null) {
|
|
|
+ row.createCell(19).setCellValue(detail.getAmount());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置单元格样式
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ row.getCell(i).setCellStyle(style);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自动调整列宽
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将工作簿写入字节数组
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ workbook.write(outputStream);
|
|
|
+ byte[] bytes = outputStream.toByteArray();
|
|
|
+ workbook.close();
|
|
|
+ return bytes;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("导出Excel失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 内部类用于处理字节数组输出流
|
|
|
+ private static class ByteArrayOutputStream extends java.io.ByteArrayOutputStream {
|
|
|
+ public byte[] toByteArray() {
|
|
|
+ return this.buf;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|