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.ucenter.common.PageInfo;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
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 javax.annotation.Resource;
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;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-08
 */
@Service
public class ScenePlusVoidServiceImpl extends ServiceImpl<IScenePlusVoidMapper, ScenePlusVoid> implements IScenePlusVoidService {

    @Autowired
    private IFusionService fusionService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IScenePlusService scenePlusService;
    @Autowired
    private FYunFileServiceInterface fYunFileService;

    @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<ScenePlusVoid> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScenePlusVoid::getPlusId,plusId);
        return this.getOne(wrapper);
    }

    @Override
    public List<ScenePlusVo> scenePageList(SceneParam param, String token) {
        if(param.getHaveVoid()!=null){
            User user = userService.getByToken(token);
            LambdaQueryWrapper<ScenePlusVoid> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ScenePlusVoid::getStatus,1)
                    .eq(ScenePlusVoid::getUserId,user.getId());
            List<ScenePlusVoid> list = this.list(wrapper);
            if(list.size() <=0 && param.getHaveVoid() == 0){
                return new ArrayList<>();
            }
            if(list.size() >0){
                List<Long> plusIds = list.parallelStream().map(ScenePlusVoid::getPlusId).collect(Collectors.toList());
                List<ScenePlus> plusList = scenePlusService.getByIds(plusIds);
                if(plusList.size() >0){
                    List<String> 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<ScenePlusVo> list = (List<ScenePlusVo>) pageInfo.getList();
        List<ScenePlusVo> newList = new ArrayList<>();
        for (ScenePlusVo scenePlusVo : list) {  //检测oss 是否存在houseType
            String key = String.format(OssPath.houseTypePath,scenePlusVo.getNum());
            if(fYunFileService.fileExist(key)){
                newList.add(scenePlusVo);
            }
        }
        return newList;
    }

    @Override
    public HashMap<Long, ScenePlusVoid> getByPlusIds(List<Long> plusIds) {
        HashMap<Long,ScenePlusVoid> map = new HashMap<>();
        LambdaQueryWrapper<ScenePlusVoid> wrapper = new LambdaQueryWrapper<>();
        if(plusIds.size()>0){
            wrapper.in(ScenePlusVoid::getPlusId,plusIds);
            List<ScenePlusVoid> list = this.list(wrapper);
            list.forEach(entity -> map.put(entity.getPlusId(),entity));
        }
        return map;
    }
}
