郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.*;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Utils.*;
import com.gk.hotwork.Domain.Vo.AccountUserVo;
import com.gk.hotwork.Domain.Vo.RegisterVo;
import com.gk.hotwork.Domain.Vo.UserVo;
import com.gk.hotwork.Mapper.UserInfoMapper;
import com.gk.hotwork.Service.*;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Service("UserService")
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserService {
 
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserFaceService userFaceService;
    @Autowired
    private UserRolesService userRolesService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DepartmentService departmentService;
 
    @Value("${locationUrl}")
    private String url;
    @Value("${locationUsername}")
    private String username;
    @Value("${locationPassword}")
    private String password;
    @Value("${slice.size}")
    private Integer sliceSize;
 
    @Override
    public void selectUserDataGrid(PageInfo pageInfo) {
        Page<UserVo> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        }else {
            orderItem.setAsc(false);
            orderItem.setColumn("createddate");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        List<UserVo> list = userInfoMapper.selectUserDataGrid(page,pageInfo.getCondition());
        for (UserVo userVo : list) {
            List<RoleInfo> roleInfoList = roleService.selectRoleByUser(userVo.getId());
            userVo.setRoles(roleInfoList);
            UserFace userFace = userFaceService.selectByUserId(userVo.getId());
            if (userFace != null)
                userVo.setCode(userFace.getCode());
        }
        pageInfo.setResult(list);
        pageInfo.setTotalCount(page.getTotal());
    }
 
    @Override
    public UserInfo selectByUser(String username) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setStatus((byte)1);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<> (userInfo);
        return userInfoMapper.selectOne(wrapper);
    }
 
    @Override
    public List<UserInfo> selectUserInfo(Long id, String username) {
        return userInfoMapper.selectUserInfo(id,username);
    }
 
    @Override
    public boolean checkUserById(String userId) {
        if (StringUtils.isBlank(userId)){
            return false;
        }
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo != null;
    }
 
    @Override
    public UserVo selectUserVoByName(String examtype,String username) {
        return userInfoMapper.selectUserVoByName(examtype,username);
    }
 
 
    @Override
    public void deleteById(Long id) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserInfo::getStatus, (byte) 0)
                .eq(UserInfo::getId, id);
        this.update(updateWrapper);
    }
 
    @Override
    public void recoverOneById(Long id) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserInfo::getStatus, (byte) 1)
                .eq(UserInfo::getId, id);
        this.update(updateWrapper);
    }
 
    @Override
    public UserInfo selectByRealName(Long id,String realname) {
        return userInfoMapper.selectByRealName(id,realname);
    }
 
    @Override
    public UserInfo getByRealName(String realname, String depName) {
        return userInfoMapper.getByRealName(realname, depName);
    }
 
    @Override
    public UserInfo selectByIdCard(Long id,String idcard) {
        return userInfoMapper.selectByIdCard(id, idcard);
    }
 
    @Override
    public List<UserInfo> selectNotUpload() {
        return userInfoMapper.selectNotUpload();
    }
 
    @Override
    public List<UserVo> selectAll(HashMap<String, Object> condition) {
        return userInfoMapper.selectAll(condition);
    }
 
    @Override
    public List<UserInfo> selectByCompany(String company) {
        LambdaQueryWrapper<UserInfo>wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getCompany,company);
        return baseMapper.selectList(wrapper);
    }
 
    @Override
    public Msg saveEmployee(String realname, String empNo, String deviceNo) throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("empName", realname);
        params.put("empNo", empNo);
        params.put("deviceNo", deviceNo);
        String path = url + "/open/information/saveEmployee.do";
        return LocationUtils.net(params,path);
    }
 
    @Override
    public Msg setCardValid(String isValid, String cardId) throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("id", cardId);
        params.put("isvalid", isValid);
        String path = url + "/open/information/updEmployeeState.do";
        return LocationUtils.net(params,path);
    }
 
    @Override
    public Msg delEmployee(String cardId) throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("id", cardId);
        String path = url +  "/open/information/delEmployee.do";
        return LocationUtils.net(params,path);
    }
 
    @Override
    public Msg getRealGps() throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        String path = url + "/open/information/getRealGps.do";
        return LocationUtils.net(params,path);
    }
 
    @Override
    public UserInfo selectUserByCard(String deviceNo, String empNo, String name) {
        UserInfo userInfo = new UserInfo();
        userInfo.setDeviceNo(deviceNo);
        userInfo.setEmpNo(empNo);
        userInfo.setRealname(name);
        userInfo.setStatus((byte)1);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<> (userInfo);
        return userInfoMapper.selectOne(wrapper);
    }
 
    @Override
    public List<UserInfo> getGps(String username) {
        LambdaQueryWrapper<UserInfo>wrapper = new LambdaQueryWrapper<>();
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus((byte)1);
        wrapper.ne(UserInfo::getDeviceNo, "");
        wrapper.eq(UserInfo::getStatus,(byte)1);
        if (StringUtils.isNotBlank(username)){
            wrapper.eq(UserInfo::getRealname,username);
        }
        return userInfoMapper.selectList(wrapper);
 
    }
 
    /**
    * @Description: 所有用户大小
    * @date 2021/12/27 10:48
    */
    @Override
    public int selectUserSize() {
        return userInfoMapper.selectCount(
                new LambdaQueryWrapper<>()
        );
    }
 
    @Override
    public UserInfo getByDeviceNo(String uid) {
        if (StringUtils.isBlank(uid)) throw new BusinessException("接收设备uid为空");
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getDeviceNo,uid)
                        .eq(UserInfo::getStatus,1));
    }
 
    @Override
    public IPage<UserVo> selectUserPageInDepartment(Page<UserVo> page, Map<String, Object> filter, UserInfo user) {
        List<UserVo> userVos = userInfoMapper.selectUserDataGrid(page, filter);
        page.setRecords(userVos);
        return page;
    }
 
    @Override
    public List<UserVo> getUserList() {
        return userInfoMapper.getUserList();
    }
 
    @Override
    public UserInfo examForegroundLogin(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw new BusinessException("账号密码不能为空");
    }
        if (username.length() != 11) throw new BusinessException("请输入11位手机号");
        UserInfo userInfo = this.selectByUser(username);
        if (userInfo == null) throw new BusinessException("账号不存在");
        if (!MD5Utils.encode(password).equals(userInfo.getPassword())) throw new BusinessException("密码不正确");
        if (userInfo.getDepartment() != null) {
            DepartmentInfo de = departmentService.getById(userInfo.getDepartment());
            if (de != null) {
                userInfo.setDepartmentname(de.getDepartment());
            }
        }
        userInfo.setPassword(null);
        return userInfo;
    }
 
    @Override
    public Integer selectCountForExam(String name, String idno, String tel, String department,String company) {
        Map<Object, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("idno", idno);
        params.put("tel", tel);
        params.put("department", department);
        params.put("company", company);
        return userInfoMapper.selectCountForExam(params);
    }
 
    @Override
    public List<UserInfo> selectPageForExam(String name, String idno, String tel, String department, String company, Integer first, Integer pageSize) {
        Map<Object, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("idno", idno);
        params.put("tel", tel);
        params.put("department", department);
        params.put("company", company);
        params.put("first", first);
        params.put("pageSize", pageSize);
        return  userInfoMapper.selectPageForExam(params);
    }
 
    @Override
    @Transactional
    public void register(RegisterVo registerVo) {
        if (StringUtils.isBlank(registerVo.getRealname())) throw new BusinessException("用户姓名不能为空");
        if (StringUtils.isBlank(registerVo.getUsername()) ||registerVo.getUsername().length() != 11) throw new BusinessException("手机号不为11");
        if (StringUtils.isBlank(registerVo.getPassword())) throw new BusinessException("密码不能为空");
        if (StringUtils.isBlank(registerVo.getCompany())) throw new BusinessException("单位不能为空");
        if (registerVo.getCompanyid() == null) throw new BusinessException("单位参数不能为空");
        CompanyInfo company = companyService.getById(registerVo.getCompanyid());
        if (company == null) throw new BusinessException("单位不存在");
 
        if (StringUtils.isBlank(registerVo.getIdcard())|| !IdCardUtil.strongVerifyIdNumber(registerVo.getIdcard()))
            throw new BusinessException("身份证非法规则");
 
        if (this.phoneIsOccupied(registerVo.getRealname()))
            throw new BusinessException("手机号已经被占用");
        if (this.idCardIsOccupied(registerVo.getIdcard()))
            throw new BusinessException("身份证已经被占用");
 
        int count = userInfoMapper.selectCount(new LambdaQueryWrapper<>());
        int slice = (count + 1) % sliceSize;
        UserInfo userInfo = new UserInfo();
        userInfo.setRealname(registerVo.getRealname());
        userInfo.setPassword(MD5Utils.encode(registerVo.getPassword()));
        userInfo.setUsername(registerVo.getUsername());
        userInfo.setIdcard(registerVo.getIdcard());
        userInfo.setJob(registerVo.getJob());
        userInfo.setIsdel((byte) 0);
        userInfo.setStatus(((byte) 1));
        userInfo.setCreatedby(userInfo.getRealname());
        userInfo.setCompany(company.getCompany());
        userInfo.setCompanyid(company.getId());
        userInfo.setDepartment(registerVo.getDepartment());
        userInfo.setCreateddate(new Date());
        userInfo.setType(3);
        userInfo.setSlice(slice == 0 ? sliceSize + "" : slice + "");
        this.save(userInfo);
        //默认普通员工角色
        UserRolesInfo ur = new UserRolesInfo();
        ur.setUserid(userInfo.getId());
        RoleInfo role = roleService.getOne(new LambdaQueryWrapper<RoleInfo>().eq(RoleInfo::getName, "员工"));
        if (role == null) throw new BusinessException("请查询角色是否存在员工");
        ur.setRoleid(role.getId());
        userRolesService.save(ur);
 
    }
 
    @Override
    public void pwdChange(String username, String password, String phone) {
        if (StringUtils.isBlank(username)) throw new BusinessException("用户姓名不能为空");
        if (StringUtils.isBlank(password)) throw new BusinessException("密码不能为空");
        if (StringUtils.isBlank(phone)) throw new BusinessException("手机号不能为空");
        if (phone.length() != 11) throw new BusinessException("手机号不为11位");
        UserInfo userInfo = this.selectByUser(phone);
        if (userInfo == null) throw new BusinessException("用户不存在");
        if (username.equals(userInfo.getRealname())) {
            userInfo.setLastmodifiedby(username);
            userInfo.setLastmodifieddate(new Date());
            userInfo.setPassword(MD5Utils.encode(password));
            this.updateById(userInfo);
        }else{
            throw new BusinessException("用户信息不匹配");
        }
 
    }
 
    @Override
    public AccountUserVo getAccount(String username, String department) {
        if (StringUtils.isBlank(username)) throw new BusinessException("用户姓名不能为空");
        AccountUserVo accountUserVo = userInfoMapper.getAccountByUsername(username);
        if (accountUserVo == null){
            if (StringUtils.isBlank(department)){
                accountUserVo = userInfoMapper.getAccountByUsername("admin");
            }else {
                accountUserVo = userInfoMapper.getAccountByDepartment(department);
            }
        }
        return accountUserVo;
    }
    /**
    * @Description: 手机号是否被占用
    * @date 2022/5/11 9:38
    */
    @Override
    public boolean phoneIsOccupied(String phone) {
        if (StringUtils.isBlank(phone)) throw new BusinessException("手机号为空");
        Integer count = userInfoMapper.selectCount(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUsername, phone).eq(UserInfo::getStatus, 1));
        return count > 0;
    }
 
    /**
    * @Description: 身份证是否被占用
    * @date 2022/5/11 9:40
    */
    @Override
    public boolean idCardIsOccupied(String idCard) {
        if (StringUtils.isBlank(idCard)) throw new BusinessException("身份证号为空");
        Integer count = userInfoMapper.selectCount(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getIdcard, idCard).eq(UserInfo::getStatus, 1));
        return count > 0;
    }
 
 
    /**
    * @Description: 考试后台登录
    * @date 2022/5/12 9:34
    */
    @Override
    public UserInfo examBackgroundLogin(String username, String password) {
        if (StringUtils.isBlank(username)) throw new BusinessException("用户姓名不能为空");
        if (StringUtils.isBlank(password)) throw new BusinessException("密码不能为空");
 
       //查找admin type = 1 的用户信息
        UserInfo admin = userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, username)
                        .eq(UserInfo::getType, 1)
                        .eq(UserInfo::getStatus, 1));
        if (admin == null) throw new BusinessException("账户不存在");
        if (!MD5Utils.encode(password).equals(admin.getPassword())) throw new BusinessException("密码输入错误");
        return admin;
    }
    /**
     查询by姓名
     */
    @Override
    public UserInfo getUserByRealName(String trim) {
        return userInfoMapper.getUserByRealName(trim);
    }
    /**
     查询by姓名 and dep
     */
    @Override
    public UserInfo getUserByRealNameAndDep(String name, Long dep) {
        return userInfoMapper.getUserByRealNameAndDep(name, dep);
    }
 
    /**
     查询by Id
     */
    @Override
    public UserInfo getByUserId(Long userId) {
        return userInfoMapper.getByUserId(userId);
    }
}