【原创】Code Demo-转账处理

AccountController.java

package com.cpto.dapp.manager.api.controller;

import com.cpto.dapp.auth.annotation.ManagerAuth;
import com.cpto.dapp.common.simple.SimpleResponseEntity;
import com.cpto.dapp.pojo.vo.AccountAssetsVO;
import com.cpto.dapp.pojo.vo.AccountLogVO;
import com.cpto.dapp.pojo.vo.PageVO;
import com.cpto.dapp.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.sql.Timestamp;

/**
 * 账户Controller
 *
 * @author sunli
 * @date 2019/04/25
 */
@Api(tags = "账户")
@RequestMapping("/users/{userId}")
@RestController
@Validated
public class AccountController extends BaseController {

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "查询用户的账目明细列表", notes = "根据用户id查询用户账户记录列表。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "int", paramType = "path", defaultValue = "1"),
            @ApiImplicitParam(name = "searchTime", value = "查询时间", dataType = "java.sql.Timestamp", paramType = "query", example = "2019-01-01 10:20:30"),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "int", paramType = "query", defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "transferTypeGroup", value = "转账类型分类(1:支付,2:收益,3:充值,4:提币)", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "fromCreatedTime", value = "创建开始时间", paramType = "query", example = "2019-01-01"),
            @ApiImplicitParam(name = "toCreatedTime", value = "创建结束时间", paramType = "query", example = "2019-01-31")
    })
    @GetMapping("/accounts/logs")
    @ManagerAuth
    public ResponseEntity<PageVO<AccountLogVO>> findAccountLogList(@PathVariable @NotNull(message = "用户id不能为空") Long userId,
                                                                   @RequestParam(required = false) Timestamp searchTime,
                                                                   @RequestParam @NotNull(message = "当前页数不能为空") @PositiveOrZero Integer page,
                                                                   @RequestParam @NotNull(message = "每页条数不能为空") @Positive Integer pageSize,
                                                                   @RequestParam(required = false) Integer transferTypeGroup,
                                                                   @RequestParam(required = false) String fromCreatedTime,
                                                                   @RequestParam(required = false) String toCreatedTime) {
        return SimpleResponseEntity.get(accountService.searchAccountLog(searchTime, page, pageSize, userId, transferTypeGroup, fromCreatedTime, toCreatedTime));
    }

    @ApiOperation(value = "取得账户资产信息", notes = "根据用户id查询账户总资产、可用资产、锁定资产和累计收益。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", dataType = "int", paramType = "path", defaultValue = "1")
    })
    @GetMapping("/assets")
    @ManagerAuth
    public ResponseEntity<AccountAssetsVO> findAssets(@PathVariable @NotNull(message = "用户id不能为空") Long userId) {
        return SimpleResponseEntity.get(accountService.findAssets(userId));
    }
}

AccountServiceImpl.java

package com.cpto.dapp.service.impl;

import com.cpto.dapp.common.util.DateUtil;
import com.cpto.dapp.common.util.LanguageUtil;
import com.cpto.dapp.common.util.ObjectUtil;
import com.cpto.dapp.constant.Constant;
import com.cpto.dapp.domain.Account;
import com.cpto.dapp.domain.LogAccount;
import com.cpto.dapp.enums.*;
import com.cpto.dapp.exception.BusinessException;
import com.cpto.dapp.exception.DataNotFoundException;
import com.cpto.dapp.pojo.dto.TransferAccountDTO;
import com.cpto.dapp.pojo.vo.AccountAssetsVO;
import com.cpto.dapp.pojo.vo.AccountLogVO;
import com.cpto.dapp.pojo.vo.PageVO;
import com.cpto.dapp.repository.AccountRepository;
import com.cpto.dapp.repository.LogAccountRepository;
import com.cpto.dapp.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 账户ServiceImpl
 *
 * @author sunli
 * @date 2019/01/15
 */
