xin
2025-07-14 a5a0a3c29a97c807cda8846994d1d8df6adeb9e5
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
package com.oying.modules.rider.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oying.exception.BadRequestException;
import com.oying.modules.rider.domain.RiderInfo;
import com.oying.modules.rider.domain.RiderSourceInfo;
import com.oying.modules.rider.domain.RiderSourceInfoHttp;
import com.oying.modules.rider.domain.RiderWalletInfo;
import com.oying.modules.rider.service.RiderWalletInfoService;
import com.oying.modules.rider.utils.Constants;
import com.oying.modules.rider.utils.RiderSourceHttpUtils;
import com.oying.utils.FileUtil;
import com.oying.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.modules.rider.service.RiderInfoService;
import com.oying.modules.rider.domain.dto.RiderInfoQueryCriteria;
import com.oying.modules.rider.mapper.RiderInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.oying.utils.PageUtil;
 
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
 
import com.oying.utils.PageResult;
 
/**
 * @author pxb
 * @description 服务实现
 * @date 2025-07-01
 **/
@Service
@RequiredArgsConstructor
public class RiderInfoServiceImpl extends ServiceImpl<RiderInfoMapper, RiderInfo> implements RiderInfoService {
 
    private final RiderInfoMapper riderInfoMapper;
 
    private final RiderWalletInfoService riderWalletInfoService;
 
    @Override
    public PageResult<RiderInfo> queryAll(RiderInfoQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(riderInfoMapper.findAll(criteria, page));
    }
 
    @Override
    public List<RiderInfo> queryAll(RiderInfoQueryCriteria criteria) {
        return riderInfoMapper.findAll(criteria);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(RiderInfo resources) {
        riderInfoMapper.insert(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RiderInfo resources) {
        RiderInfo riderInfo = getById(resources.getInfoId());
        riderInfo.copy(resources);
        riderInfoMapper.updateById(riderInfo);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        riderInfoMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void download(List<RiderInfo> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RiderInfo riderInfo : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("骑手id", riderInfo.getRiderId());
            map.put("数据来源id", riderInfo.getSourceId());
            map.put("电话", riderInfo.getPhone());
            map.put("乙方姓名", riderInfo.getCardName());
            map.put("证件号", riderInfo.getCardNum());
            map.put("数据来源编号", riderInfo.getSourceNum());
            map.put("状态0不生效,1生效 其他平台的条件是否满足", riderInfo.getEnabled());
            map.put("数据来源平台(LY,等)", riderInfo.getSourcePlatform());
            map.put("地址", riderInfo.getAddress());
            map.put("创建者", riderInfo.getCreateBy());
            map.put("更新者", riderInfo.getUpdateBy());
            map.put("创建日期", riderInfo.getCreateTime());
            map.put("更新时间", riderInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncRiderSourceInfo(String sourcePlatform) {
        Long riderId = SecurityUtils.getCurrentUserId();
        // 查询其他平台的数据
        RiderSourceInfoHttp riderSourceInfoHttp = RiderSourceHttpUtils.getRiderSourceInfoHttp(SecurityUtils.getCurrentUsername(), sourcePlatform);
        // 请求成功
        if (riderSourceInfoHttp.getCode().equals(Constants.HTTP_CODE_SUCCESS)) {
            // 获取骑手信息等于空
            if (null != riderSourceInfoHttp.getData()) {
                RiderSourceInfo infoHttpData = riderSourceInfoHttp.getData();
                // 检查是否存在该平台的骑手信息
                QueryWrapper<RiderInfo> riderInfoQueryWrapper = new QueryWrapper<>();
                riderInfoQueryWrapper.eq(RiderInfo.COL_RIDER_ID, riderId);
                RiderInfo riderInfo = riderInfoMapper.selectOne(riderInfoQueryWrapper);
                // 如果存在该平台的骑手信息
                if (null == riderInfo) {
                    // 如果不存在,则创建新的骑手三方信息
                    RiderInfo newRiderInfo = new RiderInfo();
                    BeanUtil.copyProperties(infoHttpData, newRiderInfo);
                    newRiderInfo.setRiderId(riderId);
                    newRiderInfo.setSourcePlatform(sourcePlatform);
                    create(newRiderInfo);
                    // 创建骑手钱包信息 正常
                    RiderWalletInfo riderWalletInfo = new RiderWalletInfo();
                    riderWalletInfo.setRiderId(riderId);
                    riderWalletInfo.setAmount(new BigDecimal(0.00));
                    riderWalletInfo.setAvailableBalance(new BigDecimal(0.00));
                    riderWalletInfo.setPurchasingElectricityAmount(new BigDecimal(0.00));
                    riderWalletInfo.setWithdrawTotal(new BigDecimal(0.00));
                    riderWalletInfo.setIncomeTotal(new BigDecimal(0.00));
                    riderWalletInfo.setFrozenAmount(new BigDecimal(0.00));
                    riderWalletInfo.setRunTotal(Constants.ZERO);
                    riderWalletInfo.setCashWithdrawalTotal(Constants.ZERO);
                    riderWalletInfo.setPurchasingElectricityTotal(Constants.ZERO);
                    riderWalletInfo.setStatus(Constants.WALLET_STATUS_NORMAL);
                    riderWalletInfo.setPhone(infoHttpData.getPhone());
                    riderWalletInfo.setCardName(infoHttpData.getCardName());
                    riderWalletInfo.setCardNum(infoHttpData.getCardNum());
                    riderWalletInfoService.create(riderWalletInfo);
                } else {
                    // 是否相同平台
                    if (sourcePlatform.equals(riderInfo.getSourcePlatform())) {
                        // 如果存在,接单状态不同 更新接单状态
                        if (!infoHttpData.getEnabled().equals(riderInfo.getEnabled())) {
                            riderInfo.setEnabled(infoHttpData.getEnabled());
                            // 更新骑手信息
                            updateById(riderInfo);
                        }
                    } else {
                        // 骑手平台信息变更
                        RiderInfo newRiderInfo = new RiderInfo();
                        BeanUtil.copyProperties(infoHttpData, newRiderInfo);
                        newRiderInfo.setRiderId(riderId);
                        newRiderInfo.setSourcePlatform(sourcePlatform);
                        updateById(newRiderInfo);
                        // 添加变更记录...
                    }
                }
            } else {
                // 获取骑手三方数据为空,抛出异常
                throw new BadRequestException("获取骑手三方数据为空");
            }
        } else {
            // 获取骑手三方数据失败,抛出异常
            throw new BadRequestException("获取骑手三方数据失败");
        }
    }
 
    @Override
    public RiderInfo getRiderSourceInfo(Long riderId) {
        QueryWrapper<RiderInfo> riderInfoQueryWrapper = new QueryWrapper<>();
        riderInfoQueryWrapper.eq(RiderInfo.COL_RIDER_ID, riderId);
        return riderInfoMapper.selectOne(riderInfoQueryWrapper);
    }
 
}