package com.lantone.qc.kernel.catalogue.yiwu.operationdiscussion;

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.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
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.List;
import java.util.Map;

/**
 * @ClassName : OPE03085
 * @Description : 胎儿性别不一致
 * @Author : 胡敬
 * @Date: 2020-12-02 11:32
 */
@Component
public class OPE03085 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs.size() == 0) {
            return;
        }
        String operSex = "", dicussSex = "", leaveSex = "";
        for (OperationDoc operationDoc : operationDocs) {
            OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();
            OperationDiscussionDoc operationDiscussionDoc = operationDoc.getOperationDiscussionDoc();
            if (operationRecordDoc == null) {
                continue;
            }
            Map<String, String> operationRecordStructureMap = operationRecordDoc.getStructureMap();
            String content = operationRecordStructureMap.get("手术经过及处理");
            if (StringUtil.isBlank(content)) {
                continue;
            }
            content = content.replace("：", ":");
            if (content.contains("胎儿:性别女")) {
                operSex = "女";
            } else if (content.contains("胎儿:性别男")) {
                operSex = "男";
            }
            if (operationDiscussionDoc == null) {
                continue;
            }
            String discussContent = operationDiscussionDoc.getStructureMap().get("手术简要经过");
            if (StringUtil.isBlank(discussContent) && !discussContent.contains("婴")) {
                continue;
            }
            dicussSex = getSex(discussContent);
        }
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc != null) {
            String leaveContent = leaveHospitalDoc.getStructureMap().get("诊治经过");
            leaveSex = getSex(leaveContent);
        }

        if (StringUtil.isBlank(operSex)) {
            return;
        }
        if ((StringUtil.isNotBlank(dicussSex) && !operSex.equals(dicussSex)) || (StringUtil.isNotBlank(leaveSex) && !operSex.equals(leaveSex))) {
            status.set("-1");
        }
    }

    private String getSex(String discussContent) {
        String dicussSex = "";
        int index = discussContent.indexOf("婴");
        int startIndex = Math.max(0, index - 5);
        if (index > 0) {
            String sexContent = discussContent.substring(startIndex, index);
            if (sexContent.contains("男")) {
                dicussSex = "男";
            } else if (sexContent.contains("女")) {
                dicussSex = "女";
            }
        }
        return dicussSex;
    }
}