@Service
public class AccountServiceImpl extends BaseServiceImpl implements AccountService {

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private LogAccountRepository logAccountRepository;

    /**
     * 初始化账户
     *
     * @param userId 用户id
     */
    @Override
    public void initAccount(Long userId) {

        // 锁定账户
        accountRepository.save(new Account(userId, AccountTypeEnum.LOCKED.getCode()));

        // 可用账户
        accountRepository.save(new Account(userId, AccountTypeEnum.AVAILABLE.getCode()));

        // 待审核账户
        accountRepository.save(new Account(userId, AccountTypeEnum.APPROVED.getCode()));
    }

    /**
     * 转账处理
     *
     * @param transferAccountDTO 转账信息
     */
    @Override
    public void transferAccount(TransferAccountDTO transferAccountDTO) {

        /* 1.出账处理 */
        if (ObjectUtil.isNotEmpty(transferAccountDTO.getOutAccountType())) {
            /* 1.1.判断余额是否充足 */
            outAccountBeforeCheck(transferAccountDTO);

            /* 1.2.出账处理 */
            outAccount(transferAccountDTO);

            /* 1.3.记录出账日志 */
            saveOutAccountLog(transferAccountDTO);
        }

        /* 2.入账处理 */
        if (ObjectUtil.isNotEmpty(transferAccountDTO.getInAccountType())) {

            /* 2.1.入账处理 */
            inAccount(transferAccountDTO);

            /* 2.2.记录入账日志 */
            saveInAccountLog(transferAccountDTO);
        }
    }

    /**
     * 根据用户id查询账户记录列表
     *
     * @param searchTime        查询时间
     * @param page              当前页数
     * @param pageSize          每页条数
     * @param userId            用户id
     * @param transferTypeGroup 转账类型分组
     * @param fromCreatedTime   创建开始时间
     * @param toCreatedTime     创建结束时间
     * @return 账户记录列表
     */
    @Override
    public PageVO<AccountLogVO> searchAccountLog(Timestamp searchTime,
                                                 Integer page,
                                                 Integer pageSize,
                                                 Long userId,
                                                 Integer transferTypeGroup,
                                                 String fromCreatedTime,
                                                 String toCreatedTime) {

        /* 1.生成动态查询条件 */
        // 返回查询时间之前的数据
        if (ObjectUtil.isEmpty(searchTime)) {
            searchTime = DateUtil.now();
        }

        Specification<LogAccount> specification = getSQLWhere(searchTime, userId, transferTypeGroup, fromCreatedTime, toCreatedTime);

        /* 2.设置分页 */
        Sort sort = new Sort(Sort.Direction.DESC, Constant.SORT_KEY_ID);
        Pageable pageable = PageRequest.of(page, pageSize, sort);

        /* 3.进行查询 */
        Page<LogAccount> logAccountPage = logAccountRepository.findAll(specification, pageable);

        List<AccountLogVO> accountLogVOList = new ArrayList<>();
        for (LogAccount logAccount : logAccountPage) {
            accountLogVOList.add(editAccountLogVO(logAccount));
        }

        PageVO<AccountLogVO> accountLogPageVO = new PageVO();
        accountLogPageVO.setRows(accountLogVOList);
        accountLogPageVO.setTotal(logAccountPage.getTotalElements());
        accountLogPageVO.setTotalPage(logAccountPage.getTotalPages());
        accountLogPageVO.setHasNext(logAccountPage.hasNext());
        accountLogPageVO.setSearchTime(searchTime);

        return accountLogPageVO;
    }

