You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.domain.entity.Admin;
  3. import com.example.demo.domain.entity.AdminRole;
  4. import com.example.demo.domain.entity.Role;
  5. import com.example.demo.domain.vo.AdminVo;
  6. import com.example.demo.domain.vo.Password;
  7. import com.example.demo.domain.vo.Permission;
  8. import com.example.demo.domain.vo.Result;
  9. import com.example.demo.mapper.AdminMapper;
  10. import com.example.demo.mapper.PermissionMapper;
  11. import com.example.demo.service.AdminService;
  12. import com.example.demo.service.PermissionService;
  13. import com.github.pagehelper.PageHelper;
  14. import com.github.pagehelper.PageInfo;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import org.springframework.util.ObjectUtils;
  20. import java.util.Collections;
  21. import java.util.List;
  22. /**
  23. * @program: GOLD
  24. * @ClassName PermissionServiceImpl
  25. * @description:
  26. * @author: huangqizhen
  27. * @create: 202506-26 13:20
  28. * @Version 1.0
  29. **/
  30. @Service
  31. public class PermissionServiceImpl implements PermissionService {
  32. @Autowired
  33. private PermissionMapper permissionMapper;
  34. @Autowired
  35. private AdminMapper adminMapper;
  36. @Autowired
  37. private AdminService adminService;
  38. @Override
  39. public List<String> getposition(String token) {
  40. return permissionMapper.getposition(token);
  41. }
  42. @Override
  43. public List<String> getmarket(String token) {
  44. return permissionMapper.getmarket(token);
  45. }
  46. @Override
  47. public PageInfo<Permission> getpermission(Integer pageNum, Integer pageSize, Permission permission) {
  48. PageHelper.startPage(pageNum, pageSize);
  49. List<Permission> list = permissionMapper.getPermission(permission);
  50. return new PageInfo<>(list);
  51. }
  52. @Override
  53. @Transactional
  54. public Integer addpermission(Admin admin) throws Exception {
  55. if (!ObjectUtils.isEmpty(adminMapper.getAdmin(admin.getAccount()))) {
  56. throw new Exception("账号已存在");
  57. }
  58. if (ObjectUtils.isEmpty(admin.getAccount())) {
  59. throw new Exception("账号为空!");
  60. }
  61. if (ObjectUtils.isEmpty(admin.getMarket())) {
  62. throw new Exception("地区为空!");
  63. }
  64. if (admin.getMarket().contains("总部") && admin.getMarket().size() > 1) {
  65. throw new Exception("总部不可与其他地区共存");
  66. }
  67. if (ObjectUtils.isEmpty(admin.getPostiton())) {
  68. throw new Exception("职位为空!");
  69. }
  70. if (ObjectUtils.isEmpty(admin.getRoleId())) {
  71. throw new Exception("权限类别为空!");
  72. }
  73. if (ObjectUtils.isEmpty(admin.getAdminName())) {
  74. throw new Exception("用户名为空!");
  75. }
  76. if (ObjectUtils.isEmpty(admin.getMachineId())) {
  77. throw new Exception("机器编号为空!");
  78. }
  79. if (admin.getMachineId().contains(",")) {
  80. throw new Exception("机器编号格式错误");
  81. } else {
  82. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  83. admin.setPassword(passwordEncoder.encode(("123456")));
  84. // 将地区列表转换为逗号分隔的字符串
  85. String markets = String.join(",", admin.getMarket());
  86. admin.setMarket(Collections.singletonList(markets));
  87. permissionMapper.addPermission(admin);
  88. AdminRole adminRole = new AdminRole();
  89. adminRole.setAdminId(admin.getId());
  90. adminRole.setRoleId(admin.getRoleId());
  91. return permissionMapper.addadminRole(adminRole);
  92. }
  93. }
  94. @Override
  95. public List<Role> getRole(String token) {
  96. return permissionMapper.getRole(token);
  97. }
  98. @Override
  99. @Transactional
  100. public Integer deleteAdmin(Integer id) {
  101. if (id == null) {
  102. return -1;
  103. }
  104. permissionMapper.deleteAdminRole(id);
  105. return permissionMapper.deleteAdmin(id);
  106. }
  107. @Override
  108. public Integer updateAdminRole(AdminRole adminRole) {
  109. return permissionMapper.updateAdminRole(adminRole);
  110. }
  111. //修改管理员状态(启用 不启用)
  112. @Override
  113. public Integer upadatePermission(Admin admin) throws Exception {
  114. return permissionMapper.updatePermission(admin);
  115. }
  116. //更新管理员信息
  117. @Override
  118. public Result updateAdmin(AdminVo adminVo) throws Exception {
  119. // 校验账号是否为空(账号是唯一标识,必须存在)
  120. if (ObjectUtils.isEmpty(adminVo.getAccount())) {
  121. throw new Exception("账号为空!");
  122. }
  123. // 校验用户名是否为空(管理员姓名为必填项)
  124. if (ObjectUtils.isEmpty(adminVo.getAdminName())) {
  125. throw new Exception("用户名为空!");
  126. }
  127. // 校验地区是否为空(管理员所属地区为必填项)
  128. if (ObjectUtils.isEmpty(adminVo.getMarket())) {
  129. throw new Exception("地区为空!");
  130. }
  131. if (adminVo.getMarket().contains("总部") && adminVo.getMarket().size() > 1) {
  132. throw new Exception("总部不可与其他地区共存");
  133. }
  134. // 校验职位是否为空(管理员职位为必填项)
  135. if (ObjectUtils.isEmpty(adminVo.getPostiton())) {
  136. throw new Exception("职位为空!");
  137. }
  138. // 校验机器编号格式(不允许包含逗号,避免与后续拼接逻辑冲突
  139. if (adminVo.getMachineId().contains(",")) {
  140. throw new Exception("机器编号格式错误");
  141. } else {
  142. Admin admin = new Admin();
  143. admin.setAccount(adminVo.getAccount());
  144. admin.setAdminName(adminVo.getAdminName());
  145. admin.setMarket(adminVo.getMarket());
  146. admin.setPostiton(adminVo.getPostiton());
  147. // 将地区列表转换为逗号分隔的字符串
  148. String markets = String.join(",", admin.getMarket());
  149. admin.setMarket(Collections.singletonList(markets));
  150. // 处理机器编号:若存在额外机器编号(machineIds),则与原编号拼接(用逗号分隔)
  151. if (adminVo.getMachineIds() == null) {
  152. admin.setMachineId(adminVo.getMachineId());
  153. } else {
  154. admin.setMachineId(adminVo.getMachineId() + "," + adminVo.getMachineIds());
  155. }
  156. admin.setRemark(adminVo.getRemark());
  157. permissionMapper.updateAdmin(admin);
  158. return Result.success("成功");
  159. }
  160. }
  161. }