SysUserServiceImpl.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. package com.ruoyi.system.service.impl;
  2. import java.sql.SQLException;
  3. import java.sql.SQLIntegrityConstraintViolationException;
  4. import java.time.Duration;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.format.DateTimeFormatter;
  8. import java.util.*;
  9. import java.util.stream.Collectors;
  10. import javax.validation.Validator;
  11. import com.ruoyi.common.core.domain.entity.SysDept;
  12. import com.ruoyi.common.enums.UserRolesType;
  13. import com.ruoyi.project.domain.ProjectStaff;
  14. import com.ruoyi.project.domain.ProjectStaffRecord;
  15. import com.ruoyi.project.domain.ProjectTask;
  16. import com.ruoyi.project.mapper.ProjectStaffMapper;
  17. import com.ruoyi.project.mapper.ProjectStaffRecordMapper;
  18. import com.ruoyi.project.mapper.ProjectTaskMapper;
  19. import com.ruoyi.system.domain.UserClockDtails;
  20. import com.ruoyi.system.mapper.*;
  21. import com.ruoyi.system.service.ISysDeptService;
  22. import com.ruoyi.weChat.service.WeChatService;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.util.CollectionUtils;
  29. import com.ruoyi.common.annotation.DataScope;
  30. import com.ruoyi.common.constant.UserConstants;
  31. import com.ruoyi.common.core.domain.entity.SysRole;
  32. import com.ruoyi.common.core.domain.entity.SysUser;
  33. import com.ruoyi.common.exception.ServiceException;
  34. import com.ruoyi.common.utils.SecurityUtils;
  35. import com.ruoyi.common.utils.StringUtils;
  36. import com.ruoyi.common.utils.bean.BeanValidators;
  37. import com.ruoyi.common.utils.spring.SpringUtils;
  38. import com.ruoyi.system.domain.SysPost;
  39. import com.ruoyi.system.domain.SysUserPost;
  40. import com.ruoyi.system.domain.SysUserRole;
  41. import com.ruoyi.system.service.ISysConfigService;
  42. import com.ruoyi.system.service.ISysUserService;
  43. /**
  44. * 用户 业务层处理
  45. *
  46. * @author ruoyi
  47. */
  48. @Service
  49. public class SysUserServiceImpl implements ISysUserService
  50. {
  51. private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
  52. @Autowired
  53. private SysUserMapper userMapper;
  54. @Autowired
  55. private SysRoleMapper roleMapper;
  56. @Autowired
  57. private SysPostMapper postMapper;
  58. @Autowired
  59. private SysUserRoleMapper userRoleMapper;
  60. @Autowired
  61. private SysUserPostMapper userPostMapper;
  62. @Autowired
  63. private ISysConfigService configService;
  64. @Autowired
  65. private ISysDeptService deptService;
  66. @Autowired
  67. private WeChatService weChatService;
  68. @Autowired
  69. protected Validator validator;
  70. @Autowired
  71. private ProjectStaffMapper projectStaffMapper;
  72. @Autowired
  73. private ProjectStaffRecordMapper projectStaffRecordMapper;
  74. @Autowired
  75. private ProjectTaskMapper projectTaskMapper;
  76. /**
  77. * 根据条件分页查询用户列表
  78. *
  79. * @param user 用户信息
  80. * @return 用户信息集合信息
  81. */
  82. @Override
  83. @DataScope(deptAlias = "d", userAlias = "u")
  84. public List<SysUser> selectUserList(SysUser user)
  85. {
  86. Long companyId = deptService.selectCompanyByDeptId();
  87. if (companyId!=null)user.setCompanyId(companyId);
  88. return userMapper.selectUserList(user);
  89. }
  90. /**
  91. * 根据条件分页查询已分配用户角色列表
  92. *
  93. * @param user 用户信息
  94. * @return 用户信息集合信息
  95. */
  96. @Override
  97. @DataScope(deptAlias = "d", userAlias = "u")
  98. public List<SysUser> selectAllocatedList(SysUser user)
  99. {
  100. return userMapper.selectAllocatedList(user);
  101. }
  102. /**
  103. * 根据条件分页查询未分配用户角色列表
  104. *
  105. * @param user 用户信息
  106. * @return 用户信息集合信息
  107. */
  108. @Override
  109. @DataScope(deptAlias = "d", userAlias = "u")
  110. public List<SysUser> selectUnallocatedList(SysUser user)
  111. {
  112. return userMapper.selectUnallocatedList(user);
  113. }
  114. /**
  115. * 通过用户名查询用户
  116. *
  117. * @param userName 用户名
  118. * @return 用户对象信息
  119. */
  120. @Override
  121. public SysUser selectUserByUserName(String userName)
  122. {
  123. return userMapper.selectUserByUserName(userName);
  124. }
  125. /**
  126. * 通过用户ID查询用户
  127. *
  128. * @param userId 用户ID
  129. * @return 用户对象信息
  130. */
  131. @Override
  132. public SysUser selectUserById(Long userId)
  133. {
  134. return userMapper.selectUserById(userId);
  135. }
  136. /**
  137. * 查询用户所属角色组
  138. *
  139. * @param userName 用户名
  140. * @return 结果
  141. */
  142. @Override
  143. public String selectUserRoleGroup(String userName)
  144. {
  145. List<SysRole> list = roleMapper.selectRolesByUserName(userName);
  146. if (CollectionUtils.isEmpty(list))
  147. {
  148. return StringUtils.EMPTY;
  149. }
  150. return list.stream().map(SysRole::getRoleName).collect(Collectors.joining(","));
  151. }
  152. /**
  153. * 查询用户所属岗位组
  154. *
  155. * @param userName 用户名
  156. * @return 结果
  157. */
  158. @Override
  159. public String selectUserPostGroup(String userName)
  160. {
  161. List<SysPost> list = postMapper.selectPostsByUserName(userName);
  162. if (CollectionUtils.isEmpty(list))
  163. {
  164. return StringUtils.EMPTY;
  165. }
  166. return list.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
  167. }
  168. /**
  169. * 校验用户名称是否唯一
  170. *
  171. * @param user 用户信息
  172. * @return 结果
  173. */
  174. @Override
  175. public boolean checkUserNameUnique(SysUser user)
  176. {
  177. Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
  178. SysUser info = userMapper.checkUserNameUnique(user.getUserName());
  179. if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
  180. {
  181. return UserConstants.NOT_UNIQUE;
  182. }
  183. return UserConstants.UNIQUE;
  184. }
  185. /**
  186. * 校验手机号码是否唯一
  187. *
  188. * @param user 用户信息
  189. * @return
  190. */
  191. @Override
  192. public boolean checkPhoneUnique(SysUser user)
  193. {
  194. Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
  195. SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
  196. if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
  197. {
  198. return UserConstants.NOT_UNIQUE;
  199. }
  200. return UserConstants.UNIQUE;
  201. }
  202. /**
  203. * 校验email是否唯一
  204. *
  205. * @param user 用户信息
  206. * @return
  207. */
  208. @Override
  209. public boolean checkEmailUnique(SysUser user)
  210. {
  211. Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
  212. SysUser info = userMapper.checkEmailUnique(user.getEmail());
  213. if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
  214. {
  215. return UserConstants.NOT_UNIQUE;
  216. }
  217. return UserConstants.UNIQUE;
  218. }
  219. /**
  220. * 校验用户是否允许操作
  221. *
  222. * @param user 用户信息
  223. */
  224. @Override
  225. public void checkUserAllowed(SysUser user)
  226. {
  227. if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
  228. {
  229. throw new ServiceException("不允许操作超级管理员用户");
  230. }
  231. }
  232. /**
  233. * 校验用户是否有数据权限
  234. *
  235. * @param userId 用户id
  236. */
  237. @Override
  238. public void checkUserDataScope(Long userId)
  239. {
  240. if (!SysUser.isAdmin(SecurityUtils.getUserId()))
  241. {
  242. SysUser user = new SysUser();
  243. user.setUserId(userId);
  244. List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
  245. if (StringUtils.isEmpty(users))
  246. {
  247. throw new ServiceException("没有权限访问用户数据!");
  248. }
  249. }
  250. }
  251. /**
  252. * 新增保存用户信息
  253. *
  254. * @param user 用户信息
  255. * @return 结果
  256. */
  257. @Override
  258. @Transactional
  259. public int insertUser(SysUser user)
  260. {
  261. // 新增用户信息
  262. int rows = userMapper.insertUser(user);
  263. // 新增用户岗位关联
  264. insertUserPost(user);
  265. // 新增用户与角色管理
  266. insertUserRole(user);
  267. return rows;
  268. }
  269. /**
  270. * 注册用户信息
  271. *
  272. * @param user 用户信息
  273. * @return 结果
  274. */
  275. @Override
  276. public boolean registerUser(SysUser user)
  277. {
  278. return userMapper.insertUser(user) > 0;
  279. }
  280. /**
  281. * 修改保存用户信息
  282. *
  283. * @param user 用户信息
  284. * @return 结果
  285. */
  286. @Override
  287. @Transactional
  288. public int updateUser(SysUser user)
  289. {
  290. Long userId = user.getUserId();
  291. // 删除用户与角色关联
  292. userRoleMapper.deleteUserRoleByUserId(userId);
  293. // 新增用户与角色管理
  294. insertUserRole(user);
  295. // 删除用户与岗位关联
  296. userPostMapper.deleteUserPostByUserId(userId);
  297. // 新增用户与岗位管理
  298. insertUserPost(user);
  299. //修改关联名称
  300. updateUserName(user);
  301. return userMapper.updateUser(user);
  302. }
  303. private void updateUserName(SysUser user) {
  304. SysUser sysUser = userMapper.selectUserById(user.getUserId());
  305. if (!sysUser.getNickName().equals(user.getNickName())){
  306. //修改所有名字
  307. projectStaffMapper.updateNameByAid(user);
  308. projectStaffRecordMapper.updateNameByAid(user);
  309. projectTaskMapper.updateAdminNameByAid(user);
  310. List<ProjectStaff> projectStaffs = projectStaffMapper.selectByAid(user.getUserId());
  311. for (ProjectStaff projectStaff : projectStaffs) {
  312. projectTaskMapper.updateStaffName(projectStaff.getPid());
  313. }
  314. }
  315. }
  316. /**
  317. * 用户授权角色
  318. *
  319. * @param userId 用户ID
  320. * @param roleIds 角色组
  321. */
  322. @Override
  323. @Transactional
  324. public void insertUserAuth(Long userId, Long[] roleIds)
  325. {
  326. userRoleMapper.deleteUserRoleByUserId(userId);
  327. insertUserRole(userId, roleIds);
  328. }
  329. /**
  330. * 修改用户状态
  331. *
  332. * @param user 用户信息
  333. * @return 结果
  334. */
  335. @Override
  336. public int updateUserStatus(SysUser user)
  337. {
  338. return userMapper.updateUser(user);
  339. }
  340. /**
  341. * 修改用户基本信息
  342. *
  343. * @param user 用户信息
  344. * @return 结果
  345. */
  346. @Override
  347. public int updateUserProfile(SysUser user)
  348. {
  349. return userMapper.updateUser(user);
  350. }
  351. /**
  352. * 修改用户头像
  353. *
  354. * @param userName 用户名
  355. * @param avatar 头像地址
  356. * @return 结果
  357. */
  358. @Override
  359. public boolean updateUserAvatar(String userName, String avatar)
  360. {
  361. return userMapper.updateUserAvatar(userName, avatar) > 0;
  362. }
  363. /**
  364. * 重置用户密码
  365. *
  366. * @param user 用户信息
  367. * @return 结果
  368. */
  369. @Override
  370. public int resetPwd(SysUser user)
  371. {
  372. return userMapper.updateUser(user);
  373. }
  374. /**
  375. * 重置用户密码
  376. *
  377. * @param userName 用户名
  378. * @param password 密码
  379. * @return 结果
  380. */
  381. @Override
  382. public int resetUserPwd(String userName, String password)
  383. {
  384. return userMapper.resetUserPwd(userName, password);
  385. }
  386. /**
  387. * 新增用户角色信息
  388. *
  389. * @param user 用户对象
  390. */
  391. public void insertUserRole(SysUser user)
  392. {
  393. this.insertUserRole(user.getUserId(), user.getRoleIds());
  394. }
  395. /**
  396. * 新增用户岗位信息
  397. *
  398. * @param user 用户对象
  399. */
  400. public void insertUserPost(SysUser user)
  401. {
  402. Long[] posts = user.getPostIds();
  403. if (StringUtils.isNotEmpty(posts))
  404. {
  405. // 新增用户与岗位管理
  406. List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
  407. for (Long postId : posts)
  408. {
  409. SysUserPost up = new SysUserPost();
  410. up.setUserId(user.getUserId());
  411. up.setPostId(postId);
  412. list.add(up);
  413. }
  414. userPostMapper.batchUserPost(list);
  415. }
  416. }
  417. /**
  418. * 新增用户角色信息
  419. *
  420. * @param userId 用户ID
  421. * @param roleIds 角色组
  422. */
  423. public void insertUserRole(Long userId, Long[] roleIds)
  424. {
  425. if (StringUtils.isNotEmpty(roleIds))
  426. {
  427. // 新增用户与角色管理
  428. List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
  429. for (Long roleId : roleIds)
  430. {
  431. SysUserRole ur = new SysUserRole();
  432. ur.setUserId(userId);
  433. ur.setRoleId(roleId);
  434. list.add(ur);
  435. }
  436. userRoleMapper.batchUserRole(list);
  437. }
  438. }
  439. /**
  440. * 通过用户ID删除用户
  441. *
  442. * @param userId 用户ID
  443. * @return 结果
  444. */
  445. @Override
  446. @Transactional
  447. public int deleteUserById(Long userId)
  448. {
  449. // 删除用户与角色关联
  450. userRoleMapper.deleteUserRoleByUserId(userId);
  451. // 删除用户与岗位表
  452. userPostMapper.deleteUserPostByUserId(userId);
  453. return userMapper.deleteUserById(userId);
  454. }
  455. /**
  456. * 批量删除用户信息
  457. *
  458. * @param userIds 需要删除的用户ID
  459. * @return 结果
  460. */
  461. @Override
  462. @Transactional
  463. public int deleteUserByIds(Long[] userIds)
  464. {
  465. for (Long userId : userIds)
  466. {
  467. checkUserAllowed(new SysUser(userId));
  468. checkUserDataScope(userId);
  469. }
  470. // 删除用户与角色关联
  471. userRoleMapper.deleteUserRole(userIds);
  472. // 删除用户与岗位关联
  473. userPostMapper.deleteUserPost(userIds);
  474. return userMapper.deleteUserByIds(userIds);
  475. }
  476. /**
  477. * 导入用户数据
  478. *
  479. * @param userList 用户数据列表
  480. * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
  481. * @param operName 操作用户
  482. * @return 结果
  483. */
  484. @Override
  485. public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
  486. {
  487. if (StringUtils.isNull(userList) || userList.size() == 0)
  488. {
  489. throw new ServiceException("导入用户数据不能为空!");
  490. }
  491. int successNum = 0;
  492. int failureNum = 0;
  493. StringBuilder successMsg = new StringBuilder();
  494. StringBuilder failureMsg = new StringBuilder();
  495. String password = configService.selectConfigByKey("sys.user.initPassword");
  496. Long company = deptService.selectCompanyByDeptId();
  497. for (SysUser user : userList)
  498. {
  499. try
  500. {
  501. if (StringUtils.isEmpty(user.getDeptName())){
  502. throw new ServiceException("部门数据异常");
  503. }
  504. if (StringUtils.isEmpty(user.getFullJob())){
  505. throw new ServiceException("全职/兼职数据异常");
  506. }
  507. // 验证是否存在这个用户
  508. SysUser u = userMapper.checkJobNumber(user.getJobNumber(),company);
  509. if (StringUtils.isNull(u)){
  510. SysUser sysUser1 = userMapper.selectUserByUserName(user.getUserName());
  511. if (sysUser1 != null){
  512. throw new ServiceException("用户名(账号)已存在");
  513. }
  514. user.setPassword(SecurityUtils.encryptPassword(password));
  515. user.setCreateBy(operName);
  516. user.setUserId(null);
  517. if (user.getSuperiorName()!=null){
  518. SysUser sysUser = userMapper.selectByNikeName(user.getSuperiorName(),company);
  519. if (sysUser != null){
  520. user.setSuperiorId(sysUser.getUserId());
  521. }
  522. }
  523. if (user.getDeptName()!=null){
  524. SysDept sysDept = deptService.selectByDepName(user.getDeptName(), company);
  525. if (sysDept ==null){
  526. throw new ServiceException("部门数据异常,找不到部门");
  527. }else {
  528. user.setDeptId(sysDept.getDeptId());
  529. }
  530. }
  531. userMapper.insertUser(user);
  532. if (user.getRoleName()!=null){
  533. SysRole sysRole = roleMapper.selectByRoleName(user.getRoleName());
  534. if (sysRole !=null){
  535. List<SysUserRole> list=new ArrayList<>();
  536. SysUserRole sur = new SysUserRole();
  537. sur.setUserId(user.getUserId());
  538. sur.setRoleId(sysRole.getRoleId());
  539. list.add(sur);
  540. userRoleMapper.batchUserRole(list);
  541. }
  542. }
  543. successNum++;
  544. successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
  545. } else if (isUpdateSupport) {
  546. // BeanValidators.validateWithException(validator, user);
  547. checkUserAllowed(u);
  548. checkUserDataScope(u.getUserId());
  549. user.setUserId(u.getUserId());
  550. user.setUpdateBy(operName);
  551. if (user.getSuperiorName()!=null&&!user.getSuperiorName().equals(u.getSuperiorName())){
  552. SysUser sysUser = userMapper.selectByNikeName(user.getSuperiorName(),company);
  553. if (sysUser != null){
  554. user.setSuperiorId(sysUser.getUserId());
  555. }
  556. }
  557. if (user.getDeptName()!=null&&!user.getDeptName().equals(u.getDeptName())){
  558. SysDept sysDept = deptService.selectByDepName(user.getDeptName(), company);
  559. if (sysDept ==null){
  560. throw new ServiceException("部门数据异常,找不到部门");
  561. }else {
  562. user.setDeptId(sysDept.getDeptId());
  563. }
  564. }
  565. if (user.getRoleName()!=null&&!user.getRoleName().equals(u.getRoleName())){
  566. SysRole sysRole = roleMapper.selectByRoleName(user.getRoleName());
  567. if (sysRole !=null){
  568. userRoleMapper.deleteUserRoleByUserId(user.getUserId());
  569. List<SysUserRole> list=new ArrayList<>();
  570. SysUserRole sur = new SysUserRole();
  571. sur.setUserId(user.getUserId());
  572. sur.setRoleId(sysRole.getRoleId());
  573. list.add(sur);
  574. userRoleMapper.batchUserRole(list);
  575. }
  576. }
  577. userMapper.updateUser(user);
  578. successNum++;
  579. successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
  580. }
  581. else
  582. {
  583. failureNum++;
  584. failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
  585. }
  586. }
  587. catch (Exception e)
  588. {
  589. failureNum++;
  590. String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
  591. if (e instanceof ServiceException){
  592. failureMsg.append(msg + e.getMessage());
  593. }else if (e instanceof SQLException){
  594. failureMsg.append("数据异常");
  595. }
  596. log.error(msg, e);
  597. }
  598. }
  599. if (failureNum > 0)
  600. {
  601. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  602. throw new ServiceException(failureMsg.toString());
  603. }
  604. else
  605. {
  606. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  607. }
  608. return successMsg.toString();
  609. }
  610. @Override
  611. public Object selectName(String name,Integer type){
  612. if (name==null||name.length()<2)return new ArrayList<>();
  613. Long roleId=null;
  614. if (type==null)type=0;
  615. if (type==0)roleId= UserRolesType.COMMON.getCode();
  616. else if (type==1)roleId=UserRolesType.MANAGE.getCode();
  617. if (SecurityUtils.isSuperAdmin()){
  618. roleId=UserRolesType.ADMIN.getCode();;
  619. }
  620. Long companyId = deptService.selectCompanyByDeptId();
  621. return userMapper.selectName(name,roleId,companyId);
  622. }
  623. @Override
  624. public String setOpenId(String code) {
  625. System.out.println(code);
  626. String openId = weChatService.getOpenId(code);
  627. SysUser user = new SysUser();
  628. user.setUserId(SecurityUtils.getUserId());
  629. user.setOpenId(openId);
  630. userMapper.updateUser(user);
  631. return openId;
  632. }
  633. @Override
  634. public boolean checkJobNumber(SysUser user) {
  635. Long companyId = getCompany(user.getDeptId());
  636. log.debug("company="+companyId);
  637. SysUser sysUser = userMapper.checkJobNumber(user.getJobNumber(),companyId);
  638. if (sysUser==null){
  639. return false;
  640. }else if (StringUtils.isNotNull(user.getUserId())&&user.getUserId().equals(sysUser.getUserId())){
  641. return false;
  642. }
  643. return true;
  644. }
  645. @Override
  646. public boolean checkProject(Long[] userIds) {
  647. for (Long userId : userIds) {
  648. int i = userMapper.selectParticipateProjectSum(userId);
  649. if (i>0){
  650. return true;
  651. }
  652. }
  653. return false;
  654. }
  655. private Long getCompany(Long deptId) {
  656. log.debug("depID="+deptId);
  657. SysDept sysDept = deptService.selectDeptById(deptId);
  658. String ancestors = sysDept.getAncestors();
  659. log.debug("anc="+ancestors);
  660. String[] split = ancestors.split(",");
  661. if (split.length>2)return Long.valueOf(split[2]);
  662. else return deptId;
  663. }
  664. @Override
  665. public List<UserClockDtails> pushUserClock(List<Map<Integer, String>> maps) {
  666. List<UserClockDtails> list=new ArrayList<>();
  667. List<String> names=new ArrayList<>();
  668. List<String> uids=new ArrayList<>();
  669. int month =0;
  670. int year =0;
  671. int days=0;
  672. Map<Integer,String> hand=new HashMap<>();
  673. String startClockTime=null;
  674. String endClockTime=null;
  675. if (!maps.isEmpty()){
  676. for (int i = 0; i < maps.size(); i++) {
  677. //获取头部
  678. if (i==0){
  679. Map<Integer, String> map = maps.get(i);
  680. String head = map.get(0);
  681. int i1 = head.indexOf(":")+1;
  682. int i2 = head.indexOf("至");
  683. startClockTime = head.substring(i1, i2).trim();
  684. endClockTime = head.substring(i2+1).trim();
  685. System.out.println(startClockTime+"~"+endClockTime);
  686. LocalDate date = LocalDate.parse(startClockTime, DateTimeFormatter.ISO_DATE);
  687. month=date.getMonthValue();
  688. year = date.getYear();
  689. }else if (i==2){
  690. hand=maps.get(2);
  691. }else {
  692. Map<Integer, String> map = maps.get(i);
  693. UserClockDtails userClockDtails=new UserClockDtails();
  694. //每一条是一个人的当月打卡
  695. for (int j = 0; j < map.size(); j++) {
  696. String s = map.get(j);
  697. if (j==0)userClockDtails.setName(s);
  698. else if (j==1)userClockDtails.setGroupName(s.replace("\n","-").replace(" ",""));
  699. else if (j==2)userClockDtails.setDepName(s.replace("\n","-"));
  700. else if (j==3)userClockDtails.setJobNumber(s);
  701. else if (j==4)userClockDtails.setPosition(s);
  702. else if (j==5)userClockDtails.setUserId(s);
  703. //前5为基本信息,后面则是打卡信息
  704. else {
  705. UserClockDtails newUserlock=new UserClockDtails(userClockDtails);
  706. if(StringUtils.isNotEmpty(s)){
  707. String[] split = s.split("\n");
  708. if (split.length==1){
  709. newUserlock.setStauts(1);
  710. }
  711. String regex = "\\d+";
  712. if (hand.get(j).matches(regex)){
  713. days=Integer.parseInt(hand.get(j));
  714. }else {
  715. days++;
  716. }
  717. newUserlock.setClockDate(s);
  718. newUserlock.setClockTime(LocalDate.of(year,month,days));
  719. int startHour = Integer.parseInt(split[0].split(":")[0].trim());
  720. int startMinute = Integer.parseInt(split[0].split(":")[1].trim());
  721. LocalDateTime startTime=LocalDateTime.of(year,month,days,startHour,startMinute);
  722. newUserlock.setStartTime(startTime);
  723. if (newUserlock.getStauts()==0){
  724. int endHour = Integer.parseInt(split[split.length - 1].split(":")[0].trim());
  725. int endMinute = Integer.parseInt(split[split.length - 1].split(":")[1].trim());
  726. LocalDateTime endTime=LocalDateTime.of(year,month,days,endHour,endMinute);
  727. newUserlock.setEndTime(endTime);
  728. Double hours = getHuors(startTime, endTime);
  729. newUserlock.setDuration(hours);
  730. }else {
  731. newUserlock.setDuration(0.0);
  732. }
  733. if (!names.contains(newUserlock.getName())){
  734. names.add(newUserlock.getName());
  735. uids.add(newUserlock.getUserId());
  736. }
  737. list.add(newUserlock);
  738. }
  739. }
  740. }
  741. }
  742. }
  743. }
  744. //处理数据库
  745. // List<String> errorNames = new ArrayList<>();
  746. // List<Long> userList=new ArrayList<>();
  747. // for (int i = 0; i < names.size(); i++) {
  748. // SysUser sysUser = userMapper.selectDDUserId(uids.get(i));
  749. // if (sysUser==null){
  750. // errorNames.add(names.get(i));
  751. // }else {
  752. // userList.add(sysUser.getUserId());
  753. // }
  754. // }
  755. // if (!errorNames.isEmpty())throw new ServiceException(String.format("用户%s不能匹配到账号",errorNames.toString()));
  756. // for (Long uid : userList) {
  757. // projectStaffRecordMapper.selectByAidAndTime(uid,startClockTime,endClockTime);
  758. // }
  759. return list;
  760. }
  761. private Double getHuors(LocalDateTime startTime, LocalDateTime endTime) {
  762. //打卡时间处理
  763. if (startTime.getHour()<=8&&startTime.getMinute()<30){
  764. startTime=startTime.withHour(8);
  765. startTime=startTime.withMinute(30);
  766. }else if (startTime.getHour()>8&&startTime.getMinute()>30){
  767. startTime=startTime.withMinute(startTime.getHour()+1);
  768. startTime=startTime.withMinute(0);
  769. }else if (startTime.getHour()>8&&startTime.getMinute()<30){
  770. startTime=startTime.withMinute(30);
  771. }
  772. if (endTime.getHour()<18 &&endTime.getMinute()<30){
  773. endTime=endTime.withMinute(0);
  774. }else if (endTime.getHour()>=18&&endTime.getMinute()<30){
  775. endTime=endTime.withMinute(0);
  776. }else if (endTime.getHour()>=18&&endTime.getMinute()>30){
  777. endTime=endTime.withMinute(30);
  778. }
  779. Duration duration = Duration.between(startTime, endTime);
  780. long minutes = duration.toMinutes();
  781. long l = minutes / 30;
  782. double l1 = (double) (l / 2);
  783. long l2 = l % 2;
  784. if (l2==1){
  785. l1+=0.5;
  786. }
  787. //在12点之前
  788. if (startTime.getHour()<13&&endTime.getHour()>13&&endTime.getHour()<18){
  789. l1=l1-1.5;
  790. }else if (startTime.getHour()<13&&endTime.getHour()>17){
  791. l1=l1-2;
  792. }else if (startTime.getHour()>=13&&endTime.getHour()>17){
  793. l1=l1-0.5;
  794. }
  795. return l1;
  796. }
  797. }