admin
2021-04-02 d551bef5e2b09bd98681cf97807988c9863e66fc
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package com.yeshi.buwan.service.imp;
 
import java.io.Serializable;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.buwan.dao.user.LoginUserExtraDao;
import com.yeshi.buwan.domain.user.LoginUserExtra;
import com.yeshi.buwan.dto.user.LoginInfoDto;
import com.yeshi.buwan.exception.user.LoginUserException;
import com.yeshi.buwan.exception.user.RegisterUserException;
import com.yeshi.buwan.service.inter.LoginUserService;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.buwan.dao.user.LoginUserDao;
import com.yeshi.buwan.dao.UserDao;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.domain.system.SystemInfo;
import com.yeshi.buwan.domain.UserData;
import com.yeshi.buwan.domain.UserInfo;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.StringUtil;
 
@Service
public class UserService {
 
    public UserService() {
 
    }
 
    @Resource
    private UserDao userDao;
 
    @Resource
    private LoginUserDao loginUserDao;
 
    @Resource
    private LoginUserExtraDao loginUserExtraDao;
 
    @Resource
    private LoginUserService loginUserService;
 
 
    // 用户操作
    public List<UserInfo> getUserList(int system, int page) {
        return userDao.list("from UserInfo u where u.system.id=? order by u.createtime desc",
                (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{system + ""});
    }
 
    // 获取用户数量
    public long getUserInfoCount(int system) {
 
        return userDao.getCount("select count(*)  from UserInfo u where u.system.id=?", new String[]{system + ""});
    }
 
    // 获取总的用户数量
    public long getUserInfoCount() {
 
        return userDao.getCount("select count(*)  from UserInfo u");
    }
 
    // 获取用户的页数
    public long getUserInfoPage(int system) {
        long count = getUserInfoCount(system);
        return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1;
    }
 
    // 获取某个用户的详细信息
    @Cacheable(value = "longTimeCache", key = "'getUserInfo'+'-'+#id")
    public UserInfo getUserInfo(String id) {
 
        return userDao.find(UserInfo.class, id);
    }
 
    // 更新用户信息
    public void updateUserInfo(UserInfo userInfo) {
        userDao.update(userInfo);
    }
 
    public void updateLoginUserInfo(LoginUser lu) {
        loginUserDao.update(lu);
    }
 
    public String getUid(String device, String system, String imei, String mac, String lat, String lng) {
        UserInfo info;
        info = new UserInfo();
        info.setDevice(device);
        if (!StringUtil.isNullOrEmpty(imei))
            info.setImei(imei);
        if (!StringUtil.isNullOrEmpty(mac))
            info.setMac(mac);
        if (!StringUtil.isNullOrEmpty(lat))
            info.setLat(lat);
        if (!StringUtil.isNullOrEmpty(lng))
            info.setLng(lng);
        String s;
        List<UserInfo> list = userDao.list("from UserInfo u where u.device=? and u.system.id=" + system,
                device);
 
        if (list.size() <= 0) {//
            info.setScore("0");
            info.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString());
            SystemInfo sys = new SystemInfo();
            sys.setId(system);
            info.setSystem(sys);
            userDao.create(info);
            s = queryUid(device, system);
        } else {
            UserInfo u = (UserInfo) list.get(0);
            s = u.getId();
            if (!StringUtil.isNullOrEmpty(imei))
                u.setImei(imei);
            if (!StringUtil.isNullOrEmpty(mac))
                u.setMac(mac);
            if (!StringUtil.isNullOrEmpty(lat))
                u.setLat(lat);
            if (!StringUtil.isNullOrEmpty(lng))
                u.setLng(lng);
            userDao.update(u);
        }
 
        return s;
    }
 
    @SuppressWarnings("unchecked")
    public String queryUid(String device, String system) {
        String s = "0";
        List<UserInfo> list = userDao.list("from UserInfo u where u.device=? and u.system.id=" + system,
                new String[]{device});
        if (list.size() <= 0)
            s = "0";
        else
            s = ((UserInfo) list.get(0)).getId();
        return s;
    }
 
