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 : THR03014
 * @Description : 每周无1次主任医师查房记录(Attending少于每周一次查房记录)
 * @Author : 胡敬
 * @Date: 2020-03-19 15:52
 */
@Component
public class THR03014 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);
            //如果住院天数小于7天则不判断该条规则
            if (!CatalogueUtil.isEmpty(admisTime) && !CatalogueUtil.isEmpty(dischargeTime)) {
                if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(dischargeTime), (long) (7 * 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 = 7 * 24 * 60;
            String roundRecordThisWeek = "";
            List<String> roundRecordEveryWeek = new ArrayList<>();
            beginDate = DateUtil.dateZeroClear(beginDate);//从入院记录当天0点开始算
            int i = 1;
            String lastWardDateRange = "";
            List<String> lastWardDateRangeList = new ArrayList<>();
            //每7天的病历记录
            while (i >= 1) {
                roundRecordThisWeek = extractWardRecord(inputInfo, allDoctorWradDocs, beginDate, hoursPerWeek, lastRecordDate);
                if (CatalogueUtil.isEmpty(roundRecordThisWeek)) {
                    break;
                }
                lastWardDateRange = DateUtil.formatDate(beginDate) + "    ->    " + DateUtil.formatDate(DateUtil.addDate(beginDate, 6));
                lastWardDateRangeList.add(lastWardDateRange);
                roundRecordEveryWeek.add(roundRecordThisWeek);
                beginDate = DateUtil.addDate(beginDate, 7);
                i++;
            }
            if (roundRecordEveryWeek.size() == 0) {
                status.set("0");
                return;
            }
            List<String> resultInfos = new ArrayList<>();
            for (int j = 0; j < roundRecordEveryWeek.size(); j++) {
                int attendNum = CatalogueUtil.appearNumber(roundRecordEveryWeek.get(j).split(","), Content.director);
                if (attendNum == 0) {
                    //每7天无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分钟内所有查房标题
     * 抽取一周内所有查房标题，若一周内记录少于6天，则返回""
     *
     * @param threeLevelWardDocs
     * @param admisDate
     * @param duration
     * @return
     */
    private static String extractWardRecord(InputInfo inputInfo, 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;
            }
            /* 替换查房标题中主刀/一助的职称 */
            /*List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
            if (title.contains("主刀")) {
                String doctorTitle = CatalogueUtil.getDoctorTitle(operationDocs, recordDate, "主刀医师");
                title = title.replace("主刀", doctorTitle);
            } else if (title.contains("一助")) {
                String doctorTitle = CatalogueUtil.getDoctorTitle(operationDocs, recordDate, "一助");
                title = title.replace("一助", doctorTitle);
            }*/
            if (admisDate.before(recordDate) && !CatalogueUtil.compareTime(admisDate, recordDate, Long.valueOf(duration))) {
                recordTitle += title + ",";
                dateList.add(recordDate);
            }
        }
        if (dateList.size() > 0) {
            //dateList.sort(Date::compareTo);
            if (!maxRecordDate.equals(dateList.get(dateList.size() - 1)) || CatalogueUtil.compareTime(admisDate, dateList.get(dateList.size() - 1), Long.valueOf(6 * 24 * 60))) {
                return recordTitle;
            }
        }
        return "";
    }

}