package com.fufan.mtsaas.cache;

import com.baomidou.mybatisplus.plugins.Page;
import com.fufan.mtsaas.constant.RedisKeyConstant;
import com.fufan.mtsaas.modules.sysmgr.sygroup.dao.SYgroupHDao;
import com.fufan.mtsaas.modules.sysmgr.sygroup.model.SYgroupH;
import com.fufan.mtsaas.modules.sysmgr.symenu.dao.SysButtonDao;
import com.fufan.mtsaas.modules.sysmgr.symenu.dao.SysMenuDao;
import com.fufan.mtsaas.modules.sysmgr.symenu.model.SysButton;
import com.fufan.mtsaas.modules.sysmgr.symenu.model.SysMenu;
import com.fufan.mtsaas.modules.sysmgr.syuser.dao.SYuserDao;
import com.fufan.mtsaas.modules.sysmgr.syuser.model.SYuser;
import com.fufan.mtsaas.modules.base.company.dao.TCompanyDao;
import com.fufan.mtsaas.modules.base.company.model.TCompany;
import com.fufan.mtsaas.tools.AESTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class Cache implements ApplicationRunner {
    public static Map<String,String> AREA_MAP = new HashMap<>();
    public static Map<String,String> COMPANY_MAP = new ConcurrentHashMap<>();
    public static Map<String,String> COMPANY_NAME_MAP = new ConcurrentHashMap<>();
    public static Map<Long,String> ROLE_MAP = new ConcurrentHashMap<>();
    public static Map<String,Long> FIELD_MAP = new HashMap<>();
    public static Map<Long,String> ID_FIELD_MAP = new HashMap<>();
    public static Map<String, SysMenu> MENU_MAP = new HashMap<>();
    public static Map<String, String> BUTTON_MAP = new HashMap<>();
    public static Map<String, String> ROLE_URL_MAP = new HashMap<>();
    public static Map<String, String> MR_INFO_MAP = new HashMap<>();
    @Autowired
    private TCompanyDao tCompanyDao;
//    @Autowired
//    private TFieldTypeDao tFieldTypeDao;

    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysButtonDao sysButtonDao;
    @Autowired
    private SYgroupHDao sYgroupHDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SYuserDao sYuserDao;
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        //北京
        AREA_MAP.put("11","11");
        //天津
        AREA_MAP.put("12","12");
        //上海
        AREA_MAP.put("31","31");
        //重庆
        AREA_MAP.put("50","50");
        //台湾
        AREA_MAP.put("71","71");
        //香港
        AREA_MAP.put("81","81");
        //澳门
        AREA_MAP.put("82","82");

        updateCompanyMap();
        //初始化模板映射数据
//        initFiledMap();
        //初始化菜单数据
        initMenuMap();
        //初始化角色数据
        updateRoleMap();
        //初始化菜单权限数据
        initUrlMap();
        //初始化redis 数据
        initRedisData();

        initMetaMap();

        System.out.println("初始化Cache完成。");
      //  testRedis();
    }

    public void updateData() {
        //初始化菜单数据
        initMenuMap();
        //初始化角色数据
        updateRoleMap();
        //初始化菜单权限数据
        initUrlMap();
        //初始化redis 数据
        initRedisData();

        initMetaMap();

        log.debug("更新Cache完成。");
    }

    public void testRedis(){
        stringRedisTemplate.opsForValue().increment("test-8-6767",1);
    }
    public void updateCompanyMap(){
        List<TCompany> tCompanyList = tCompanyDao.queryAllCompanyName();
        if(tCompanyList.size() > 0){
            for (TCompany tCompany : tCompanyList){
                COMPANY_MAP.put(String.valueOf(tCompany.getId()),tCompany.getCompanyName());
                COMPANY_NAME_MAP.put(tCompany.getCompanyName(),String.valueOf(tCompany.getId()));
            }
        }
    }

