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.product.BdProduct;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.sale.product.dto.ProductDTO;
import com.hzs.sale.product.service.IBdProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@DubboService
public class IProductServiceProvider implements IProductServiceApi {

    @Autowired
    private IBdProductService iBdProductService;

    @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<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);
    }

}
