package com.wzj.soopin.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.goods.domain.bo.ProductBo;
import com.wzj.soopin.goods.domain.entity.Product;
import com.wzj.soopin.goods.domain.entity.ProductCategory;
import com.wzj.soopin.goods.domain.vo.ProductVO;
import com.wzj.soopin.goods.domain.vo.SkuVO;
import com.wzj.soopin.goods.mapper.ProductCategoryMapper;
import com.wzj.soopin.goods.mapper.ProductMapper;
import com.wzj.soopin.goods.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.event.Constants;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.service.ISysTenantService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品信息Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    private final ProductMapper productMapper;
    private final ProductCategoryMapper productCategoryMapper;
    private final ISysTenantService sysTenantService;


    @Override
    public Page<ProductVO> getList(ProductBo query, Page<Product> page) {
        return productMapper.getlist(page, query);
    }


    @Override
    public IPage<ProductVO> getRecommendPage( Page<Product> page) {

        //推荐商品要根据算法获取


        return productMapper.getlist(page,new ProductBo());
    }


    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public ProductVO selectById(Long id) {
        ProductVO productVO = TenantHelper.ignore(() -> productMapper.selectProductWithSkus(id));
        SysTenantVo sysTenantVo = sysTenantService.queryByTenantId(productVO.getTenantId());
        productVO.setCanOrder(StrUtil.isNotBlank(sysTenantVo.getMchtCode()));
        List<SkuVO> skuList = productVO.getSkuList();
        skuList.forEach(sku -> {
            JSONObject jsonObject = JSONObject.parseObject(sku.getSpData());
            sku.setAttributes(jsonObject.values().stream()
                .map(Object::toString)
                .collect(Collectors.joining(",")));
        });
        return productVO;
    }



    /**
     * 不带租户的获取商品信息
     * @param page
     * @param query
     * @return
     */
    public IPage<ProductVO> getProduct(Page<Product> page, ProductBo query) {
        if(query.getCategoryId() != null){
            ArrayList<Long> categoryIds = CollUtil.newArrayList(query.getCategoryId());
            categoryIds.addAll(productCategoryMapper.selectByParentId(query.getCategoryId())
                .stream().map(ProductCategory::getId).toList());
            query.setCategoryIds(categoryIds);
        }
        query.setPublishStatus(Constants.PublishStatus.GROUNDING);
        return TenantHelper.ignore(() -> productMapper.getProduct(page, query));
    }


    /**
     * 不带租户的获取商品信息
     *
     * @param query
     * @param page
     * @return
     */
    @Override
    public IPage<ProductVO> getProductPageList(ProductBo query, Page<Product> page) {
        return TenantHelper.ignore(() -> productMapper.getProduct(page, query));
    }
}
