package com.sckw.product.dubbo;

import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.response.HttpResult;
import com.sckw.product.api.dubbo.GoodsInfoService;
import com.sckw.product.api.model.GoodsDetail;
import com.sckw.product.api.model.KwpGoods;
import com.sckw.product.service.KwpGoodsService;
import com.sckw.redis.constant.RedisConstant;
import com.sckw.redis.utils.RedissonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @desc: 商品信息dubbo接口
 * @author: yzc
 * @date: 2023-07-13 14:17
 */
@DubboService(group = "design", version = "1.0.0")
@Slf4j
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Autowired
    private KwpGoodsService kwpGoodsService;

    @Override
    public GoodsDetail getDetailById(Long id) {
        return BeanUtils.copyProperties(kwpGoodsService.detail(id), GoodsDetail.class);
    }

    @Override
    public KwpGoods getGoodsById(Long id) {
        return BeanUtils.copyProperties(kwpGoodsService.selectById(id), KwpGoods.class);
    }

    @Override
    public Map<Long, KwpGoods> getGoodsByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<KwpGoods> list = BeanUtils.copyToList(kwpGoodsService.selectByIds(ids), KwpGoods.class);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(KwpGoods::getId, e -> e, (k1, k2) -> k1));
    }

    @Override
    public HttpResult updateGoodsAmount(Long id, BigDecimal amount) {
        String lockKey = String.format(RedisConstant.GOODS_UPDATE_AMOUNT_KEY, id);
        if (Boolean.FALSE.equals(RedissonUtils.tryLock(lockKey, 10L, 30L))) {
            return HttpResult.error("业务繁忙，请稍后再试!");
        }
        try {
            com.sckw.product.model.KwpGoods goods = kwpGoodsService.getOnShelfGoodsById(id);
            if (Objects.isNull(goods)) {
                return HttpResult.error("商品不存在!");
            }
            BigDecimal goodsAmount = goods.getAmount();
            if (goods.getAmount().compareTo(amount) < 0) {
                return HttpResult.error("商品库存不足，请联系供应方!");
            }
            goods.setAmount(goodsAmount.subtract(amount))
                    .setPerformedAmount(Objects.nonNull(goods.getPerformedAmount()) ? goods.getPerformedAmount().add(amount) : amount);
            kwpGoodsService.updateById(goods);
        } finally {
            RedissonUtils.unlock(lockKey);
        }
        return HttpResult.ok();
    }

    @Override
    public Boolean associateManager(Long userId) {
        Long count = kwpGoodsService.getCountByManager(userId);
        return Objects.nonNull(count) && count > 0L;
    }

    @Override
    public Boolean associateSupplyEnt(Long entId) {
        Long count = kwpGoodsService.getCountBySupplyEnt(entId);
        return Objects.nonNull(count) && count > 0L;
    }
}
