com.roncoo.pay.account.service.impl.RpAccountTransactionServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.roncoo.pay.account.service.impl.RpAccountTransactionServiceImpl.java

Source

/*
 * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.roncoo.pay.account.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roncoo.pay.account.dao.RpAccountDao;
import com.roncoo.pay.account.dao.RpAccountHistoryDao;
import com.roncoo.pay.account.entity.RpAccount;
import com.roncoo.pay.account.entity.RpAccountHistory;
import com.roncoo.pay.account.enums.AccountFundDirectionEnum;
import com.roncoo.pay.account.exception.AccountBizException;
import com.roncoo.pay.account.service.RpAccountTransactionService;
import com.roncoo.pay.common.core.enums.PublicEnum;
import com.roncoo.pay.common.core.utils.DateUtils;
import com.roncoo.pay.common.core.utils.StringUtil;
import com.roncoo.pay.trade.enums.TrxTypeEnum;

/**
 * ?service
 * www.roncoo.com
 * @authorzenghao
 */
@Service("rpAccountTransactionService")
public class RpAccountTransactionServiceImpl implements RpAccountTransactionService {

    private static final Log LOG = LogFactory.getLog(RpAccountTransactionServiceImpl.class);

    @Autowired
    private RpAccountDao rpAccountDao;
    @Autowired
    private RpAccountHistoryDao rpAccountHistoryDao;

