package com.benyun.core.service.impl;

import com.benyun.core.entity.Category;
import com.benyun.core.dao.CategoryMapper;
import com.benyun.core.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> getAddrCategory() {
        List<Category> provinces = categoryMapper.searchProvince();
        if (provinces.isEmpty())
            return null;
        for (Category province : provinces){
            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 provinces;
    }

    @Override
    public List<Category> getTypeByCategory() {
        List<Category> bigs = categoryMapper.searchBigCategory();
        if (bigs.isEmpty())
            return null;
        for (Category big : bigs){
            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 bigs;
    }

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

    @Override
    public List<Category> getManageCategory() {
        List<Category> bigs = categoryMapper.searchBigManage();
        if (bigs.isEmpty())
            return null;
        for (Category big : bigs){
            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 bigs;
    }
}
