主表与多个子表的保存与更新


/**
 * 质量检查计划
 *
 * @author build code generator
 * @date 2023-04-14 14:56:41
 */
@Slf4j
@Service
public class InspectPlanServiceImpl implements InspectPlanService {

    @Resource
    private InspectPlanMapper inspectPlanMapper;

    @Resource
    private QualityInspectPlanMapper qualityInspectPlanMapper;
    @Resource
    private QualityInspectPlanInfoMapper qualityInspectPlanInfoMapper;
    @Override
    public Page getPage(InspectPlanListDTO dto){
        //return inspectPlanMapper.getPage(dto);
        if (ObjectUtil.isNotEmpty(dto.getWriteTimeEnd())) {
            dto.setWriteTimeEnd(dto.getWriteTimeEnd().plusDays(1L));
        }
        if (ObjectUtil.isNotEmpty(dto.getPlanDateEnd())) {
            dto.setPlanDateEnd(dto.getPlanDateEnd().plusDays(1L));
        }
        Page page = inspectPlanMapper.getPage(dto);
        return page;
    }
    @Override
    public List getList(InspectPlanListDTO dto){
        List list = inspectPlanMapper.getList(dto);
        return list;
    }


    /**
    * 详情
    * @param dto 入参
    * @return
    */
    @SneakyThrows
    @Override
    public InspectPlanDetailVO getInfo(InspectPlanDetailDTO dto){
        if (Objects.isNull(dto)) {
            new IllegalStateException("参数异常");
        }
        Long mainColumn = dto.getId();
        if(Objects.isNull(mainColumn)){
            new IllegalStateException("参数异常");
        }
        InspectPlanDetailVO mainTableVO = new InspectPlanDetailVO();
        QualityInspectPlan mainTableEntity = qualityInspectPlanMapper.selectOne(Wrappers.lambdaQuery().eq(QualityInspectPlan::getId,mainColumn));
        if(Objects.isNull(mainTableEntity)) return null;
        BeanUtil.copyProperties(mainTableEntity,mainTableVO);
        List qualityInspectPlanInfo = qualityInspectPlanInfoMapper.selectList(Wrappers.lambdaQuery().eq(QualityInspectPlanInfo::getInspectPlanId,mainTableEntity.getId() ).eq(QualityInspectPlanInfo::getInspectPlanNum,mainTableEntity.getInspectPlanNum() ));
        List qualityInspectPlanInfoList = new ArrayList<>();
        for (QualityInspectPlanInfo iterm : qualityInspectPlanInfo) {
            QualityInspectPlanInfoVO tempVO = new QualityInspectPlanInfoVO();
            BeanUtil.copyProperties(iterm,tempVO);
            qualityInspectPlanInfoList.add(tempVO);
        }
        mainTableVO.setQualityInspectPlanInfo(qualityInspectPlanInfoList);
        return mainTableVO;
    }
    /**
    * 保存
    * @param dto
    */
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    @Override
    public InspectPlanDetailVO save(InspectPlanDetailDTO dto) {
        if (Objects.isNull(dto)) {
            throw new IllegalStateException("参数异常");
        }
        LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(QualityInspectPlan::getPlanDate,dto.getPlanDate());
        if (qualityInspectPlanMapper.exists(queryWrapper)) {
            Assert.isTrue(false, dto.getPlanDate() + "已有计划,请重新编辑");
        }
        //创建主表
        QualityInspectPlan mainEntity = new QualityInspectPlan();
        BeanUtil.copyProperties(dto,mainEntity);
        // 制单人
        BuildUser user = SecurityUtils.getUser();
        mainEntity.setWriteUser(StrUtil.toString(user.getId()));
        // 制单时间
        mainEntity.setWriteTime(LocalDateTime.now());
        //主表保存
        qualityInspectPlanMapper.insert(mainEntity);
        List qualityInspectPlanInfoList = new ArrayList<>();

        List qualityInspectPlanInfoDTOList = dto.getQualityInspectPlanInfo();
        if (CollectionUtil.isNotEmpty(qualityInspectPlanInfoDTOList)) {
            for (QualityInspectPlanInfoDTO tempDTO : qualityInspectPlanInfoDTOList) {
                QualityInspectPlanInfo temp = new QualityInspectPlanInfo();
                BeanUtil.copyProperties( tempDTO,temp);
                //关联字段
                temp.setInspectPlanId(mainEntity.getId());
                //关联字段
                temp.setInspectPlanNum(mainEntity.getInspectPlanNum());
                qualityInspectPlanInfoList.add(temp);
            }
        }
        if (CollectionUtil.isNotEmpty(qualityInspectPlanInfoList)&&qualityInspectPlanInfoList.size()>0) {
            for (QualityInspectPlanInfo temp : qualityInspectPlanInfoList) {
                 qualityInspectPlanInfoMapper.insert(temp);
            }
        }
        InspectPlanDetailDTO query = new InspectPlanDetailDTO();
        query.setId(mainEntity.getId());
        return this.getInfo(query);
    }
    /**
    * 更新
    * @param dto
    */
    @Transactional(rollbackFor = Exception.class)
    @SneakyThrows
    @Override
    public void update(InspectPlanDetailDTO dto) {
        if (Objects.isNull(dto)) {
            throw new IllegalStateException("参数异常");
        }
        QualityInspectPlan mainEntity = new QualityInspectPlan();
        BeanUtil.copyProperties(dto,mainEntity);
        if (Objects.nonNull(mainEntity.getId())) {
            qualityInspectPlanMapper.updateById(mainEntity);
        } else {
            qualityInspectPlanMapper.insert(mainEntity);
        }
        List qualityInspectPlanInfoDTO = dto.getQualityInspectPlanInfo();
        qualityInspectPlanInfoMapper.delete(Wrappers.lambdaQuery().eq(QualityInspectPlanInfo::getInspectPlanId,mainEntity.getId()).eq(QualityInspectPlanInfo::getInspectPlanNum,mainEntity.getInspectPlanNum()));
        if(CollectionUtil.isNotEmpty(qualityInspectPlanInfoDTO)){
            for (QualityInspectPlanInfoDTO tempDTO : qualityInspectPlanInfoDTO) {
                QualityInspectPlanInfo entity = new QualityInspectPlanInfo();
                BeanUtil.copyProperties(tempDTO,entity);
                entity.setInspectPlanId(mainEntity.getId());
                entity.setInspectPlanNum(mainEntity.getInspectPlanNum());
                qualityInspectPlanInfoMapper.insert(entity);
            }
        }
    }
    /**
     * 删除
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @SneakyThrows
    @Override
    public void deleteMultiTables(InspectPlanDetailDTO dto) {
        if (Objects.isNull(dto.getId()))
            throw new IllegalStateException("参数异常");
        QualityInspectPlan mainTableEntity = qualityInspectPlanMapper.selectOne(Wrappers.lambdaQuery().eq(QualityInspectPlan::getId,dto.getId()));
        if (Objects.isNull(mainTableEntity))
            throw new IllegalStateException("参数异常");
        List qualityInspectPlanInfo = qualityInspectPlanInfoMapper.selectList(Wrappers.lambdaQuery().eq(QualityInspectPlanInfo::getInspectPlanId,mainTableEntity.getId() ).eq(QualityInspectPlanInfo::getInspectPlanNum,mainTableEntity.getInspectPlanNum() ));
        Assert.isFalse(CollectionUtil.isNotEmpty(qualityInspectPlanInfo),"该日期有计划任务,不可删除。");
        //删除主表数据
        qualityInspectPlanMapper.deleteById(mainTableEntity.getId());
        if(StringUtils.isNotBlank( mainTableEntity.getInspectPlanNum())){
            qualityInspectPlanInfoMapper.delete(Wrappers.lambdaQuery().eq(QualityInspectPlanInfo::getInspectPlanId,mainTableEntity.getId() ).eq(QualityInspectPlanInfo::getInspectPlanNum,mainTableEntity.getInspectPlanNum() ));
        }
    }
    /**
     * 批量删除
     * @param ids 主键数组
     */
    @Transactional(rollbackFor = Exception.class)
    @SneakyThrows
    @Override
    public void deleteBatch(Long[] ids){
        if (ArrayUtils.isEmpty(ids))
            throw new IllegalStateException("参数异常");
        for (Long id : ids) {
            //查询主表数据
            QualityInspectPlan mainTableEntity = qualityInspectPlanMapper.selectById(id);
            if (Objects.isNull(mainTableEntity))
                throw new IllegalStateException("参数异常");
            //删除主表数据
            if (Objects.nonNull(mainTableEntity.getId())) {
                qualityInspectPlanMapper.deleteById(mainTableEntity.getId());
            }
            if( StringUtils.isNotBlank(mainTableEntity.getInspectPlanNum())){
                qualityInspectPlanInfoMapper.delete(Wrappers.lambdaQuery().eq(QualityInspectPlanInfo::getInspectPlanId,mainTableEntity.getId() ).eq(QualityInspectPlanInfo::getInspectPlanNum,mainTableEntity.getInspectPlanNum() ));
            }
        }
    }

}

