123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- package com.fdkankan.openApi.service.system.impl;
- import cn.dev33.satoken.stp.SaLoginModel;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.common.constant.CommonStatus;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.common.util.Base64Converter;
- import com.fdkankan.common.util.PasswordUtils;
- import com.fdkankan.openApi.common.PageInfo;
- import com.fdkankan.openApi.constant.CountType;
- import com.fdkankan.openApi.entity.system.Account;
- import com.fdkankan.openApi.entity.system.UserAuthInfo;
- import com.fdkankan.openApi.entity.www.User;
- import com.fdkankan.openApi.httpclient.client.FdKKClient;
- import com.fdkankan.openApi.mapper.system.IAccountMapper;
- import com.fdkankan.openApi.service.system.IAccountService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.openApi.service.system.IUserAuthInfoService;
- import com.fdkankan.openApi.service.www.IUserService;
- import com.fdkankan.openApi.util.JwtUtil;
- import com.fdkankan.openApi.util.TokenUtil;
- import com.fdkankan.openApi.vo.system.*;
- import com.fdkankan.openApi.vo.www.FdkkLoginVo;
- import com.fdkankan.web.response.Result;
- import com.fdkankan.web.response.ResultData;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.Calendar;
- import java.util.List;
- import java.util.Objects;
- /**
- * <p>
- * 账号表 服务实现类
- * </p>
- *
- * @author
- * @since 2023-05-18
- */
- @Service
- public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> implements IAccountService {
- @Autowired
- private FdKKClient fdKKClient;
- @Autowired
- private IUserAuthInfoService userAuthService;
- @Autowired
- private IUserService userService;
- @Override
- public Account findByUserName(String userName) {
- LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Account::getUserName, userName);
- Account one = this.getOne(wrapper);
- if (ObjectUtil.isNotNull(one)) {
- return one;
- } else {
- Account u = new Account();
- u.setUserName(userName);
- this.save(u);
- return u;
- }
- }
- @Override
- public Account findByUserName(String userName, Long userId, String head) {
- LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Account::getUserName, userName);
- Account account = this.getOne(wrapper);
- if (ObjectUtil.isNotNull(account)) {
- if (ObjectUtil.isNull(account.getHead())) {
- account.setHead(head);
- updateById(account);
- }
- return account;
- }
- account = new Account();
- account.setUserName(userName);
- account.setUserId(userId);
- account.setDescription("官网账号密码登录创建");
- this.save(account);
- return account;
- }
- @Override
- public Account findByTokenCovUserName(String token) {
- String userName = JwtUtil.getUserName(token);
- return findByUserName(userName);
- }
- @Override
- public ResultData longinV1(LoginParam param) {
- String password = PasswordUtils.decycptPasswordWeb(Base64Converter.encode(param.getPassword()));
- param.setPassword(password);
- Result<FdkkLoginVo> fdkkLoginVo = fdKKClient.fdkkLogin(param);
- // SaLoginModel saLoginModel = new SaLoginModel();
- // saLoginModel
- // .setIsLastingCookie(false)
- // .setToken(fdkkLoginVo.getData().getToken());
- FdkkLoginVo data = fdkkLoginVo.getData();
- Account account = this.findByUserName(data.getUser().getUserName(), data.getUser().getId(), data.getUser().getHead());
- if(Objects.nonNull(account)){
- UserAuthInfo userAuthInfo = userAuthService.findByUserIdCreateIfNotExits(account.getId(), account.getUserId());
- data.setAppKey(userAuthInfo.getAppKey());
- }
- // StpUtil.login(account.getUserName(), saLoginModel);
- // StpUtil.getSession().set("user", account);
- return ResultData.ok(data.getAppKey());
- }
- @Override
- public ResultData createApp(CreateAppUserParamVo param) {
- Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUserName, param.getUserName()));
- if(Objects.nonNull(account)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_10002);
- }
- User user = userService.getByUserName(param.getUserName());
- if(Objects.isNull(user)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_3021);
- }
- if(param.getState() != CommonStatus.NO.code().intValue()
- && param.getState() != CommonStatus.YES.code().intValue()){
- throw new BusinessException(ErrorCode.PARAM_ERROR);
- }
- Integer count = param.getCount();
- if(CountType.NO_LIMIT.code() != param.getCountType() && (Objects.isNull(count) || count < 1)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_10004);
- }else{
- count = -1;
- }
- account = new Account();
- account.setUserId(user.getId());
- account.setUserName(user.getUserName());
- account.setCustomerName(param.getCustomerName());
- account.setDescription(param.getDescription());
- account.setCreaterId(param.getCreaterId());
- this.save(account);
- UserAuthInfo userAuthInfo = new UserAuthInfo();
- userAuthInfo.setAccountId(account.getId());
- userAuthInfo.setUserId(user.getId());
- userAuthInfo.setAppKey(TokenUtil.createToken());
- userAuthInfo.setTotalCount(count);
- userAuthInfo.setCallCount(0);
- userAuthInfo.setState(param.getState());
- userAuthInfo.setEffectTime(param.getEffectTime());
- userAuthInfo.setCreaterId(param.getCreaterId());
- userAuthService.save(userAuthInfo);
- //设置redis缓存
- userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
- return ResultData.ok();
- }
- @Override
- public ResultData updateApp(UpdateAppUserParamVo param) {
- Account account = this.getById(param.getId());
- if(Objects.isNull(account)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_10003);
- }
- if(StrUtil.isNotEmpty(param.getUserName()) && !param.getUserName().equals(account.getUserName())){
- User user = userService.getByUserName(param.getUserName());
- if(Objects.isNull(user)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_10003);
- }
- account.setUserName(param.getUserName());
- account.setUserId(user.getId());
- }
- account.setCustomerName(param.getCustomerName());
- account.setDescription(param.getDescription());
- account.setUpdaterId(param.getUpdaterId());
- this.updateById(account);
- UserAuthInfo userAuthInfo = userAuthService.findByAccountId(account.getId());
- if(Objects.nonNull(param.getCountType())){
- boolean dbIsInfinite = false;//原数据中是否无限制
- Integer totalCount = userAuthInfo.getTotalCount();
- if(totalCount < 0){
- totalCount = 0;
- dbIsInfinite = true;
- }
- //无限制次数不能减少次数
- if(dbIsInfinite && CountType.REDUCE.code() == param.getCountType()){
- throw new BusinessException(ErrorCode.PARAM_ERROR);
- }
- boolean currentIsInfinite = false;//当前修改数据中是否无限制
- Integer count = param.getCount();
- if(CountType.NO_LIMIT.code() == param.getCountType()){
- totalCount = -1;
- currentIsInfinite = true;
- }else if(CountType.REDUCE.code() == param.getCountType()){
- totalCount -= count;
- totalCount = totalCount < 0 ? 0 : totalCount;
- }else{
- totalCount += count;
- }
- userAuthInfo.setTotalCount(totalCount);
- if(dbIsInfinite != currentIsInfinite){//无限制改为有限制或者有限制改为无限制时,需要初始化call_count为0
- userAuthInfo.setCallCount(0);
- }
- //设置redis缓存
- userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
- }
- userAuthInfo.setUserId(account.getUserId());
- userAuthInfo.setState(param.getState());
- userAuthInfo.setUpdaterId(param.getUpdaterId());
- userAuthService.updateById(userAuthInfo);
- return ResultData.ok();
- }
- @Override
- public ResultData listApp(PageAppUserParamVo param) {
- Page<AppUserVo> appUserVoPage = this.baseMapper.pageList(new Page<>(param.getPageNum(), param.getPageSize()), param);
- List<AppUserVo> records = appUserVoPage.getRecords();
- if(CollUtil.isNotEmpty(records)){
- records.stream().forEach(record -> {
- record.setLeaveCount(record.getTotalCount() - record.getCallCount());
- });
- }
- return ResultData.ok(PageInfo.PageInfo(appUserVoPage));
- }
- @Override
- public ResultData createApp4Jmga(CreateAppUser4JmgaParamVo param) {
- Account account = this.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUserName, param.getSystemId()));
- if(Objects.nonNull(account)){
- UserAuthInfo userAuthInfo = userAuthService.findByAccountId(account.getId());
- return ResultData.ok(userAuthInfo.getAppKey());
- }
- if(param.getState() != CommonStatus.NO.code().intValue()
- && param.getState() != CommonStatus.YES.code().intValue()){
- throw new BusinessException(ErrorCode.PARAM_ERROR);
- }
- Integer count = param.getCount();
- if(CountType.NO_LIMIT.code() != param.getCountType() && (Objects.isNull(count) || count < 1)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_10004);
- }else{
- count = -1;
- }
- account = new Account();
- account.setUserName(param.getSystemId());
- account.setCustomerName(param.getSystemId());
- this.save(account);
- UserAuthInfo userAuthInfo = new UserAuthInfo();
- userAuthInfo.setAccountId(account.getId());
- userAuthInfo.setAppKey(TokenUtil.createToken());
- userAuthInfo.setTotalCount(count);
- userAuthInfo.setCallCount(0);
- userAuthInfo.setState(param.getState());
- userAuthInfo.setEffectTime(Calendar.getInstance().getTime());
- userAuthService.save(userAuthInfo);
- //设置redis缓存
- userAuthService.setReidsCount(userAuthInfo.getAppKey(), param.getCountType(), count);
- return ResultData.ok(userAuthInfo.getAppKey());
- }
- @Override
- public void deleteApp(UpdateAppUserParamVo param) {
- Account account = new Account();
- account.setId(param.getId());
- account.setUpdaterId(param.getUpdaterId());
- this.updateById(account);
- this.removeById(account.getId());
- }
- }
|