package com.tzy.service.impl;

import com.tzy.app.domain.order.OrderListDTO;
import com.tzy.app.mapper.OrderListMapper;
import com.tzy.common.annotation.RedisLock;
import com.tzy.pojo.item.OrderList;
import com.tzy.pojo.item.Sku;
import com.tzy.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.teasoft.bee.osql.SuidRich;

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


@Service
@Slf4j
public class SkuServiceImpl implements SkuService {

    @Resource
    private SuidRich suidRich;
    @Resource
    private OrderListMapper orderListMapper;

    @Override
    public List<Sku> getShelfOnBySpuId(Long spuId) {
        Sku condition = new Sku();
        condition.setSpuId(spuId);
        condition.setStatus(201L);
        return suidRich.select(condition);
    }

    @Override
    public Sku selectOne(Integer skuId) {
        return selectSkuByIdAndStatus(skuId,201L);
    }

    @Override
    public Sku selectSkuByIdAndStatus(Integer skuId,Long status) {
        Sku condition = new Sku();
        condition.setId(skuId.longValue());
        condition.setStatus(status);
        return suidRich.selectOne(condition);
    }

    @Override
    @RedisLock(lockKey="SKU_STOCK_LOCK_+#{skuId}",waitTime=10,leaseTime = 31)
    public void updateSkuStockV2(Long skuId) {
        orderListMapper.updateSkuStockV2(skuId);
        log.info("商品库存修改,skuId:{}", skuId);
    }

    @Override
    @Deprecated
    public void updateSkuSoldStock(OrderList orderList) {
        OrderListDTO orderListDTO = new OrderListDTO();
        orderListDTO.setSpuId(orderList.getSpuId());
        orderListDTO.setQty(orderList.getQty());
        orderListDTO.setId(orderList.getId());
        //asyncAppService.updateStock(orderListDTO);
    }
}
