|
@@ -0,0 +1,235 @@
|
|
|
|
|
+package cn.iocoder.yudao.module.system.service.doctororder;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
|
+import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.doctororder.vo.LongDoctorOrderPageReqVO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.doctororder.vo.LongDoctorOrderRespVO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.doctororder.vo.LongDoctorOrderSaveReqVO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.biz.LongDoctorOrderDO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.biz.LongDoctorOrderEntryDO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.biz.LongDoctorOrderEntryMapper;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.biz.LongDoctorOrderMapper;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.enums.biz.LongOrderEntryStatusEnum;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.service.biz.BuildService;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.Period;
|
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 长期医嘱 Service 实现
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author 系统
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Validated
|
|
|
|
|
+public class LongDoctorOrderServiceImpl implements LongDoctorOrderService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private LongDoctorOrderMapper longDoctorOrderMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private LongDoctorOrderEntryMapper longDoctorOrderEntryMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ElderlyInfoMapper elderlyInfoMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private BuildService buildService;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public PageResult<LongDoctorOrderRespVO> getLongDoctorOrderPage(LongDoctorOrderPageReqVO reqVO) {
|
|
|
|
|
+ PageResult<LongDoctorOrderDO> pageResult = longDoctorOrderMapper.selectPage(reqVO);
|
|
|
|
|
+ List<LongDoctorOrderRespVO> list = pageResult.getList().stream()
|
|
|
|
|
+ .map(this::convertToResp)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ return new PageResult<>(list, pageResult.getTotal());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public LongDoctorOrderRespVO getLongDoctorOrder(Long id) {
|
|
|
|
|
+ LongDoctorOrderDO orderDO = longDoctorOrderMapper.selectById(id);
|
|
|
|
|
+ if (orderDO == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return convertToResp(orderDO);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public Long createLongDoctorOrder(LongDoctorOrderSaveReqVO reqVO) {
|
|
|
|
|
+ ElderlyInfoDO elder = elderlyInfoMapper.selectById(reqVO.getElderId());
|
|
|
|
|
+ if (elder == null) {
|
|
|
|
|
+ throw new ServiceException(ErrorCodeConstants.ELDERLY_DOCTOR_ORDER_ELDER_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ LongDoctorOrderDO orderDO = new LongDoctorOrderDO();
|
|
|
|
|
+ fillOrderBase(orderDO, elder);
|
|
|
|
|
+ longDoctorOrderMapper.insert(orderDO);
|
|
|
|
|
+
|
|
|
|
|
+ if (CollUtil.isNotEmpty(reqVO.getEntries())) {
|
|
|
|
|
+ for (LongDoctorOrderSaveReqVO.Entry entry : reqVO.getEntries()) {
|
|
|
|
|
+ LongDoctorOrderEntryDO entryDO = new LongDoctorOrderEntryDO();
|
|
|
|
|
+ copyEntry(entryDO, entry);
|
|
|
|
|
+ if (entryDO.getStatus() == null) {
|
|
|
|
|
+ entryDO.setStatus(LongOrderEntryStatusEnum.NOT_STARTED.getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ entryDO.setLongDoctorOrderId(orderDO.getId());
|
|
|
|
|
+ longDoctorOrderEntryMapper.insert(entryDO);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return orderDO.getId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void updateLongDoctorOrder(LongDoctorOrderSaveReqVO reqVO) {
|
|
|
|
|
+ LongDoctorOrderDO orderDO = longDoctorOrderMapper.selectById(reqVO.getId());
|
|
|
|
|
+ if (orderDO == null) {
|
|
|
|
|
+ throw new ServiceException(ErrorCodeConstants.ELDERLY_DOCTOR_ORDER_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ ElderlyInfoDO elder = elderlyInfoMapper.selectById(reqVO.getElderId());
|
|
|
|
|
+ if (elder == null) {
|
|
|
|
|
+ throw new ServiceException(ErrorCodeConstants.ELDERLY_DOCTOR_ORDER_ELDER_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ fillOrderBase(orderDO, elder);
|
|
|
|
|
+ longDoctorOrderMapper.updateById(orderDO);
|
|
|
|
|
+
|
|
|
|
|
+ // 条目 新增/修改/删除
|
|
|
|
|
+ List<LongDoctorOrderEntryDO> existEntries = longDoctorOrderEntryMapper.selectListByOrderId(orderDO.getId());
|
|
|
|
|
+ Set<Long> existIds = existEntries.stream().map(LongDoctorOrderEntryDO::getId).collect(Collectors.toSet());
|
|
|
|
|
+ Set<Long> reqIds = new HashSet<>();
|
|
|
|
|
+ if (CollUtil.isNotEmpty(reqVO.getEntries())) {
|
|
|
|
|
+ for (LongDoctorOrderSaveReqVO.Entry entry : reqVO.getEntries()) {
|
|
|
|
|
+ if (entry.getId() != null) {
|
|
|
|
|
+ reqIds.add(entry.getId());
|
|
|
|
|
+ LongDoctorOrderEntryDO entryDO = longDoctorOrderEntryMapper.selectById(entry.getId());
|
|
|
|
|
+ if (entryDO == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ copyEntry(entryDO, entry);
|
|
|
|
|
+ longDoctorOrderEntryMapper.updateById(entryDO);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LongDoctorOrderEntryDO entryDO = new LongDoctorOrderEntryDO();
|
|
|
|
|
+ copyEntry(entryDO, entry);
|
|
|
|
|
+ if (entryDO.getStatus() == null) {
|
|
|
|
|
+ entryDO.setStatus(LongOrderEntryStatusEnum.NOT_STARTED.getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ entryDO.setLongDoctorOrderId(orderDO.getId());
|
|
|
|
|
+ longDoctorOrderEntryMapper.insert(entryDO);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 删除请求中不存在的旧条目
|
|
|
|
|
+ for (Long existId : existIds) {
|
|
|
|
|
+ if (!reqIds.contains(existId)) {
|
|
|
|
|
+ longDoctorOrderEntryMapper.deleteById(existId);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void deleteLongDoctorOrder(Long id) {
|
|
|
|
|
+ if (longDoctorOrderMapper.selectById(id) == null) {
|
|
|
|
|
+ throw new ServiceException(ErrorCodeConstants.ELDERLY_DOCTOR_ORDER_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ longDoctorOrderEntryMapper.delete(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<LongDoctorOrderEntryDO>()
|
|
|
|
|
+ .eq(LongDoctorOrderEntryDO::getLongDoctorOrderId, id));
|
|
|
|
|
+ longDoctorOrderMapper.deleteById(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void updateLongDoctorOrderEntryStatus(Long id, Integer status) {
|
|
|
|
|
+ LongDoctorOrderEntryDO entryDO = longDoctorOrderEntryMapper.selectById(id);
|
|
|
|
|
+ if (entryDO == null) {
|
|
|
|
|
+ throw new ServiceException(ErrorCodeConstants.ELDERLY_DOCTOR_ORDER_ENTRY_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ entryDO.setStatus(status);
|
|
|
|
|
+ if (LongOrderEntryStatusEnum.STOPPED.getStatus().equals(status)) {
|
|
|
|
|
+ if (entryDO.getStopTime() == null) {
|
|
|
|
|
+ entryDO.setStopTime(LocalDateTime.now());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ longDoctorOrderEntryMapper.updateById(entryDO);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ======================== 私有方法 ========================
|
|
|
|
|
+
|
|
|
|
|
+ private LongDoctorOrderRespVO convertToResp(LongDoctorOrderDO orderDO) {
|
|
|
|
|
+ LongDoctorOrderRespVO respVO = new LongDoctorOrderRespVO();
|
|
|
|
|
+ org.springframework.beans.BeanUtils.copyProperties(orderDO, respVO);
|
|
|
|
|
+ List<LongDoctorOrderEntryDO> entries = longDoctorOrderEntryMapper.selectListByOrderId(orderDO.getId());
|
|
|
|
|
+ respVO.setEntryCount(entries.size());
|
|
|
|
|
+ LocalDateTime latestOrderTime = entries.stream()
|
|
|
|
|
+ .map(LongDoctorOrderEntryDO::getOrderTime)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .max(LocalDateTime::compareTo)
|
|
|
|
|
+ .orElse(null);
|
|
|
|
|
+ respVO.setLatestOrderTime(latestOrderTime);
|
|
|
|
|
+ respVO.setEntries(entries.stream().map(this::convertEntry).collect(Collectors.toList()));
|
|
|
|
|
+ fillElderAge(respVO, orderDO.getElderId());
|
|
|
|
|
+ return respVO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void fillElderAge(LongDoctorOrderRespVO respVO, Long elderId) {
|
|
|
|
|
+ if (elderId != null) {
|
|
|
|
|
+ ElderlyInfoDO elder = elderlyInfoMapper.selectById(elderId);
|
|
|
|
|
+ if (elder != null && elder.getBirthday() != null) {
|
|
|
|
|
+ respVO.setElderAge(calcAge(elder.getBirthday()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static int calcAge(Date birthday) {
|
|
|
|
|
+ LocalDate birth = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
|
|
+ return Period.between(birth, LocalDate.now()).getYears();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private LongDoctorOrderRespVO.Entry convertEntry(LongDoctorOrderEntryDO entryDO) {
|
|
|
|
|
+ LongDoctorOrderRespVO.Entry entry = new LongDoctorOrderRespVO.Entry();
|
|
|
|
|
+ org.springframework.beans.BeanUtils.copyProperties(entryDO, entry);
|
|
|
|
|
+ LongOrderEntryStatusEnum statusEnum = LongOrderEntryStatusEnum.getByStatus(entryDO.getStatus());
|
|
|
|
|
+ entry.setStatusText(statusEnum == null ? "" : statusEnum.getName());
|
|
|
|
|
+ return entry;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void copyEntry(LongDoctorOrderEntryDO entryDO, LongDoctorOrderSaveReqVO.Entry entry) {
|
|
|
|
|
+ entryDO.setOrderTime(entry.getOrderTime());
|
|
|
|
|
+ entryDO.setOrderContent(entry.getOrderContent());
|
|
|
|
|
+ entryDO.setFrequency(entry.getFrequency());
|
|
|
|
|
+ entryDO.setMedTime(entry.getMedTime());
|
|
|
|
|
+ entryDO.setRoute(entry.getRoute());
|
|
|
|
|
+ entryDO.setDoctorSign(entry.getDoctorSign());
|
|
|
|
|
+ entryDO.setNurseSign(entry.getNurseSign());
|
|
|
|
|
+ entryDO.setStopTime(entry.getStopTime());
|
|
|
|
|
+ entryDO.setStopDoctorSign(entry.getStopDoctorSign());
|
|
|
|
|
+ entryDO.setStopNurseSign(entry.getStopNurseSign());
|
|
|
|
|
+ entryDO.setRemark(entry.getRemark());
|
|
|
|
|
+ if (entry.getStatus() != null) {
|
|
|
|
|
+ entryDO.setStatus(entry.getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void fillOrderBase(LongDoctorOrderDO orderDO, ElderlyInfoDO elder) {
|
|
|
|
|
+ orderDO.setElderId(elder.getId());
|
|
|
|
|
+ orderDO.setElderName(elder.getElderName());
|
|
|
|
|
+ if (elder.getElderSex() != null) {
|
|
|
|
|
+ orderDO.setGender(elder.getElderSex());
|
|
|
|
|
+ orderDO.setGenderName(elder.getElderSex() == 1 ? "男" : elder.getElderSex() == 2 ? "女" : "");
|
|
|
|
|
+ }
|
|
|
|
|
+ orderDO.setBedName(buildService.getFullBedName(elder.getId()));
|
|
|
|
|
+ orderDO.setNurseLevelName(elder.getNurseLevelName());
|
|
|
|
|
+ orderDO.setTenantId(TenantContextHolder.getTenantId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|