package com.casic.missiles.core.util;

import com.casic.missiles.modular.system.dao.AlarmRuleMapper;
import com.casic.missiles.modular.system.dao.SysDictMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@DependsOn("springContextHolder")
public class EhcacheConstant {

    private AlarmRuleMapper alarmRuleMapper = SpringContextHolder.getBean(AlarmRuleMapper.class);
    private SysDictMapper sysDictMapper = SpringContextHolder.getBean(SysDictMapper.class);

    public static EhcacheConstant retBean() {
        return SpringContextHolder.getBean("ehcacheConstant");}

    @Cacheable(value = "CONSTANT", key = "'deviceType-'+#deviceTypeId")
    public String getDeviceTypeNameById ( String deviceTypeId)
    {
        String retStr = sysDictMapper.getDeviceTypeNameById(deviceTypeId);
        return retStr;
    }

    @Cacheable(value = "CONSTANT", key = "'deptName-'+#deptid")
    public String getDeptNameById(long deptid)
    {
        return sysDictMapper.getDeptNameById(deptid);
    }

    @Cacheable(value = "CONSTANT", key = "'alarmContentName-'+#id")
    public String getAlarmContentName(int id) {
        return sysDictMapper.getAlarmContentName(id);
    }

    @Cacheable(value = "CONSTANT", key = "'alarmContent-'+#sName")
    public int getAlarmContentByName(String sName) {
        return sysDictMapper.getAlarmContentByName(sName);
    }

    @Cacheable(value = "CONSTANT", key = "'wellInfo-'+#wellCode")
    public Map<String ,Object> getWellInfoViaCode(String wellCode) {
        Map<String ,Object> retMap = new HashMap<>();
        retMap = sysDictMapper.getWellInfoViaCode(wellCode);
        return retMap;
    }

    @Cacheable(value = "CONSTANT", key = "'jobStatus-'+#jobStatus")
    public String getJobStatusNameByCodeNum( String jobStatus)
    {
        return sysDictMapper.getJobStatusNameByCodeNum(jobStatus);
    }

    @Cacheable(value = "CONSTANT", key = "'alarmType-'+#alarmType")
    public String getAlarmTypeNameByCodeNum( String alarmType)
    {
        return sysDictMapper.getAlarmTypeNameByCodeNum(alarmType);
    }

    @Cacheable(value = "CONSTANT", key = "#code+'-dict-'+#codeNum")
    public String getDictNameByCodeCodeNum( String codeNum,String code)
    {
        return sysDictMapper.getDictNameByCodeCodeNum(codeNum, code);
    }

    @Cacheable(value = "CONSTANT", key = "'userNameId-'+#id")
    public String getUsernameById(long id){
        return sysDictMapper.getUsernameById(id);
    }

    @Cacheable(value = "CONSTANT", key = "'userInfoId-'+#id")
    public Map<String ,Object> getUserInfoById(long id){
        return sysDictMapper.getUserInfoById(id);
    }

    @Cacheable(value = "CONSTANT", key = "'deptUser'+#deptid")
    public Long getUserByDept(Long deptid){
        return sysDictMapper.getUserByDept(deptid);
    }

    @CacheEvict(value = "CONSTANT", allEntries = true ,beforeInvocation = true)
    public void resetEhcache()
    {
        System.out.println("cacheReset");
    }

    public static boolean isNumL(Object obj) {
        try {
            Long.parseLong(obj.toString());
            return true;
        } catch (Exception var2) {
            return false;
        }
    }
}
