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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : FIRP03083
 * @Description : 诊断名称不标准
 * @Author : WANGSY
 * @Date: 2020-11-18 17:45
 */
@Component
public class FIRP03083 extends QCCatalogue {
    private static String[] strings = {"2型糖尿病"};
    @Autowired
    private SpecialStorageUtil specialStorageUtil;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() == null) {
            return;
        }
        List<String> diags_out = new ArrayList<>();
        Map<String, String> hostpital_standDiag = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP);
        if (hostpital_standDiag == null) {
            status.set("0");
            return;
        }
        if (inputInfo.getFirstPageRecordDoc().getStructureMap() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            JSONArray out_diag = (JSONArray) firstpageStructureMap.get("出院诊断");
            if (out_diag != null && out_diag.size() > 0) {
                for (int i = 0; i < out_diag.size(); i++) {
                    List<String> disList = Arrays.asList(strings);
                    JSONObject jsonObject = out_diag.getJSONObject(i);
                    String diag_name = (String) jsonObject.get("诊断名称");
                    if ("ACT".equals(diag_name)) {
                        continue;
                    }
                    String hospitalDiagName = diag_name.trim().replace("待查", "").replace("?", "").replace("？", "");
                    if (hospitalDiagName.contains("伴")) {
                        hospitalDiagName = hospitalDiagName.substring(0, hospitalDiagName.indexOf("伴"));
                    }
                    if (hospitalDiagName.length() > 15) {
                        continue;
                    }
                    if (!disList.contains(hospitalDiagName)) {
                        hospitalDiagName = hospitalDiagName.replaceAll("^[0-9]", "");
                    }
                    if (!hostpital_standDiag.containsKey(hospitalDiagName)) {
                        diags_out.add(hospitalDiagName);
                    }
                }

                if (diags_out.size() > 0) {
                    info.set(diags_out.toString().replace("[", "").replace("]", ""));
                } else {
                    status.set("0");
                }
            } else {
                status.set("0");
            }
        }
    }
}