//    private void test(){
//        TFieldType tFieldType = new TFieldType();
//        List<TFieldType> tPrintTemplateList =  tFieldTypeDao.queryAllByLimit(tFieldType);
//        for (TFieldType tFieldType1 : tPrintTemplateList ){
//            tFieldType1.setId(0L);
//        }
//        tFieldTypeDao.insertBatch(tPrintTemplateList);
//
//        System.out.println(JSONObject.toJSONString(tPrintTemplateList));
//    }
    public void updateRoleMap(){
        Map<String,String> mapData = new HashMap<>();
        Page page = new Page<>();
        List<SYgroupH> sYgroupHList = sYgroupHDao.selectList(page,mapData);
        for (SYgroupH sYgroupH : sYgroupHList){
            ROLE_MAP.put(Long.valueOf(sYgroupH.getId()),sYgroupH.getGroupName());
        }
    }

//    private void initFiledMap(){
//        TFieldType tFieldType = new TFieldType();
//        List<TFieldType> tPrintTemplateList =  tFieldTypeDao.queryAllByLimit(tFieldType);
//        for(TFieldType moduleType : tPrintTemplateList){
//            FIELD_MAP.put(moduleType.getRemark(),moduleType.getId());
//        }
//
//        for(TFieldType moduleType : tPrintTemplateList){
//            ID_FIELD_MAP.put(moduleType.getId(),moduleType.getRemark());
//        }
//    }
    private void initMetaMap(){
        MR_INFO_MAP.put("安全","0");
        MR_INFO_MAP.put("不安全","1");
        MR_INFO_MAP.put("不包含MR信息","2");
    }

    private void initRedisData(){
      List<SYuser> sYuserList = sYuserDao.selectList(new Page<>(),new HashMap<>());
      for (SYuser sYuser : sYuserList){
          try {
              if (Objects.nonNull(sYuser.getUserID())) {
                  stringRedisTemplate.opsForValue().set(RedisKeyConstant.USER_MODIFY_LOCK + ":" + AESTool.decrypt(sYuser.getUserID()), String.valueOf(sYuser.getId()));
              }
              stringRedisTemplate.opsForValue().set(RedisKeyConstant.USER_MODIFY_LOCK + ":" + sYuser.getEmpNo(), String.valueOf(sYuser.getId()));
          }catch (Exception e){

          }
      }
    }

    private void initUserData() {
        List<SYuser> sYuserList = sYuserDao.selectList(new Page<>(), new HashMap<>());
        for (SYuser sYuser : sYuserList) {
            try {
//                sYuser.setUserID(AESTool.encrypt(sYuser.getUserID()));
//                sYuser.setPhoneNo(sYuser.getUserID());
//                sYuserDao.updateRecords(sYuser);
                if (Objects.nonNull(sYuser.getUserID())) {
                    stringRedisTemplate.opsForValue().set(RedisKeyConstant.USER_MODIFY_LOCK + ":" + AESTool.decrypt(sYuser.getUserID()), String.valueOf(sYuser.getId()));
                }

                stringRedisTemplate.opsForValue().set(RedisKeyConstant.USER_MODIFY_LOCK + ":" + sYuser.getEmpNo(), String.valueOf(sYuser.getId()));
            } catch (Exception e) {

            }
        }
    }

    private void initMenuMap(){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setIsEnabled(0);
        List<SysMenu> sysMenuList =  sysMenuDao.queryAllByLimit(sysMenu);
        for(SysMenu sysMenuData : sysMenuList){
            MENU_MAP.put(sysMenuData.getMenuCode(),sysMenuData);
        }

        SysButton sysButton = new SysButton();
        List<SysButton> sysButtonList =  sysButtonDao.queryAllByLimit(sysButton);
        for(SysButton sysButtonData : sysButtonList){
            BUTTON_MAP.put(sysButtonData.getButtonCode(),sysButtonData.getLabel());
        }
    }
    public void initUrlMap(){
        ROLE_URL_MAP.put("/" + "UDI/Company/doQueryPage","detail:Company");
        ROLE_URL_MAP.put("/" + "UDI/Company/doUpdate","edit:Company");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doSave","add:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doQueryPage","detail:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doUpdate","edit:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doDelete","delete:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doImport","import:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/RegistrationCertificate/doExport","export:Regcard");
        ROLE_URL_MAP.put("/" + "UDI/Product/doSave","add:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/Product/doQueryPage","detail:Proinfo;query:Proinfo;" + "query:Upload;" + "detail:Upload");
        ROLE_URL_MAP.put("/" + "UDI/Product/doUpdate","edit:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/Product/doDelete","delete:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/Product/doImport","import:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/Product/doExport","export:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/Product/doDownload","download:Proinfo");
        ROLE_URL_MAP.put("/" + "UDI/PrintTemplate/doSaveOrUpdateModule","add:Templates"+ ";" + "edit:Templates");
        ROLE_URL_MAP.put("/" + "UDI/PrintTemplate/doQueryPage","detail:Templates" + ";" + "query:Templates");
        ROLE_URL_MAP.put("/" + "UDI/PrintTemplate/doDelete","delete:Templates");
        ROLE_URL_MAP.put("/" + "UDI/PrintTemplate/doPrintView","preview:Templates");
        ROLE_URL_MAP.put("/" + "UDI/Order/doSave","add:Order");
        ROLE_URL_MAP.put("/" + "UDI/Order/doUpdate","edit:Order");
        ROLE_URL_MAP.put("/" + "UDI/Order/doQueryPage","detail:Order" + ";" + "query:Order");
        ROLE_URL_MAP.put("/" + "UDI/Order/doDelete","delete:Order");
        ROLE_URL_MAP.put("/" + "UDI/PrintTemplate/doPrint","print:Print" + ";" + "query:Printfix;" + "print:Order");
        ROLE_URL_MAP.put("/" + "UDI/CodeReprint/doQueryPage","query:Printfix");
        ROLE_URL_MAP.put("/" + "UDI/CodeReprint/doPrint","print:Printfix");
        ROLE_URL_MAP.put("/" + "UDI/DI/doUpload","upload:Upload");
        ROLE_URL_MAP.put("/" + "UDI/DI/doSubmit","publish:Upload");
        ROLE_URL_MAP.put("/" + "SYS/SYuser/doSave","add:User" + ";" + "add:Customeruser");
        ROLE_URL_MAP.put("/" + "SYS/SYuser/doUpdate","edit:User" + ";" + "edit:Customeruser");
        ROLE_URL_MAP.put("/" + "SYS/SYuser/doQueryPage","detail:User" + ";" + "query:User" + ";" + "detail:Customeruser" + ";" + "query:Customeruser");
        ROLE_URL_MAP.put("/" + "SYS/SYuser/doDelete","delete:User" + ";" + "delete:Customerrole");
        ROLE_URL_MAP.put("/" + "SYS/SYgroupH/doSave","add:Role" + ";" + "add:Customerrole");
        ROLE_URL_MAP.put("/" + "SYS/SYgroupH/doUpdate","edit:Role" + ";" + "edit:Customerrole");
        ROLE_URL_MAP.put("/" + "SYS/SYgroupH/doQueryPage","detail:Role" + ";" + "query:Role" + ";" + "detail:Customerrole" + ";" + "query:Customerrole" );
        ROLE_URL_MAP.put("/" + "SYS/SYgroupH/doDelete","delete:Role" + ";" +  "delete:Role");
        ROLE_URL_MAP.put("/" + "UDI/Company/backData/doSave","add:Customer");
        ROLE_URL_MAP.put("/" + "UDI/Company/backData/doUpdate","edit:Customer");
        ROLE_URL_MAP.put("/" + "UDI/Company/backData/doQueryPage","detail:Customer" + ";" + "query:Customer");
    }

    public static void main(String[] args) throws Exception {
        System.out.println(AESTool.encrypt("12012020201"));
        System.out.println(AESTool.decrypt(AESTool.encrypt("12012020201")));
    }
}
