package com.hzs.sale.product.provider;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.*;

@Slf4j
@DubboService
public class IProductServiceProvider implements IProductServiceApi {

    @Autowired
    private IBdProductService iBdProductService;
    @Autowired
    private IBdSpecsService iBdSpecsService;

    /**
     * 根据产品id 查询产品
     *
     * @param productId
     * @return
     */
    @Override
    public R<ProductDTO> getProduct(Integer productId) {
        BdProduct product = iBdProductService.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 = iBdProductService.queryProductExtById(productSpecsList, pkCountry);
        return R.ok(productExtList);
    }

    @Override
    public R<ProductDTO> getProduct(String productCode) {
        LambdaQueryWrapper<BdProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdProduct::getProductCode, productCode);
        BdProduct product = iBdProductService.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(iBdSpecsService.queryBdSpecsByIdList(idList));
    }

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

}
