package com.lantone.qc.kernel.util;

import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
import com.lantone.qc.nlp.participle.cfg.Configuration;
import com.lantone.qc.nlp.participle.cfg.DefaultConfig;
import com.lantone.qc.pub.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : CacheUtil
 * @Description : Redis存放
 * @Author : 楼辉荣
 * @Date: 2020-03-11 11:42
 */
@Slf4j
@Component
public class CacheUtil implements ApplicationRunner {
    private Configuration configuration = new DefaultConfig();
    @Autowired
    private SpecialStorageUtil specialStorageUtil;
    @Value("${qc.hospital_id}")
    private String hospitalIds;

    public void run(ApplicationArguments var1) {
        if (StringUtil.isBlank(hospitalIds)) {
            return;
        }
        for (String i : hospitalIds.split(",")) {
            if (StringUtil.isBlank(i)) {
                continue;
            }
            specialStorageUtil.getHospitalCache().put(i, Maps.newHashMap());
            this.putConceptDiagPropertyMap(i);//疾病相关信息放入缓存 包括疾病是否是慢病、传染病、常见症状、同义词
            this.putHospitalDiagMap(i);//医院疾病信息
            this.putHospitalDiagHuaZMap(i);//医院华卓映射
            this.putLantoneDiagHuaZMap(i);//朗通华卓映射
            this.putClinicBodyPartMap(i);//症状需要部位描述词典放入redis缓存
            this.putHospitalDoctorInfoMap(i);//症状需要部位描述词典放入redis缓存
        }
        log.info("缓存加载完成！");
    }

    /**
     * 疾病相关信息放入缓存
     * 包括疾病是否是慢病、传染病、常见症状、同义词
     *
     * @param hospitalId
     */
    public void putConceptDiagPropertyMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/concept_diag_properties.dict");

            Map<String, Map<String, Object>> diagMap = new HashMap<>();
            for (String line : lines) {
                String[] line_arr = line.split("\\|", -1);
                if (line_arr.length == 5) {
                    List<String> synonymsList = new ArrayList<>();
                    List<String> symptomsList = new ArrayList<>();
                    if (StringUtils.isNotEmpty(line_arr[3])) {
                        line_arr[3] = line_arr[3].trim();
                        synonymsList = java.util.Arrays.asList(line_arr[3].split(","));
                    }
                    if (StringUtils.isNotEmpty(line_arr[4])) {
                        line_arr[4] = line_arr[4].trim();
                        symptomsList = java.util.Arrays.asList(line_arr[4].split(","));
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("infectious", line_arr[2].trim());
                    map.put("chronic", line_arr[1].trim());
                    map.put("synonyms", synonymsList);
                    map.put("symptoms", symptomsList);
                    diagMap.put(line_arr[0], map);
                }
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.CONCEPT_DIAG_PROPERTY_MAP, diagMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 医院医生信息
     *
     * @param hospitalId
     */
    public void putHospitalDoctorInfoMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/hospital_doctor_info.dict");

            Map<String, Map<String, Object>> hospitalDoctorMap = new HashMap<>();
            for (String line : lines) {
                String[] line_arr = line.split("\\|", -1);
                if (line_arr.length == 4) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("doctorName", line_arr[0]);
                    map.put("deptName", line_arr[1]);
                    map.put("professor", line_arr[2]);
                    map.put("occup", line_arr[3]);
                    hospitalDoctorMap.put(line_arr[0], map);
                }
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.HOSPITAL_DOCTOR_MAP, hospitalDoctorMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 医院疾病信息放入redis缓存
     *
     * @param hospitalId
     */
    public void putHospitalDiagMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/hospital_diag_info.dict");
            Map<String, Map<String, String>> hospitalDiagMap = new HashMap<>();
            for (String line : lines) {
                Map<String, String> detail = new HashMap<>();
                detail.put("name", line.trim());
                detail.put("icd10", "A001");
                hospitalDiagMap.put(line, detail);
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.HOSPITAL_DIAG_MAP, hospitalDiagMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 医院和华卓映射疾病信息放入redis缓存
     *
     * @param hospitalId
     */
    public void putHospitalDiagHuaZMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/hospital_diag_info.dict");
            Map<String, Map<String, String>> hospitalDiagMap = new HashMap<>();
            for (String line : lines) {
                Map<String, String> detail = new HashMap<>();
                detail.put("name", line.trim());
                detail.put("icd10", "A001");
                hospitalDiagMap.put(line, detail);
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.HOSPITAL_DIAG_HUAZ_MAP, hospitalDiagMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 医院和华卓映射疾病信息放入redis缓存
     *
     * @param hospitalId
     */
    public void putLantoneDiagHuaZMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/hospital_diag_info.dict");
            Map<String, Map<String, String>> hospitalDiagMap = new HashMap<>();
            for (String line : lines) {
                Map<String, String> detail = new HashMap<>();
                detail.put("name", line.trim());
                detail.put("icd10", "A001");
                hospitalDiagMap.put(line, detail);
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.LANTONE_DIAG_HUAZ_MAP, hospitalDiagMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 症状需要部位描述词典放入redis缓存
     *
     * @param hospitalId
     */
    public void putClinicBodyPartMap(String hospitalId) {
        try {
            List<String> lines = configuration.readTargetFileContents("cache/" + hospitalId + "/concept_clinic_bodypart_properties.dict");
            List<String> clinicBodyPartList = new ArrayList<>();
            for (String line : lines) {
                clinicBodyPartList.add(line);
            }
            specialStorageUtil.setValByHospitalId(hospitalId, KernelConstants.CONCEPT_CLINIC_BODYPART_PROPERTIES_LIST, clinicBodyPartList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}