下面是DTO层:

@Data
@ApiModel(value = "quality_inspect_plan传输对象")
public class InspectPlanDetailDTO extends BasePage {

    /**
     * 主键
     */
    @NotNull(groups = {UpdateGroup.class, HoldUpdateGroup.class}, message = "主键不能为空")
    @ApiModelProperty(value = "主键")
    private Long id;
    /**
     * 编码
     */
    @ApiModelProperty(value = "编码")
    private String inspectPlanNum;
    /**
     * 项目编码
     */
    @ApiModelProperty(value = "项目编码")
    private String projectNum;
    /**
     * 所属部门编号
     */
    @ApiModelProperty(value = "所属部门编号")
    private String deptNum;
    /**
     * 计划日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone="GMT+8")
//    @NotNull(groups ={UpdateGroup.class, SaveGroup.class, HoldGroup.class, HoldUpdateGroup.class}, message = "计划日期不能为空")
    @ApiModelProperty(value = "计划日期")
    private LocalDate planDate;
    /**
     * 制单人
     */
    @ApiModelProperty(value = "制单人")
    private String writeUser;
    /**
     * 制单部门编号
     */
    @ApiModelProperty(value = "制单部门编号")
    private String writeDeptNum;
    /**
     * 制单时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @ApiModelProperty(value = "制单时间")
    private LocalDateTime writeTime;
    /**
     * 审批状态;01-未提交,02-运行中,03-流程正常结束,05-流程异常
     */
    @ApiModelProperty(value = "审批状态;01-未提交,02-运行中,03-流程正常结束,05-流程异常")
    private String applicationStatus;
    /**
     * 暂存状态;0-暂存,1-已保存
     */
    @ApiModelProperty(value = "暂存状态;0-暂存,1-已保存")
    private String holdStatus;
    /**
     * 是否启用;1-启用,0-未启用
     */
    @ApiModelProperty(value = "是否启用;1-启用,0-未启用")
    private Boolean enable;
    /**
     * 删除标识;0-未删除,1-已删除
     */
    @ApiModelProperty(value = "删除标识;0-未删除,1-已删除")
    private String delFlag;
    /**
     * 版本号
     */
    @ApiModelProperty(value = "版本号")
    private Integer version;
    /**
     * 创建人编码
     */
    @ApiModelProperty(value = "创建人编码")
    private String createBy;
    @ApiModelProperty(value = "创建人编码Name")
    private String createByName;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;
    /**
     * 更新人编码
     */
    @ApiModelProperty(value = "更新人编码")
    private String updateBy;
    @ApiModelProperty(value = "更新人编码Name")
    private String updateByName;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;
    /**
     * 租户
     */
    @ApiModelProperty(value = "租户")
    private Long tenantId;
    /**
     * quality_inspect_plan_info
     */
    @ApiModelProperty(value = "quality_inspect_plan_info")
    private List qualityInspectPlanInfo;
}



