package com.fdkankan.ucenter.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.constant.SceneConstant;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.ucenter.common.PageInfo;
import com.fdkankan.ucenter.common.RedisKeyUtil;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.httpClient.service.LaserService;
import com.fdkankan.ucenter.mapper.ISceneCooperationMapper;
import com.fdkankan.ucenter.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.request.SceneCooperationParam;
import com.fdkankan.ucenter.vo.request.SceneParam;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-07-04
*/
@Service
public class SceneCooperationServiceImpl extends ServiceImpl implements ISceneCooperationService {
@Autowired
ISceneProService sceneProService;
@Autowired
ISceneService sceneService;
@Autowired
IScenePlusService scenePlusService;
@Autowired
LaserService fdkkLaserService;
@Autowired
ISceneResourceCooperationService sceneResourceCooperationService;
@Autowired
ICameraService cameraService;
@Autowired
ICameraDetailService cameraDetailService;
@Autowired
ISceneResourceService sceneResourceService;
@Autowired
IUserService userService;
@Autowired
RedisUtil redisUtil;
@Autowired
IMailTemplateService mailTemplateService;
@Override
public Long getCooperationSceneNum(Long userId, List sceneSourceList) {
Long cooperationSceneProNum = this.getBaseMapper().getCooperationSceneProNum(userId, sceneSourceList);
Long cooperationScenePlusNum = this.getBaseMapper().getCooperationScenePlusNum(userId, sceneSourceList);
return cooperationSceneProNum + cooperationScenePlusNum;
}
@Override
public void deleteCooperationList(List sceneProList,List scenePlusList) {
if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
return;
}
List numList = sceneProList.stream().map(ScenePro::getNum).collect(Collectors.toList());
List numList2 = scenePlusList.stream().map(ScenePlus::getNum).collect(Collectors.toList());
numList.addAll(numList2);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(SceneCooperation::getSceneNum,numList);
List list = this.list(wrapper);
List ids = list.stream().map(SceneCooperation::getId).collect(Collectors.toList());
if(ids.size() >0){
this.removeByIds(ids);
sceneResourceCooperationService.deleteBatchByCooperationIds(ids);
}
for (String num : numList) {
redisUtil.hdel(RedisKeyUtil.SCENE_COOPERATION_NUM_USERID,num);
}
}
@Override
public void saveBatchByList(List sceneProList, List scenePlusList, Long userId,List resourceIdList) {
List list = new ArrayList<>();
for (ScenePro scenePro : sceneProList) {
SceneCooperation sceneCooperationEntity = new SceneCooperation();
sceneCooperationEntity.setUserId(userId);
sceneCooperationEntity.setSceneNum(scenePro.getNum());
sceneCooperationEntity.setRecStatus("A");
sceneCooperationEntity.setCreateTime(DateUserUtil.getDate(new Date()));
sceneCooperationEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
list.add(sceneCooperationEntity);
}
for (ScenePlus scenePlus : scenePlusList) {
SceneCooperation sceneCooperationEntity = new SceneCooperation();
sceneCooperationEntity.setUserId(userId);
sceneCooperationEntity.setSceneNum(scenePlus.getNum());
sceneCooperationEntity.setRecStatus("A");
sceneCooperationEntity.setCreateTime(DateUserUtil.getDate(new Date()));
sceneCooperationEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
list.add(sceneCooperationEntity);
}
for (SceneCooperation sceneCooperation : list) {
redisUtil.hset(RedisKeyUtil.SCENE_COOPERATION_NUM_USERID, sceneCooperation.getSceneNum(), sceneCooperation.getUserId() + "");
}
this.saveBatch(list);
if(resourceIdList.size() >0){
List resourceCooperationList = new ArrayList<>();
for (SceneCooperation sceneCooperation : list) {
for (Long resourceId : resourceIdList) {
SceneResourceCooperation sceneResourceCooperation = new SceneResourceCooperation();
sceneResourceCooperation.setSceneResourceId(resourceId);
sceneResourceCooperation.setSceneCooperationId(sceneCooperation.getId());
sceneResourceCooperation.setRecStatus("A");
sceneResourceCooperation.setCreateTime(DateUserUtil.getDate(new Date()));
sceneResourceCooperation.setUpdateTime(DateUserUtil.getDate(new Date()));
resourceCooperationList.add(sceneResourceCooperation);
}
}
if(resourceCooperationList.size() >0){
sceneResourceCooperationService.saveBatch(resourceCooperationList,10000);
}
}
}
@Override
public JSONObject sceneResourceList(SceneCooperationParam param) {
JSONObject jsonObject = new JSONObject();
List exclude = new ArrayList<>();
SceneResource excludeEntity = new SceneResource();
excludeEntity.setKeyWord("data");
exclude.add(excludeEntity);
jsonObject.put("exclude", exclude);
if(param.getCameraId() != null){
Camera cameraEntity = cameraService.getById(param.getCameraId());
if(cameraEntity != null){
CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
if(cameraDetailEntity.getCompanyId() != null && cameraDetailEntity.getCompanyId() == 1){
jsonObject.put("exclude", new ArrayList<>());
}
}
}
List sceneProList;
List scenePlusList;
//如果是场景协作,判断是V3的场景还是V4的场景,如果是v4场景,则查询v4的菜单资源
if(param.getType() != null && param.getType() == 1){
if(StringUtils.isEmpty(param.getSceneNum()) && CollectionUtils.isEmpty(param.getNumList())){
throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
}
List numList = param.getNumList();
if(numList == null){
numList= new ArrayList<>();
numList.add(param.getSceneNum());
}
sceneProList = sceneProService.getListByNums(numList);
scenePlusList = scenePlusService.getListByNums(numList);
if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
}
}else {
//如果是相机协作,判断这个相机id下有没有V4的场景,如果有,则列出对应的菜单资源
if(ObjectUtils.isEmpty(param.getCameraId()) && CollectionUtils.isEmpty(param.getCameraIdList())){
throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
}
List cameraIdList = param.getCameraIdList();
if(cameraIdList == null){
cameraIdList= new ArrayList<>();
cameraIdList.add(param.getCameraId());
}
sceneProList = sceneProService.getListByCameraIds(cameraIdList);
scenePlusList = scenePlusService.getListByCameraIds(cameraIdList);
}
List versionList = Lists.newArrayList();
if(sceneProList.size() >0){
versionList.add("v3");
}
if(scenePlusList.size() >0){
versionList.add("v4");
}
if(versionList.size() <=0){
versionList.add("v3");
}
List results = sceneResourceService.getByVersion(versionList);
jsonObject.put("include", results);
return jsonObject;
}
@Override
public JSONObject cooperationSceneListNew(SceneParam param, String username) {
User user = userService.getByUserName(username);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SceneCooperation::getUserId,user.getId());
List list = this.list(wrapper);
List numList = list.parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toList());
if(numList.size() <=0){
JSONObject jsonObject = new JSONObject();
jsonObject.put("list", new ArrayList<>());
jsonObject.put("total",0);
return jsonObject;
}
param.setNumList(numList);
param.setHasFolder(0);
JSONObject jsonObject = sceneProService.newList(param, username);
return jsonObject.getJSONObject("pageInfo") ;
}
@Override
public void saveCooperation(SceneCooperationParam param, String username) {
if(StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getSceneNum()) || StringUtils.isEmpty(param.getResourceIds())){
throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
}
if(param.getUserName().equals( username)){
throw new BusinessException(LoginConstant.FAILURE_CODE_3024, LoginConstant.FAILURE_MSG_3024);
}
User user = userService.getByUserName(param.getUserName());
if(user == null){
throw new BusinessException(LoginConstant.FAILURE_CODE_3021, LoginConstant.FAILURE_MSG_3021);
}
String[] nums = param.getSceneNum().split(",");
List numList = Arrays.asList(nums);
List proList = sceneProService.getListByNums(numList);
List plusList = scenePlusService.getListByNums(numList);
this.deleteCooperationList(proList,plusList);
List resourceIdList = new ArrayList<>();
if (StringUtils.isNotEmpty(param.getResourceIds())) {
for (String rId : param.getResourceIds().split(",")) {
resourceIdList.add(Long.valueOf(rId));
}
}
this.saveBatchByList(proList,plusList,user.getId(),resourceIdList);
mailTemplateService.sendSceneCooperation(proList,plusList,param.getUserName(),param.getLang());
}
@Override
public void deleteCooperation(SceneCooperationParam param, String username) {
if(StringUtils.isEmpty(param.getSceneNum())){
throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
}
String[] nums = param.getSceneNum().split(",");
List numList = Arrays.asList(nums);
List proList = sceneProService.getListByNums(numList);
List plusList = scenePlusService.getListByNums(numList);
this.deleteCooperationList(proList,plusList);
}
@Override
public List getResourceByNum(String sceneNum) {
if(StringUtils.isEmpty(sceneNum)){
throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SceneCooperation::getSceneNum,sceneNum);
List list = this.list(wrapper);
if(list == null || list.size()<=0){
return new ArrayList<>();
}
return sceneResourceService.getByCooperationId(list.get(0).getId());
}
@Override
public List getNumByUserIds(List userIds) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(SceneCooperation::getUserId,userIds);
Set collect = this.list(wrapper).parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toSet());
return new ArrayList<>(collect);
}
@Override
public HashMap getByNumList(List numList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(SceneCooperation::getSceneNum,numList);
List list = this.list(wrapper);
HashMap cooMap = new HashMap<>();
if(list.size() >0){
List userIds = list.parallelStream().map(SceneCooperation::getUserId).collect(Collectors.toList());
if(userIds.size() >0){
HashMap userMap = userService.getByIds(userIds);
list.forEach(entity -> cooMap.put(entity.getSceneNum(),userMap.get(entity.getUserId())));
}
}
return cooMap;
}
@Override
public SceneCooperation getByNum(String num) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SceneCooperation::getSceneNum,num);
List list = this.list(wrapper);
if(list == null || list.size() <=0){
return null;
}
return list.get(0);
}
@Override
public Object cooperationSceneList(SceneParam param, String username) {
return null;
}
}