package com.lantone.qc.kernel.catalogue.threelevelward;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : THR03016
 * @Description : 每3天无1次主治医师查房记录(Fellow查房记录每3天1次)
 * @Author : 胡敬
 * @Date: 2020-03-19 15:52
 */
@Component
public class THR03016 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getLeaveHospitalDoc() != null) {
            Map<String, String> leaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
            String admisTime = leaveHospitalStructureMap.get(Content.admisTime);
            String dischargeTime = leaveHospitalStructureMap.get(Content.dischargeTime);
            //如果住院天数小于3天则不判断该条规则
            if (!CatalogueUtil.isEmpty(admisTime) && !CatalogueUtil.isEmpty(dischargeTime)) {
                if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(dischargeTime), (long) (3 * 24 * 60))) {
                    return;
                }
            }
        }

        if (inputInfo.getBeHospitalizedDoc() != null && inputInfo.getThreeLevelWardDocs().size() > 0) {
            Map<String, String> beHospitalStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String admisTime = beHospitalStructureMap.get("入院日期");
            if (CatalogueUtil.isEmpty(admisTime)) {
                return;
            }
            //开始时间(入院时间)
            Date beginDate = StringUtil.parseDateTime(admisTime);
            if (beginDate == null) {
                return;
            }

            ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
            List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDoc.getAllDoctorWradDocs();
            if (allDoctorWradDocs.size() == 0) {
                status.set("0");
                return;
            }
            Date lastRecordDate = getLastRecordDate(allDoctorWradDocs);
            if (lastRecordDate == null) {
                return;
            }
            int hoursPerWeek = 3 * 24 * 60;
            String roundRecordThisWeek = "";
            String recordDoctorThisWeek = "";
            List<String> roundRecordEveryWeek = new ArrayList<>();
            List<String> recordDoctorEveryWeek = new ArrayList<>();
            beginDate = DateUtil.dateZeroClear(DateUtil.addDate(beginDate, 1));//从入院时间第二天0点开始算
            int i = 1;
            String lastWardDateRange = "";
            List<String> lastWardDateRangeList = new ArrayList<>();
            //每3天的病历记录
            while (i >= 1) {
                roundRecordThisWeek = extractWardRecord(allDoctorWradDocs, beginDate, hoursPerWeek, lastRecordDate);
                recordDoctorThisWeek = extractRecordDoctor(allDoctorWradDocs, beginDate, hoursPerWeek, lastRecordDate);
                if (CatalogueUtil.isEmpty(roundRecordThisWeek)) {
                    break;
                }
                lastWardDateRange = DateUtil.formatDate(beginDate) + "    ->    " + DateUtil.formatDate(DateUtil.addDate(beginDate, 2));
                lastWardDateRangeList.add(lastWardDateRange);
                roundRecordEveryWeek.add(roundRecordThisWeek);
                recordDoctorEveryWeek.add(recordDoctorThisWeek);
                beginDate = DateUtil.addDate(beginDate, 3);
                i++;
            }
            if (roundRecordEveryWeek.size() == 0) {
                status.set("0");
                return;
            }
            List<String> resultInfos = new ArrayList<>();
            for (int j = 0; j < recordDoctorEveryWeek.size(); j++) {
                int attendNum = CatalogueUtil.appearNumber(recordDoctorEveryWeek.get(j).split(","), "F");
                int attendNum2 = CatalogueUtil.appearNumber(roundRecordEveryWeek.get(j).split(","), Content.attend);
                if (attendNum == 0 && attendNum2 == 0)  {
                    //每3天无1次主治医师查房记录
                    status.set("-1");
                    resultInfos.add(lastWardDateRangeList.get(j));
                }
            }
            if (resultInfos.size() == 0) {
                return;
            }

            /*for (int j = 0; j < roundRecordEveryWeek.size(); j++) {
                int attendNum = CatalogueUtil.appearNumber(roundRecordEveryWeek.get(j).split(","), Content.attend);
                if (attendNum == 0) {
                    //每3天无1次主治医师查房记录
                    status.set("-1");
                    resultInfos.add(lastWardDateRangeList.get(j));
                }
            }*/
            if (resultInfos.size() > 0) {
                info.set(StringUtils.join(resultInfos.toArray(), "；"));
            }
        }
    }

    private Date getLastRecordDate(List<ThreeLevelWardDoc> allDoctorWradDocs) {
        ThreeLevelWardDoc threeLevelWardDoc = allDoctorWradDocs.get(allDoctorWradDocs.size() - 1);
        Map<String, String> lastWardDocStructureMap = threeLevelWardDoc.getStructureMap();
        String wardDateStr = lastWardDocStructureMap.get("查房日期");
        if (StringUtil.isNotBlank(wardDateStr)) {
            return StringUtil.parseDateTime(wardDateStr);
        }
        return null;
    }

    /**
     * 抽取duration分钟内所有查房标题
     * 抽取一周内所有查房标题，若一周内记录少于2天，则返回""
     *
     * @param threeLevelWardDocs
     * @param admisDate
     * @param duration
     * @return
     */
    private static String extractWardRecord(List<ThreeLevelWardDoc> threeLevelWardDocs, Date admisDate, int duration, Date maxRecordDate) {
        String recordTime = "", recordTitle = "", title = "";
        List<Date> dateList = new ArrayList();
        for (ThreeLevelWardDoc threeLevelWardDoc : threeLevelWardDocs) {
            Map<String, String> threeLevelWardStructureMap = threeLevelWardDoc.getStructureMap();
            recordTime = threeLevelWardStructureMap.get("查房日期");
            title = threeLevelWardStructureMap.get("查房标题");
            if (StringUtil.isBlank(recordTime) || StringUtil.isBlank(title)) {
                continue;
            }
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }
            if (admisDate.before(recordDate) && !CatalogueUtil.compareTime(admisDate, recordDate, Long.valueOf(duration))) {
                recordTitle += title + ",";
                dateList.add(recordDate);
            }
        }
        if (dateList.size() > 0) {
            if (!maxRecordDate.equals(dateList.get(dateList.size() - 1)) || CatalogueUtil.compareTime(admisDate, dateList.get(dateList.size() - 1), Long.valueOf(2 * 24 * 60))) {
                return recordTitle;
            }
        }
        return "";
    }

    /**
     * 抽取duration分钟内所有查房标题
     * 抽取一周内所有记录医师，若一周内记录少于2天，则返回""
     *
     * @param threeLevelWardDocs
     * @param admisDate
     * @param duration
     * @return
     */
    private static String extractRecordDoctor(List<ThreeLevelWardDoc> threeLevelWardDocs, Date admisDate, int duration, Date maxRecordDate) {
        String recordTime = "", recordTitle = "", title = "";
        List<Date> dateList = new ArrayList();
        for (ThreeLevelWardDoc threeLevelWardDoc : threeLevelWardDocs) {
            Map<String, String> threeLevelWardStructureMap = threeLevelWardDoc.getStructureMap();
            recordTime = threeLevelWardStructureMap.get("查房日期");
            title = threeLevelWardStructureMap.get("记录医师个人职称");
            if (StringUtil.isBlank(recordTime) || StringUtil.isBlank(title)) {
                continue;
            }
            Date recordDate = StringUtil.parseDateTime(recordTime);
            if (recordDate == null) {
                continue;
            }

            if (admisDate.before(recordDate) && !CatalogueUtil.compareTime(admisDate, recordDate, Long.valueOf(duration))) {
                recordTitle += title + ",";
                dateList.add(recordDate);
            }
        }
        if (dateList.size() > 0) {
            if (!maxRecordDate.equals(dateList.get(dateList.size() - 1)) || CatalogueUtil.compareTime(admisDate, dateList.get(dateList.size() - 1), Long.valueOf(2 * 24 * 60))) {
                return recordTitle;
            }
        }
        return "";
    }

}