package com.ruoyi.benyun.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.benyun.mapper.AddrCategoryDao;
import com.ruoyi.benyun.mapper.TypeByDao;
import com.ruoyi.benyun.entity.AddrCategory;
import com.ruoyi.benyun.entity.TypeBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class CategoryUtil {
    public Map<String,String> typeMap;
    public Map<String, List<String>> typeChildren = new HashMap<>();

    public Map<String,String> addrMap;
    public Map<String,List<String>> addrChildren = new HashMap<>();

    public Map<String,String> wdTypeMap;

    @Autowired
    TypeByDao byDao;
    @Autowired
    AddrCategoryDao addrCategoryDao;

    public String whatis(String addrCode){
        String big = String.valueOf(addrCode.charAt(0))+String.valueOf(addrCode.charAt(1));
        String mid = String.valueOf(addrCode.charAt(2))+String.valueOf(addrCode.charAt(3));
        String sub = String.valueOf(addrCode.charAt(4))+String.valueOf(addrCode.charAt(5));
        if (sub.equals("00")){
            if (mid.equals("00")){
                return big;
            }
            return big+mid;
        }
        return big+mid+sub;
    }

    public void setTypeMap(){
        if (typeMap == null){
            typeMap = new HashMap<>();
            QueryWrapper<TypeBy> queryWrapper = new QueryWrapper<>();
            List<TypeBy> typeByList = byDao.selectList(queryWrapper);
            for (TypeBy by : typeByList){
                if (!by.getSubCategory().equals(""))
                    typeMap.put(by.getTypeCodeBy(),by.getSubCategory());
                else if (!by.getMidCategory().equals(""))
                    typeMap.put(by.getTypeCodeBy(),by.getMidCategory());
                else
                    typeMap.put(by.getTypeCodeBy(),by.getBigCategory());
            }
        }
    }

    public void setAddrMap(){
        if (addrMap == null){
            addrMap = new HashMap<>();
            QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
            List<AddrCategory> addrCategories = addrCategoryDao.selectList(queryWrapper);
            for (AddrCategory addr : addrCategories){
                if (!addr.getDistrict().equals(""))
                    addrMap.put(addr.getAddrCode(),addr.getDistrict());
                else if (!addr.getCity().equals(""))
                    addrMap.put(addr.getAddrCode(),addr.getCity());
                else
                    addrMap.put(addr.getAddrCode(),addr.getProvince());
            }
        }
    }

    public void setWdTypeMap(){
        if (wdTypeMap == null){
            wdTypeMap = new HashMap<>();
            wdTypeMap.put("0","基础设施网点");
            wdTypeMap.put("1","门店网点");
            wdTypeMap.put("2","小区网点");
            wdTypeMap.put("3","楼宇网点");
            wdTypeMap.put("4","交通设施网点");
            wdTypeMap.put("5","公司网点");
        }
    }

    public List<String> getAllOtherAddrCode(String addrCode){
        setAddrMap();
        if (addrChildren.get(addrCode) != null){
//            System.out.println("quickly");
            return addrChildren.get(addrCode);
        }
        List<String> addrCodes = new ArrayList<>();
        String big = String.valueOf(addrCode.charAt(0))+String.valueOf(addrCode.charAt(1));
        String mid = String.valueOf(addrCode.charAt(2))+String.valueOf(addrCode.charAt(3));
        String sub = String.valueOf(addrCode.charAt(4))+String.valueOf(addrCode.charAt(5));
        if (sub.equals("00")){
            QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
            if (mid.equals("00")){
                queryWrapper.likeRight("addr_code",big).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("city","");
                }).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("district","");
                });
            }else{
                queryWrapper.likeRight("addr_code",big+mid).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("city","");
                }).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("district","");
                });
            }
            List<AddrCategory> zones = addrCategoryDao.selectList(queryWrapper);
            for (AddrCategory zone : zones){
                addrCodes.add(zone.getAddrCode());
            }
            addrChildren.put(addrCode,addrCodes);
        }else
            addrCodes.add(addrCode);
        return addrCodes;
    }

    public List<String> getAllOtherTypeBy(String typeCode){
        setTypeMap();
        if (typeChildren.get(typeCode) != null){
//            System.out.println("quickly");
            return typeChildren.get(typeCode);
        }
        List<String> types = new ArrayList<>();
        String big = String.valueOf(typeCode.charAt(0)) + String.valueOf(typeCode.charAt(1));
        String mid = String.valueOf(typeCode.charAt(2)) + String.valueOf(typeCode.charAt(3));
        String sub = String.valueOf(typeCode.charAt(4)) + String.valueOf(typeCode.charAt(5));
        if (sub.equals("00")){
            QueryWrapper<TypeBy> queryWrapper = new QueryWrapper<>();
            if (mid.equals("00")){
                queryWrapper.likeRight("type_code_by",big).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("mid_category","");
                }).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("sub_category","");
                });
            }else{
                queryWrapper.likeRight("type_code_by",big+mid).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("mid_category","");
                }).and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("sub_category","");
                });
            }
            List<TypeBy> typeByList = byDao.selectList(queryWrapper);
            for (TypeBy typeBy : typeByList){
                types.add(typeBy.getTypeCodeBy());
            }
            typeChildren.put(typeCode,types);
        }else
            types.add(typeCode);
        return types;
    }
}
