| | |
| | | package com.zy.acs.manager.manager.service.impl; |
| | | |
| | | import com.zy.acs.manager.manager.mapper.StaReserveMapper; |
| | | import com.zy.acs.manager.manager.entity.StaReserve; |
| | | import com.zy.acs.manager.manager.service.StaReserveService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.zy.acs.manager.common.exception.BusinessException; |
| | | import com.zy.acs.manager.manager.entity.Sta; |
| | | import com.zy.acs.manager.manager.entity.StaReserve; |
| | | import com.zy.acs.manager.manager.entity.Task; |
| | | import com.zy.acs.manager.manager.enums.StaReserveStateType; |
| | | import com.zy.acs.manager.manager.enums.StaReserveType; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.mapper.StaMapper; |
| | | import com.zy.acs.manager.manager.mapper.StaReserveMapper; |
| | | import com.zy.acs.manager.manager.service.StaReserveService; |
| | | import com.zy.acs.manager.manager.service.StaService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Optional; |
| | | |
| | | @Service("staReserveService") |
| | | public class StaReserveServiceImpl extends ServiceImpl<StaReserveMapper, StaReserve> implements StaReserveService { |
| | | |
| | | public static final Long RESERVE_EXPIRE_TIME = 30 * 60 * 1000L; |
| | | |
| | | @Autowired |
| | | private StaService staService; |
| | | @Autowired |
| | | private StaMapper staMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public StaReserve reserveStaIn(Sta sta, Task task, Integer qty) { |
| | | qty = Optional.ofNullable(qty).orElse(1); |
| | | |
| | | // reserve sta in |
| | | if (qty < staMapper.tryReserveIn(sta.getId(), qty)) { |
| | | throw new BusinessException("Sta[" + sta.getStaNo() + "] can't reserve IN"); |
| | | } |
| | | Date now = new Date(); |
| | | |
| | | // save staReserve |
| | | StaReserve reserve = new StaReserve(); |
| | | reserve.setStaId(sta.getId()); |
| | | reserve.setTaskId(task.getId()); |
| | | reserve.setType(StaReserveType.IN.toString()); |
| | | reserve.setQty(qty); |
| | | reserve.setState(StaReserveStateType.RESERVED.toString()); |
| | | |
| | | reserve.setUniqKey(this.generateReserveUniqKey(sta.getId(), task.getId())); |
| | | reserve.setExpireTime(new Date(now.getTime() + RESERVE_EXPIRE_TIME)); |
| | | |
| | | reserve.setStatus(StatusType.ENABLE.val); |
| | | reserve.setCreateTime(now); |
| | | reserve.setUpdateTime(now); |
| | | |
| | | if (!this.save(reserve)) { |
| | | staMapper.releaseReserveIn(sta.getId(), qty); |
| | | throw new BusinessException("reserve record insert failed"); |
| | | } |
| | | |
| | | return reserve; |
| | | } |
| | | |
| | | |
| | | private String generateReserveUniqKey(Long staId, Long taskId) { |
| | | return "STA:" + staId + "-TASK:" + taskId + "-T"; |
| | | } |
| | | |
| | | } |