@Data
@ApiModel(value = "quality_inspect_plan_info传输对象")
public class QualityInspectPlanInfoDTO extends BasePage {

    /**
     * 主键
     */
    @NotNull(groups = {UpdateGroup.class, HoldUpdateGroup.class}, message = "主键不能为空")
    @ApiModelProperty(value = "主键")
    private Long id;
    /**
     * 检查计划id
     */
    @ApiModelProperty(value = "检查计划id")
    private Long inspectPlanId;
    /**
     * 检查计划编码
     */
    @ApiModelProperty(value = "检查计划编码")
    private String inspectPlanNum;

    /**
     * 检查内容id
     */
    @ApiModelProperty(value="检查内容id")
    private Long inspectInfoId;

    /**
     * 计划日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone="GMT+8")
    @ApiModelProperty(value = "计划日期")
    private LocalDate planDate;

    /**
     * 推送标识;0-手动,1-自动
     */
    @ApiModelProperty(value = "推送标识;0-手动,1-自动")
    private Boolean pushFlag;
    /**
     * wbs分项id
     */
    @ApiModelProperty(value = "wbs分项id")
    private Long wbsId;
    /**
     * wbs编号
     */
    @ApiModelProperty(value = "wbs编号")
    private String wbsNum;
    /**
     * 工序id
     */
    @ApiModelProperty(value = "工序id")
    private Long processId;
    /**
     * 控制标准
     */
    @ApiModelProperty(value = "控制标准")
    private String controlStandard;
    /**
     * 质量通病
     */
    @ApiModelProperty(value = "质量通病")
    private String commonProblem;
    /**
     * 问题等级
     */
    @ApiModelProperty(value = "问题等级")
    private String problemLevel;
    /**
     * 整改时限(天)
     */
    @ApiModelProperty(value = "整改时限(天)")
    private Integer rectifyLimit;
    /**
     * 控制项
     */
    @ApiModelProperty(value = "控制项")
    private String controlItem;
    /**
     * 检测方法
     */
    @ApiModelProperty(value = "检测方法")
    private String checkMethod;
    /**
     * 检测频率
     */
    @ApiModelProperty(value = "检测频率")
    private String checkFrequency;
    /**
     * 频次
     */
    @ApiModelProperty(value = "频次")
    private String frequency;
    /**
     * 检验批
     */
    @ApiModelProperty(value = "检验批")
    private String inspectionLot;
    /**
     * 检查标识;0-未检查,1-已检查
     */
    @ApiModelProperty(value = "检查标识;0-未检查,1-已检查")
    private Boolean inspectionFlag;
    /**
     * 未检查原因
     */
    @ApiModelProperty(value = "未检查原因")
    private String uninspectedReason;
    /**
     * 责任人
     */
    @ApiModelProperty(value = "责任人")
    private String principal;
    /**
     * 审批状态;01-未提交,02-运行中,03-流程正常结束,05-流程异常
     */
    @ApiModelProperty(value = "审批状态;01-未提交,02-运行中,03-流程正常结束,05-流程异常")
    private String applicationStatus;
    /**
     * 暂存状态;0-暂存,1-已保存
     */
    @ApiModelProperty(value = "暂存状态;0-暂存,1-已保存")
    private String holdStatus;
    /**
     * 是否启用;1-启用,0-未启用
     */
    @ApiModelProperty(value = "是否启用;1-启用,0-未启用")
    private Boolean enable;
    /**
     * 删除标识;0-未删除,1-已删除
     */
    @ApiModelProperty(value = "删除标识;0-未删除,1-已删除")
    private String delFlag;
    /**
     * 版本号
     */
    @ApiModelProperty(value = "版本号")
    private Integer version;
    /**
     * 创建人编码
     */
    @ApiModelProperty(value = "创建人编码")
    private String createBy;
    @ApiModelProperty(value = "创建人编码Name")
    private String createByName;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;
    /**
     * 更新人编码
     */
    @ApiModelProperty(value = "更新人编码")
    private String updateBy;
    @ApiModelProperty(value = "更新人编码Name")
    private String updateByName;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;
    /**
     * 租户
     */
    @ApiModelProperty(value = "租户")
    private Long tenantId;
}

你可能感兴趣的:(java,数学建模,mybatis)