JyUserPlatformServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.JyPlatform;
  9. import com.fdkankan.manage.entity.JyPlatformUserWait;
  10. import com.fdkankan.manage.entity.JyUser;
  11. import com.fdkankan.manage.entity.SysUser;
  12. import com.fdkankan.manage.service.*;
  13. import com.fdkankan.manage.vo.JyUserPlatform;
  14. import com.fdkankan.manage.exception.BusinessException;
  15. import com.fdkankan.manage.mapper.IJyUserPlatformMapper;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.fdkankan.manage.vo.request.*;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Objects;
  26. import java.util.stream.Collectors;
  27. /**
  28. * <p>
  29. * 服务实现类
  30. * </p>
  31. *
  32. * @author
  33. * @since 2024-11-15
  34. */
  35. @Service
  36. @Slf4j
  37. public class JyUserPlatformServiceImpl extends ServiceImpl<IJyUserPlatformMapper, JyUserPlatform> implements IJyUserPlatformService {
  38. @Autowired
  39. IJyPlatformService platformService;
  40. @Autowired
  41. IJyUserService jyUserService;
  42. @Autowired
  43. ISysUserService sysUserService;
  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. for (JyUserPlatform jyUserPlatform : jyUserPlatforms) {
  127. if(jyUserPlatform.getPlatformId()!=null){
  128. JyPlatform jyPlatform =map.get(jyUserPlatform.getPlatformId());
  129. if(jyPlatform != null){
  130. jyUserPlatform.setPlatformName(jyPlatform.getPlatformName());
  131. }
  132. }
  133. }
  134. return jyUserPlatforms;
  135. }
  136. @Override
  137. public Integer getLoginPlatformId(){
  138. try {
  139. return Integer.valueOf(StpUtil.getExtra("platformId").toString());
  140. }catch (Exception e){
  141. log.info("StpUtil.getExtra -- platformId-error:{}",e);
  142. }
  143. return null;
  144. }
  145. @Autowired
  146. IExcelService excelService;
  147. @Override
  148. public Integer addPlatformUser(List<HashMap<Integer, String>> excelRowList) {
  149. List<JyUserPlatformAddParam> params = new ArrayList<>();
  150. List<Integer> errorIndex = new ArrayList<>();
  151. List<Integer> updatePlatformUserList = new ArrayList<>();
  152. Integer index = 0;
  153. for (HashMap<Integer, String> map : excelRowList) {
  154. index++;
  155. if (map.isEmpty()) {
  156. continue;
  157. }
  158. if (index == 0 && !map.get(0).equals("平台用户模板")) {
  159. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  160. }
  161. if (index < 4) {
  162. continue;
  163. }
  164. String name = map.get(0);
  165. if(StringUtils.isBlank(name) ){
  166. errorIndex.add(index -3 );
  167. continue;
  168. }
  169. JyUser user = jyUserService.getByNickName(name);
  170. if(user != null && user.getPlatformId() == null){
  171. updatePlatformUserList.add(user.getId());
  172. continue;
  173. }
  174. if(user != null ){
  175. errorIndex.add(index -3 );
  176. continue;
  177. }
  178. JyPlatformUserWait jyPlatformUserWait = platformUserWaitService.getByName(name);
  179. if(jyPlatformUserWait != null ){
  180. errorIndex.add(index -3 );
  181. continue;
  182. }
  183. List<JyUserPlatformAddParam> collect = params.stream().filter(e -> e.getName().equals(name)).collect(Collectors.toList());
  184. if(!collect.isEmpty()){
  185. errorIndex.add(index -3 );
  186. continue;
  187. }
  188. JyUserPlatformAddParam param = new JyUserPlatformAddParam();
  189. param.setName(name);
  190. params.add(param);
  191. }
  192. if(!errorIndex.isEmpty()){
  193. excelService.toExcelError(errorIndex);
  194. }
  195. if(params.size() <=0 && updatePlatformUserList.isEmpty()){
  196. throw new BusinessException(ResultCode.TEMPLATE_EMPTY);
  197. }
  198. Integer count = this.addPlatformUsers(params);
  199. Integer count1 = 0;
  200. for (Integer jyUserId : updatePlatformUserList) {
  201. if(getLoginPlatformId() != null){
  202. jyUserService.updatePlatformId(jyUserId,getLoginPlatformId());
  203. count1 ++;
  204. }
  205. }
  206. return count1 + count;
  207. }
  208. @Autowired
  209. IJyPlatformUserWaitService platformUserWaitService;
  210. private Integer addPlatformUsers(List<JyUserPlatformAddParam> params) {
  211. Integer count = 0;
  212. for (JyUserPlatformAddParam param : params) {
  213. JyPlatformUserWait jyPlatformUserWait = new JyPlatformUserWait();
  214. jyPlatformUserWait.setName(param.getName());
  215. jyPlatformUserWait.setPlatformId(this.getLoginPlatformId());
  216. platformUserWaitService.save(jyPlatformUserWait);
  217. count ++;
  218. }
  219. return count;
  220. }
  221. }