| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.enums.TankUsageTypeEnum; |
| | | import com.ruoyi.basic.mapper.TankInfoMapper; |
| | | import com.ruoyi.basic.pojo.TankInfo; |
| | | import com.ruoyi.basic.service.ITankInfoService; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | @Service |
| | | public class TankInfoServiceImpl extends ServiceImpl<TankInfoMapper, TankInfo> implements ITankInfoService { |
| | | |
| | | /** |
| | | * 是否启用:启用。与 tank_info.is_enabled 的默认值一致 |
| | | */ |
| | | private static final Integer TANK_ENABLED = 1; |
| | | |
| | | @Override |
| | | public IPage<TankInfo> selectTankInfoList(Page<TankInfo> page, TankInfo tankInfo) { |
| | | LambdaQueryWrapper<TankInfo> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.like(StringUtils.isNotBlank(tankInfo.getTankNo()), TankInfo::getTankNo, tankInfo.getTankNo()) |
| | | .like(StringUtils.isNotBlank(tankInfo.getCategory()), TankInfo::getCategory, tankInfo.getCategory()) |
| | | .eq(tankInfo.getIsEnabled() != null, TankInfo::getIsEnabled, tankInfo.getIsEnabled()) |
| | | .eq(tankInfo.getUsageType() != null, TankInfo::getUsageType, tankInfo.getUsageType()) |
| | | .orderByAsc(TankInfo::getId); |
| | | return this.page(page, queryWrapper); |
| | | } |
| | | |
| | | /** |
| | | * 下拉用:只返回已启用的储罐。未启用表示还没投用,不应被入库等业务选中 |
| | | */ |
| | | @Override |
| | | public List<TankInfo> selectAll() { |
| | | LambdaQueryWrapper<TankInfo> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.orderByAsc(TankInfo::getId); |
| | | queryWrapper.eq(TankInfo::getIsEnabled, TANK_ENABLED) |
| | | .orderByAsc(TankInfo::getId); |
| | | return this.list(queryWrapper); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public boolean insertTankInfo(TankInfo tankInfo) { |
| | | normalizeUsage(tankInfo); |
| | | return this.save(tankInfo); |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateTankInfo(TankInfo tankInfo) { |
| | | return this.updateById(tankInfo); |
| | | if (tankInfo.getId() == null) { |
| | | throw new ServiceException("缺少储罐id"); |
| | | } |
| | | normalizeUsage(tankInfo); |
| | | boolean updated = this.updateById(tankInfo); |
| | | if (updated) { |
| | | // usageType / tenantInfo 是允许被清空的,而 updateById 默认跳过 null 字段(FieldStrategy.NOT_NULL), |
| | | // 清空动作根本写不进库,所以这两列单独补一条更新 |
| | | updated = this.update(Wrappers.<TankInfo>lambdaUpdate() |
| | | .eq(TankInfo::getId, tankInfo.getId()) |
| | | .set(TankInfo::getUsageType, tankInfo.getUsageType()) |
| | | .set(TankInfo::getTenantInfo, tankInfo.getTenantInfo())); |
| | | } |
| | | return updated; |
| | | } |
| | | |
| | | /** |
| | | * 归一化「是否启用 / 用途 / 租客信息」三者的关系: |
| | | * <ul> |
| | | * <li>未启用:用途与租客信息一律清空 —— 启用之后才谈租赁/自用</li> |
| | | * <li>启用 + 租赁:租客信息必填</li> |
| | | * <li>启用 + 自用:租客信息清空</li> |
| | | * <li>启用 + 用途为空:视为未选择,既不校验也不清空。迁移后的存量储罐就是这个状态, |
| | | * 这样老前端不改也不会被拦,更不会把已有的租客信息抹掉</li> |
| | | * </ul> |
| | | */ |
| | | private void normalizeUsage(TankInfo tankInfo) { |
| | | if (tankInfo.getIsEnabled() == null) { |
| | | tankInfo.setIsEnabled(TANK_ENABLED); |
| | | } |
| | | if (!TANK_ENABLED.equals(tankInfo.getIsEnabled())) { |
| | | tankInfo.setUsageType(null); |
| | | tankInfo.setTenantInfo(null); |
| | | return; |
| | | } |
| | | Integer usageType = tankInfo.getUsageType(); |
| | | if (usageType == null) { |
| | | return; |
| | | } |
| | | if (TankUsageTypeEnum.LEASE.getCode().equals(usageType)) { |
| | | if (StringUtils.isBlank(tankInfo.getTenantInfo())) { |
| | | throw new ServiceException("租赁的储罐必须填写租客信息"); |
| | | } |
| | | return; |
| | | } |
| | | if (TankUsageTypeEnum.SELF_USE.getCode().equals(usageType)) { |
| | | tankInfo.setTenantInfo(null); |
| | | return; |
| | | } |
| | | throw new ServiceException("储罐用途只能是租赁或自用"); |
| | | } |
| | | |
| | | @Override |