package com.fdkankan.manage.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.dingtalk.DingTalkSendUtils;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.entity.*;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.mapper.ICameraDetailMapper;
import com.fdkankan.manage.mapper.IRtkAccountMapper;
import com.fdkankan.manage.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.task.DingdingService;
import com.fdkankan.manage.task.TaskService;
import com.fdkankan.manage.util.SendMailUtils;
import com.fdkankan.manage.vo.request.RtkInfoParam;
import com.fdkankan.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2024-07-22
*/
@Service
@Slf4j
public class RtkAccountServiceImpl extends ServiceImpl implements IRtkAccountService {
@Autowired
RedisUtil redisUtil;
@Autowired
IRtkUseLogService rtkUseLogService;
@Autowired
ISysUserService sysUserService;
@Autowired
DingdingService dingdingService;
@Autowired
TaskService taskService;
@Autowired
ICameraService cameraService;
@Autowired
ICameraDetailService cameraDetailService;
@Override
public RtkAccount getOneNotUseAccount(String rtkSnCode,String cameraSn) {
String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
Long time = 8 * 60 * 60L;
if(StringUtils.isNotBlank(cameraSn)){
Camera camera = cameraService.getBySnCode(cameraSn);
if(camera != null){
CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
if(cameraDetail != null && ( cameraDetail.getType() == 10 || cameraDetail.getType() == 11)){
time = 4 * 60 * 60L;
}
}
}
if(redisUtil.hasKey(redisKey)){
String jsonStr = redisUtil.get(redisKey);
redisUtil.expire(redisKey,time);
return JSONObject.parseObject(jsonStr, RtkAccount.class);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(RtkAccount::getStatus,0,1);
wrapper.orderByAsc(RtkAccount::getUpdateTime);
wrapper.orderByAsc(RtkAccount::getId);
List list = this.list(wrapper);
if(list == null || list.isEmpty()){
//账号库存不足,钉钉通知
dingdingService.sendDingDingMsg(0);
throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT);
}
dingdingService.modelThreshold(list.size() -1,this.count());
RtkAccount rtkAccount = null;
RtkUseLog rtkUseLog = rtkUseLogService.getByRtkSn(rtkSnCode);
if(rtkUseLog != null && rtkUseLog.getRtkAccountId() != null){
RtkAccount rtkAccount2 = this.getById(rtkUseLog.getRtkAccountId());
if(rtkAccount2 != null && rtkAccount2.getStatus() == 1){
rtkAccount = rtkAccount2;
}
}
if(rtkAccount == null){
rtkAccount = list.get(0);
}
updateAccountStatus(rtkAccount.getId(),2);
redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount),time);
return rtkAccount;
}
@Override
public void updateAccountStatus(Integer id, int status) {
LambdaUpdateWrapper wrapper1 = new LambdaUpdateWrapper<>();
wrapper1.eq(RtkAccount::getId,id);
wrapper1.set(RtkAccount::getStatus,status);
this.update(wrapper1);
}
@Override
public void stop(String rtkSnCode) {
String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
if(redisUtil.hasKey(redisKey)){
String jsonStr = redisUtil.get(redisKey);
RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
updateAccountStatus(rtkAccount.getId(),1);
redisUtil.del(redisKey);
}
}
@Override
public Object pageList(RtkInfoParam param) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(param.getUserName())){
wrapper.like(RtkAccount::getUserName,param.getUserName());
}
if(StringUtils.isNotBlank(param.getStartTime())&& StringUtils.isNotBlank(param.getEndTime()) ) {
wrapper.between(RtkAccount::getCreateTime,param.getStartTime(),param.getEndTime());
}
wrapper.orderByDesc(RtkAccount::getCreateTime);
wrapper.orderByDesc(RtkAccount::getId);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
Set createUserIds = page.getRecords().stream().map(RtkAccount::getCreateUserId).collect(Collectors.toSet());
HashMap byIds = sysUserService.getByIds(createUserIds);
for (RtkAccount record : page.getRecords()) {
SysUser sysUser = byIds.get(record.getCreateUserId());
if(sysUser != null){
record.setCreateUserName(sysUser.getUserName());
record.setCreateNickName(sysUser.getNickName());
}
}
return PageInfo.PageInfo(page);
}
@Override
public void saveOrEditEntity(RtkAccount param) {
if(StringUtils.isBlank(param.getUserName()) || param.getPassword() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
RtkAccount rtkAccount = this.getByUserName(param.getUserName());
if(rtkAccount != null && param.getId() == null){
throw new BusinessException(ResultCode.RTK_SN_EXIST);
}
if(rtkAccount != null && !param.getId().equals(rtkAccount.getId())){
throw new BusinessException(ResultCode.RTK_SN_EXIST);
}
if(param.getId()== null){
param.setCreateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
}
if(param.getId()!= null){
param.setUpdateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
}
this.saveOrUpdate(param);
}
@Override
public void del(RtkAccount param) {
if(param.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
RtkAccount rtkAccount = this.getById(param.getId());
if(rtkAccount != null){
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(RtkAccount::getId,param.getId());
wrapper.set(RtkAccount::getDelUserId,Long.valueOf(StpUtil.getLoginId().toString()));
wrapper.set(RtkAccount::getRecStatus,rtkAccount.getId());
this.update(wrapper);
}
}
@Override
public RtkAccount getByUserName(String userName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(RtkAccount::getUserName,userName);
return this.getOne(wrapper);
}
}