package cn.iocoder.yudao.module.product.api.storeproductattrvalue;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.api.storeproductattrvalue.dto.StoreProductAttrValueApiDTO;
import cn.iocoder.yudao.module.product.api.storeproductattrvalue.vo.StoreProductAttrValueApiVO;
import cn.iocoder.yudao.module.product.dal.dataobject.storeproductattrvalue.StoreProductAttrValueDO;
import cn.iocoder.yudao.module.product.service.storeproductattrvalue.StoreProductAttrValueService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 商品 SPU API 接口实现类
 *
 * @author LeeYan9
 * @since 2022-09-06
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class StoreProductAttrValueApiImpl implements StoreProductAttrValueApi {

    @Resource
    private StoreProductAttrValueService storeProductAttrValueService;

    @Override
    public CommonResult<List<StoreProductAttrValueApiVO>> getStoreProductAttrValueList(StoreProductAttrValueApiDTO dto) {
        List<StoreProductAttrValueDO> list = storeProductAttrValueService.list(new LambdaQueryWrapperX<StoreProductAttrValueDO>()
                .inIfPresent(StoreProductAttrValueDO::getProductId, dto.getProductIds())
                .inIfPresent(StoreProductAttrValueDO::getUnique, dto.getUniques())
        );
        List<StoreProductAttrValueApiVO> result = BeanUtil.copyToList(list, StoreProductAttrValueApiVO.class);
        return success(result);
    }

    @Override
    public CommonResult<Integer> decStockIncSales(Integer num, Long productId, String unique) {
        return success(storeProductAttrValueService.decStockIncSales(num, productId, unique));
    }

    @Override
    public CommonResult<Integer> inStockIncSales(Integer num, Long productId, String unique) {
        return success(storeProductAttrValueService.inStockIncSales(num, productId, unique));
    }

}
