package cn.iocoder.yudao.module.trade.service.foreign; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.trade.controller.admin.foreign.vo.TradePortBatchPageReqVO; import cn.iocoder.yudao.module.trade.controller.admin.foreign.vo.TradePortBatchSaveReqVO; import cn.iocoder.yudao.module.trade.dal.dataobject.foreign.TradePortBatchDO; import cn.iocoder.yudao.module.trade.dal.mysql.foreign.TradePortBatchMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*; /** * 港口入库批次 Service 实现类 * * @author mom */ @Service @Validated public class TradePortBatchServiceImpl implements TradePortBatchService { @Resource private TradePortBatchMapper batchMapper; @Override public Long createBatch(TradePortBatchSaveReqVO reqVO) { if (batchMapper.selectByCode(reqVO.getCode()) != null) { throw exception(PORT_BATCH_CODE_EXISTS); } TradePortBatchDO batch = BeanUtils.toBean(reqVO, TradePortBatchDO.class); batch.setMatchedCount(BigDecimal.ZERO); batchMapper.insert(batch); return batch.getId(); } @Override public void updateBatch(TradePortBatchSaveReqVO reqVO) { TradePortBatchDO old = batchMapper.selectById(reqVO.getId()); if (old == null) { throw exception(PORT_BATCH_NOT_EXISTS); } TradePortBatchDO sameCode = batchMapper.selectByCode(reqVO.getCode()); if (sameCode != null && !sameCode.getId().equals(reqVO.getId())) { throw exception(PORT_BATCH_CODE_EXISTS); } TradePortBatchDO update = BeanUtils.toBean(reqVO, TradePortBatchDO.class); update.setMatchedCount(old.getMatchedCount()); // 占用数不因编辑而改变 batchMapper.updateById(update); } @Override public void deleteBatch(Long id) { if (batchMapper.selectById(id) == null) { throw exception(PORT_BATCH_NOT_EXISTS); } batchMapper.deleteById(id); } @Override public TradePortBatchDO getBatch(Long id) { return batchMapper.selectById(id); } @Override public PageResult getBatchPage(TradePortBatchPageReqVO reqVO) { return batchMapper.selectPage(reqVO); } @Override public List getBatchList() { return batchMapper.selectList(); } @Override public void occupy(String code, BigDecimal count) { if (code == null || code.isEmpty() || count == null) { return; } TradePortBatchDO batch = batchMapper.selectByCode(code); if (batch == null) { throw exception(PORT_BATCH_NOT_EXISTS); } BigDecimal matched = batch.getMatchedCount() == null ? BigDecimal.ZERO : batch.getMatchedCount(); BigDecimal total = batch.getTotalCount() == null ? BigDecimal.ZERO : batch.getTotalCount(); if (matched.add(count).compareTo(total) > 0) { throw exception(PORT_BATCH_EXCEED, code); } batchMapper.updateById(new TradePortBatchDO().setId(batch.getId()).setMatchedCount(matched.add(count))); } @Override public void release(String code, BigDecimal count) { if (code == null || code.isEmpty() || count == null) { return; } TradePortBatchDO batch = batchMapper.selectByCode(code); if (batch == null) { return; } BigDecimal matched = batch.getMatchedCount() == null ? BigDecimal.ZERO : batch.getMatchedCount(); BigDecimal newMatched = matched.subtract(count); if (newMatched.compareTo(BigDecimal.ZERO) < 0) { newMatched = BigDecimal.ZERO; } batchMapper.updateById(new TradePortBatchDO().setId(batch.getId()).setMatchedCount(newMatched)); } }