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

import com.alibaba.fastjson.JSONArray;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.client.ChiefPresentSimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
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.FirstPageRecordDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : FIRP0192
 * @Description : 主要手术及操作名称与病程记录不一致
 * @Author : 胡敬
 * @Date: 2020-03-16 20:28
 */
@Component
public class FIRP0192 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        FirstPageRecordDoc firstPageRecordDoc = inputInfo.getFirstPageRecordDoc();
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (firstPageRecordDoc == null || operationDocs.size() == 0) {
            return;
        }
        Map<String, Object> firstPageRecordDocStructureMap = firstPageRecordDoc.getStructureExtMap();
        List<Map<String, String>> mapList = (List<Map<String, String>>) firstPageRecordDocStructureMap.get(Content.operative_information);
        if (mapList != null && mapList.size() > 0) {
            Map<String, String> op = mapList.get(0);
            String oName = op.get(Content.operative_name);
            ModelAI modelAI = new ModelAI();
            String operationName = "";
            for (OperationDoc operationDoc : operationDocs) {
                if (operationDoc.getOperationRecordDoc() != null) {
                    operationName = operationDoc.getOperationRecordDoc().getStructureMap().get("手术名称");
                }
                if (StringUtil.isBlank(operationName) && operationDoc.getOperationDiscussionDoc() != null) {
                    operationName = operationDoc.getOperationDiscussionDoc().getStructureMap().get("手术名称");
                }
                if (StringUtil.isNotBlank(operationName)) {
                    operationName = operationName.replaceAll("[\\p{Punct}\\pP]", "").replaceAll("\\s*","");
                    oName = oName.replaceAll("[\\p{Punct}\\pP]", "").replaceAll("\\s*","");
                    if(operationName.contains(oName)){
                        return;
                    }
                    String[] operationNames = operationName.split("\\+");
                    JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(oName, Arrays.asList(operationNames), false
                            , "operation", chiefPresentSimilarityServiceClient);
                    if (jsonArray.size() == 2) {
                        /* 相似度最高手术 */
                        String operation = jsonArray.getString(0);
                        /* 相似度分数 */
                        double likeRate = jsonArray.getDoubleValue(1);
                        if (likeRate < 0.85) {
                            status.set("-1");
                            return;
                        }else {
                            return;
                        }
                    }
                }
            }
        }
    }
}