package com.hzs.sale.product.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.sale.classify.BdSpecs;
import com.hzs.common.domain.sale.ext.BdProductExt;
import com.hzs.common.domain.sale.product.BdProduct;
import com.hzs.common.domain.sale.product.BdProductSpecs;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.sale.product.controller.manage.params.ProductParams;
import com.hzs.sale.product.dto.ProductDTO;
import com.hzs.sale.product.dto.ProductParamDTO;
import com.hzs.sale.product.service.IBdProductService;
import com.hzs.sale.product.service.IBdSpecsService;
import com.hzs.system.sys.dto.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/11/15 17:56
 * @Classname: IProductServiceProvider
 * @PackageName: com.hzs.sale.product.provider
 */
@Slf4j
@DubboService
public class IProductServiceProvider implements IProductServiceApi {

    @Autowired
    private IBdProductService productService;

    @Autowired
    private IBdSpecsService specsService;

    /**
     * 根据产品id 查询产品
     *
     * @param productId
     * @return
     */
    @Override
    public R<ProductDTO> getProduct(Integer productId) {
        BdProduct product = productService.getProduct(productId);
        if (product == null) {
            return R.fail("产品不存在");
        }
        ProductDTO productDTO = BeanUtil.copyProperties(product, ProductDTO.class);
        productDTO.setProductId(product.getPkId());
        return R.ok(productDTO);
    }

    @Override
    public R<List<BdProductExt>> queryProductDtoById(List<BdProductSpecs> productSpecsList, Integer pkCountry) {
        List<BdProductExt> productExtList = productService.queryProductExtById(productSpecsList, pkCountry);
        return R.ok(productExtList);
    }

    @Override
    public R<ProductDTO> getProduct(String productCode) {
        QueryWrapper<BdProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PRODUCT_CODE", productCode);
        BdProduct product = productService.getOne(queryWrapper);
        if (product == null) {
            return R.fail("产品不存在");
        }
        ProductDTO productDTO = BeanUtil.copyProperties(product, ProductDTO.class);
        productDTO.setProductId(product.getPkId());
        return R.ok(productDTO);
    }

    @Override
    public R<List<BdSpecs>> queryBdSpecsByIdList(List<Integer> idList) {
        return R.ok(specsService.queryBdSpecsByIdList(idList));
    }
    /**
     * 产品新增
     * @param productParamDTO
     * @return
     */
    @Override
    public R<String> saveProduct(ProductParamDTO productParamDTO, LoginUser loginUser) {
        ProductParams productParams = BeanUtil.copyProperties(productParamDTO, ProductParams.class);
        return R.ok(productService.saveProduct(productParams,loginUser));
    }

    /**
     * 根据产品主键修改所关联商品是下架/预售
     * @param productDTOList
     * @return
     */
    @Override
    public R<Boolean> updateWaresPreSaleStatus(List<ProductDTO> productDTOList) {
        List<BdProduct> productList = productDTOList.stream().map(a -> {
            BdProduct bdProduct = new BdProduct();
            bdProduct.setPkId(a.getProductId());
            return bdProduct;
        }).collect(Collectors.toList());
        return R.ok(productService.updateWaresPreSaleStatus(productList));
    }
}
