package jnpf.limsService.impl; import jnpf.audit.sdk.annotation.AuditLog; import jnpf.base.UserInfo; import jnpf.base.service.SuperServiceImpl; import jnpf.exception.DataException; import jnpf.limsEntity.LimsJieyangTuihuiEntity; import jnpf.limsMapper.LimsJieyangTuihuiMapper; import jnpf.limsService.LimsJieyangTuihuiService; import jnpf.util.UserProvider; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.Date; @Service public class LimsJieyangTuihuiServiceImpl extends SuperServiceImpl implements LimsJieyangTuihuiService { @Override @AuditLog(action = "CREATE", label = "样品退回记录新增", bizType = "JIEYANG", bizCodeExpr = "#entity.jianyanLiushuihao", targetTable = "lims_jieyang_tuihui", targetIdExpr = "#result?.id", diff = true, entityClass = LimsJieyangTuihuiEntity.class) public LimsJieyangTuihuiEntity createTuihui(LimsJieyangTuihuiEntity entity) { if (entity.getJianyanLiushuihao() == null || entity.getJianyanLiushuihao().isEmpty()) { throw new DataException("检验流水号不能为空"); } if (entity.getJieyangBanzuId() == null || entity.getJieyangBanzuId().isEmpty()) { throw new DataException("接样班组 id 不能为空"); } BigDecimal tuihui = parsePositive(entity.getTuihuiShuliang(), "退回数量"); BigDecimal ketuihui = parseOptional(entity.getKetuihuiShuliang()); if (ketuihui != null && tuihui.compareTo(ketuihui) > 0) { throw new DataException("退回数量不能大于可退回数量"); } // 结余数量:前端若未传,则按 (ketuihui - tuihui) 兜底 if ((entity.getJieyuShuliang() == null || entity.getJieyuShuliang().isEmpty()) && ketuihui != null) { entity.setJieyuShuliang(ketuihui.subtract(tuihui).toPlainString()); } if (entity.getCunruRen() == null || entity.getCunruRen().isEmpty()) { UserInfo userInfo = UserProvider.getUser(); entity.setCunruRen(userInfo.getUserId()); } if (entity.getCunruShijian() == null) { entity.setCunruShijian(new Date()); } entity.setDeleteMark(null); entity.setDeleteTime(null); entity.setDeleteUserId(null); if (!this.save(entity)) { throw new DataException("退回记录保存失败"); } return entity; } private BigDecimal parsePositive(String value, String fieldName) { if (value == null || value.isEmpty()) { throw new DataException(fieldName + "不能为空"); } try { BigDecimal v = new BigDecimal(value.trim()); if (v.signum() <= 0) { throw new DataException(fieldName + "必须大于 0"); } return v; } catch (NumberFormatException e) { throw new DataException(fieldName + "必须是数字"); } } private BigDecimal parseOptional(String value) { if (value == null || value.isEmpty()) { return null; } try { return new BigDecimal(value.trim()); } catch (NumberFormatException e) { return null; } } }