    /**
     * ?????
     * 
     * @param userNO
     *            ?
     * @param isPessimist
     *            ??
     * @return
     */
    private RpAccount getByUserNo_IsPessimist(String userNo, boolean isPessimist) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userNo", userNo);
        map.put("isPessimist", isPessimist);
        return rpAccountDao.getByUserNo(map);
    }

    /**
     * 
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ?
     * @param requestNo
     *            ?
     * @param trxType
     *            
     * @param remark
     *            
     */
    @Transactional(rollbackFor = Exception.class)
    public RpAccount creditToAccount(String userNo, BigDecimal amount, String requestNo, String trxType,
            String remark) {

        return this.creditToAccount(userNo, amount, requestNo, null, trxType, remark);
    }

    /**
     * :?
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ?
     * @param requestNo
     *            ?
     * @param trxType
     *            
     * @param remark
     *            
     */
    @Transactional(rollbackFor = Exception.class)
    public RpAccount creditToAccount(String userNo, BigDecimal amount, String requestNo, String bankTrxNo,
            String trxType, String remark) {
        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT;
        }

        Date lastModifyDate = account.getEditTime();
        // ??0
        if (!DateUtils.isSameDayWithToday(lastModifyDate)) {
            account.setTodayExpend(BigDecimal.ZERO);
            account.setTodayIncome(BigDecimal.ZERO);
        }

        // 
        if (TrxTypeEnum.EXPENSE.name().equals(trxType)) {// 
            account.setTotalIncome(account.getTotalIncome().add(amount));

            /***** ? *******/
            if (DateUtils.isSameDayWithToday(lastModifyDate)) {
                // ?
                account.setTodayIncome(account.getTodayIncome().add(amount));
            } else {
                // ??
                account.setTodayIncome(amount);
            }
            /************************************/
        }

        String completeSett = PublicEnum.NO.name();
        String isAllowSett = PublicEnum.YES.name();

        /** ? **/
        account.setBalance(account.getBalance().add(amount));
        account.setEditTime(new Date());

        // ?
        RpAccountHistory accountHistoryEntity = new RpAccountHistory();
        accountHistoryEntity.setCreateTime(new Date());
        accountHistoryEntity.setEditTime(new Date());
        accountHistoryEntity.setIsAllowSett(isAllowSett);
        accountHistoryEntity.setAmount(amount);
        accountHistoryEntity.setBalance(account.getBalance());
        accountHistoryEntity.setRequestNo(requestNo);
        accountHistoryEntity.setBankTrxNo(bankTrxNo);
        accountHistoryEntity.setIsCompleteSett(completeSett);
        accountHistoryEntity.setRemark(remark);
        accountHistoryEntity.setFundDirection(AccountFundDirectionEnum.ADD.name());
        accountHistoryEntity.setAccountNo(account.getAccountNo());
        accountHistoryEntity.setTrxType(trxType);
        accountHistoryEntity.setId(StringUtil.get32UUID());
        accountHistoryEntity.setUserNo(userNo);

        this.rpAccountHistoryDao.insert(accountHistoryEntity);
        this.rpAccountDao.update(account);
        LOG.info("??");
        return account;
    }

    /**
     * ?
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ??
     * @param requestNo
     *            ?
     * @param trxType
     *            
     * @param remark
     *            
     */
    @Transactional(rollbackFor = Exception.class)
    public RpAccount debitToAccount(String userNo, BigDecimal amount, String requestNo, String trxType,
            String remark) {
        return this.debitToAccount(userNo, amount, requestNo, null, trxType, remark);
    }

    /**
     * ?:?
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ??
     * @param requestNo
     *            ?
     * @param trxType
     *            
     * @param remark
     *            
     */
    @Transactional(rollbackFor = Exception.class)
    public RpAccount debitToAccount(String userNo, BigDecimal amount, String requestNo, String bankTrxNo,
            String trxType, String remark) {
        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT;
        }
        // ???
        BigDecimal availableBalance = account.getAvailableBalance();

        String isAllowSett = PublicEnum.YES.name();
        String completeSett = PublicEnum.NO.name();

        if (availableBalance.compareTo(amount) == -1) {
            throw AccountBizException.ACCOUNT_SUB_AMOUNT_OUTLIMIT;
        }

        /** ?? **/
        account.setBalance(account.getBalance().subtract(amount));

        Date lastModifyDate = account.getEditTime();
        // ??0
        if (!DateUtils.isSameDayWithToday(lastModifyDate)) {
            account.setTodayExpend(BigDecimal.ZERO);
            account.setTodayIncome(BigDecimal.ZERO);
            account.setTodayExpend(amount);
        } else {
            account.setTodayExpend(account.getTodayExpend().add(amount));
        }
        account.setTotalExpend(account.getTodayExpend().add(amount));
        account.setEditTime(new Date());

        // ?
        RpAccountHistory accountHistoryEntity = new RpAccountHistory();
        accountHistoryEntity.setCreateTime(new Date());
        accountHistoryEntity.setEditTime(new Date());
        accountHistoryEntity.setIsAllowSett(isAllowSett);
        accountHistoryEntity.setAmount(amount);
        accountHistoryEntity.setBalance(account.getBalance());
        accountHistoryEntity.setRequestNo(requestNo);
        accountHistoryEntity.setBankTrxNo(bankTrxNo);
        accountHistoryEntity.setIsCompleteSett(completeSett);
        accountHistoryEntity.setRemark(remark);
        accountHistoryEntity.setFundDirection(AccountFundDirectionEnum.SUB.name());
        accountHistoryEntity.setAccountNo(account.getAccountNo());
        accountHistoryEntity.setTrxType(trxType);
        accountHistoryEntity.setId(StringUtil.get32UUID());
        accountHistoryEntity.setUserNo(userNo);
        this.rpAccountHistoryDao.insert(accountHistoryEntity);
        this.rpAccountDao.update(account);
        return account;
    }

    /**
     * 
     * 
     * @param userNo
     *            ?
     * @param freezeAmount
     *            ?
     **/

    @Transactional(rollbackFor = Exception.class)
    public RpAccount freezeAmount(String userNo, BigDecimal freezeAmount) {
        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT;
        }
        account.setEditTime(new Date());
        // ???
        if (!account.availableBalanceIsEnough(freezeAmount)) {
            // ???
            throw AccountBizException.ACCOUNT_FROZEN_AMOUNT_OUTLIMIT;
        }
        account.setUnbalance(account.getUnbalance().add(freezeAmount));
        this.rpAccountDao.update(account);
        return account;
    }

    /**
     * ? ?+?
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ??
     * @param requestNo
     *            ??
     * @param trxType
     *            
     * @param remark
     *            
     */

    @Transactional(rollbackFor = Exception.class)
    public RpAccount unFreezeAmount(String userNo, BigDecimal amount, String requestNo, String trxType,
            String remark) {
        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT;
        }

        Date lastModifyDate = account.getEditTime();
        // ??0
        if (!DateUtils.isSameDayWithToday(lastModifyDate)) {
            account.setTodayExpend(BigDecimal.ZERO);
            account.setTodayIncome(BigDecimal.ZERO);
            account.setTodayExpend(amount);
        } else {
            account.setTodayExpend(account.getTodayExpend().add(amount));
        }
        account.setTotalExpend(account.getTodayExpend().add(amount));

        // ??
        if (account.getUnbalance().subtract(amount).compareTo(BigDecimal.ZERO) == -1) {
            // ??
            throw AccountBizException.ACCOUNT_UN_FROZEN_AMOUNT_OUTLIMIT;
        }
        account.setEditTime(new Date());
        account.setBalance(account.getBalance().subtract(amount));// ?
        account.setUnbalance(account.getUnbalance().subtract(amount));// 
        account.setSettAmount(account.getSettAmount().subtract(amount));// ???

        String isAllowSett = PublicEnum.NO.name();
        String completeSett = PublicEnum.NO.name();
        // ?
        RpAccountHistory accountHistoryEntity = new RpAccountHistory();
        accountHistoryEntity.setCreateTime(new Date());
        accountHistoryEntity.setEditTime(new Date());
        accountHistoryEntity.setIsAllowSett(isAllowSett);
        accountHistoryEntity.setAmount(amount);
        accountHistoryEntity.setBalance(account.getBalance());
        accountHistoryEntity.setRequestNo(requestNo);
        accountHistoryEntity.setIsCompleteSett(completeSett);
        accountHistoryEntity.setRemark(remark);
        accountHistoryEntity.setFundDirection(AccountFundDirectionEnum.SUB.name());
        accountHistoryEntity.setAccountNo(account.getAccountNo());
        accountHistoryEntity.setTrxType(trxType);
        accountHistoryEntity.setUserNo(userNo);
        this.rpAccountHistoryDao.insert(accountHistoryEntity);
        this.rpAccountDao.update(account);
        return account;
    }

    /**
     *  ?
     * 
     * @param userNo
     *            ?
     * @param amount
     *            ??
     * @param requestNo
     *            ??
     * @param trxType
     *            
     * @param remark
     *            
     */

    @Transactional(rollbackFor = Exception.class)
    public RpAccount unFreezeSettAmount(String userNo, BigDecimal amount) {
        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT;
        }

        Date lastModifyDate = account.getEditTime();
        // ??0
        if (!DateUtils.isSameDayWithToday(lastModifyDate)) {
            account.setTodayExpend(BigDecimal.ZERO);
            account.setTodayIncome(BigDecimal.ZERO);
        }

        // ??
        if (account.getUnbalance().subtract(amount).compareTo(BigDecimal.ZERO) == -1) {
            // ??
            throw AccountBizException.ACCOUNT_UN_FROZEN_AMOUNT_OUTLIMIT;
        }
        account.setEditTime(new Date());
        account.setUnbalance(account.getUnbalance().subtract(amount));// 

        this.rpAccountDao.update(account);
        return account;
    }

    /**
     * ????
     * 
     * @param userNo
     *            ?
     * @param collectDate
     *            
     * @param riskDay
     *            
     * @param totalAmount
     *            ??
     * 
     */
    @Transactional(rollbackFor = Exception.class)
    public void settCollectSuccess(String userNo, String collectDate, int riskDay, BigDecimal totalAmount) {

        LOG.info("==>settCollectSuccess");
        LOG.info(String.format("==>userNo:%s, collectDate:%s, riskDay:%s", userNo, collectDate, riskDay));

        RpAccount account = this.getByUserNo_IsPessimist(userNo, true);
        if (account == null) {
            throw AccountBizException.ACCOUNT_NOT_EXIT.newInstance("?,?{%s}", userNo)
                    .print();
        }
        // ??
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountNo", account.getAccountNo());
        params.put("statDate", collectDate);
        params.put("riskDay", riskDay);
        rpAccountHistoryDao.updateCompleteSettTo100(params);

        // ??
        account.setSettAmount(account.getSettAmount().add(totalAmount));
        rpAccountDao.update(account);
        LOG.info("==>settCollectSuccess<==");
    }
}