JyUserPlatformServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.fdkankan.manage.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.fdkankan.manage.common.ExcelErrorUtil;
  6. import com.fdkankan.manage.common.PageInfo;
  7. import com.fdkankan.manage.common.ResultCode;
  8. import com.fdkankan.manage.entity.*;
  9. import com.fdkankan.manage.service.*;
  10. import com.fdkankan.manage.vo.JyUserPlatform;
  11. import com.fdkankan.manage.exception.BusinessException;
  12. import com.fdkankan.manage.mapper.IJyUserPlatformMapper;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.fdkankan.manage.vo.request.*;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import javax.management.relation.Role;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Objects;
  24. import java.util.stream.Collectors;
  25. /**
  26. * <p>
  27. * 服务实现类
  28. * </p>
  29. *
  30. * @author
  31. * @since 2024-11-15
  32. */
  33. @Service
  34. @Slf4j
  35. public class JyUserPlatformServiceImpl extends ServiceImpl<IJyUserPlatformMapper, JyUserPlatform> implements IJyUserPlatformService {
  36. @Autowired
  37. IJyPlatformService platformService;
  38. @Autowired
  39. IJyUserService jyUserService;
  40. @Autowired
  41. ISysUserService sysUserService;
  42. @Autowired
  43. ISysRoleService sysRoleService;
  44. @Override
  45. public void bindPlatform(Integer id, Integer platformId) {
  46. jyUserService.updatePlatformId(id,platformId);
  47. }
  48. @Override
  49. public void addByParam(JyUserPlatformAddParam param) {
  50. if(param.getId() == null){
  51. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  52. }
  53. Integer loginPlatformId = this.getLoginPlatformId();
  54. if(loginPlatformId == null){
  55. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  56. }
  57. param.setPlatformId(loginPlatformId);
  58. JyUser jyUser = jyUserService.getById(param.getId());
  59. SysUser byId = sysUserService.getById(jyUser.getSysUserId());
  60. if(byId.getRoleId() != 47L ){
  61. throw new BusinessException(ResultCode.PLATFORM_USER_ADDERROR);
  62. }
  63. this.bindPlatform(param.getId(),loginPlatformId);
  64. }
  65. @Override
  66. public void updateByParam(JyUserPlatformAddParam param) {
  67. if(param.getId() == null || param.getPlatformId() == null){
  68. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  69. }
  70. JyUser jyUser = jyUserService.getById(param.getId());
  71. SysUser byId = sysUserService.getById(jyUser.getSysUserId());
  72. if(byId.getRoleId() != 47L ){
  73. throw new BusinessException(ResultCode.PLATFORM_USER_ADDERROR);
  74. }
  75. this.bindPlatform(param.getId(),param.getPlatformId());
  76. }
  77. @Override
  78. public void del(JyUserPlatformAddParam param) {
  79. if(param.getId() == null){
  80. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  81. }
  82. LambdaQueryWrapper<JyUserPlatform> wrapper = new LambdaQueryWrapper<>();
  83. wrapper.eq(JyUserPlatform::getId,param.getId());
  84. this.removeById(wrapper);
  85. }
  86. @Override
  87. public Object pageList(JyUserPlatformParam param) {
  88. SysUser byId = sysUserService.getById(Long.valueOf(StpUtil.getLoginId().toString()));
  89. if(byId.getRoleId() != 1L ){
  90. Integer loginPlatformId = getLoginPlatformId();
  91. if(loginPlatformId == null){
  92. return PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize()));
  93. }
  94. param.setPlatformId(getLoginPlatformId());
  95. }
  96. Page<JyUserPlatformVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  97. List<Integer> ids = page.getRecords().stream().map(JyUserPlatform::getPlatformId).collect(Collectors.toList());
  98. HashMap<Integer,JyPlatform> map = new HashMap<>();
  99. if(!ids.isEmpty()){
  100. List<JyPlatform> jyPlatforms = platformService.listByIds(ids);
  101. jyPlatforms.forEach(e -> map.put(e.getId(),e));
  102. }
  103. for (JyUserPlatformVo record : page.getRecords()) {
  104. if(record.getRyId() == null){
  105. record.setCreateTime(null);
  106. }
  107. JyPlatform jyPlatform = map.get(record.getPlatformId());
  108. if(jyPlatform != null){
  109. record.setPlatformName(jyPlatform.getPlatformName());
  110. }
  111. }
  112. return PageInfo.PageInfo(page);
  113. }
  114. @Override
  115. public Object queryByKey(JyUserPlatformParam param) {
  116. if(StringUtils.isBlank(param.getQueryKey())){
  117. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  118. }
  119. List<JyUserPlatform> jyUserPlatforms = this.getBaseMapper().queryByKey(param.getQueryKey());
  120. List<Integer> collect = jyUserPlatforms.stream().map(JyUserPlatform::getPlatformId).filter(Objects::nonNull).collect(Collectors.toList());
  121. HashMap<Integer,JyPlatform> map = new HashMap<>();
  122. if(!collect.isEmpty()){
  123. List<JyPlatform> jyPlatforms = platformService.listByIds(collect);
  124. jyPlatforms.forEach(e -> map.put(e.getId(),e));
  125. }
  126. List<Long> sysUserIds = jyUserPlatforms.stream().map(JyUserPlatform::getSysUserId).collect(Collectors.toList());
  127. HashMap<Long,SysUser> sysUserMap = new HashMap<>();
  128. HashMap<Long, SysRole> roleMap = new HashMap<>();
  129. if(!sysUserIds.isEmpty()){
  130. List<SysUser> sysUsers = sysUserService.listByIds(sysUserIds);
  131. sysUsers.forEach(e -> sysUserMap.put(e.getId(),e));
  132. List<Long> roleIds = sysUsers.stream().map(SysUser::getRoleId).collect(Collectors.toList());
  133. if(!roleIds.isEmpty()){
  134. List<SysRole> sysRoles = sysRoleService.listByIds(roleIds);
  135. sysRoles.forEach(e -> roleMap.put(e.getId(),e));
  136. }
  137. }
  138. for (JyUserPlatform jyUserPlatform : jyUserPlatforms) {
  139. if(jyUserPlatform.getPlatformId()!=null){
  140. JyPlatform jyPlatform =map.get(jyUserPlatform.getPlatformId());
  141. if(jyPlatform != null){
  142. jyUserPlatform.setPlatformName(jyPlatform.getPlatformName());
  143. }
  144. SysUser sysUser = sysUserMap.get(jyUserPlatform.getSysUserId());
  145. if(sysUser != null){
  146. jyUserPlatform.setRoleId(sysUser.getRoleId());
  147. }
  148. SysRole sysRole = roleMap.get(jyUserPlatform.getRoleId());
  149. if(sysRole != null){
  150. jyUserPlatform.setRoleName(sysRole.getRoleName());
  151. }
  152. }
  153. }
  154. return jyUserPlatforms;
  155. }
  156. @Override
  157. public Integer getLoginPlatformId(){
  158. try {
  159. return Integer.valueOf(StpUtil.getExtra("platformId").toString());
  160. }catch (Exception e){
  161. log.info("StpUtil.getExtra -- platformId-error:{}",e);
  162. }
  163. return null;
  164. }
  165. @Autowired
  166. IExcelService excelService;
  167. @Override
  168. public Integer addPlatformUser(List<HashMap<Integer, String>> excelRowList) {
  169. List<JyUserPlatformAddParam> params = new ArrayList<>();
  170. List<Integer> errorIndex = new ArrayList<>();
  171. List<Integer> updatePlatformUserList = new ArrayList<>();
  172. Integer index = 0;
  173. for (HashMap<Integer, String> map : excelRowList) {
  174. index++;
  175. if (map.isEmpty()) {
  176. continue;
  177. }
  178. if (index == 0 && !map.get(0).equals("平台用户模板")) {
  179. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  180. }
  181. if (index < 4) {
  182. continue;
  183. }
  184. String name = map.get(0);
  185. if(StringUtils.isBlank(name) ){
  186. errorIndex.add(index -3 );
  187. continue;
  188. }
  189. JyUser user = jyUserService.getByNickName(name);
  190. if(user != null && user.getPlatformId() == null){
  191. updatePlatformUserList.add(user.getId());
  192. continue;
  193. }
  194. if(user != null ){
  195. errorIndex.add(index -3 );
  196. continue;
  197. }
  198. JyPlatformUserWait jyPlatformUserWait = platformUserWaitService.getByName(name);
  199. if(jyPlatformUserWait != null ){
  200. errorIndex.add(index -3 );
  201. continue;
  202. }
  203. List<JyUserPlatformAddParam> collect = params.stream().filter(e -> e.getName().equals(name)).collect(Collectors.toList());
  204. if(!collect.isEmpty()){
  205. errorIndex.add(index -3 );
  206. continue;
  207. }
  208. JyUserPlatformAddParam param = new JyUserPlatformAddParam();
  209. param.setName(name);
  210. params.add(param);
  211. }
  212. if(!errorIndex.isEmpty()){
  213. excelService.toExcelError(errorIndex);
  214. }
  215. if(params.size() <=0 && updatePlatformUserList.isEmpty()){
  216. throw new BusinessException(ResultCode.TEMPLATE_EMPTY);
  217. }
  218. Integer count = this.addPlatformUsers(params);
  219. Integer count1 = 0;
  220. for (Integer jyUserId : updatePlatformUserList) {
  221. if(getLoginPlatformId() != null){
  222. jyUserService.updatePlatformId(jyUserId,getLoginPlatformId());
  223. count1 ++;
  224. }
  225. }
  226. return count1 + count;
  227. }
  228. @Autowired
  229. IJyPlatformUserWaitService platformUserWaitService;
  230. private Integer addPlatformUsers(List<JyUserPlatformAddParam> params) {
  231. Integer count = 0;
  232. for (JyUserPlatformAddParam param : params) {
  233. JyPlatformUserWait jyPlatformUserWait = new JyPlatformUserWait();
  234. jyPlatformUserWait.setName(param.getName());
  235. jyPlatformUserWait.setPlatformId(this.getLoginPlatformId());
  236. platformUserWaitService.save(jyPlatformUserWait);
  237. count ++;
  238. }
  239. return count;
  240. }
  241. }