1. 程式人生 > >設定收貨地址預設介面思路

設定收貨地址預設介面思路

控制層

 @ApiOperation(value = "買家收貨地址設定預設介面", notes = "買家收貨地址設定預設業務", produces = "application/json")
    @PostMapping("/setDefault")
    public BaseResponse<Map<String, Object>> setDefault(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                                                        @RequestBody MemberAddressSetDefaultRequest memberAddressSetDefaultRequest) {

        ServiceResult<Boolean> serviceResult = memberAddressService.defaultMemberAddress(
            memberAddressSetDefaultRequest.getId(), memberAddressSetDefaultRequest.getMemberId());

        if (!serviceResult.getSuccess()) {
            return new BaseResponse<>(false, null, serviceResult.getMessage());
        }
        return new BaseResponse<>(true, null, null);
    }

這個介面只用把服務層的事務處理好就行,確保這個狀態改為預設後其他狀態要改為非預設狀態才能提交成功,所以其他邏輯就省略了

model層

 public boolean defaultMemberAddress(Integer id, Integer memberId) {
        // 事務管理
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            // 引數校驗。。這個步驟如果切面處理了引數校驗的話可以不寫了
            if (id == null || id == 0) {
                throw new BusinessException("地址id不能為空,請重試!");
            }

            MemberAddress memberAddress = new MemberAddress();
            // 設為預設   狀態1就是預設狀態,更新的話要傳入id不然不知道是誰的地址
            memberAddress.setState(MemberAddress.STATE_1);
            memberAddress.setId(id);
            int count = memberAddressWriteDao.update(memberAddress);
            if (count == 0) {
                throw new BusinessException("收貨地址更新失敗,請重試!");
            }

            // 更新該使用者其他地址為非預設
            memberAddressWriteDao.updateNotDefByMId(id, memberId);
			//事務處理
            transactionManager.commit(status);
            return true;
        } catch (BusinessException be) {
            transactionManager.rollback(status);
            throw be;
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
    }