package com.czg.service.product.service.impl;

import com.czg.product.enums.InOutItemEnum;
import com.czg.product.enums.InOutTypeEnum;
import com.czg.product.service.ProductRpcService;
import com.czg.product.service.ProductService;
import com.czg.product.vo.ProductStockVO;
import com.czg.product.vo.ProductVO;
import com.czg.service.RedisService;
import com.czg.service.product.mapper.ProductMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.czg.constant.CacheConstant.ADMIN_CLIENT_PRODUCT_LIST;

/**
 * 商品RPC远程调用服务接口实现
 *
 * @author tankaikai
 * @since 2025-03-01 10:19
 */
@Slf4j
@DubboService
public class ProductRpcServiceImpl implements ProductRpcService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductService productService;
    @Resource
    private RedisService redisService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySuccessSubtractStock(Long shopId, Long orderId, List<ProductStockVO> list) {
        productService.consStockByProduct(shopId, InOutTypeEnum.OUT, InOutItemEnum.ORDER_OUT, list, orderId, "订单消费");

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderCancelRecoverStock(Long shopId, Long orderId, List<ProductStockVO> list) {
        productService.consStockByProduct(shopId, InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, list, orderId, "订单取消/过期返还库存");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderRefundReturnStock(Long shopId, Long orderId, List<ProductStockVO> list) {
        productService.consStockByProduct(shopId, InOutTypeEnum.IN, InOutItemEnum.ORDER_IN, list, orderId, "订单退菜/退款返还库存");
    }

    @Override
    public List<ProductVO> listAndLowPrice(Long shopId, List<Long> productIds) {
        return productMapper.productList(shopId, productIds);
    }

    @Override
    public void cleanCategoryProduct(Long shopId, Long categoryId) {
        String key = ADMIN_CLIENT_PRODUCT_LIST + "::" + shopId + "::" + categoryId;
        redisService.del(key);
    }
}