package com.gree.mall.miniapp.logic.goods;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.goods.CategoryBean;
import com.gree.mall.miniapp.bean.goods.GoodsNewBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.CategoryMapper;
import com.gree.mall.miniapp.commonmapper.CommonMapper;
import com.gree.mall.miniapp.commonmapper.SecActivityStageMapper;
import com.gree.mall.miniapp.enums.IsYesNoEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.GoodsCategoryService;
import com.gree.mall.miniapp.plus.service.GoodsService;
import com.gree.mall.miniapp.plus.service.GoodsTagRelaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryLogic {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    SecActivityStageMapper secActivityStageMapper;
    @Autowired
    CommonLogic commonLogic;

    @Autowired
    GoodsTagRelaService goodsTagRelaService;


    /**
     * 分类列表
     * @return
     */
    public List<GoodsCategory> list(HttpServletRequest request,String parentId,Integer type,String name,Boolean yjhx){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        if(type == null){
            type = 1;
        }
        List<String> parentIds = null;
        if(yjhx != null && yjhx){
            //拥有以旧换新商品的子分类id
            List<Goods> categoryIds2List = goodsService.lambdaQuery()
                    .select(Goods::getCategoryId)
                    .eq(Goods::getDel, false)
                    .eq(Goods::getStatus, true)
                    .eq(Goods::getIsTrade, IsYesNoEnum.YES.getKey())
                    .eq(Goods::getCompanyWechatId, currentCompanyWechat.getCurrentCompanyWechatId())
                    .list();
            if(CollectionUtils.isEmpty(categoryIds2List)){
                return new ArrayList<>();
            }
            List<String> categoryIds2 = categoryIds2List.stream().map(Goods::getCategoryId).distinct().collect(Collectors.toList());
            List<GoodsCategory> goodsCategoryList = goodsCategoryService.lambdaQuery().in(GoodsCategory::getCategoryId, categoryIds2).list();
            if(CollectionUtils.isEmpty(goodsCategoryList)){
                return new ArrayList<>();
            }
            parentIds = goodsCategoryList.stream().map(GoodsCategory::getParentId).distinct().collect(Collectors.toList());
        }
        List<GoodsCategory> list = goodsCategoryService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(parentIds) && StringUtils.isBlank(parentId),GoodsCategory::getCategoryId,parentIds)
                .eq(type != null,GoodsCategory::getType,type)
                .eq(GoodsCategory::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .eq(StringUtils.isNotBlank(parentId), GoodsCategory::getParentId, parentId)
                .eq(StringUtils.isBlank(parentId), GoodsCategory::getLevel, 1)
                .like(StringUtils.isNotBlank(name),GoodsCategory::getName,name)
                .eq(GoodsCategory::getStatus, true)
                .eq(GoodsCategory::getDel,false)
                .orderByAsc(GoodsCategory::getSortNum)
                //.orderByDesc(GoodsCategory::getCreateTime)
                .list();
        return list;
    }


    public List<CategoryBean> queryAll(HttpServletRequest request,Boolean isServiceShow){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        return  categoryMapper.queryCategory(currentCompanyWechat.getCurrentCompanyWechatId(), isServiceShow);
    }
    public List<GoodsCategory> queryFirst(HttpServletRequest request,Boolean isServiceShow){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        return goodsCategoryService.lambdaQuery()
                .isNull(GoodsCategory::getParentId)
                .eq(isServiceShow != null,GoodsCategory::getIsServiceShow,isServiceShow)
                .eq(GoodsCategory::getStatus,true)
                .eq(GoodsCategory::getDel,false)
                .eq(GoodsCategory::getCompanyWechatId,currentCompanyWechat.getCurrentCompanyWechatId())
                .orderByAsc(GoodsCategory::getSortNum)
               // .orderByDesc(GoodsCategory::getCreateTime)
                .list();
    }
    public List<GoodsCategory> querySecond(HttpServletRequest request , String categoryId){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        return goodsCategoryService.lambdaQuery()
                .eq(GoodsCategory::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .eq(GoodsCategory::getParentId,categoryId)
                .eq(GoodsCategory::getStatus,true)
                .eq(GoodsCategory::getDel,false)
                .orderByAsc(GoodsCategory::getSortNum)
                //.orderByDesc(GoodsCategory::getCreateTime)
                .list();
    }




    public IPage<Goods> categoryGoodsList(HttpServletRequest request,String categoryId, Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        List<GoodsCategory> goodsCategories = goodsCategoryService.lambdaQuery()
                .eq(GoodsCategory::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .eq(GoodsCategory::getParentId, categoryId)
                .list();
        List<String> categoryIds = goodsCategories.stream().map(GoodsCategory::getCategoryId).collect(Collectors.toList());
        if(categoryIds.size() == 0){
            return new Page<>(pageNum,pageSize);
        }

        return goodsService.lambdaQuery()
                .eq(Goods::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .in(Goods::getCategoryId,categoryIds)
                .eq(Goods::getStatus,true)
                .eq(Goods::getDel,false)
                .eq(Goods::getPromotionGroup,false)
                .orderByDesc(Goods::getSortNum)
                .orderByDesc(Goods::getSoldNum)
                .orderByDesc(Goods::getCreateTime)
                .page(new Page(pageNum, pageSize));
    }


    /**
     * 根据分类查询品牌
     * @param categoryId
     * @return
     */
    public List<Brand> brandList(String categoryId){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        List<Brand> brands = categoryMapper.brandList(categoryId,currentCompanyWechat.getCompanyWechatId());
        return brands;
    }




}