    /**
     * 根据用户id查询账户资产信息
     *
     * @param userId 用户id
     * @return 账户资产信息
     */
    @Override
    public AccountAssetsVO findAssets(Long userId) {

        AccountAssetsVO accountAssetsVO = new AccountAssetsVO();

        Account availableAccount = findAccount(userId, AccountTypeEnum.AVAILABLE.getCode());
        Account lockedAccount = findAccount(userId, AccountTypeEnum.LOCKED.getCode());
        Account approvedAccount = findAccount(userId, AccountTypeEnum.APPROVED.getCode());

        accountAssetsVO.setAvailableAmount(availableAccount.getAmount());
        accountAssetsVO.setLockedAmount(lockedAccount.getAmount());
        accountAssetsVO.setApprovedAmount(approvedAccount.getAmount());
        accountAssetsVO.setIncomeAmountSum(sumIncomeAmount(userId));
        accountAssetsVO.setTotalAmount(accountAssetsVO.getAvailableAmount().add(accountAssetsVO.getLockedAmount()));

        return accountAssetsVO;
    }

    /**
     * 根据用户id和账户类型查询账户信息
     *
     * @param accountType 账户类型
     * @return 账户信息
     */
    @Override
    public Account findAccount(Long userId, Integer accountType) {
        Account account = accountRepository.findByUserIdAndAccountType(userId, accountType);
        if (ObjectUtil.isEmpty(account)) {
            throw new DataNotFoundException();
        }
        return account;
    }

    /**
     * 计算该用户的累计收益
     *
     * @param userId 项目id
     * @return 累计收益
     */
    public BigDecimal sumIncomeAmount(Long userId) {

        List<Integer> transferList = new ArrayList<>();
        transferList.add(TransferTypeEnum.INCOME.getCode());
        transferList.add(TransferTypeEnum.REWARD.getCode());

        List<Integer> statusList = new ArrayList<>();
        statusList.add(StatusEnum.VALID.getCode());

        return logAccountRepository.sumIncomeAmountByUserId(userId, transferList, statusList);
    }

