admin
2025-08-20 f318c9c7c127b00f353bf45f273096d1dc4b424f
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
package com.taoke.autopay.service.impl;
 
import com.taoke.autopay.dao.ClientInfoMapper;
import com.taoke.autopay.entity.ClientAdditionalInfo;
import com.taoke.autopay.entity.ClientInfo;
import com.taoke.autopay.entity.SystemConfigKeyEnum;
import com.taoke.autopay.exception.LoginException;
import com.taoke.autopay.service.ClientAdditionalInfoService;
import com.taoke.autopay.service.ClientInfoService;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * @author hxh
 * @title: ClientInfoServiceImpl
 * @description: TODO
 * @date 2024/6/14 18:43
 */
@Service
public class ClientInfoServiceImpl implements ClientInfoService {
    @Resource
    private ClientInfoMapper clientInfoMapper;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private ClientAdditionalInfoService clientAdditionalInfoService;
 
    @Override
    public ClientInfo login(String account, String pwd) throws LoginException {
        ClientInfoMapper.DaoQuery query = new ClientInfoMapper.DaoQuery();
        query.start = 0;
        query.count = 1;
        query.account = account;
        List<ClientInfo> list = clientInfoMapper.list(query);
        if (list.size() == 0) {
            throw new LoginException("账号不存在");
        }
        if (!list.get(0).getPwd().equalsIgnoreCase(pwd)) {
            throw new LoginException("密码错误");
        }
 
        return list.get(0);
    }
 
    @Override
    public void logout(Long uid) {
        clientInfoMapper.clearActiveTime(uid);
    }
 
    @Override
    public ClientInfo selectByPrimaryKey(Long id) {
        return clientInfoMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void setActiveTime(Long id, Date date) {
        ClientInfo info = ClientInfo.builder().id(id).activeTime(date).build();
        clientInfoMapper.updateByPrimaryKeySelective(info);
    }
 
    @Override
    public List<ClientInfo> listByIds(List<Long> ids) {
        return clientInfoMapper.listByIds(ids);
    }
 
    @Override
    public List<ClientInfo> list(ClientInfoMapper.DaoQuery query) {
        return clientInfoMapper.list(query);
    }
 
    @Override
    public long count(ClientInfoMapper.DaoQuery query) {
        return clientInfoMapper.count(query);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ClientInfo info) {
        if (info.getPwd() == null) {
            info.setPwd("123456");
        }
        if (info.getRule() == null) {
            info.setRule(ClientInfo.RULE_COMMON);
        }
        if (info.getCreateTime() == null) {
            info.setCreateTime(new Date());
        }
 
        if (info.getAccount() == null) {
            // 查询最大的账号
            ClientInfoMapper.DaoQuery query = new ClientInfoMapper.DaoQuery();
            query.sortList = Arrays.asList(new String[]{"id desc"});
            query.clientType = info.getClientType();
            query.count = 1;
            List<ClientInfo> list = list(query);
            long maxId = 0;
            if (!list.isEmpty()) {
                maxId =Long.parseLong(StringUtil.getNumberFromString(list.get(0).getAccount()).split(",")[0]) + 1;
            }
            if(info.getClientType()==ClientInfo.CLIENT_TYPE_AGENT_PAYMENT) {
                info.setAccount(ClientInfo.ClientType.AGENT_PAYMENT.getAccountPrefix() + maxId);
            }else if(info.getClientType()==ClientInfo.CLIENT_TYPE_ORDER){
                info.setAccount(ClientInfo.ClientType.ORDER.getAccountPrefix() + maxId);
            }
        }
        clientInfoMapper.insertSelective(info);
        ClientInfo update =  ClientInfo.builder()
                .id(info.getId())
                .name("客户端" + (info.getId() - 1))
                .build();
        clientInfoMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public void setPwd(Long id, String pwd) {
        ClientInfo update = ClientInfo.builder()
                .id( id)
                .pwd(pwd)
                .build();
        clientInfoMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public List<Long> getRePayClientIds() {
        List<Long> idList = new ArrayList<>();
 
        String clientIds = systemConfigService.getValueCache(SystemConfigKeyEnum.RE_EXCUTE_PAY_CLIENTS);
        if (StringUtil.isNullOrEmpty(clientIds)) {
            return idList;
        }
        String[] ids = clientIds.trim().split(",");
        for (String id : ids) {
            idList.add(Long.parseLong(id));
        }
        return idList;
    }
 
    @Override
    public List<ClientInfo> getAvailableClientsForOrder() {
        ClientInfoMapper.DaoQuery query = new ClientInfoMapper.DaoQuery();
        query.clientType = ClientInfo.CLIENT_TYPE_ORDER;
        query.count = Integer.MAX_VALUE;
        query.minActiveTime = new Date(System.currentTimeMillis()-1000*60*30);// 30分钟内活跃的设备
        List<ClientInfo> clientInfoList = list(query);
        if(clientInfoList.isEmpty()){
            return clientInfoList;
        }
        // 获取电话号码,支付宝账号,支付宝密码
        List<Long> clientIds=new ArrayList<>();
        clientInfoList.forEach(clientInfo -> {
            clientIds.add(clientInfo.getId());
        });
        Map<Long, ClientAdditionalInfo> map = clientAdditionalInfoService.getClientAdditionalInfoMap(clientIds);
        for(int i=0;i<clientInfoList.size();i++){
            ClientAdditionalInfo info =   map.get(clientInfoList.get(i).getId());
            if(info==null){
                clientInfoList.remove(i);
                i--;
                continue;
            }
            if(StringUtil.isNullOrEmpty(info.getMobile())){
                clientInfoList.remove(i);
                i--;
                continue;
            }
        }
        return clientInfoList;
    }
}