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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : THR02911
 * @Description : 非一助代主刀医师查房
 * @Author : 胡敬
 * @Date: 2020-06-12 14:39
 */
@Component
public class THR02911 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getThreeLevelWardDocs().size() == 0 || inputInfo.getOperationDocs().size() == 0) {
            return;
        }
        ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDoc.getAllDoctorWradDocs();
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (allDoctorWradDocs.size() == 0 || operationDocs.size() == 0) {
            return;
        }

        for (ThreeLevelWardDoc wardDoc : allDoctorWradDocs) {
            Map<String, String> wardMap = wardDoc.getStructureMap();
            String title = wardMap.get("查房标题");
            String wardDateStr = wardMap.get("查房日期");
            if (StringUtil.isBlank(title) || StringUtil.isBlank(wardDateStr)) {
                continue;
            }
            Date wardDate = StringUtil.parseDateTime(wardDateStr);
            if (wardDate == null) {
                continue;
            }
            if (title.contains("一助") && title.contains("代") && title.contains("主刀")) {
                String firstAssistant = getFirstAssistant(operationDocs, wardDate);
                if (StringUtil.isNotBlank(firstAssistant) && !title.contains(firstAssistant)) {
                    status.set("-1");
                    return;
                }
            }
        }
    }

    /**
     * 获取主刀医师/一助职称(取距离查房记录时间最近的一次手术记录)
     *
     * @param operationDocs
     * @return
     */
    public static String getFirstAssistant(List<OperationDoc> operationDocs, Date recordDate) {
        String name = "";
        /* 用于存放手术结束时间距离查房时间最近的一次时间差 */
        long timeDiff = 0L;
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> structureMap = operationRecordDoc.getStructureMap();
            String firstAssistant = structureMap.get("一助");
            String operatorEndDateStr = structureMap.get("手术结束时间");
            if (StringUtil.isBlank(firstAssistant) || StringUtil.isBlank(operatorEndDateStr)) {
                continue;
            }
            Date operatorEndDate = StringUtil.parseDateTime(operatorEndDateStr);
            if (operatorEndDate != null) {
                long timeDifference = timeDifference(operatorEndDate, recordDate);
                if (timeDifference == 0){
                    continue;
                }
                if (timeDiff == 0 || timeDiff > timeDifference) {
                    /* 取距离此次主刀医师查房最近的一次手术时间 */
                    timeDiff = timeDifference;
                    name = firstAssistant;
                }
            }
        }
        return name;
    }

    public static long timeDifference(Date firstDate, Date secondDate) {
        long timeDifference = 0L;
        if (firstDate == null || secondDate == null) {
            return timeDifference;
        }
        boolean flag = false;
        if (firstDate.before(secondDate)) {
            flag = true;
        }
        Calendar calendarS = Calendar.getInstance();
        Calendar calendarE = Calendar.getInstance();
        long timeS, timeE;
        try {
            calendarS.setTime(firstDate);
            calendarE.setTime(secondDate);

            timeS = calendarS.getTimeInMillis();
            timeE = calendarE.getTimeInMillis();

            if (flag) {
                timeDifference = (timeE - timeS) / (1000 * 60);
            } else {
                timeDifference = (timeS - timeE) / (1000 * 60);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDifference;
    }
}