    /**
     * 生成动态查询条件
     *
     * @param searchTime        查询时间
     * @param userId            用户id
     * @param transferTypeGroup 转账类型分组
     * @param fromCreatedTime   创建开始时间
     * @param toCreatedTime     创建结束时间
     * @return 动态查询条件
     */
    private Specification<LogAccount> getSQLWhere(Timestamp searchTime,
                                                  Long userId,
                                                  Integer transferTypeGroup,
                                                  String fromCreatedTime,
                                                  String toCreatedTime) {

        Specification<LogAccount> specification = new Specification<LogAccount>() {
            @Override
            public Predicate toPredicate(Root<LogAccount> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {

                List<Predicate> predicatesList = new LinkedList<>();

                // 返回查询时间之前的数据
                if (ObjectUtil.isNotEmpty(searchTime)) {
                    predicatesList.add(cb.lessThanOrEqualTo(root.get("createdTime"), searchTime));
                }

                // 精确查询用户id
                if (ObjectUtil.isNotEmpty(userId)) {
                    predicatesList.add(cb.equal(root.get("userId"), userId));
                }

                // 精确查询转账类型
                if (ObjectUtil.isNotEmpty(transferTypeGroup)) {

                    List<Integer> transferTypeList = getTransferType(transferTypeGroup);

                    CriteriaBuilder.In<Integer> in = cb.in(root.get("transferType"));
                    for (Integer transferType : transferTypeList) {
                        in.value(transferType);
                    }

                    predicatesList.add(in);
                }

                // 范围查询创建时间
                if (ObjectUtil.isNotEmpty(fromCreatedTime)) {
                    predicatesList.add(cb.greaterThanOrEqualTo(root.get("createdTime"), Timestamp.valueOf(DateUtil.fullFromTime(fromCreatedTime))));
                }
                if (ObjectUtil.isNotEmpty(toCreatedTime)) {
                    predicatesList.add(cb.lessThanOrEqualTo(root.get("createdTime"), Timestamp.valueOf(DateUtil.fullToTime(toCreatedTime))));
                }

                // 返回生成的条件(条件为并且的关系)
                return cb.and(predicatesList.toArray(new Predicate[predicatesList.size()]));
            }
        };

        return specification;
    }

    /**
     * 取得转账类型
     *
     * @param transferTypeGroup 转账类型分组
     * @return 转账类型
     */
    private List<Integer> getTransferType(Integer transferTypeGroup) {

        List<Integer> transferTypeList = new ArrayList<>();

        // 支付相关
        if (ObjectUtil.equals(transferTypeGroup, TransferTypeGroupEnum.PAY.getCode())) {

            // 投资项目、退出投资、释放锁定
            transferTypeList.add(TransferTypeEnum.PAY_ORDER.getCode());
            transferTypeList.add(TransferTypeEnum.CANCEL_ORDER.getCode());
            transferTypeList.add(TransferTypeEnum.RETURN.getCode());
        }
        // 收益相关
        else if (ObjectUtil.equals(transferTypeGroup, TransferTypeGroupEnum.INCOME.getCode())) {

            // 项目收益、大盘奖励
            transferTypeList.add(TransferTypeEnum.INCOME.getCode());
            transferTypeList.add(TransferTypeEnum.REWARD.getCode());
        }
        // 充值相关
        else if (ObjectUtil.equals(transferTypeGroup, TransferTypeGroupEnum.TOP_UP.getCode())) {

            // 充值
            transferTypeList.add(TransferTypeEnum.TOP_UP.getCode());
        }
        // 提币相关
        else if (ObjectUtil.equals(transferTypeGroup, TransferTypeGroupEnum.WITHDRAW.getCode())) {

            // 提币申请、提币申请-手续费、提币失败、提币失败-手续费
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_APPLICATION.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_APPLICATION_FEE.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_FAILURE.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_FAILURE_FEE.getCode());
        }
        // 全部
        else {
            transferTypeList.add(TransferTypeEnum.PAY_ORDER.getCode());
            transferTypeList.add(TransferTypeEnum.CANCEL_ORDER.getCode());
            transferTypeList.add(TransferTypeEnum.RETURN.getCode());
            transferTypeList.add(TransferTypeEnum.INCOME.getCode());
            transferTypeList.add(TransferTypeEnum.REWARD.getCode());
            transferTypeList.add(TransferTypeEnum.TOP_UP.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_APPLICATION.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_APPLICATION_FEE.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_FAILURE.getCode());
            transferTypeList.add(TransferTypeEnum.WITHDRAWAL_FAILURE_FEE.getCode());
        }

        return transferTypeList;
    }

    /**
     * 转账前检查
     *
     * @param transferAccountDTO 转账信息
     */
    private void outAccountBeforeCheck(TransferAccountDTO transferAccountDTO) {

        /* 1.取得出账前账户信息 */
        Account outAccount = findAccount(transferAccountDTO.getOutUserId(), transferAccountDTO.getOutAccountType());

        /* 2.检查余额是否充足 */
        if (outAccount.getAmount().compareTo(transferAccountDTO.getAmount()) < 0) {
            throw new BusinessException(ErrorEnum.BALANCE_NOT_ENOUGH);
        }
    }

    /**
     * 出账处理
     *
     * @param transferAccountDTO 转账信息
     */
    private void outAccount(TransferAccountDTO transferAccountDTO) {
        int rowCount = accountRepository.modifyAmount(transferAccountDTO.getOutUserId(), transferAccountDTO.getOutAccountType(), transferAccountDTO.getAmount().negate(), DateUtil.now());
        if (rowCount != 1) {
            throw new BusinessException(ErrorEnum.TRANSFER_FAILED);
        }
    }

    /**
     * 入账处理
     *
     * @param transferAccountDTO 转账信息
     */
    private void inAccount(TransferAccountDTO transferAccountDTO) {
        int rowCount = accountRepository.modifyAmount(transferAccountDTO.getInUserId(), transferAccountDTO.getInAccountType(), transferAccountDTO.getAmount(), DateUtil.now());
        if (rowCount != 1) {
            throw new BusinessException(ErrorEnum.TRANSFER_FAILED);
        }
    }

