package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.basic.domain.YjStore;
import com.ruoyi.basic.service.YjStoreService;
import com.ruoyi.course.domain.req.ReqSearchClaTime;
import com.ruoyi.course.service.ScClaTimeService;
import com.ruoyi.mall.domain.Product;
import com.ruoyi.mall.domain.Sku;
import com.ruoyi.mall.domain.UserFavorite;
import com.ruoyi.mall.domain.query.ProductQuery;
import com.ruoyi.mall.domain.vo.ProductDetailVO;
import com.ruoyi.mall.domain.vo.ProductVO;
import com.ruoyi.mall.mapper.BrandMapper;
import com.ruoyi.mall.mapper.ProductMapper;
import com.ruoyi.mall.mapper.SkuMapper;
import com.ruoyi.mall.mapper.UserFavoriteMapper;
import com.ruoyi.mall.service.ProductService;
import com.ruoyi.mall.service.UserFavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品信息Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private YjStoreService storeService;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    public ProductVO selectById(Long id) {
        Product product = productMapper.selectById(id);
        ProductVO productVO = BeanUtil.toBean(product,ProductVO.class);
        QueryWrapper<Sku> qw = new QueryWrapper<>();
        qw.eq("product_id", product.getId());
        List<Sku> skus = skuMapper.selectList(qw);
        productVO.setSkuList(skus);
        return productVO;
    }

    /**
     * 查询商品信息列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 商品信息
     */
    public List<Product> selectList(ProductQuery query, Pageable page) {



        Long categoryId = query.getCategoryId();
        if (categoryId != null && categoryId.equals(1L)) {
            YjStore store= storeService.getOne(Long.valueOf(query.getStoreId()));
            if (store==null){
                return new ArrayList<>();
            }
            if (page != null) {
                PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
            }
            QueryWrapper<Product> qw = new QueryWrapper<>();
            qw.eq("publish_status", 1);

            qw.eq("category_id", categoryId);
            qw.eq("store_id", query.getStoreId());//门店id
            String search = query.getSearch();
            if (StringUtils.isNoneEmpty(search)){
                qw.like("name", "%".concat(query.getSearch().trim()).concat("%"));
            }
            qw.orderByAsc("sort");
            return  productMapper.selectList(qw);
        }
        //商城商品
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        return  productMapper.getMallProductList(categoryId,query.getSearch(),null);

    }

    public ProductDetailVO queryDetail(Long id) {
        ProductDetailVO res = new ProductDetailVO();
        Product d = productMapper.selectById(id);
        res.setProduct(d);
        LambdaQueryWrapper<Sku> qw = new LambdaQueryWrapper<>();
        qw.eq(Sku::getProductId, id);
        res.setSkus(skuMapper.selectList(qw));
        if (d.getBrandId() != null) {
            res.setBrand(brandMapper.selectById(d.getBrandId()));
        }
//        if(d.getIsCourse()==1 && ObjectUtil.isNotEmpty(d.getCourseId())){
//            ReqSearchClaTime r=new ReqSearchClaTime();
//            r.setCourseId(d.getCourseId());
//            res.setClaTimeCalendar( scClaTimeService.searchListForCalendar(r));
//        }
        return res;
    }
    public Map selectByGoodStuff(String storeId) {
        Map<String,List<Product>> map=new HashMap<>();
        List<Product> courses=productMapper.getMallProductList(null,null,1);
        List<Product> goods=productMapper.getMallProductList(null,null,0);
        map.put("goods",goods.size()>2?goods.subList(0,2):goods);
        map.put("courses",courses.size()>2?courses.subList(0,2):courses);
        return map;

    }

}
