liding
5 天以前 db628292cb9b69b901b44b146ac88c5f68e38389
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.ruoyi.account.service.impl;
 
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.dto.AccountTransferDto;
import com.ruoyi.account.mapper.AccountMapper;
import com.ruoyi.account.pojo.Account;
import com.ruoyi.account.service.AccountService;
import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
 
@AllArgsConstructor
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
 
    private AccountMapper accountMapper;
 
    @Override
    public IPage<Account> accountListPage(Page page, Account account) {
        return accountMapper.accountListPage(page, account);
    }
 
    /**
     * 流水类型 -> 收支方向
     * 0收入 +1, 1支出 -1, 3存入 +1, 4取出 -1, 2转账由 transfer() 处理
     */
    private Integer resolveDirection(String flowType) {
        if (flowType == null) {
            return null;
        }
        switch (flowType) {
            case "0":
            case "3":
                return 1;
            case "1":
            case "4":
                return -1;
            default:
                return null;
        }
    }
 
    @Override
    public boolean saveFlow(Account account) {
        if (account.getAccountId() == null) {
            throw new ServiceException("资金账户不能为空");
        }
        if (account.getAmount() == null || account.getAmount().signum() <= 0) {
            throw new ServiceException("流水金额必须大于0");
        }
        if ("2".equals(account.getFlowType())) {
            throw new ServiceException("转账请使用转账接口,以确保生成成对流水");
        }
        if (account.getDirection() == null) {
            account.setDirection(resolveDirection(account.getFlowType()));
        }
        if (account.getDirection() == null) {
            throw new ServiceException("请选择有效的流水类型");
        }
        if (account.getAccountDate() == null) {
            account.setAccountDate(new Date());
        }
        return this.save(account);
    }
 
    @Override
    public boolean updateFlow(Account account) {
        if (account.getId() == null) throw new ServiceException("流水ID不能为空");
        Account existing = this.getById(account.getId());
        if (existing == null) throw new ServiceException("资金流水不存在");
        if ("2".equals(existing.getFlowType()) || "2".equals(account.getFlowType())) {
            throw new ServiceException("转账成对流水不可单独编辑");
        }
        if (account.getAccountId() == null) throw new ServiceException("资金账户不能为空");
        if (account.getAmount() == null || account.getAmount().signum() <= 0) throw new ServiceException("流水金额必须大于0");
        Integer direction = resolveDirection(account.getFlowType());
        if (direction == null) throw new ServiceException("请选择有效的流水类型");
        account.setDirection(direction);
        return this.updateById(account);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean transfer(AccountTransferDto dto) {
        if (dto.getFromAccountId() == null || dto.getToAccountId() == null) {
            throw new ServiceException("转出/转入账户不能为空");
        }
        if (dto.getFromAccountId().equals(dto.getToAccountId())) {
            throw new ServiceException("转出与转入账户不能相同");
        }
        if (dto.getAmount() == null || dto.getAmount().signum() <= 0) {
            throw new ServiceException("转账金额必须大于0");
        }
        Date date = dto.getTransferDate() == null ? new Date() : dto.getTransferDate();
 
        Account out = new Account();
        out.setAccountDate(date);
        out.setAccountId(dto.getFromAccountId());
        out.setFlowType("2");
        out.setDirection(-1);
        out.setCounterAccountId(dto.getToAccountId());
        out.setAmount(dto.getAmount());
        out.setRemark(dto.getRemark());
 
        Account in = new Account();
        in.setAccountDate(date);
        in.setAccountId(dto.getToAccountId());
        in.setFlowType("2");
        in.setDirection(1);
        in.setCounterAccountId(dto.getFromAccountId());
        in.setAmount(dto.getAmount());
        in.setRemark(dto.getRemark());
 
        this.save(out);
        this.save(in);
        return true;
    }
}