AccountServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package com.fdkankan.openApi.service.system.impl;
  2. import cn.dev33.satoken.stp.SaLoginModel;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.collection.CollUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.fdkankan.common.constant.CommonStatus;
  10. import com.fdkankan.common.constant.ErrorCode;
  11. import com.fdkankan.common.exception.BusinessException;
  12. import com.fdkankan.common.util.Base64Converter;
  13. import com.fdkankan.common.util.PasswordUtils;
  14. import com.fdkankan.openApi.common.PageInfo;
  15. import com.fdkankan.openApi.constant.CountType;
  16. import com.fdkankan.openApi.entity.system.Account;
  17. import com.fdkankan.openApi.entity.system.UserAuthInfo;
  18. import com.fdkankan.openApi.entity.www.User;
  19. import com.fdkankan.openApi.httpclient.client.FdKKClient;
  20. import com.fdkankan.openApi.mapper.system.IAccountMapper;
  21. import com.fdkankan.openApi.service.system.IAccountService;
  22. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  23. import com.fdkankan.openApi.service.system.IUserAuthInfoService;
  24. import com.fdkankan.openApi.service.www.IUserService;
  25. import com.fdkankan.openApi.util.JwtUtil;
  26. import com.fdkankan.openApi.util.TokenUtil;
  27. import com.fdkankan.openApi.vo.system.*;
  28. import com.fdkankan.openApi.vo.www.FdkkLoginVo;
  29. import com.fdkankan.web.response.Result;
  30. import com.fdkankan.web.response.ResultData;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.stereotype.Service;
  33. import java.util.Calendar;
  34. import java.util.List;
  35. import java.util.Objects;
  36. /**
  37. * <p>
  38. * 账号表 服务实现类
  39. * </p>
  40. *
  41. * @author
  42. * @since 2023-05-18
  43. */
  44. @Service
  45. public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> implements IAccountService {
  46. @Autowired
  47. private FdKKClient fdKKClient;
  48. @Autowired
  49. private IUserAuthInfoService userAuthService;
  50. @Autowired
  51. private IUserService userService;
  52. @Override
  53. public Account findByUserName(String userName) {
  54. LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
  55. wrapper.eq(Account::getUserName, userName);
  56. Account one = this.getOne(wrapper);
  57. if (ObjectUtil.isNotNull(one)) {
  58. return one;
  59. } else {
  60. Account u = new Account();
  61. u.setUserName(userName);
  62. this.save(u);
  63. return u;
  64. }
  65. }
  66. @Override
  67. public Account findByUserName(String userName, Long userId, String head) {
  68. LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
  69. wrapper.eq(Account::getUserName, userName);
  70. Account account = this.getOne(wrapper);
  71. if (ObjectUtil.isNotNull(account)) {
  72. if (ObjectUtil.isNull(account.getHead())) {
  73. account.setHead(head);
  74. updateById(account);
  75. }
  76. return account;
  77. }
  78. account = new Account();
  79. account.setUserName(userName);
  80. account.setUserId(userId);
  81. account.setDescription("官网账号密码登录创建");
  82. this.save(account);
  83. return account;
  84. }
  85. @Override
  86. public Account findByTokenCovUserName(String token) {
  87. String userName = JwtUtil.getUserName(token);
  88. return findByUserName(userName);
  89. }
  90. @Override
  91. public ResultData longinV1(LoginParam param) {
  92. String password = PasswordUtils.decycptPasswordWeb(Base64Converter.encode(param.getPassword()));
  93. param.setPassword(password);
  94. Result<FdkkLoginVo> fdkkLoginVo = fdKKClient.fdkkLogin(param);
  95. // SaLoginModel saLoginModel = new SaLoginModel();
  96. // saLoginModel
  97. // .setIsLastingCookie(false)
  98. // .setToken(fdkkLoginVo.getData().getToken());
  99. FdkkLoginVo data = fdkkLoginVo.getData();
  100. Account account = this.findByUserName(data.getUser().getUserName(), data.getUser().getId(), data.getUser().getHead());
  101. if(Objects.nonNull(account)){
  102. UserAuthInfo userAuthInfo = userAuthService.findByUserIdCreateIfNotExits(account.getId(), account.getUserId());
  103. data.setAppKey(userAuthInfo.getAppKey());
  104. }
  105. // StpUtil.login(account.getUserName(), saLoginModel);
  106. // StpUtil.getSession().set("user", account);
  107. return ResultData.ok(data.getAppKey());
  108. }
  109. @Override
  110. public ResultData createApp(CreateAppUserParamVo param) {
  111. Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUserName, param.getUserName()));
  112. if(Objects.nonNull(account)){
  113. throw new BusinessException(ErrorCode.FAILURE_CODE_10002);
  114. }
  115. User user = userService.getByUserName(param.getUserName());
  116. if(Objects.isNull(user)){
  117. throw new BusinessException(ErrorCode.FAILURE_CODE_3021);
  118. }
  119. if(param.getState() != CommonStatus.NO.code().intValue()
  120. && param.getState() != CommonStatus.YES.code().intValue()){
  121. throw new BusinessException(ErrorCode.PARAM_ERROR);
  122. }
  123. Integer count = param.getCount();
  124. if(CountType.NO_LIMIT.code() != param.getCountType() && (Objects.isNull(count) || count < 1)){
  125. throw new BusinessException(ErrorCode.FAILURE_CODE_10004);
  126. }else{
  127. count = -1;
  128. }
  129. account = new Account();
  130. account.setUserId(user.getId());
  131. account.setUserName(user.getUserName());
  132. account.setCustomerName(param.getCustomerName());
  133. account.setDescription(param.getDescription());
  134. account.setCreaterId(param.getCreaterId());
  135. this.save(account);
  136. UserAuthInfo userAuthInfo = new UserAuthInfo();
  137. userAuthInfo.setAccountId(account.getId());
  138. userAuthInfo.setUserId(user.getId());
  139. userAuthInfo.setAppKey(TokenUtil.createToken());
  140. userAuthInfo.setTotalCount(count);
  141. userAuthInfo.setCallCount(0);
  142. userAuthInfo.setState(param.getState());
  143. userAuthInfo.setEffectTime(param.getEffectTime());
  144. userAuthInfo.setCreaterId(param.getCreaterId());
  145. userAuthService.save(userAuthInfo);
  146. //设置redis缓存
  147. userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
  148. return ResultData.ok();
  149. }
  150. @Override
  151. public ResultData updateApp(UpdateAppUserParamVo param) {
  152. Account account = this.getById(param.getId());
  153. if(Objects.isNull(account)){
  154. throw new BusinessException(ErrorCode.FAILURE_CODE_10003);
  155. }
  156. if(StrUtil.isNotEmpty(param.getUserName()) && !param.getUserName().equals(account.getUserName())){
  157. User user = userService.getByUserName(param.getUserName());
  158. if(Objects.isNull(user)){
  159. throw new BusinessException(ErrorCode.FAILURE_CODE_10003);
  160. }
  161. account.setUserName(param.getUserName());
  162. account.setUserId(user.getId());
  163. }
  164. account.setCustomerName(param.getCustomerName());
  165. account.setDescription(param.getDescription());
  166. account.setUpdaterId(param.getUpdaterId());
  167. this.updateById(account);
  168. UserAuthInfo userAuthInfo = userAuthService.findByAccountId(account.getId());
  169. if(Objects.nonNull(param.getCountType())){
  170. boolean dbIsInfinite = false;//原数据中是否无限制
  171. Integer totalCount = userAuthInfo.getTotalCount();
  172. if(totalCount < 0){
  173. totalCount = 0;
  174. dbIsInfinite = true;
  175. }
  176. //无限制次数不能减少次数
  177. if(dbIsInfinite && CountType.REDUCE.code() == param.getCountType()){
  178. throw new BusinessException(ErrorCode.PARAM_ERROR);
  179. }
  180. boolean currentIsInfinite = false;//当前修改数据中是否无限制
  181. Integer count = param.getCount();
  182. if(CountType.NO_LIMIT.code() == param.getCountType()){
  183. totalCount = -1;
  184. currentIsInfinite = true;
  185. }else if(CountType.REDUCE.code() == param.getCountType()){
  186. totalCount -= count;
  187. totalCount = totalCount < 0 ? 0 : totalCount;
  188. }else{
  189. totalCount += count;
  190. }
  191. userAuthInfo.setTotalCount(totalCount);
  192. if(dbIsInfinite != currentIsInfinite){//无限制改为有限制或者有限制改为无限制时,需要初始化call_count为0
  193. userAuthInfo.setCallCount(0);
  194. }
  195. //设置redis缓存
  196. userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
  197. }
  198. userAuthInfo.setUserId(account.getUserId());
  199. userAuthInfo.setState(param.getState());
  200. userAuthInfo.setUpdaterId(param.getUpdaterId());
  201. userAuthService.updateById(userAuthInfo);
  202. return ResultData.ok();
  203. }
  204. @Override
  205. public ResultData listApp(PageAppUserParamVo param) {
  206. Page<AppUserVo> appUserVoPage = this.baseMapper.pageList(new Page<>(param.getPageNum(), param.getPageSize()), param);
  207. List<AppUserVo> records = appUserVoPage.getRecords();
  208. if(CollUtil.isNotEmpty(records)){
  209. records.stream().forEach(record -> {
  210. record.setLeaveCount(record.getTotalCount() - record.getCallCount());
  211. });
  212. }
  213. return ResultData.ok(PageInfo.PageInfo(appUserVoPage));
  214. }
  215. @Override
  216. public ResultData createApp4Jmga(CreateAppUser4JmgaParamVo param) {
  217. Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUserName, param.getSystemId()));
  218. if(Objects.nonNull(account)){
  219. UserAuthInfo userAuthInfo = userAuthService.findByAccountId(account.getId());
  220. return ResultData.ok(userAuthInfo.getAppKey());
  221. }
  222. if(param.getState() != CommonStatus.NO.code().intValue()
  223. && param.getState() != CommonStatus.YES.code().intValue()){
  224. throw new BusinessException(ErrorCode.PARAM_ERROR);
  225. }
  226. Integer count = param.getCount();
  227. if(CountType.NO_LIMIT.code() != param.getCountType() && (Objects.isNull(count) || count < 1)){
  228. throw new BusinessException(ErrorCode.FAILURE_CODE_10004);
  229. }else{
  230. count = -1;
  231. }
  232. account = new Account();
  233. account.setUserName(param.getSystemId());
  234. account.setCustomerName(param.getSystemId());
  235. this.save(account);
  236. UserAuthInfo userAuthInfo = new UserAuthInfo();
  237. userAuthInfo.setAccountId(account.getId());
  238. userAuthInfo.setAppKey(TokenUtil.createToken());
  239. userAuthInfo.setTotalCount(count);
  240. userAuthInfo.setCallCount(0);
  241. userAuthInfo.setState(param.getState());
  242. userAuthInfo.setEffectTime(Calendar.getInstance().getTime());
  243. userAuthService.save(userAuthInfo);
  244. //设置redis缓存
  245. userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
  246. return ResultData.ok(userAuthInfo.getAppKey());
  247. }
  248. @Override
  249. public void deleteApp(UpdateAppUserParamVo param) {
  250. Account account = new Account();
  251. account.setId(param.getId());
  252. account.setUpdaterId(param.getUpdaterId());
  253. this.updateById(account);
  254. this.removeById(account.getId());
  255. }
  256. }