puzhibing
2026-08-27 768ae5d15078c31afad2bc6781ca570cea52fa6c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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.api.dto.ChargingCardBalanceDTO;
import com.ruoyi.account.api.dto.ChargingCardExcelDTO;
import com.ruoyi.account.api.dto.ChargingCardQueryDTO;
import com.ruoyi.account.api.dto.ChargingCardSaveDTO;
import com.ruoyi.account.api.dto.ChargingCardStatusDTO;
import com.ruoyi.account.api.model.TChargingCard;
import com.ruoyi.account.mapper.TChargingCardMapper;
import com.ruoyi.account.service.TChargingCardService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
@Service
public class TChargingCardServiceImpl extends ServiceImpl<TChargingCardMapper, TChargingCard>
        implements TChargingCardService {
 
    private static final int ENABLED = 1;
    private static final int DISABLED = 2;
    private static final int BALANCE_ADD = 1;
    private static final int BALANCE_SUBTRACT = 2;
    private static final BigDecimal MAX_AMOUNT = new BigDecimal("9999999999.99");
 
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private SysUserClient sysUserClient;
 
    @Override
    public IPage<TChargingCard> selectCardPage(ChargingCardQueryDTO query) {
        ChargingCardQueryDTO safeQuery = query == null ? new ChargingCardQueryDTO() : query;
        long pageNum = safeQuery.getPageCurr() == null || safeQuery.getPageCurr() < 1 ? 1 : safeQuery.getPageCurr();
        long pageSize = safeQuery.getPageSize() == null || safeQuery.getPageSize() < 1 ? 10 : safeQuery.getPageSize();
        Page<TChargingCard> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectCardPage(page, safeQuery);
    }
 
    @Override
    public boolean saveCard(ChargingCardSaveDTO dto) {
        validateCard(dto, false);
        TChargingCard card = new TChargingCard();
        card.setCardNo(dto.getCardNo().trim());
        card.setBalance(dto.getBalance() == null ? BigDecimal.ZERO : dto.getBalance());
        card.setStatus(dto.getStatus() == null ? ENABLED : dto.getStatus());
        card.setDelFlag(0);
        card.setCreateTime(LocalDateTime.now());
        card.setCreateBy(SecurityUtils.getUserId());
        if (!save(card)) {
            throw new ServiceException("新增卡片失败");
        }
        return true;
    }
 
    @Override
    public boolean updateCard(ChargingCardSaveDTO dto) {
        validateCard(dto, true);
        if (getById(dto.getId()) == null) {
            throw new ServiceException("卡片不存在或已删除");
        }
        TChargingCard card = new TChargingCard();
        card.setId(dto.getId());
        card.setCardNo(dto.getCardNo().trim());
        card.setStatus(dto.getStatus() == null ? ENABLED : dto.getStatus());
        card.setUpdateTime(LocalDateTime.now());
        card.setUpdateBy(SecurityUtils.getUserId());
        if (!updateById(card)) {
            throw new ServiceException("修改卡片失败");
        }
        return true;
    }
 
    @Override
    public boolean deleteCards(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择需要删除的卡片");
        }
        List<Long> distinctIds = new java.util.ArrayList<>(new HashSet<>(ids));
        if (listByIds(distinctIds).size() != distinctIds.size()) {
            throw new ServiceException("部分卡片不存在或已删除");
        }
        if (!removeByIds(distinctIds)) {
            throw new ServiceException("删除卡片失败");
        }
        return true;
    }
 
    @Override
    public boolean updateStatus(ChargingCardStatusDTO dto) {
        if (dto == null || dto.getId() == null || !isValidStatus(dto.getStatus())) {
            throw new ServiceException("卡片状态参数错误");
        }
        if (getById(dto.getId()) == null) {
            throw new ServiceException("卡片不存在或已删除");
        }
        TChargingCard card = new TChargingCard();
        card.setId(dto.getId());
        card.setStatus(dto.getStatus());
        card.setUpdateTime(LocalDateTime.now());
        card.setUpdateBy(SecurityUtils.getUserId());
        if (!updateById(card)) {
            throw new ServiceException("更新卡片状态失败");
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBalance(ChargingCardBalanceDTO dto) {
        validateBalanceRequest(dto);
        verifyOperationPassword(dto.getOperationPassword());
 
        List<TChargingCard> cards = listByIds(dto.getIds());
        if (cards.size() != new HashSet<>(dto.getIds()).size()) {
            throw new ServiceException("部分卡片不存在或已删除");
        }
        int updated = baseMapper.adjustBalance(dto.getIds(), dto.getAmount(), dto.getType(),
                MAX_AMOUNT, SecurityUtils.getUserId());
        if (updated != cards.size()) {
            throw new ServiceException(dto.getType() == BALANCE_SUBTRACT
                    ? "部分卡片余额不足,无法完成扣减"
                    : "部分卡片余额超过上限,无法完成增加");
        }
        for (TChargingCard card : cards) {
            baseMapper.insertBalanceRecord(card.getId(), dto.getType(), dto.getAmount(),
                    StringUtils.isEmpty(dto.getRemark()) ? null : dto.getRemark().trim(), SecurityUtils.getUserId());
        }
        return true;
    }
 
    @Override
    public List<ChargingCardExcelDTO> selectExportList(ChargingCardQueryDTO query) {
        return baseMapper.selectExportList(query == null ? new ChargingCardQueryDTO() : query);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importCards(List<ChargingCardExcelDTO> cards) {
        if (cards == null || cards.isEmpty()) {
            throw new ServiceException("导入数据不能为空");
        }
        Set<String> importCardNos = new HashSet<>();
        int row = 1;
        for (ChargingCardExcelDTO item : cards) {
            row++;
            if (item == null || StringUtils.isEmpty(item.getCardNo())) {
                throw new ServiceException("第" + row + "行卡号不能为空");
            }
            String cardNo = item.getCardNo().trim();
            if (cardNo.length() > 64) {
                throw new ServiceException("第" + row + "行卡号不能超过64个字符");
            }
            if (!importCardNos.add(cardNo)) {
                throw new ServiceException("第" + row + "行卡号在文件中重复");
            }
            if (existsCardNo(cardNo, null)) {
                throw new ServiceException("第" + row + "行卡号已存在");
            }
            if (item.getBalance() != null) {
                validateAmount(item.getBalance(), true, "第" + row + "行卡余额");
            }
            parseExcelStatus(item.getStatus(), row);
        }
 
        Long userId = SecurityUtils.getUserId();
        LocalDateTime now = LocalDateTime.now();
        for (ChargingCardExcelDTO item : cards) {
            TChargingCard card = new TChargingCard();
            card.setCardNo(item.getCardNo().trim());
            card.setBalance(item.getBalance() == null ? BigDecimal.ZERO : item.getBalance());
            card.setStatus(parseExcelStatus(item.getStatus(), 0));
            card.setDelFlag(0);
            card.setCreateTime(now);
            card.setCreateBy(userId);
            save(card);
        }
        return "成功导入" + cards.size() + "条卡片数据";
    }
 
    private void validateCard(ChargingCardSaveDTO dto, boolean update) {
        if (dto == null || StringUtils.isEmpty(dto.getCardNo())) {
            throw new ServiceException("卡号不能为空");
        }
        if (update && dto.getId() == null) {
            throw new ServiceException("卡片ID不能为空");
        }
        String cardNo = dto.getCardNo().trim();
        if (cardNo.length() > 64) {
            throw new ServiceException("卡号不能超过64个字符");
        }
        if (!update && dto.getBalance() != null) {
            validateAmount(dto.getBalance(), true, "卡余额");
        }
        if (dto.getStatus() != null && !isValidStatus(dto.getStatus())) {
            throw new ServiceException("卡片状态参数错误");
        }
        if (existsCardNo(cardNo, dto.getId())) {
            throw new ServiceException("卡号已存在");
        }
    }
 
    private boolean existsCardNo(String cardNo, Long excludeId) {
        return baseMapper.countByCardNo(cardNo, excludeId) > 0;
    }
 
    private void validateBalanceRequest(ChargingCardBalanceDTO dto) {
        if (dto == null || dto.getIds() == null || dto.getIds().isEmpty()) {
            throw new ServiceException("请选择需要更新余额的卡片");
        }
        if (dto.getType() == null || (dto.getType() != BALANCE_ADD && dto.getType() != BALANCE_SUBTRACT)) {
            throw new ServiceException("余额调整类型错误");
        }
        validateAmount(dto.getAmount(), false, "调整金额");
        if (dto.getRemark() != null && dto.getRemark().trim().length() > 255) {
            throw new ServiceException("备注不能超过255个字符");
        }
        if (StringUtils.isEmpty(dto.getOperationPassword())) {
            throw new ServiceException("请输入操作密码");
        }
    }
 
    private void validateAmount(BigDecimal amount, boolean allowZero, String fieldName) {
        if (amount == null || (allowZero ? amount.compareTo(BigDecimal.ZERO) < 0 : amount.compareTo(BigDecimal.ZERO) <= 0)) {
            throw new ServiceException(fieldName + (allowZero ? "不能小于0" : "必须大于0"));
        }
        if (amount.scale() > 2) {
            throw new ServiceException(fieldName + "最多保留2位小数");
        }
        if (amount.compareTo(MAX_AMOUNT) > 0) {
            throw new ServiceException(fieldName + "不能超过" + MAX_AMOUNT.toPlainString());
        }
    }
 
    private Integer parseExcelStatus(String status, int row) {
        if (StringUtils.isEmpty(status)) {
            return ENABLED;
        }
        String value = status.trim();
        if ("1".equals(value) || "启用".equals(value)) {
            return ENABLED;
        }
        if ("2".equals(value) || "禁用".equals(value)) {
            return DISABLED;
        }
        String prefix = row > 0 ? "第" + row + "行" : "";
        throw new ServiceException(prefix + "启用状态错误");
    }
 
    private void verifyOperationPassword(String operationPassword) {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser == null || loginUser.getUserid() == null) {
            throw new ServiceException("登录状态已失效,请重新登录");
        }
        R<SysUser> response = sysUserClient.getSysUser(loginUser.getUserid());
        SysUser user = response == null ? null : response.getData();
        if (user == null || StringUtils.isEmpty(user.getOperationPassword())) {
            throw new ServiceException("当前账号未设置操作密码");
        }
        if (!SecurityUtils.matchesPassword(operationPassword, user.getOperationPassword())) {
            throw new ServiceException("操作密码错误");
        }
    }
 
    private boolean isValidStatus(Integer status) {
        return status != null && (status == ENABLED || status == DISABLED);
    }
}