package com.ruoyi.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.demo.entity.AddrCategory;
import com.ruoyi.demo.entity.TypeBy;
import com.ruoyi.demo.entity.vo.AddrCategoryBody;
import com.ruoyi.demo.entity.vo.TypeByBody;
import com.ruoyi.demo.mapper.AddrCategoryDao;
import com.ruoyi.demo.mapper.TypeByDao;
import com.ruoyi.demo.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CommonServiceImpl implements CommonService {

    @Autowired
    TypeByDao typeByDao;

    @Autowired
    AddrCategoryDao addrCategoryDao;

    @Override
    public List<TypeByBody> tag() {
        List<TypeBy> typeBIES = typeByDao.selectList(null);
        List<TypeBy> collect = typeBIES.stream().filter(typeBy -> {
            if (typeBy.getMidCategory() == null || typeBy.getMidCategory().equals(""))
                return true;
            return false;
        }).collect(Collectors.toList());

        List<TypeByBody> typeByBodies = new ArrayList<>();
        for (TypeBy typeBy : collect) {
            TypeByBody typeByBody = new TypeByBody();
            typeByBody.setTypeCodeBy(typeBy.getTypeCodeBy());
            typeByBody.setTypeNameBy(typeBy.getBigCategory());
            typeByBody.setTypeByBodies(findChild(typeBIES, typeByBody.getTypeCodeBy(), 2));

            typeByBodies.add(typeByBody);
        }

        return typeByBodies;
    }

    @Override
    public List<AddrCategory> addrCodeLevel(String code,String level) {
        if ("province".equals(level)){
            QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("city","");
            return addrCategoryDao.selectList(queryWrapper);
        }else if("city".equals(level)){
            QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.likeRight("addr_code",code.substring(0,2));
            queryWrapper.and(categoryQueryWrapper -> {
                categoryQueryWrapper.eq("district", "").and(addrCategoryQueryWrapper -> {
                    addrCategoryQueryWrapper.ne("city", "");
                });
            });
            return addrCategoryDao.selectList(queryWrapper);
        }else if("zone".equals(level)){
            QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.likeRight("addr_code",code.substring(0,4));
            queryWrapper.and(categoryQueryWrapper -> {
                categoryQueryWrapper.ne("district", "");
            });
            return addrCategoryDao.selectList(queryWrapper);
        }
        return null;
    }

    public List<TypeByBody> findChild(List<TypeBy> typeBIES, String typeCodeBy, int randType){
        String substring = typeCodeBy.substring(0, randType);
        if(randType == 2){
            List<TypeByBody> list = new ArrayList<>();
            for (TypeBy typeBY : typeBIES) {
                if(typeBY.getTypeCodeBy().startsWith(substring)  && (typeBY.getMidCategory() != null && !typeBY.getMidCategory().equals("")) && (typeBY.getSubCategory() == null || typeBY.getSubCategory().equals(""))){
                    TypeByBody typeByBody = new TypeByBody();
                    typeByBody.setTypeCodeBy(typeBY.getTypeCodeBy());
                    typeByBody.setTypeNameBy(typeBY.getMidCategory());
                    typeByBody.setTypeByBodies(findChild(typeBIES, typeByBody.getTypeCodeBy(), 4));

                    list.add(typeByBody);
                }
            }
            return list;
        }else if(randType == 4){
            List<TypeByBody> list = new ArrayList<>();
            for (TypeBy typeBY : typeBIES) {
                if(typeBY.getTypeCodeBy().startsWith(substring)  && typeBY.getSubCategory() != null && !typeBY.getSubCategory().equals("")){
                    TypeByBody typeByBody = new TypeByBody();
                    typeByBody.setTypeCodeBy(typeBY.getTypeCodeBy());
                    typeByBody.setTypeNameBy(typeBY.getSubCategory());
                    typeByBody.setTypeByBodies(null);

                    list.add(typeByBody);
                }
            }
            return list;
        }
        return null;
    }

    @Override
    public List<AddrCategoryBody> addrCodeMap() {
        List<AddrCategory> addrCategories = addrCategoryDao.selectList(null);
        List<AddrCategory> collect = addrCategories.stream().filter(addrCategory -> {
            if (addrCategory.getCity() == null || addrCategory.getCity().equals(""))
                return true;
            return false;
        }).collect(Collectors.toList());

        List<AddrCategoryBody> addrCategoryBodies = new ArrayList<>();
        for (AddrCategory addrCategory : collect) {
            AddrCategoryBody addrCategoryBody = new AddrCategoryBody();
            addrCategoryBody.setAddrCoreName(addrCategory.getProvince());
            addrCategoryBody.setAddrCore(addrCategory.getAddrCode());
            addrCategoryBody.setAddrCategoryes(findChildAddr(addrCategories, addrCategoryBody.getAddrCore(), 2));

            addrCategoryBodies.add(addrCategoryBody);
        }

        return addrCategoryBodies;
    }

    public List<AddrCategoryBody> findChildAddr(List<AddrCategory> addrCategories, String addrCore, int randType){
        String substring = addrCore.substring(0, randType);
        if(randType == 2){
            List<AddrCategoryBody> list = new ArrayList<>();
            for (AddrCategory addrCategory : addrCategories) {
                if(addrCategory.getAddrCode().startsWith(substring)  && (addrCategory.getCity() != null && !addrCategory.getCity().equals("")) && (addrCategory.getDistrict() == null || addrCategory.getDistrict().equals(""))){
                    AddrCategoryBody addrCategoryBody = new AddrCategoryBody();
                    addrCategoryBody.setAddrCoreName(addrCategory.getCity());
                    addrCategoryBody.setAddrCore(addrCategory.getAddrCode());
                    addrCategoryBody.setAddrCategoryes(findChildAddr(addrCategories, addrCategoryBody.getAddrCore(), 4));

                    list.add(addrCategoryBody);
                }
            }
            return list;
        }else if(randType == 4){
            List<AddrCategoryBody> list = new ArrayList<>();
            for (AddrCategory addrCategory : addrCategories) {
                if(addrCategory.getAddrCode().startsWith(substring)  && addrCategory.getDistrict() != null && !addrCategory.getDistrict().equals("")){
                    AddrCategoryBody addrCategoryBody = new AddrCategoryBody();
                    addrCategoryBody.setAddrCoreName(addrCategory.getDistrict());
                    addrCategoryBody.setAddrCore(addrCategory.getAddrCode());
                    addrCategoryBody.setAddrCategoryes(null);

                    list.add(addrCategoryBody);
                }
            }
            return list;
        }
        return null;
    }
}
