新版仿ok交易所-后端
1
zj
2025-08-12 b38469269d05b02cd67afbbbae65f0ae28f87b4e
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
package com.yami.trading.admin.facade;
 
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.customer.CustomerDto;
import com.yami.trading.bean.model.Customer;
import com.yami.trading.bean.model.Log;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.ApplicationContextUtils;
import com.yami.trading.common.util.IPHelper;
import com.yami.trading.dao.customer.CustomerMapper;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.chat.online.OnlineChatMessageService;
import com.yami.trading.service.customer.CustomerService;
import com.yami.trading.service.system.LogService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.sys.model.SysUser;
import com.yami.trading.sys.service.SysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
 
    @Autowired
    SysUserService userService;
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        init();
    }
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private LogService logService;
 
 
    private Map<String, Customer> cache = new ConcurrentHashMap<String, Customer>();
 
    public void init() {
        List<Customer> list =  list();
        for (Customer customer : list) {
            cache.put(customer.getUserName(), customer);
        }
    }
 
    @Autowired
    PasswordEncoder passwordEncoder;
 
 
 
    @Override
    public Page<CustomerDto> listData(Page page, String userName) {
 
        return baseMapper.listPage(userName,page);
    }
 
    @Override
    @Transactional
    public void updateCustomer(String autoAnswer, String remarks, int status, String id,String operator) {
        Customer customer = getById(id);
        if (customer == null) {
            throw new YamiShopBindException("参数错误!");
        }
        SysUser sysUser = sysUserService.getByUserName(customer.getUserName());
        if (sysUser == null) {
            throw new YamiShopBindException("客服不存在!");
        }
        sysUser.setStatus(status);
        sysUser.setRemarks(remarks);
        sysUserService.updateById(sysUser);
        customer.setAutoAnswer(autoAnswer);
        updateById(customer);
        String ip = IPHelper.getIpAddr();
        saveLog(sysUser,operator,"ip:"+ip+"修改了客服["+sysUser.getUsername()+"]自动回复,原自动回复["+autoAnswer+"],新自动回复["+autoAnswer+"]");
    }
 
    @Override
    @Transactional
    public void updateCustomerPassword(String password, String id) {
 
        Customer customer = getById(id);
        if (customer == null) {
            throw new YamiShopBindException("参数错误!");
        }
        SysUser sysUser = sysUserService.getByUserName(customer.getUserName());
        SysUser userDB = BeanUtil.copyProperties(sysUser, SysUser.class);
        if (sysUser == null) {
            throw new YamiShopBindException("客服不存在!");
        }
        sysUser.setPassword(passwordEncoder.encode(password));
        updateById(customer);
        sysUserService.updateById(sysUser);
 
        String ip = IPHelper.getIpAddr();
        saveLog(sysUser,sysUser.getUsername(),"ip:"+ip+"管理员修改系统用户,修改前角色为["+userDB.getRoleName()+"],邮箱为["+userDB.getEmail()+"],"
                + "修改后角色为["+sysUser.getRoleName()+"],邮箱为["+sysUser.getEmail()+"]");
    }
    public void saveLog(SysUser secUser, String operator,String context) {
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator);
        log.setUsername(secUser.getUsername());
        log.setUserId(secUser.getUserId().toString());
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.save(log);
    }
    @Override
    public void updateCustomerSafePassword(String safePassword, String id) {
        Customer customer = getById(id);
        if (customer == null) {
            throw new YamiShopBindException("参数错误!");
        }
        SysUser sysUser = sysUserService.getByUserName(customer.getUserName());
        if (sysUser == null) {
            throw new YamiShopBindException("客服不存在!");
        }
        sysUser.setSafePassword(passwordEncoder.encode(safePassword));
        sysUserService.updateById(sysUser);
    }
 
    @Override
    public void forceOffline(String id) {
        Customer customer = getById(id);
        if (customer == null) {
            throw new YamiShopBindException("参数错误!");
        }
        SysUser sysUser = sysUserService.getByUserName(customer.getUserName());
        if (sysUser == null) {
            throw new YamiShopBindException("客服不存在!");
        }
        customer.setOnlineState(0);
        customer.setLastOfflineTime(new Date());
        updateById(customer);
 
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(SecurityUtils.getSysUser().getUsername());
        log.setUsername(sysUser.getUsername());
        log.setUserId(sysUser.getUserId()+"");
        log.setLog("ip:"+ IPHelper.getIpAddr() +"管理员强制下线客服["+sysUser.getUsername()+"]");
        log.setCreateTime(new Date());
        logService.save(log);
    }
 
 
 
    @Override
    public Customer cacheByUsername(String username) {
        return getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getUserName, username));
    }
 
    /**
     * 更新
     *
     * @param entity
     * @param isOnline true:必须在线才更新,false:都能更新
     */
    public boolean update(Customer entity, boolean isOnline) {
        if (isOnline && cacheByUsername(entity.getUserName()).getOnlineState() != 1) {
            return false;
        }
        updateById(entity);
        cache.put(entity.getUserName(), entity);
        return true;
    }
 
    @Override
    public void saveCustomer(String userName, String remarks, String password, int status, String safePassword,String autoAnswer,String operator) {
 
        Date now = new Date();
        SysUser sysUser = sysUserService.getByUserName(userName);
        if (null != sysUser) {
            throw new YamiShopBindException("系统存在相同[系统登录名]!");
        }
        sysUser = new SysUser();
        sysUser.setUsername(userName);
        sysUser.setRemarks(remarks);
//        sysUser.setPassword(passwordEncoder.encode(passwordManager.decryptPassword(password)));
        sysUser.setPassword(passwordEncoder.encode(password));
        //sysUser.setPassword(password);
        sysUser.setStatus(status);
        sysUser.setGoogleAuthBind(true);
        sysUser.setSafePassword(passwordEncoder.encode(safePassword));
        sysUser.setGoogleAuthSecret("AAAAAAAAAAA");
        sysUserService.save(sysUser);
        String ip = IPHelper.getIpAddr();
 
        saveLog(sysUser,operator,"ip:"+ip+"管理员新增系统用户,角色为["+sysUser.getRoleName()+"],邮箱为["+sysUser.getEmail()+"]");
        Customer customer = new Customer();
        customer.setUserName(sysUser.getUsername());
        customer.setOnlineState(0);
        customer.setCreateTime(now);
        customer.setAutoAnswer(autoAnswer);
        save(customer);
 
    }
 
    /**
     * 分配一个在线客服给用户
     *
     * @return
     */
    @Override
    public Customer cacheOnlineOne() {
        List<Customer> list = new ArrayList<Customer>(cache.values());
 
        CollectionUtils.filter(list, new Predicate() {// 在线客服
            @Override
            public boolean evaluate(Object arg0) {
                // TODO Auto-generated method stub
                return ((Customer) arg0).getOnlineState() == 1;
            }
        });
        if (CollectionUtils.isEmpty(list))
            return null;
        Collections.sort(list, new Comparator<Customer>() {
            @Override
            public int compare(Customer arg0, Customer arg1) {
                // TODO Auto-generated method stub
                if (arg0.getLastCustomerTime() == null) {
                    return -1;
                } else if (arg1.getLastCustomerTime() == null) {
                    return 1;
                }
                return (int) (arg0.getLastCustomerTime().getTime() - arg1.getLastCustomerTime().getTime());
            }
        });
        return list.get(0);
    }
 
    public void offline(String username) {
        Customer customer = cacheByUsername(username);
        if(customer==null) {
            throw new YamiShopBindException("客服不存在");
        }
        customer.setOnlineState(0);
        customer.setLastOfflineTime(new Date());
        updateById(customer);
    }
    public void online(String username) {
        Customer customer = cacheByUsername(username);
        if(customer==null) {
            throw new YamiShopBindException("客服不存在");
        }
        customer.setOnlineState(1);
        customer.setLastOnlineTime(new Date());
        updateById(customer);
        OnlineChatMessageService onlineChatMessageService = ApplicationContextUtils.getBean(OnlineChatMessageService.class);
        onlineChatMessageService.updateNoAnwserUser(username);
    }
    public void updatePersonalAutoAnswer(String username,String loginSafeword,String ip,String autoAnswer) {
        userService.checkSafeWord(loginSafeword);
        SysUser user = userService.getByUserName(username);
        updateAutoAnswer(user,username,ip,autoAnswer);
    }
    public void updateAutoAnswer(SysUser user,String operatorUsername,String ip,String autoAnswer) {
//        this.adminSystemUserService.update(user,newPassword,type,operatorUsername,loginSafeword,code,ip,superGoogleAuthCode);
        Customer customer = cacheByUsername(user.getUsername());
        if (customer==null){
            throw  new YamiShopBindException("客服不存在!");
        }
        String sourceAutoAnswer = customer.getAutoAnswer();
        customer.setAutoAnswer(autoAnswer);
        updateById(customer);
        saveLog(user,operatorUsername,"ip:"+ip+"修改了客服["+user.getUsername()+"]自动回复,原自动回复["+sourceAutoAnswer+"],新自动回复["+autoAnswer+"]");
    }
}