    /**
     * 保存出账记录
     *
     * @param transferAccountDTO 转账信息
     */
    private void saveOutAccountLog(TransferAccountDTO transferAccountDTO) {
        Account account = findAccount(transferAccountDTO.getOutUserId(), transferAccountDTO.getOutAccountType());
        saveAccountLog(transferAccountDTO, account, transferAccountDTO.getAmount().negate());
    }

    /**
     * 保存入账记录
     *
     * @param transferAccountDTO 转账信息
     */
    private void saveInAccountLog(TransferAccountDTO transferAccountDTO) {
        Account account = findAccount(transferAccountDTO.getInUserId(), transferAccountDTO.getInAccountType());
        saveAccountLog(transferAccountDTO, account, transferAccountDTO.getAmount());
    }

    /**
     * 保存记录
     *
     * @param transferAccountDTO 转账信息
     * @param account            账户信息
     * @param amount             转账数额
     */
    private void saveAccountLog(TransferAccountDTO transferAccountDTO, Account account, BigDecimal amount) {

        LogAccount logAccount = new LogAccount();

        logAccount.setTransferType(transferAccountDTO.getTransferType());
        logAccount.setTransferMemoZh(transferAccountDTO.getTransferMemoZh());
        logAccount.setTransferMemoEn(transferAccountDTO.getTransferMemoEn());
        logAccount.setUserId(transferAccountDTO.getInUserId());
        logAccount.setAccountId(account.getId());
        logAccount.setAccountType(account.getAccountType());
        logAccount.setAmount(amount);
        logAccount.setBalance(account.getAmount());
        logAccount.setRelationType(RelationTypeEnum.ORDER.getCode());
        logAccount.setRelationId(transferAccountDTO.getRelationId());
        logAccount.setRelationType(transferAccountDTO.getRelationType());
        logAccount.setStatus(StatusEnum.VALID.getCode());
        logAccount.setCreatedTime(DateUtil.now());

        logAccountRepository.save(logAccount);
    }

    /**
     * 编辑账户记录VO
     *
     * @param logAccount 账户记录
     * @return 账户记录VO
     */
    private AccountLogVO editAccountLogVO(LogAccount logAccount) {

        AccountLogVO accountLogVO = new AccountLogVO();

        /* 1.设置转账类型分组 */
        Integer transferTypeGroup = 0;

        // 支付相关:投资项目、退出投资、释放锁定
        if (ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.PAY_ORDER.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.PAY_ORDER.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.CANCEL_ORDER.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.RETURN.getCode())) {

            transferTypeGroup = TransferTypeGroupEnum.PAY.getCode();
        }
        // 收益相关:项目收益、大盘奖励
        else if (ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.INCOME.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.REWARD.getCode())) {

            transferTypeGroup = TransferTypeGroupEnum.INCOME.getCode();
        }
        // 充值相关:充值
        else if (ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.TOP_UP.getCode())) {

            transferTypeGroup = TransferTypeGroupEnum.TOP_UP.getCode();
        }
        // 提币相关:提币申请、提币申请-手续费、提币失败、提币失败-手续费
        else if (ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.WITHDRAWAL_APPLICATION.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.WITHDRAWAL_APPLICATION_FEE.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.WITHDRAWAL_FAILURE.getCode()) ||
                ObjectUtil.equals(logAccount.getTransferType(), TransferTypeEnum.WITHDRAWAL_FAILURE_FEE.getCode())) {

            transferTypeGroup = TransferTypeGroupEnum.WITHDRAW.getCode();
        }

        /* 2.设置其他信息 */
        accountLogVO.setId(logAccount.getId());
        accountLogVO.setAccountId(logAccount.getAccountId());
        accountLogVO.setAccountType(logAccount.getAccountType());
        accountLogVO.setAccountTypeName(AccountTypeEnum.getNameByCode(logAccount.getAccountType()));
        accountLogVO.setTransferType(logAccount.getTransferType());
        accountLogVO.setTransferTypeName(TransferTypeEnum.getNameByCode(logAccount.getTransferType()));
        accountLogVO.setTransferTypeGroup(transferTypeGroup);
        accountLogVO.setTransferTypeGroupName(TransferTypeGroupEnum.getNameByCode(transferTypeGroup));
        accountLogVO.setTransferMemo(LanguageUtil.getTextByLanguage(logAccount.getTransferMemoZh(), logAccount.getTransferMemoEn()));
        accountLogVO.setAmount(logAccount.getAmount());
        accountLogVO.setBalance(logAccount.getBalance());
        accountLogVO.setCoinKind(Constant.COIN_CPTO);
        accountLogVO.setCreatedTime(logAccount.getCreatedTime());

        return accountLogVO;
    }
}

