package com.xintong.visualinspection.util;

import com.xintong.visualinspection.bean.Constant;
import com.xintong.visualinspection.bean.FeeStation;
import com.xintong.visualinspection.bean.Item;
import com.xintong.visualinspection.bean.Job;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.service.ConstantService;
import com.xintong.visualinspection.service.DepartmentService;
import com.xintong.visualinspection.service.ItemService;
import com.xintong.visualinspection.service.JobService;
import com.xintong.visualinspection.service.UserService;
import org.slf4j.LoggerFactory;

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

public class CacheUtil {

    public static Map<String, String> commonDataMap = new HashMap<String, String>();

    public static Map<Long, User> userMap = new HashMap<Long, User>();

    public static Map<Long, Organ> deptMap = new HashMap<Long, Organ>();

    public static Map<String, Constant> codeMap = new HashMap<String, Constant>();

    public static Map<String, List<Constant>> codeFlagMap = new HashMap<String, List<Constant>>();

    public static Map<Long, Job> jobMap = new HashMap<Long, Job>();

    public static Map<Long, Item> itemMap = new HashMap<Long, Item>();

    public static Map<Long, List<String>> itemNameMap = new HashMap<Long, List<String>>();

    public static Map<String, List<FeeStation>> allDeptMap = new HashMap<String, List<FeeStation>>();

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheUtil.class);

    public static void refreshCodeMap(ConstantService constantService) {
        //加载用户信息
        List<Constant> constantList = constantService.getAll();
        for (Constant c : constantList) {
            CacheUtil.codeMap.put(c.getCode_flag() + "_" + c.getCode_value(), c);
            List<Constant> list = CacheUtil.codeFlagMap.get(c.getCode_flag());
            if (list == null) {
                list = new ArrayList<Constant>();
            }
            list.add(c);
            CacheUtil.codeFlagMap.put(c.getCode_flag(), list);
        }
        logger.info("加载CODE信息成功，数据数：" + CacheUtil.codeFlagMap.size());
    }

    public static void refreshUserMap(UserService userService) {
        //加载用户信息
        List<User> userList = userService.getAll();
        for (User user : userList) {
            CacheUtil.userMap.put(new Long(user.getId()), user);
        }
        logger.info("加载用户信息成功，数据数：" + CacheUtil.userMap.size());
    }

    public static void refreshDeptMap(DepartmentService deptService) {
        //加载部门信息
        List<Organ> deptList = deptService.getAll();
        for (Organ organ : deptList) {
            CacheUtil.deptMap.put(new Long(organ.getId()), organ);
        }
        logger.info("加载部门信息成功，数据数：" + CacheUtil.deptMap.size());
    }

    public static void refreshJobMap(JobService jobService) {
        //加载用户信息
        Job j = new Job();
        j.setDept_id(1L);
        List<Job> jobList = jobService.getJobList(j);
        for (Job job : jobList) {
            CacheUtil.jobMap.put(new Long(job.getId()), job);
        }
        logger.info("加载职位信息成功，数据数：" + CacheUtil.jobMap.size());
    }

    public static void refreshItemMap(ItemService itemService) {
        //加载部门信息
        List<Item> itemList = itemService.getAll();
        for (Item checkItem : itemList) {
            CacheUtil.itemMap.put(new Long(checkItem.getId()), checkItem);
        }
        logger.info("加载考核项信息成功，数据数：" + CacheUtil.itemMap.size());
    }

    public static void refreshItemNameMap(ItemService itemService) {
        List<Long> list = new ArrayList<Long>();
        list.add(26L);// 第三方暗访
        list.add(27L);// 现场检查
        list.add(28L);// 服务区考核办法
        for (Long id : list) {
            List<String> itemList = itemService.getCheckItemName(id);
            CacheUtil.itemNameMap.put(id, itemList);
            logger.info("加载ruleId={},查询服务区考核项信息成功，数据数：{}", id, CacheUtil.itemNameMap.size());
        }
    }

    public static void refreshSAList(DepartmentService deptService) {
        List<FeeStation> saList = deptService.getAllSA();
        List<FeeStation> rmList = deptService.getAllRM();
        CacheUtil.allDeptMap.put("SA", saList);
        CacheUtil.allDeptMap.put("RM", rmList);
        logger.info(CacheUtil.allDeptMap.toString());
        logger.info("加载所有服务区,道管信息成功,数据list数:{}", CacheUtil.allDeptMap.size());
    }

    public static User getUserFromMap(Long key) {
        if (key == null) {
            return null;
        }
        return CacheUtil.userMap.get(key);
    }

    public static String getUserTurenameFromMap(Long key) {
        if (getUserFromMap(key) != null && key != null) {
            return getUserFromMap(key).getTruename();
        }
        return null;
    }

    public static Organ getOrganFromMap(Long key) {
        if (key == null) {
            return null;
        }
        return CacheUtil.deptMap.get(key);
    }

    public static String getOrgannameFromMap(Long key) {
        if (getOrganFromMap(key) != null) {
            return getOrganFromMap(key).getOrganname();
        }
        return null;
    }

    public static Job getJobFromMap(Long key) {
        if (key == null) {
            return null;
        }
        return CacheUtil.jobMap.get(key);
    }

    public static String getJobnameFromMap(Long key) {
        if (getJobFromMap(key) != null) {
            return getJobFromMap(key).getName();
        }
        return null;
    }

    public static Item getItemFromMap(Long key) {
        if (key == null) {
            return null;
        }
        return CacheUtil.itemMap.get(key);
    }

    public static String getItemNameFromMap(Long key) {
        if (getItemFromMap(key) != null) {
            return getItemFromMap(key).getName();
        }
        return null;
    }

    public static List<String> getItemNameList(Long key) {
        return CacheUtil.itemNameMap.get(key);
    }

    public static List<FeeStation> getDeptByLayerFromMap(String key) {
        return CacheUtil.allDeptMap.get(key);
    }
}
