package com.diagbot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.ExamineInfoDTO;
import com.diagbot.dto.ExamineSonInfoDTO;
import com.diagbot.entity.MedLisInfo;
import com.diagbot.mapper.MedLisInfoMapper;
import com.diagbot.service.MedLisInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diagbot.vo.ExamineInfoVO;
import com.diagbot.vo.ExamineSonInfoVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoutg
 * @since 2020-07-28
 */

@Service
public class MedLisInfoServiceImpl extends ServiceImpl<MedLisInfoMapper, MedLisInfo> implements MedLisInfoService {


    public IPage<ExamineInfoDTO> getExamineInfo(ExamineInfoVO examineInfoVO) {

        List<String> infos = this.baseMapper.getInfo(examineInfoVO.getHospitalId(), examineInfoVO.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(examineInfoVO.getBehospitalCode());
        if (null != timeMap) {
            if(!ObjectUtils.isEmpty( timeMap.get("behospital_date"))){
                Date behospital_date = (Date) timeMap.get("behospital_date");
                examineInfoVO.setBehospitalDate(behospital_date);
            }
            if(!ObjectUtils.isEmpty(timeMap.get("leave_hospital_date"))){
                Date leaveHospitalDate = (Date) timeMap.get("leave_hospital_date");
                examineInfoVO.setLeaveHospitalDate(leaveHospitalDate);
            }
        }

        String behospitalCode = examineInfoVO.getBehospitalCode();
        if (2l == examineInfoVO.getHospitalId()) {
            examineInfoVO.setBehospitalCode(behospitalCode.split("_")[0]);
        }

        //获取缺陷检验项目
        List<String> badCheckInfo = this.baseMapper.getBadExamineInfo(examineInfoVO, infoSet);
        //获取缺陷判别类别检查项目
        IPage<ExamineInfoDTO> page = this.baseMapper.getExamineInfo(examineInfoVO, infoSet,badCheckInfo);
        return page;
    }

    public IPage<ExamineSonInfoDTO> getExamineSonInfo(ExamineSonInfoVO examineSonInfoVO) {
        List<String> infos = this.baseMapper.getInfo(examineSonInfoVO.getHospitalId(), examineSonInfoVO.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);
                        }
                    }
                }
            }
        }
        String behospitalCode = examineSonInfoVO.getBehospitalCode();
        if (2l == examineSonInfoVO.getHospitalId()) {
            examineSonInfoVO.setBehospitalCode(behospitalCode.split("_")[0]);
        }

        IPage<ExamineSonInfoDTO> page = this.baseMapper.getExamineSonInfo(examineSonInfoVO, infoSet);
        return page;
    }


}