AccountRepository.java

package com.cpto.dapp.repository;


import com.cpto.dapp.domain.Account;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * 账户Repository
 *
 * @author sunli
 * @date 2019/01/15
 */
public interface AccountRepository extends BaseRepository<Account, Long> {

    /**
     * 根据用户id查询用户的所有账户信息
     *
     * @param userId 用户id
     * @return 账户信息
     */
    List<Account> findByUserId(Long userId);

    /**
     * 根据用户id和账户类型查询账户信息
     *
     * @param userId      用户id
     * @param accountType 账户类型
     * @return 账户信息
     */
    Account findByUserIdAndAccountType(Long userId, Integer accountType);

    /**
     * 改变账户数额
     *
     * @param userId       用户id
     * @param accountType  账户类型
     * @param amount       改变数额(增加为正数,减少为负数)
     * @param modifiedTime 更新时间
     * @return 影响行数
     */
//    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true, value = "update account set amount = amount + :amount, modified_time = :modifiedTime where user_id = :userId and account_type = :accountType and amount + :amount >= 0")
    Integer modifyAmount(Long userId, Integer accountType, BigDecimal amount, Timestamp modifiedTime);
}

Account.java

package com.cpto.dapp.domain;


import com.cpto.dapp.common.constant.Constant;
import com.cpto.dapp.common.util.DateUtil;
import com.cpto.dapp.common.util.IdUtil;
import com.cpto.dapp.common.util.ObjectUtil;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;

/**
 * 账户
 *
 * @author sunli
 * @date 2019/01/15
 */
@Data
@Entity
@org.hibernate.annotations.Table(appliesTo = "account", comment = "账户")
public class Account extends Base {

    @Value("${spring.profiles.active}")
    @Ignore
    private String profilesActive;

    @Id
    @Column(columnDefinition = "bigint unsigned comment 'id'", nullable = false)
    private Long id;

    @Column(columnDefinition = "bigint unsigned comment '用户id'", nullable = false)
    private Long userId;

    @Column(columnDefinition = "int unsigned comment '账户类型(1:可用账户,2:锁定账户,3:待审核账户)'", nullable = false)
    private Integer accountType;

    @Column(columnDefinition = "decimal(18, 6) unsigned comment '数额'", nullable = false)
    private BigDecimal amount;

    /**
     * 构造方法
     */
    public Account() {

    }

    /**
     * 构造方法
     *
     * @param userId      用户id
     * @param accountType 账户类型
     */
    public Account(Long userId, Integer accountType) {
        this.id = IdUtil.generateIdByCurrentTime();
        this.userId = userId;
        this.accountType = accountType;
        this.amount = BigDecimal.ZERO;
        super.setStatus(1);
        super.setCreatedTime(DateUtil.now());

        // 设置测试数据
        if (ObjectUtil.notEquals(profilesActive, Constant.PROD)) {
            this.amount = new BigDecimal("10000000");
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,348评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,122评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,936评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,427评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,467评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,785评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,931评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,696评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,141评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,483评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,625评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,291评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,892评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,741评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,324评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,492评论 2 348