package com.lantone.qc.kernel.util;


import com.lantone.qc.nlp.participle.cfg.Configuration;
import com.lantone.qc.nlp.participle.cfg.DefaultConfig;
import com.lantone.qc.security.util.EncrypDES;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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
 */
@Component
public class CacheUtil implements ApplicationRunner {
    Logger logger = LoggerFactory.getLogger(DefaultConfig.class);
    @Autowired
    private RedisUtil redisUtil;

    public void run(ApplicationArguments var1) {
        try {
            this.putConceptDiagPropertyMap();//疾病相关信息放入缓存 包括疾病是否是慢病、传染病、常见症状、同义词
            this.putHospitalDiagMap();//医院疾病信息
            this.putHospitalDiagHuaZMap();//医院华卓映射
            this.putLantoneDiagHuaZMap();//朗通华卓映射
            this.putClinicBodyPartMap();//症状需要部位描述词典放入redis缓存
            this.putHospitalDoctorInfoMap();//症状需要部位描述词典放入redis缓存
        } catch (Exception e) {
            logger.error("缓存文件读取错误。。。");
        }
    }

    /**
     * 疾病相关信息放入缓存
     * 包括疾病是否是慢病、传染病、常见症状、同义词
     */
    public void putConceptDiagPropertyMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/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])) {
                    synonymsList = java.util.Arrays.asList(line_arr[3].split(","));
                }
                if (StringUtils.isNotEmpty(line_arr[4])) {
                    symptomsList = java.util.Arrays.asList(line_arr[4].split(","));
                }
                Map<String, Object> map = new HashMap<>();
                map.put("infectious", line_arr[2]);
                map.put("chronic", line_arr[1]);
                map.put("synonyms", synonymsList);
                map.put("symptoms", symptomsList);
                diagMap.put(line_arr[0], map);
            }
        }
        redisUtil.setJsonStringValue(KernelConstants.CONCEPT_DIAG_PROPERTY_MAP, diagMap);
    }

    /**
     * 医院医生信息
     * @throws Exception
     */
    public void putHospitalDoctorInfoMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/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 == 3) {
                Map<String, Object> map = new HashMap<>();
                map.put("doctorName", line_arr[0]);
                map.put("deptName", line_arr[1]);
                map.put("professor", line_arr[2]);
                hospitalDoctorMap.put(line_arr[0], map);
            }
        }
        redisUtil.setJsonStringValue(KernelConstants.HOSPITAL_DOCTOR_MAP, hospitalDoctorMap);
    }

    /**
     * 医院疾病信息放入redis缓存
     */
    public void putHospitalDiagMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/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);
            detail.put("icd10", "A001");
            hospitalDiagMap.put(line, detail);
        }
        redisUtil.setJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP, hospitalDiagMap);
    }

    /**
     * 医院和华卓映射疾病信息放入redis缓存
     */
    public void putHospitalDiagHuaZMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/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);
            detail.put("icd10", "A001");
            hospitalDiagMap.put(line, detail);
        }
        redisUtil.setJsonStringValue(KernelConstants.HOSPITAL_DIAG_HUAZ_MAP, hospitalDiagMap);
    }

    /**
     * 医院和华卓映射疾病信息放入redis缓存
     */
    public void putLantoneDiagHuaZMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/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);
            detail.put("icd10", "A001");
            hospitalDiagMap.put(line, detail);
        }
        redisUtil.setJsonStringValue(KernelConstants.LANTONE_DIAG_HUAZ_MAP, hospitalDiagMap);
    }
    /**
     * 症状需要部位描述词典放入redis缓存
     */
    public void putClinicBodyPartMap() throws Exception {
        Configuration configuration = new DefaultConfig();
        List<String> lines = configuration.readTargetFileContents("cache/concept_clinic_bodypart_properties.dict");
        List<String> clinicBodyPartList = new ArrayList<>();
        for (String line : lines) {
            clinicBodyPartList.add(line);
        }
        redisUtil.setJsonStringValue(KernelConstants.CONCEPT_CLINIC_BODYPART_PROPERTIES_LIST, clinicBodyPartList);
    }
}