package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.GoodsService;
import org.jeecg.modules.teambuyCategory.entity.TeambuyCategory;
import org.jeecg.modules.teambuyCategory.service.ITeambuyCategoryService;
import org.jeecg.modules.teambuyGoods.entity.TeambuyGoods;
import org.jeecg.modules.teambuyGoods.service.ITeambuyGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GoodsServiceImpl implements GoodsService {
    /******************************************************************************************************************/
    //商品分类信息
    @Resource
    private ITeambuyGoodsService teambuyGoodsService;
    //商品信息
    @Resource
    private ITeambuyCategoryService teambuyCategoryService;
    /******************************************************************************************************************/

    //查看商品分类列表
    @Override
    public Result<?> queryCategoryList(TeambuyCategory teambuyCategory, PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyCategory> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyCategory> query = null;
        //返回信息
        Page<TeambuyCategory> pageList = null;

        try{
            //分页
            page = new Page<TeambuyCategory>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyCategoryService
                    .lambdaQuery();

            //组装查询条件
            if(StringUtils.isNotBlank(teambuyCategory.getId())){
                //分类编号
                query.eq(TeambuyCategory::getId, teambuyCategory.getId());
            }
            if(StringUtils.isNotBlank(teambuyCategory.getPid())){
                //父id
                query.eq(TeambuyCategory::getPid, teambuyCategory.getPid());
            }else {
                //父id
                query.eq(TeambuyCategory::getPid, "0");
            }
            if(StringUtils.isNotBlank(teambuyCategory.getName())){
                //分类名称
                query.like(TeambuyCategory::getPid, teambuyCategory.getName());
            }

            //按照排序编号升序排列
            query.orderByDesc(TeambuyCategory::getCreateTime);

            //获取商品信息信息
            pageList = query.page(page);

            return Result.OK("商品分类列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("商品分类列表查询失败");
        }
    }

    //查看商品信息列表
    @Override
    public Result<?> queryGoodsList(TeambuyGoods teambuyGoods, PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyGoods> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyGoods> query = null;
        //返回信息
        Page<TeambuyGoods> pageList = null;

        try{
            //分页
            page = new Page<TeambuyGoods>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyGoodsService
                    .lambdaQuery();

            //组装查询条件
            if(StringUtils.isNotBlank(teambuyGoods.getId())){
                //商品编号
                query.eq(TeambuyGoods::getId, teambuyGoods.getId());
            }
            if(StringUtils.isNotBlank(teambuyGoods.getTitle())){
                //商品名称
                query.like(TeambuyGoods::getTitle, teambuyGoods.getTitle());
            }
            if(StringUtils.isNotBlank(teambuyGoods.getCategoryId())){
                //商品分类id
                query.eq(TeambuyGoods::getCategoryId, teambuyGoods.getCategoryId());
            }

            //按照排序编号升序排列
            query.orderByDesc(TeambuyGoods::getCreateTime);

            //获取商品信息信息
            pageList = query.page(page);

            return Result.OK("商品信息列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("商品信息列表查询失败");
        }
    }

    //查看商品信息详情
    @Override
    public Result<?> queryGoodsById(String id) {
        //返回信息
        String massege = "";

        try{
            TeambuyGoods one = teambuyGoodsService
                    .lambdaQuery()
                    .eq(TeambuyGoods::getId, id)
                    .one();

            return Result.OK("商品信息详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("商品信息详情查询失败");
        }
    }
}
