package com.xinelu.applet.service.goodstock.impl;

import com.xinelu.applet.service.goodstock.GoodsStockService;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.RedisDistributedLockUtils;
import com.xinelu.manage.domain.goodsOrderDetails.GoodsOrderDetails;
import com.xinelu.manage.mapper.goodsAttributeDetails.GoodsAttributeDetailsMapper;
import com.xinelu.manage.mapper.goodsOrderDetails.GoodsOrderDetailsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @Description 商品库存公共服务业务层实现类
 * @Author 纪寒
 * @Date 2022-10-24 13:57:28
 * @Version 1.0
 */
@Slf4j
@Service
public class GoodsStockServiceImpl implements GoodsStockService {

    @Resource
    private RedisDistributedLockUtils redisDistributedLockUtils;
    @Resource
    private GoodsOrderDetailsMapper goodsOrderDetailsMapper;
    @Resource
    private GoodsAttributeDetailsMapper goodsAttributeDetailsMapper;

    /**
     * 新增库存数量服务
     *
     * @param orderNo                 订单编号
     * @param goodsAttributeDetailsId 商品属性明细id
     * @param stockNum                商品数量
     */
    @Override
    public void addGoodsStockInfo(String orderNo, Long goodsAttributeDetailsId, Integer stockNum) {
        if (StringUtils.isBlank(orderNo)) {
            throw new ServiceException("订单编号不能为空！");
        }
        if (Objects.isNull(goodsAttributeDetailsId)) {
            throw new ServiceException("商品属性明细id不能为空！");
        }
        String orderNoLockKey = Constants.ADD_GOODS_STOCK_KEY + orderNo + "_" + goodsAttributeDetailsId;
        boolean tryLock = redisDistributedLockUtils.tryLock(orderNoLockKey, 5);
        if (!tryLock) {
            return;
        }
        try {
            int updateCount = goodsAttributeDetailsMapper.addGoodsStockCount(goodsAttributeDetailsId, stockNum);
            if (updateCount < 1) {
                log.info("新增库存数量失败，订单编号：{}，商品属性明细id：{}，数量数量：{}", orderNo, goodsAttributeDetailsId, stockNum);
            }
        } catch (Exception e) {
            log.error("新增库存信息失败，失败原因为 ====> {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        } finally {
            redisDistributedLockUtils.unlock(orderNoLockKey);
        }
    }

    /**
     * 减少库存数量服务
     *
     * @param orderNo                 订单编号
     * @param goodsAttributeDetailsId 商品属性明细id
     */
    @Override
    public void reduceGoodsStockInfo(String orderNo, Long goodsAttributeDetailsId) {
        if (StringUtils.isBlank(orderNo)) {
            throw new ServiceException("订单编号不能为空！");
        }
        if (Objects.isNull(goodsAttributeDetailsId)) {
            throw new ServiceException("商品属性明细id不能为空！");
        }
        GoodsOrderDetails goodsOrderDetailInfo = goodsOrderDetailsMapper.getGoodsOrderDetailInfo(orderNo, goodsAttributeDetailsId);
        if (Objects.isNull(goodsOrderDetailInfo)) {
            return;
        }
        String orderNoLockKey = Constants.REDUCE_GOODS_STOCK_KEY + orderNo + "_" + goodsAttributeDetailsId;
        boolean tryLock = redisDistributedLockUtils.tryLock(orderNoLockKey, 5);
        if (!tryLock) {
            return;
        }
        try {
            int updateCount = goodsAttributeDetailsMapper.reduceGoodsStockCount(goodsAttributeDetailsId, goodsOrderDetailInfo.getGoodsCount());
            if (updateCount < 1) {
                log.info("减少库存数量失败，订单编号：{}，商品属性明细id：{}，数量数量：{}", orderNo, goodsAttributeDetailsId, goodsOrderDetailInfo.getGoodsCount());
            }
        } catch (Exception e) {
            log.error("减少库存信息失败，失败原因为 ====> {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        } finally {
            redisDistributedLockUtils.unlock(orderNoLockKey);
        }
    }
}