    @Cacheable(value = "userCache", key = "'getUserData'+'-'+#uid")
    public UserData getUserData(String uid) {
        UserData data = new UserData();
        data.setCollectCount((new StringBuilder(String.valueOf(userDao.getCount(
                (new StringBuilder("select count(*)  from Collection c where c.user.id=")).append(uid).toString()))))
                .toString());
        data.setWatchCount((new StringBuilder(String.valueOf(userDao.getCount(
                (new StringBuilder("select count(*)  from GetScoreWatch c where c.user.id=")).append(uid).toString()))))
                .toString());
        List<UserInfo> user = userDao
                .list((new StringBuilder("select count(*)  from UserInfo u where u.id=")).append(uid).toString());
        // if (user != null && user.size() > 0)
        // data.setScore(((UserInfo) user.get(0)).getScore());
        // else
        // data.setScore("0");
        return data;
    }
 
 
    /**
     * 登录
     *
     * @param dto
     * @return
     * @throws LoginUserException
     */
    public LoginUser login(LoginInfoDto dto) throws LoginUserException {
 
        LoginUser loginUser = null;
 
        LoginUserExtra loginUserExtra = null;
        switch (dto.getLoginType()) {
            case LoginUser.LOGIN_TYPE_EMAIL: {
                if (StringUtil.isNullOrEmpty(dto.getEmail()) || StringUtil.isNullOrEmpty(dto.getPwd())) {
                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "邮箱和密码不能为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.email=? and lu.state=?", dto.getSystemId(), dto.getEmail(), LoginUser.STATE_NORMAL);
                if (userList == null || userList.size() == 0) {
                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "用户不存在");
                }
 
                if (!dto.getPwd().equalsIgnoreCase(userList.get(0).getPwd())) {
                    throw new LoginUserException(LoginUserException.CODE_PWD_WRONG, "密码错误");
                }
                loginUser = userList.get(0);
                loginUserExtra = new LoginUserExtra(loginUser.getId());
                loginUserExtra.setEmail(dto.getEmail());
            }
            break;
            case LoginUser.LOGIN_TYPE_QQ: {
                if (dto.getQqUserInfo() == null || StringUtil.isNullOrEmpty(dto.getQqUserInfo().getOpenId())) {
                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "QQ授权信息为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.qqOpenId=? and lu.state=?", dto.getSystemId(), dto.getQqUserInfo().getOpenId(), LoginUser.STATE_NORMAL);
                if (userList == null || userList.size() == 0) {
                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "用户不存在");
                }
                loginUser = userList.get(0);
                loginUserExtra = new LoginUserExtra(loginUser.getId());
                loginUserExtra.setQqPortrait(dto.getQqUserInfo().getPortrait());
                loginUserExtra.setQqOpenId(dto.getQqUserInfo().getOpenId());
                loginUserExtra.setQqNickName(dto.getQqUserInfo().getName());
            }
            break;
            case LoginUser.LOGIN_TYPE_WX: {
                if (dto.getWeiXinUser() == null || StringUtil.isNullOrEmpty(dto.getWeiXinUser().getOpenid())) {
                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "微信授权信息为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.wxOpenId=? and lu.state=?", dto.getSystemId(), dto.getWeiXinUser().getOpenid(), LoginUser.STATE_NORMAL);
                if (userList == null || userList.size() == 0) {
                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "用户不存在");
                }
                loginUser = userList.get(0);
 
                //更新微信信息
                loginUser.setWxUnionId(dto.getWeiXinUser().getUnionid());
 
