yanderong
2026-07-03 90663502f2520e8cfa7f6d8a887e46d2cf61be2f
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.TravelCardMapper;
import com.stylefeng.guns.modular.system.model.TravelCard;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.model.vo.BalanceModifyRecordVo;
import com.stylefeng.guns.modular.system.model.vo.BalanceRecordVo;
import com.stylefeng.guns.modular.system.service.ITravelCardService;
import com.stylefeng.guns.modular.system.service.IUserInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * <p>
 *    出行卡,服务实现类
 * </p>
 * @author Lux
 * @version 1.0.0
 * @since 2026/6/17 18:21
 */
@Slf4j
@Service
@AllArgsConstructor
public class TravelCardServiceImpl extends ServiceImpl<TravelCardMapper, TravelCard> implements ITravelCardService {
 
    private final IUserInfoService userInfoService;
 
    /**
     * 出行卡兑换
     *
     * @param userId 用户id
     * @param cardNo 卡号
     * @param cardPassword 卡密码
     */
    @Transactional
    public BigDecimal travelCardExchange(Integer userId, String cardNo, String cardPassword) {
        Optional.ofNullable(cardNo).filter(StringUtils::isNotEmpty).orElseThrow(() -> new ServiceException("出行卡兑换-卡号不能为空"));
        Optional.ofNullable(cardPassword).filter(StringUtils::isNotEmpty).orElseThrow(() -> new ServiceException("出行卡兑换-卡密码不能为空"));
        UserInfo userInfo = Optional.ofNullable(userInfoService.selectById(userId)).orElseThrow(() -> new ServiceException("出行卡兑换当前用户不存在"));
        TravelCard travelCard = Optional.ofNullable(this.baseMapper.getTravelCardByCardNo(cardNo)).orElseThrow(() -> new ServiceException("出行卡号不存在"));
        if(!travelCard.getCardPassword().equals(cardPassword)) {
            throw new ServiceException("出行卡密码错误");
        }
        if(Objects.nonNull(travelCard.getAmount())) {
            double newBalance = new BigDecimal(String.valueOf(userInfo.getBalance())).add(travelCard.getAmount()).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
            userInfo.setBalance(newBalance);
            userInfoService.updateById(userInfo);
            travelCard.setStatus(2); // 已充值
            travelCard.setRechargeUserId(userInfo.getId());
            travelCard.setRechargeUserPhone(userInfo.getPhone());
            travelCard.setRechargeAmount(travelCard.getAmount());
            travelCard.setRechargeTime(new Date());
            this.baseMapper.updateById(travelCard);
            // 新增余额变更记录(出行卡兑换类型)
            // this.baseMapper.insertBalanceModifyRecord(BalanceModifyRecordVo.convert(userInfo.getId(), userInfo.getNickName(), userInfo.getPhone(), travelCard.getAmount()));
        }
        return travelCard.getAmount();
    }
 
    /**
     * 查询用户余额变更记录
     *
     * @param userId 用户id
     * @return 变更记录列表
     */
    public List<BalanceRecordVo> getBalanceModifyRecord(Integer userId, Integer type, String startDate, String endDate) {
        if(StringUtils.isNotEmpty(startDate)) {
            startDate = startDate + " 00:00:00";
        }
        if(StringUtils.isNotEmpty(endDate)) {
            endDate = endDate + " 23:59:59";
        }
        List<BalanceModifyRecordVo> recordVoList = this.baseMapper.getBalanceModifyRecord(userId, type, startDate, endDate);
        // List<BalanceRecordVo> orderRecords = this.baseMapper.getOrderRecord(userId, startDate, endDate);
        List<BalanceRecordVo> balanceRecordVos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(recordVoList)) {
            recordVoList.forEach(r -> balanceRecordVos.add(BalanceRecordVo.convert(r)));
        }
       /* if(CollectionUtils.isNotEmpty(orderRecords)) {
            orderRecords.forEach(order -> balanceRecordVos.add(BalanceRecordVo.convert(order, userId)));
        }*/
        return balanceRecordVos;
    }
 
}