【实际开发06】- XxxxServiceImpl 写 one , 其他直接批量赋值 -【model】

目录

1. Mode

1. IotTypeServiceImpl 基础 × 7 tips

2. 尽量依赖 IXxxService 层而非 mapper 层

3. 基础 - 新增 / 修改 抽模 ( 特殊逻辑以调用形式使用 , 添加限制 )

4. 校验 ( 抽取 ) : 公共的异常处理 ( 特别是 insert · update )

5. 校验 : ID 对应数据源是否存在 - ( 通用 )

6. 校验 : ID 对应资源是否存在被其他资源占用的情况 ( 删除时 )

7. 校验 : ID 对应 Status 是否禁用

8. 校验 : ID 对应资源 name 去重

1. 修改情况的去重相较于新增情况 : 需要考虑自身 ( 排除影响 )

9. 校验 : Pattern 数据正则校验


1. Mode


1. IotTypeServiceImpl 基础 × 7 tips

    private final IotTypeMapper iotTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveIotType(IotTypeDTO iotTypeDTO) {
        // 校验:name 是否重复
        checkNameUnity(iotTypeDTO.getName());

        IotType iotType = new IotType();
        BeanUtils.copyProperties(iotTypeDTO ,  iotType);
        iotType.setStatus(CommonConstants.STATUS_NORMAL);
        iotType.setDelFlag(CommonConstants.STATUS_NORMAL);
        iotType.setCreateTime(LocalDateTime.now());
        iotType.setUpdateTime(iotType.getCreateTime());
        baseMapper.insert(iotType);
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteIotType(Integer id) {
        // 校验:资源是否存在
        checkResource(id);

        this.removeById(id);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateIotType(IotTypeDTO iotTypeDTO) {
        // 校验:name 是否重复(修改时 , 排除自身)
        if(StringUtils.isNotBlank(iotTypeDTO.getName())){
            checkNameUnity(iotTypeDTO);
        }

        IotType iotType = new IotType();
        BeanUtils.copyProperties(iotTypeDTO ,  iotType);
        iotType.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(iotType);
        return Boolean.TRUE;
    }

    @Override
    public IotTypeVO queryIotType(Integer id) {
        IotType iotType = iotTypeMapper.selectById(id);
        IotTypeVO iotTypeVO = new IotTypeVO();
        BeanUtils.copyProperties(iotType ,  iotTypeVO);
        return iotTypeVO;
    }

    @Override
    public List<IotTypeVO> queryIotTypeList() {
        List<IotTypeVO> iotTypeVOS = iotTypeMapper.queryIotTypeList();
        return iotTypeVOS;
    }

    @Override
    public List<IotTypeVO> queryListByConditions(IotTypeDTO iotTypeDTO) {
        List<IotTypeVO> iotTypeVOS = iotTypeMapper.queryByConditions(iotTypeDTO);
        return iotTypeVOS;
    }

    @Override
    public IPage<IotTypeVO> queryPageByConditions(Page page ,  IotTypeDTO iotTypeDTO) {
        IPage<IotTypeVO> iotTypeVOIPage = iotTypeMapper.queryByConditions(page ,  iotTypeDTO);
        return iotTypeVOIPage;
    }


    /* --------------------------------------------异常处理-----------------------------------------*/

    // 校验:资源是否存在
    private void checkResource(Integer id){
        IotType iotType = baseMapper.selectById(id);
        if(null == iotType){
            throw new IotResourceException("参数Id:"+ id + "对应资源不存在 , 请核实后输入");
        }
    }

    // 校验:name 是否重复
    private void checkNameUnity(String name){
        LambdaQueryWrapper<IotType> queryWrapper =
                Wrappers.<IotType>lambdaQuery()
                        .eq(StringUtils.isNotEmpty(name) , IotType::getName , name);
        List<IotType> iotTypes = baseMapper.selectList(queryWrapper);
        if(null != iotTypes && !iotTypes.isEmpty()){
            throw new IotResourceException("参数name:" + name + "已存在 , 请核实后输入");
        }
    }

    // 校验:name 是否重复(修改时 , 排除自身重复的情况)
    private void checkNameUnity(IotTypeDTO iotTypeDTO){
        LambdaQueryWrapper<IotType> queryWrapper =
                Wrappers.<IotType>lambdaQuery()
                        .eq(StringUtils.isNotEmpty(iotTypeDTO.getName()) , IotType::getName , iotTypeDTO.getName());
        List<IotType> iotTypes = baseMapper.selectList(queryWrapper);
        // 排除自身重复的情况
        if(null != iotTypes && !iotTypes.isEmpty()){
            for (IotType iotType : iotTypes) {
                // 若非修改自身且名称还存在重复 , 则抛出异常
                if(!iotType.getId().equals(iotTypeDTO.getId()) && iotType.getName().equals(iotTypeDTO.getName())){
                    throw new IotResourceException("参数name:“" + iotTypeDTO.getName() + "”已存在 , 请核实后输入");
                }
            }
        }
    }


2. 尽量依赖 IXxxService 层而非 mapper 层

情景 : 2020.07.22

 iot项目中
     1、通过资产id获取其子节点id+自己id的接口 , 经过了impl层逻辑处理才能最终返回核实的list<Integer>
     2、然而 , 我直接通过mapper层调用相应接口 , 是直接通过xxxmapper.xml里的sql进行查询 , 少了业务逻辑上的处理
     3、结果其表观显示的是List<Integer> , 实际上返回的的实体类对象形式的数据List
     4、导致其他业务逻辑调用时出现异常

1、依赖 mapper 层 , 是可直接使用 xxxMapper.xml 里的 sql

2、依赖 Impl 是可直接获取经过业务逻辑处理的数据 , 这是 sql 无法直接做到的 ( 同时 , 可直接使用 xxxMapper.xml 里的 sql ) 。


3. 基础 - 新增 / 修改 抽模 ( 特殊逻辑以调用形式使用 , 添加限制 )


4. 校验 ( 抽取 ) : 公共的异常处理 ( 特别是 insert · update )

1、save 与 update 对应的异常校验 , 存在大量重复情况 , 可抽取

    /**
     * 检验:3d-tiles入口路劲是否重复
     *
     * @param iotTrdTilesDTO 3d-Tiles文件数据接收入参DTO
     * @return integer(Count)
     */
    private Integer checkAbbrUrl(IotTrdTilesDTO iotTrdTilesDTO) {
        LambdaQueryWrapper<IotTrdTiles> queryWrapper = Wrappers.<IotTrdTiles>lambdaQuery()
                .eq(StringUtil.isNoneBlank(iotTrdTilesDTO.getAbbrUrl()) ,  IotTrdTiles::getAbbrUrl ,  iotTrdTilesDTO.getAbbrUrl());
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer > 0) {
            throw new IotAssetStructureException("3d-tiles入口路劲重复 , 请核实后输入");
        }
        return integer;
    }

使用:

    @Override
    public Boolean updateIotTrdTiles(IotTrdTilesDTO iotTrdTilesDTO) {
        // 检验:3d-tiles入口路劲是否重复
        checkAbbrUrl(iotTrdTilesDTO);
        /** -------------------------异常处理部分:up-----------------------------*/
        IotTrdTiles iotTrdTiles = new IotTrdTiles();
        BeanUtils.copyProperties(iotTrdTilesDTO ,  iotTrdTiles);
        iotTrdTiles.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(iotTrdTiles);
        return Boolean.TRUE;
    }


5. 校验 : ID 对应数据源是否存在 - ( 通用 )

    // 校验:资源是否存在
    private void checkResource(Integer id){
        IotResources iotResources = baseMapper.selectById(id);
        if(null == iotResources){
            throw new IotResourceException("参数Id:"+ id + "对应资源不存在 , 请核实后输入");
        }
    }


6. 校验 : ID 对应资源是否存在被其他资源占用的情况 ( 删除时 )

    @Override
    public Boolean deleteIotContractModel(Integer id) {
        // 校验:资源是否存在
        IotContractModel iotContractModel = baseMapper.selectById(id);
        if (null == iotContractModel){
            throw new IotContractException("参数Id:"+ id + "对应资源不存在,请核实后输入");
        }
        // 校验:资源是否被占用(公寓)
        IotHouseDTO iotHouseDTO = new IotHouseDTO();
        iotHouseDTO.setContractModelId(id);
        R<List<IotHouseVO>> listR = remoteIotHouseService.queryListByConditions(iotHouseDTO);
        if(listR.getCode() == 0 && !listR.getData().isEmpty()){
            throw new IotContractException("当前资源公寓已被占用,请核实后输入");
        }
        // 校验:资源是否被占用(办公室)

        return this.removeById(id);
    }


7. 校验 : ID 对应 Status 是否禁用

   // 校验:资源是否存在
    private void checkResource(Integer id){
        IotResources iotResources = baseMapper.selectById(id);
        if("1".equals(iotResources.getStatus){
            throw new IotResourceException("参数Id:"+ id + "对应资源已禁用 , 请核实后输入");
        }
    }


8. 校验 : ID 对应资源 name 去重

    // 校验:TypeId + name 是否重复
    private void checkNameUnity(String name ,  Integer typeId){
        LambdaQueryWrapper<IotResources> queryWrapper =
                Wrappers.<IotResources>lambdaQuery()
                        .eq(null != name , IotResources::getName , name)
                        .eq(null != typeId , IotResources::getTypeId , typeId);
        List<IotResources> iotResources = baseMapper.selectList(queryWrapper);
        if(null != iotResources && !iotResources.isEmpty()){
            throw new IotResourceException("参数typeId + name:"+typeId+ "+" + name + " , 已同时存在 , 请核实后输入");
        }
    }

.eq(null != id, IotResources::getId , id) 即可排除自己


1. 修改情况的去重相较于新增情况 : 需要考虑自身 ( 排除影响 )


9. 校验 : Pattern 数据正则校验

    // 设备名称数据转换
    if(StringUtils.isNotBlank(eventVO.getEquipmentId())){
        // 校验:字符串转换整数
        Pattern pattern = Pattern.compile("[0-9]*");
        if(pattern.matcher(eventVO.getEquipmentId()).matches()){
            eventVO.setEquipmentName(machineMap.get(Integer.parseInt(eventVO.getEquipmentId())));
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值