package com.neusoft.smart.pos.inventory.service;

import com.neusoft.smart.pos.inventory.constants.InventoryConstant;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsLockInfo;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsLockInfoExample;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryGoodsLockInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Component
@EnableScheduling
public class InventoryLockSchedule {
    
    @Resource
    private InventoryGoodsLockInfoMapper inventoryGoodsLockInfoMapper;
    
    private InventoryGoodsLockInfoExample example;
    
    @Autowired
    private GoodsDefaultService goodsDefaultService;
    
    @PostConstruct
    private void init() {
        example = new InventoryGoodsLockInfoExample();
        example.createCriteria().andStatusEqualTo(InventoryConstant.LOCKER_DEFAULT);
    }
    
    @Scheduled(fixedDelay = 60000)
    private void scheduleTask() {
        List<InventoryGoodsLockInfo> inventoryGoodsLockInfoList = inventoryGoodsLockInfoMapper.selectByExample(example);
        inventoryGoodsLockInfoList.forEach(lock -> goodsDefaultService.lockTimeOut(lock));
    }
    
}
