package com.gree.mall.miniapp.logic.promotion;

import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.plus.entity.PromotionGroup;
import com.gree.mall.miniapp.plus.entity.PromotionGroupSpec;
import com.gree.mall.miniapp.plus.entity.PromotionGroupSpecUser;
import com.gree.mall.miniapp.plus.entity.SecKillActivity;
import com.gree.mall.miniapp.plus.service.PromotionGroupService;
import com.gree.mall.miniapp.plus.service.PromotionGroupSpecService;
import com.gree.mall.miniapp.plus.service.PromotionGroupSpecUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Service
public class PromotionGroupLogic {

    @Autowired
    PromotionGroupService promotionGroupService;
    @Autowired
    PromotionGroupSpecService promotionGroupSpecService;
    @Autowired
    PromotionGroupSpecUserService promotionGroupSpecUserService;
    @Autowired
    RedisLockRegistry redisLockRegistry;

    public boolean checkPromotionGroup(String promotionGroupId){
        PromotionGroup promotionGroup = promotionGroupService.lambdaQuery()
                .eq(PromotionGroup::getPromotionGroupId,promotionGroupId)
                .eq(PromotionGroup::getStatus, true)
                .le(PromotionGroup::getStartTime, new Date())
                .ge(PromotionGroup::getEndTime, new Date()).one();
        return promotionGroup != null;
    }


    public PromotionGroupSpecUser getStock(String userId,String goodsSpecId,int num) throws InterruptedException {
        PromotionGroupSpecUser promotionGroupSpecUser = promotionGroupSpecUserService.lambdaQuery()
                .eq(PromotionGroupSpecUser::getUserId, userId)
                .eq(PromotionGroupSpecUser::getGoodsSpecId, goodsSpecId).one();
        if (promotionGroupSpecUser == null) {
            throw new RemoteServiceException("存在商品所属活动已结束");
        }
        boolean b = this.checkPromotionGroup(promotionGroupSpecUser.getPromotionGroupId());
        if(!b){
            throw new RemoteServiceException("存在商品所属活动已结束");
        }
        //处理库存
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER_STOCK + ":" + promotionGroupSpecUser.getPromotionGroupSpecId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                PromotionGroupSpec promotionGroupSpec = promotionGroupSpecService.getById(promotionGroupSpecUser.getPromotionGroupSpecId());
                if(promotionGroupSpec.getStock() < num){
                    throw new RemoteServiceException("下单失败,商品库存不足");
                }
                promotionGroupSpec.setStock(promotionGroupSpec.getStock() - num);
                //promotionGroupSpec.setSalesNum(promotionGroupSpec.getSalesNum() + num);
                promotionGroupSpec.updateById();
            }
        }finally {
            obtain.unlock();
        }

        return promotionGroupSpecUser;
    }

}
