package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.CategoryService;
import org.jeecg.modules.employBanner.entity.EmployBanner;
import org.jeecg.modules.employBanner.service.IEmployBannerService;
import org.jeecg.modules.employCategory.entity.EmployCategory;
import org.jeecg.modules.employCategory.service.IEmployCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    /******************************************************************************************************************/
    //分类信息
    @Resource
    private IEmployCategoryService employCategoryService;
    /******************************************************************************************************************/

    //首页-分类列表
    @Override
    public Result<?> queryCategoryList(String categoryOneId, PageBean pageBean) {
        //返回信息
        String message = "商品分类列表查询失败!";

        try{
            //一级分类列表
            List<EmployCategory> carrentCategoryoneList = getCatogoryoneList(categoryOneId);
            //二级分类列表
            for (EmployCategory category : carrentCategoryoneList) {
                List<EmployCategory> employCategorytwoList = getCatogoryTwoList(category.getId());
                category.setEmployCategoryList(employCategorytwoList);
            }
            message = "商品分类列表信息";

            return Result.OK(message, carrentCategoryoneList);

        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //获取分类信息列表
    public List<EmployCategory> getCatogoryoneList(String categoryOneId) {
        //返回信息
        String message = "商品分类信息列表查询失败!";

        try{
            LambdaQueryChainWrapper<EmployCategory> query = employCategoryService
                    .lambdaQuery();
            //组装查询条件
            if(null != categoryOneId){
                query.eq(EmployCategory::getId, categoryOneId);
            }else {
                //如果一级分类id为空，则查询所有一级分类
                EmployCategory category = employCategoryService
                        .lambdaQuery()
                        .isNull(EmployCategory::getPid)
                        .one();
                query.eq(EmployCategory::getPid, category.getId());
            }

            //获取分类列表
            List<EmployCategory> pageList = query.list();

            //返回执行结果
            return pageList;
        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return null;
        }

    }

    //获取二级分类列表信息
    public List<EmployCategory> getCatogoryTwoList(String categoryoneId) {
        //返回信息
        String message = "商品二级分类列表查询失败!";

        try{
            //获取商品二级分类列表
            List<EmployCategory> pageList = employCategoryService
                    .lambdaQuery()
                    .eq(EmployCategory::getPid, categoryoneId)
                    .list();

            //判断执行结果
            if(null != pageList){
                message = "商品二级分类列表信息";
            }else {
                message = "商品二级分类列表为空";
            }

            //返回执行结果
            return pageList;
        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return null;
        }

    }

}
