編碼式事務管理使用例項

weixin_34249678發表於2018-12-20

前段傳入一個planIdList,每一個planID下會有一個或者多個詳細planDetailId,我們在刪除

/**
     * 刪除預防性維護計劃
     * @param ids
     * @return
     */
    public FunctionResult deletePlan(List<String> ids){
        TransactionStatus transactionStatus = transactionManager.startTransaction();
        ErrorCode ret=ErrorCode.Success;
        //刪除預防性維護計劃維護標準
        for (String planId:ids) {
            ret=deletePlan(planId);
            if(ret!=ErrorCode.Success){
                break;
            }
        }
        if(ErrorCode.Success.equals(ret)){
            transactionManager.commit(transactionStatus);
        }
        if(!ErrorCode.Success.equals(ret)){
            transactionManager.rollback(transactionStatus);
        }
        return new FunctionResult(ret);
    }
    /**
     * 刪除計劃
     * @param id
     * @return
     */
    private ErrorCode deletePlan(String id){
        SpareReplacePlanDO spareReplacePlanDO=spareReplacePlanMapper.selectByPrimaryKey(id);
        if(spareReplacePlanDO==null){
            return ErrorCode.SpareReplacePlanNotExist;
        }
        if(!isNextMonth(spareReplacePlanDO.getPlanMonth())){
            return ErrorCode.PlanNotNextMonth;
        }
        spareReplacePlanMapper.deleteByPrimaryKey(id);
        spareReplacePlanDetailMapper.deleteByPlanId(id);
        return ErrorCode.Success;
    }

相關文章