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.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.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : THR02900
 * @Description : 术后24小时内无主刀或一助查房记录
 * @Author : 胡敬
 * @Date: 2020-05-27 14:23
 */
@Component
public class THR02900 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();

        String allTitle = "";
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> structureMap = operationRecordDoc.getStructureMap();
            String chiefSurgeon = structureMap.get("主刀医师");
            String firstAssistant = structureMap.get("一助");
            String opeEndTime = structureMap.get("手术结束时间");
            Date opeEndDate = CatalogueUtil.parseStringDate(opeEndTime);
            if (opeEndDate == null) {
                continue;
            }
            for (ThreeLevelWardDoc wardDoc : allDoctorWradDocs) {
                Map<String, String> wardStructureMap = wardDoc.getStructureMap();
                String recordDateStr = wardStructureMap.get("查房日期");
                String recordTitle = wardStructureMap.get("查房标题");
                Date recordDate = CatalogueUtil.parseStringDate(recordDateStr);
                if (StringUtil.isBlank(recordTitle) || recordDate == null) {
                    continue;
                }
                if (opeEndDate.before(recordDate) && !CatalogueUtil.compareTime(opeEndDate, recordDate, 24 * 60L)) {
                    allTitle += recordTitle;
                }
            }

            if (!allTitle.contains(chiefSurgeon) && !allTitle.contains(firstAssistant)) {
                status.set("-1");
                return;
            }
        }

    }
}