云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cn.iocoder.yudao.module.erp.service.finance;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.account.ErpAccountImportExcelVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.account.ErpAccountImportReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.account.ErpAccountImportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.account.ErpAccountPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.account.ErpAccountSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpAccountDO;
import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpAccountMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;
 
/**
 * ERP 结算账户 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ErpAccountServiceImpl implements ErpAccountService {
 
    @Resource
    private ErpAccountMapper accountMapper;
 
    @Override
    public Long createAccount(ErpAccountSaveReqVO createReqVO) {
        // 插入
        ErpAccountDO account = BeanUtils.toBean(createReqVO, ErpAccountDO.class);
        accountMapper.insert(account);
        // 返回
        return account.getId();
    }
 
    @Override
    public void updateAccount(ErpAccountSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountExists(updateReqVO.getId());
        // 更新
        ErpAccountDO updateObj = BeanUtils.toBean(updateReqVO, ErpAccountDO.class);
        accountMapper.updateById(updateObj);
    }
 
    @Override
    public void updateAccountDefaultStatus(Long id, Boolean defaultStatus) {
        // 1. 校验存在
        validateAccountExists(id);
 
        // 2.1 如果开启,则需要关闭所有其它的默认
        if (defaultStatus) {
            ErpAccountDO account = accountMapper.selectByDefaultStatus();
            if (account != null) {
                accountMapper.updateById(new ErpAccountDO().setId(account.getId()).setDefaultStatus(false));
            }
        }
        // 2.2 更新对应的默认状态
        accountMapper.updateById(new ErpAccountDO().setId(id).setDefaultStatus(defaultStatus));
    }
 
    @Override
    public void deleteAccount(Long id) {
        // 校验存在
        validateAccountExists(id);
        // 删除
        accountMapper.deleteById(id);
    }
 
    private void validateAccountExists(Long id) {
        if (accountMapper.selectById(id) == null) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
    }
 
    @Override
    public ErpAccountDO getAccount(Long id) {
        return accountMapper.selectById(id);
    }
 
    @Override
    public ErpAccountDO validateAccount(Long id) {
        ErpAccountDO account = accountMapper.selectById(id);
        if (account == null) {
            throw exception(ACCOUNT_NOT_EXISTS);
        }
        if (CommonStatusEnum.isDisable(account.getStatus())) {
            throw exception(ACCOUNT_NOT_ENABLE, account.getName());
        }
        return account;
    }
 
    @Override
    public List<ErpAccountDO> getAccountListByStatus(Integer status) {
        return accountMapper.selectListByStatus(status);
    }
 
    @Override
    public List<ErpAccountDO> getAccountList(Collection<Long> ids) {
        return accountMapper.selectByIds(ids);
    }
 
    @Override
    public PageResult<ErpAccountDO> getAccountPage(ErpAccountPageReqVO pageReqVO) {
        return accountMapper.selectPage(pageReqVO);
    }
 
    @Override
    public ErpAccountImportRespVO importAccountList(List<ErpAccountImportExcelVO> importAccounts,
                                                    ErpAccountImportReqVO importReqVO) {
        // 1. 校验非空
        importAccounts = filterList(importAccounts, item -> StrUtil.isNotBlank(item.getName()));
        if (CollUtil.isEmpty(importAccounts)) {
            throw exception(ACCOUNT_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        ErpAccountImportRespVO respVO = ErpAccountImportRespVO.builder()
                .createAccountNames(new ArrayList<>())
                .updateAccountNames(new ArrayList<>())
                .failureAccountNames(new LinkedHashMap<>())
                .build();
        importAccounts.forEach(importAccount -> {
            try {
                ErpAccountDO existAccount = accountMapper.selectByName(importAccount.getName());
                if (existAccount == null) {
                    // 新增
                    ErpAccountDO account = BeanUtils.toBean(importAccount, ErpAccountDO.class);
                    accountMapper.insert(account);
                    respVO.getCreateAccountNames().add(importAccount.getName());
                    return;
                }
                // 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureAccountNames().put(importAccount.getName(),
                            StrUtil.format(ACCOUNT_NAME_EXISTS.getMsg(), importAccount.getName()));
                    return;
                }
                ErpAccountDO updateAccount = BeanUtils.toBean(importAccount, ErpAccountDO.class)
                        .setId(existAccount.getId());
                accountMapper.updateById(updateAccount);
                respVO.getUpdateAccountNames().add(importAccount.getName());
            } catch (ServiceException ex) {
                respVO.getFailureAccountNames().put(importAccount.getName(), ex.getMessage());
            }
        });
        return respVO;
    }
 
}