package com.diagbot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.CheckInfoDTO;
import com.diagbot.entity.MedPacsInfo;
import com.diagbot.mapper.MedPacsInfoMapper;
import com.diagbot.service.MedPacsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diagbot.vo.CheckInfoVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cy
 * @time: 2024-09-04
 */
@Service
public class MedPacsInfoServiceImpl extends ServiceImpl<MedPacsInfoMapper, MedPacsInfo> implements MedPacsInfoService {


    public IPage<CheckInfoDTO> getCheckInfo(CheckInfoVO checkInfoVO) {

        List<String> infos = this.baseMapper.getInfo(checkInfoVO.getHospitalId(), checkInfoVO.getBehospitalCode());

        Set<String> infoSet = new HashSet<>();
        for (String s : infos) {
            if (s.contains("检查：")) {
                String[] split = s.split("_");
                for (String info : split) {
                    String[] targetInfos = info.split("：");
                    for (String targetInfo : targetInfos) {
                        if (!"检查".equals(targetInfo)) {
                            infoSet.add(targetInfo);
                        }

                    }

                }
            }
        }

        //获取病人进出院时间
        Map<String, Object> timeMap = this.baseMapper.getTime(checkInfoVO.getBehospitalCode());
        if (null != timeMap) {
            if(!ObjectUtils.isEmpty( timeMap.get("behospital_date"))){
                Date behospital_date = (Date) timeMap.get("behospital_date");
                checkInfoVO.setBehospitalDate(behospital_date);
            }
            if(!ObjectUtils.isEmpty(timeMap.get("leave_hospital_date"))){
                Date leaveHospitalDate = (Date) timeMap.get("leave_hospital_date");
                checkInfoVO.setLeaveHospitalDate(leaveHospitalDate);
            }
        }
        String behospitalCode = checkInfoVO.getBehospitalCode();
        if(2l == checkInfoVO.getHospitalId()){
            checkInfoVO.setBehospitalCode(behospitalCode.split("_")[0]);
        }

        IPage<CheckInfoDTO> page = this.baseMapper.getCheckInfo(checkInfoVO, infoSet);
        return page;
    }
}
