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;
/**
*
* 账号表 服务实现类
*
*
* @author
* @since 2023-05-18
*/
@Service
public class AccountServiceImpl extends ServiceImpl implements IAccountService {
@Autowired
private FdKKClient fdKKClient;
@Autowired
private IUserAuthInfoService userAuthService;
@Autowired
private IUserService userService;
@Override
public Account findByUserName(String userName) {
LambdaQueryWrapper 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 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 = 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().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 appUserVoPage = this.baseMapper.pageList(new Page<>(param.getPageNum(), param.getPageSize()), param);
List 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().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());
}
}