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

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 病案首页手术名称未填
 * 如果有手术记录，病案首页手术为空就报
 * @author wangfeng
 * @Description:
 * @date 2020-06-28 17:10
 */
@Component
public class FIRP02993 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getFirstPageRecordDoc().getStructureMap() != null) {
            //取出病程信息里的手术记录的名称
            List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
            long count = operationDocs.stream().map(i -> i.getOperationRecordDoc()).filter(v -> v != null && StringUtils.isNotBlank(v.getOperationName())).count();
            //取出病案首页里的手术名称operationNameList；
            Map<String, Object> structureExtMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            Object operation = structureExtMap.get(Content.operative_information);//手术信息
            List<String> operationNameList = new ArrayList<String>();
            List<Map<String, String>> mapStrs = (List<Map<String, String>>) operation;
            // 解决空指针，如果无数据，报错
            if (mapStrs == null && count == 0) {
                return ;
            }
            if (mapStrs.size() > 0) {
                for (Map<String, String> mapStr : mapStrs
                ) {
                    String name = mapStr.get(Content.operative_name);//手术名称
                    operationNameList.add(name);
                }
            }
            if(count > 0 && operationNameList.size() == 0){
                status.set("-1");
                return;
            }

            /*if (!operationDocs.isEmpty()) {
                List<String> nameberList = new ArrayList<String>();
                for (OperationDoc operationDoc : operationDocs
                ) {
                    OperationRecordDoc operationRecordDoc = operationDoc.getOperationRecordDoc();//手术名称
                    if(operationRecordDoc!=null){
                        String s = RegularUtil.ClearBracket(operationRecordDoc.getStructureMap().get(Content.operative_name));
                        String[] digitalSplit = s.split("[\\+|,|、|，]");
                        for (int i = 0; i < digitalSplit.length; i++) {
                            if (!digitalSplit[i].equals("")) {
                                nameberList.add(digitalSplit[i]);
                            }
                        }
                    }

                }
                if(nameberList.size()==0){
                    status.set("0");
                    return;
                }
                //取出病案首页里的手术名称operationNameList；
                Map<String, Object> structureExtMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
                Object operation = structureExtMap.get(Content.operative_information);//手术信息
                List<String> operationNameList = new ArrayList<String>();
                List<Map<String, String>> mapStrs = (List<Map<String, String>>) operation;
                // 解决空指针，如果无数据，报错
                if (mapStrs == null) {
                    status.set("-1");
                    return ;
                }
                if (mapStrs.size() > 0) {
                    for (Map<String, String> mapStr : mapStrs
                    ) {
                        String name = mapStr.get(Content.operative_name);//手术名称
                        operationNameList.add(name);
                    }
                    //病程信息里的手术记录的名称 和 病案首页里的手术名称 对比， 找不到则报错！
                    for (String str1 : nameberList) {
                        int sun = 0;
                        for (String str2 : operationNameList) {
                            if (str2.indexOf(str1) != -1) {
                                sun++;
                            }
                        }
                        if (sun == 0) {
                            status.set("-1");
                            return;
                        }
                    }
                } else {
                    status.set("-1");
                    return;
                }

            }*/

        }
    }


}
