关于实际业务中的请求接口使用 2

关于上篇中提到的在Service层进行业务逻辑的编写过于简单
因此按照实际应用的逻辑进行了修改
根据传入的对象中属性,判断各种不同情况,分别进行处理

public Result saveAndUpdateAndDelete(SettingAndDetails settingAndDetails){

        StringBuffer stringBuffer = new StringBuffer();

        SysTableSettingDTO sysTableSettingDTO = settingAndDetails.getSysTableSettingDTO();
        SysTableSetting sysTableSetting = sysTableSettingMapStruct.toEntity(sysTableSettingDTO);

        List<SysTableSettingDetailsDTO> list = settingAndDetails.getSysTableSettingDetailsDTOList();
        List<SysTableSettingDetails> list1 = sysTableSettingDetailsMapStruct.toEntity(list);

        List<SysTableSettingDetailsDTO> detailsDTOListDeleted = settingAndDetails.getDetailsDTOListDeleted();
        List<SysTableSettingDetails> deleteList = sysTableSettingDetailsMapStruct.toEntity(detailsDTOListDeleted);

        if (sysTableSetting.getId()==null || sysTableSetting.getId()==0){
            //如果id为null或0就插入Setting
            Integer LastId;
            List<SysTableSetting> sysTableSettings = sysTableSettingMapper.selectAll();
            if (sysTableSettings!=null && sysTableSettings.size()>0){
                LastId = sysTableSettings.get(sysTableSettings.size() - 1).getId()+1;
            }else {
                LastId = 1;
            }
            sysTableSetting.setId(LastId);
            sysTableSettingMapper.insertSelective(sysTableSetting);//插入

            if (list1!=null && list.size()>0){
                //如果存在details就同时插入Details
                for (int i = 0; i < list1.size(); i++) {
                    List<SysTableSettingDetails> list3 = sysTableSettingDetailsMapper.selectAll();
                    Integer LastIdDetail;
                    if (list3!=null && list3.size()>0){
                        LastIdDetail = list3.get(list3.size()-1).getId()+1;
                    }else {
                        LastIdDetail = 1;
                    }
                    list1.get(i).setId(LastIdDetail);
                    list1.get(i).setParentId(sysTableSetting.getId());
                    sysTableSettingDetailsMapper.insertSelective(list1.get(i));//插入
                }
            }

        }else {
            //这里注意,如果除了id剩下的字段全部为null更新时就会出现sql语句错误!
            sysTableSettingMapper.updateByPrimaryKeySelective(sysTableSetting);//更新

            if (list1 != null && list1.size()>0){
                for (int i = 0; i < list1.size(); i++){
                    if (list1.get(i).getId()==null || list1.get(i).getId()==0){
                        List<SysTableSettingDetails> list3 = sysTableSettingDetailsMapper.selectAll();
                        Integer LastIdDetail;
                        if (list3!=null && list3.size()>0){
                            LastIdDetail = list3.get(list3.size()-1).getId()+1;
                        }else {
                            LastIdDetail = 1;
                        }
                        list1.get(i).setId(LastIdDetail);
                        list1.get(i).setParentId(sysTableSetting.getId());
                        sysTableSettingDetailsMapper.insertSelective(list1.get(i));
                    }else {
                        sysTableSettingDetailsMapper.updateByPrimaryKeySelective(list1.get(i));
                    }
                }
            }
        }

        if (deleteList!=null && deleteList.size()>0){
            for (int i = 0; i < deleteList.size(); i++) {
                sysTableSettingDetailsMapper.deleteByPrimaryKey(deleteList.get(i));
            }
        }

        return new Result();
    }