package com.fdkankan.ucenter.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.PageInfo;
import com.fdkankan.fyun.oss.UploadToOssUtil;
import com.fdkankan.ucenter.common.OssPath;
import com.fdkankan.ucenter.entity.ScenePlus;
import com.fdkankan.ucenter.entity.ScenePlusVoid;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.mapper.IScenePlusVoidMapper;
import com.fdkankan.ucenter.service.IFusionService;
import com.fdkankan.ucenter.service.IScenePlusService;
import com.fdkankan.ucenter.service.IScenePlusVoidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.vo.request.SceneParam;
import com.fdkankan.ucenter.vo.response.ScenePlusVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-08-08
*/
@Service
public class ScenePlusVoidServiceImpl extends ServiceImpl implements IScenePlusVoidService {
@Autowired
IFusionService fusionService;
@Autowired
IUserService userService;
@Autowired
IScenePlusService scenePlusService;
@Autowired
UploadToOssUtil uploadToOssUtil;
@Override
public void setStatus(String num, Integer type,String token) {
if(StringUtils.isEmpty(num) || type == null){
throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
}
ScenePlus scenePlus = scenePlusService.getByNum(num);
if(scenePlus == null){
throw new BusinessException(-1,"场景不存在");
}
User user = userService.getByToken(token);
ScenePlusVoid plusVoid = this.getByPlusId(scenePlus.getId());
if(plusVoid == null){
plusVoid = new ScenePlusVoid();
plusVoid.setUserId(user.getId());
plusVoid.setStatus(type);
plusVoid.setPlusId(scenePlus.getId());
this.save(plusVoid);
}else {
plusVoid.setStatus(type);
plusVoid.setUpdateTime(null);
this.updateById(plusVoid);
}
}
private ScenePlusVoid getByPlusId(Long plusId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ScenePlusVoid::getPlusId,plusId);
return this.getOne(wrapper);
}
@Override
public List scenePageList(SceneParam param, String token) {
if(param.getHaveVoid()!=null){
User user = userService.getByToken(token);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ScenePlusVoid::getStatus,1)
.eq(ScenePlusVoid::getUserId,user.getId());
List list = this.list(wrapper);
if(list.size() <=0 && param.getHaveVoid() == 0){
return new ArrayList<>();
}
if(list.size() >0){
List plusIds = list.parallelStream().map(ScenePlusVoid::getPlusId).collect(Collectors.toList());
List plusList = scenePlusService.getByIds(plusIds);
if(plusList.size() >0){
List numList = plusList.parallelStream().map(ScenePlus::getNum).collect(Collectors.toList());
param.setNumList(numList);
}
}
}
param.setStatus(2); //计算成功的数据
param.setPageSize(Integer.MAX_VALUE);
PageInfo pageInfo = fusionService.scenePageList(param, token);
List list = (List) pageInfo.getList();
List newList = new ArrayList<>();
for (ScenePlusVo scenePlusVo : list) { //检测oss 是否存在houseType
String key = String.format(OssPath.houseTypePath,scenePlusVo.getNum());
if(uploadToOssUtil.existKey(key)){
newList.add(scenePlusVo);
}
}
return newList;
}
@Override
public HashMap getByPlusIds(List plusIds) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(ScenePlusVoid::getPlusId,plusIds);
List list = this.list(wrapper);
HashMap map = new HashMap<>();
list.forEach(entity -> map.put(entity.getPlusId(),entity));
return map;
}
}