                loginUserExtra = new LoginUserExtra(loginUser.getId());
                loginUserExtra.setWxPortrait(dto.getWeiXinUser().getHeadimgurl());
                loginUserExtra.setWxOpenId(dto.getWeiXinUser().getOpenid());
                loginUserExtra.setWxNickName(dto.getWeiXinUser().getNickname());
                loginUserExtra.setWxSex(dto.getWeiXinUser().getSex());
                loginUserExtra.setWxUnionId(dto.getWeiXinUser().getUnionid());
            }
            break;
            case LoginUser.LOGIN_TYPE_PHONE: {
 
                if (StringUtil.isNullOrEmpty(dto.getPhone())) {
                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "手机号为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.phone=? and lu.state=?", dto.getSystemId(), dto.getPhone(), LoginUser.STATE_NORMAL);
                if (userList == null || userList.size() == 0) {
                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "用户不存在");
                }
                loginUser = userList.get(0);
                loginUserExtra = new LoginUserExtra(loginUser.getId());
            }
            break;
        }
 
        if (loginUser != null) {
            loginUser.setLoginType(dto.getLoginType());
            loginUserDao.update(loginUser);
        }
 
        if (loginUserExtra != null) {
            loginUserExtra.setUtdId(dto.getUtdId());
            loginUserExtraDao.updateSelective(loginUserExtra);
        }
 
        return loginUser;
    }
 
 
    /**
     * 注册
     *
     * @param dto
     */
    public LoginUser register(LoginInfoDto dto) throws RegisterUserException {
        LoginUser loginUser = null;
        LoginUserExtra loginUserExtra = null;
        switch (dto.getLoginType()) {
            case LoginUser.LOGIN_TYPE_EMAIL: {
                if (StringUtil.isNullOrEmpty(dto.getEmail()) || StringUtil.isNullOrEmpty(dto.getPwd())) {
                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "邮箱和密码不能为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.email=? and lu.state=?", dto.getSystemId(), dto.getEmail(), LoginUser.STATE_NORMAL);
                if (userList != null && userList.size() > 0) {
                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "用户已存在");
                }
 
                loginUser = new LoginUser();
                loginUser.setLoginType(LoginUser.LOGIN_TYPE_EMAIL);
                loginUser.setEmail(dto.getEmail());
                loginUser.setPwd(dto.getPwd());
                loginUser.setName(dto.getNickName());
                loginUser.setState(LoginUser.STATE_NORMAL);
                loginUser.setSystemId(dto.getSystemId());
                loginUserExtra = new LoginUserExtra();
                loginUserExtra.setEmail(dto.getEmail());
            }
            break;
            case LoginUser.LOGIN_TYPE_QQ: {
                if (dto.getQqUserInfo() == null || StringUtil.isNullOrEmpty(dto.getQqUserInfo().getOpenId())) {
                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "QQ授权信息为空");
                }
 
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.qqOpenId=? and lu.state=?", dto.getSystemId(), dto.getQqUserInfo().getOpenId(), LoginUser.STATE_NORMAL);
                if (userList != null && userList.size() > 0) {
                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "用户已存在");
                }
 
                loginUser = new LoginUser();
                loginUser.setLoginType(LoginUser.LOGIN_TYPE_QQ);
                loginUser.setQqOpenId(dto.getQqUserInfo().getOpenId());
                loginUser.setName(dto.getQqUserInfo().getName());
                loginUser.setPortrait(dto.getQqUserInfo().getPortrait());
 
 
                loginUserExtra = new LoginUserExtra();
                loginUserExtra.setQqPortrait(dto.getQqUserInfo().getPortrait());
                loginUserExtra.setQqOpenId(dto.getQqUserInfo().getOpenId());
                loginUserExtra.setQqNickName(dto.getQqUserInfo().getName());
            }
            break;
            case LoginUser.LOGIN_TYPE_WX: {
                if (dto.getWeiXinUser() == null || StringUtil.isNullOrEmpty(dto.getWeiXinUser().getOpenid())) {
                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "微信授权信息为空");
                }
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.wxOpenId=? and lu.state=?", dto.getSystemId(), dto.getWeiXinUser().getOpenid(), LoginUser.STATE_NORMAL);
                if (userList != null && userList.size() > 0) {
                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "用户已存在");
                }
                loginUser = new LoginUser();
                loginUser.setLoginType(LoginUser.LOGIN_TYPE_WX);
                loginUser.setWxUnionId(dto.getWeiXinUser().getUnionid());
                loginUser.setName(dto.getWeiXinUser().getNickname());
                loginUser.setWxOpenId(dto.getWeiXinUser().getOpenid());
                loginUser.setPortrait(dto.getWeiXinUser().getHeadimgurl());
 
 
                loginUserExtra = new LoginUserExtra();
                loginUserExtra.setWxPortrait(dto.getWeiXinUser().getHeadimgurl());
                loginUserExtra.setWxOpenId(dto.getWeiXinUser().getOpenid());
                loginUserExtra.setWxNickName(dto.getWeiXinUser().getNickname());
                loginUserExtra.setWxSex(dto.getWeiXinUser().getSex());
                loginUserExtra.setWxUnionId(dto.getWeiXinUser().getUnionid());
            }
            break;
            case LoginUser.LOGIN_TYPE_PHONE: {
 
                if (StringUtil.isNullOrEmpty(dto.getPhone())) {
                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "手机号为空");
                }
 
                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.phone=? and lu.state=?", dto.getSystemId(), dto.getPhone(), LoginUser.STATE_NORMAL);
                if (userList != null && userList.size() > 0) {
                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "用户已存在");
                }
 
                loginUser = new LoginUser();
                loginUser.setLoginType(LoginUser.LOGIN_TYPE_PHONE);
                loginUser.setName(null);
                loginUser.setPhone(dto.getPhone());
 
                loginUserExtra = new LoginUserExtra();
            }
            break;
        }
 
        if (loginUser == null) {
            throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "注册类型出错");
        }
 
 
        loginUser.setIpinfo(dto.getIpInfo());
        loginUser.setState(LoginUser.STATE_NORMAL);
        loginUser.setSystemId(dto.getSystemId());
        loginUser.setCreatetime(System.currentTimeMillis() + "");
 
        Serializable uid = loginUserDao.save(loginUser);
        loginUser.setId(uid + "");
 
        if (StringUtil.isNullOrEmpty(loginUser.getName())) {
            //设置默认用户昵称
            String nickName = "无名氏";
            LoginUser update = new LoginUser();
            update.setId(uid + "");
            update.setName(nickName);
            loginUserDao.updateSelective(update);
        }
 
        loginUserExtra.setId(loginUser.getId());
        loginUserExtra.setCreateTime(new Date());
        loginUserService.initExtra(loginUserExtra);
 
        return loginUser;
    }
 
    public LoginUser getLoginUser(String id) {
        return loginUserDao.find(LoginUser.class, id);
    }
 
    public LoginUser getLoginUserByOpenId(String openid) {
        List<LoginUser> list = loginUserDao.list("from LoginUser lu where lu.openid=?", new Serializable[]{openid});
        if (list != null && list.size() > 0)
            return list.get(0);
        return null;
    }
 
    /**
     * 用户注销
     *
     * @param uid
     */
    @Transactional
    public void unRegister(Long uid) throws Exception {
        LoginUser user = loginUserDao.find(LoginUser.class, uid + "");
        if (user == null) {
            throw new Exception("用户不存在");
        }
        if (user.getState() != LoginUser.STATE_NORMAL) {
            throw new Exception("账户已被注销");
        }
        user.setState(LoginUser.STATE_UNREGISTER);
        loginUserDao.update(user);
    }
 
    // 邮箱注册
    public String registerByEmail(final LoginUser user) {
        return userDao.excute(new HibernateCallback<String>() {
            @SuppressWarnings("unchecked")
            public String doInHibernate(Session session) throws HibernateException {
                List<LoginUser> list = session.createQuery("from LoginUser lu where lu.openid=? and lu.loginType=?")
                        .setParameter(0, user.getOpenid()).setParameter(1, user.getLoginType()).list();
                if (list != null && list.size() > 0)
                    return "该邮箱已注册";
                try {
                    user.setState(LoginUser.STATE_NORMAL);
                    session.getTransaction().begin();
                    session.save(user);
                    session.flush();
                    session.getTransaction().commit();
                    return "注册成功";
                } catch (Exception e) {
                    return "注册失败";
                }
            }
        }) + "";
    }
 
    /**
     * 邮箱登录
     *
     * @param user
     * @return
     */
    public LoginUser loginByEmail(final LoginUser user) {
        return (LoginUser) userDao.excute(new HibernateCallback<LoginUser>() {
            @SuppressWarnings("unchecked")
            public LoginUser doInHibernate(Session session) throws HibernateException {
                List<LoginUser> list = session.createQuery("from LoginUser lu where lu.email=?")
                        .setParameter(0, user.getOpenid()).list();
                if (list != null && list.size() > 0) {
                    if (list.get(0).getPwd().equalsIgnoreCase(user.getPwd()))
                        return list.get(0);
                }
 
                return null;
            }
        });
    }
}