package com.ruoyi.demo.service.impl;


import com.ruoyi.demo.entity.Category;
import com.ruoyi.demo.mapper.CategoryMapper;
import com.ruoyi.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    public List<Category> addrCategory;
    public List<Category> typeBy;
    public List<Category> manageCategory;
    public List<Category> industryCategory;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> getAddrCategory() {
        if (addrCategory != null)
            return addrCategory;
        addrCategory = categoryMapper.searchProvince();
        if (addrCategory.isEmpty())
            return null;
        for (Category province : addrCategory){
            province.setType("province");
            List<Category> citys = categoryMapper.searchCityByProvince(province.getName());
            if (citys.isEmpty())
                province.setChildren(null);
            else{
                for (Category city : citys){
                    city.setType("city");
                    List<Category> zones = categoryMapper.searchZoneByCity(city.getName());
                    if (zones.isEmpty())
                        city.setChildren(null);
                    else{
                        for (Category zone : zones){
                            zone.setType("zone");
                            zone.setChildren(null);
                        }
                        city.setChildren(zones);
                    }
                }
                province.setChildren(citys);
            }
        }
        return addrCategory;
    }

    @Override
    public List<Category> getTypeByCategory() {
        if (typeBy != null)
            return typeBy;
        typeBy = categoryMapper.searchBigCategory();
        if (typeBy.isEmpty())
            return null;
        for (Category big : typeBy){
            big.setType("bigCategory");
            List<Category> mids = categoryMapper.searchMidCategoryByBig(big.getName());
            if (mids.isEmpty())
                big.setChildren(null);
            else {
                for (Category mid : mids){
                    mid.setType("midCategory");
                    List<Category> subs = categoryMapper.searchSubCategoryByMid(mid.getName());
                    if (subs.isEmpty())
                        mid.setChildren(null);
                    else{
                        for (Category sub : subs){
                            sub.setType("subCategory");
                            sub.setChildren(null);
                        }
                        mid.setChildren(subs);
                    }
                }
                big.setChildren(mids);
            }
        }
        return typeBy;
    }

    @Override
    public List<Category> getWdTypeCategory() {
        return categoryMapper.searchWdType();
    }

    @Override
    public List<Category> getManageCategory() {
        if (manageCategory != null){
            return manageCategory;
        }
        manageCategory = categoryMapper.searchBigManage();
        if (manageCategory.isEmpty())
            return null;
        for (Category big : manageCategory){
            big.setType("bigCategory");
            List<Category> mids = categoryMapper.searchMidManageByBig(big.getName());
            if (mids.isEmpty())
                big.setChildren(null);
            else {
                for (Category mid : mids){
                    mid.setType("midCategory");
                    List<Category> subs = categoryMapper.searchSubManageByMid(mid.getName());
                    if (subs.isEmpty())
                        mid.setChildren(null);
                    else{
                        for (Category sub : subs){
                            sub.setType("subCategory");
                            sub.setChildren(null);
                        }
                        mid.setChildren(subs);
                    }
                }
                big.setChildren(mids);
            }
        }
        return manageCategory;
    }

    @Override
    public List<Category> getIndustryCategory() {
        if (industryCategory != null){
            return industryCategory;
        }
        industryCategory = categoryMapper.searchBigIndustry();
        if (industryCategory.isEmpty())
            return null;
        for (Category big : industryCategory){
            big.setType("bigCategory");
            List<Category> mids = categoryMapper.searchMidIndustryByBig(big.getName());
            if (mids.isEmpty())
                big.setChildren(null);
            else {
                for (Category mid : mids){
                    mid.setType("midCategory");
                    List<Category> subs = categoryMapper.searchSubIndustryByMid(mid.getName());
                    if (subs.isEmpty())
                        mid.setChildren(null);
                    else{
                        for (Category sub : subs){
                            sub.setType("subCategory");
                            sub.setChildren(null);
                        }
                        mid.setChildren(subs);
                    }
                }
                big.setChildren(mids);
            }
        }
        return industryCategory;
    }
}
