package com.fdkankan.scene.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.RebuildResult;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.entity.VideoSceneProgress;
import com.fdkankan.scene.mapper.IVideoSceneProgressMapper;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.scene.service.IVideoSceneProgressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.vo.RebuildVedioSceneParamVO;
import com.fdkankan.scene.vo.VideoSceneProgressVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频重算进度 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-01-14
 */
@Service
public class VideoSceneProgressServiceImpl extends ServiceImpl<IVideoSceneProgressMapper, VideoSceneProgress> implements IVideoSceneProgressService {

    @Autowired
    private ISceneProService sceneProService;

    @Override
    public List<VideoSceneProgressVO> findVideoSceneProgress(RebuildVedioSceneParamVO param) {

        List<VideoSceneProgress> videoSceneProgressyList =
                this.list(new LambdaQueryWrapper<VideoSceneProgress>()
                        .select(VideoSceneProgress::getId, VideoSceneProgress::getVideoName, VideoSceneProgress::getRebuildResult)
                        .eq(VideoSceneProgress::getTbStatus, TbStatus.VALID.code())
                        .eq(VideoSceneProgress::getNum, param.getNum()));

        List<VideoSceneProgressVO> result = videoSceneProgressyList.parallelStream().map(vsp -> {
            VideoSceneProgressVO vo = new VideoSceneProgressVO();
            BeanUtils.copyProperties(vsp, vo);
            return vo;
        }).collect(Collectors.toList());

        this.update(new LambdaUpdateWrapper<VideoSceneProgress>()
                .set(VideoSceneProgress::getRebuildResult, RebuildResult.POLLED.code())
                .eq(VideoSceneProgress::getNum, param.getNum())
                .eq(VideoSceneProgress::getTbStatus, TbStatus.VALID.code())
                .eq(VideoSceneProgress::getRebuildResult, RebuildResult.SUCCESS.code()));

        //更新访问量
        return result;
    }

    @Override
    public void updateProgressRec(RebuildVedioSceneParamVO param) {
        this.update(new LambdaUpdateWrapper<VideoSceneProgress>()
                .set(VideoSceneProgress::getTbStatus, TbStatus.DELETE.code())
                .eq(VideoSceneProgress::getNum, param.getNum())
                .eq(VideoSceneProgress::getVideoName, param.getPanoId()));
    }

    @Override
    public VideoSceneProgress findrebuildVideoProgressDetail(String num,String panId) {
        VideoSceneProgress videoSceneProgress =
                this.getOne(new LambdaQueryWrapper<VideoSceneProgress>()
                        .eq(VideoSceneProgress::getTbStatus, TbStatus.VALID.code())
                        .eq(VideoSceneProgress::getNum, num)
                        .eq(VideoSceneProgress::getVideoName, panId));
        return videoSceneProgress;
    }

    @Override
    public void updateProgress(String num, String panId, Integer status) {

        this.update(new LambdaUpdateWrapper<VideoSceneProgress>()
                .set(VideoSceneProgress::getRebuildResult, status)
                .set(VideoSceneProgress::getRebuildEndTime, Calendar.getInstance().getTime())
                .eq(VideoSceneProgress::getNum, num)
                .eq(VideoSceneProgress::getTbStatus, TbStatus.VALID.code())
                .eq(VideoSceneProgress::getVideoName, panId));

    }

    public ResultData rebuildVideoSceneProgress(RebuildVedioSceneParamVO param) throws Exception {

        if(StringUtils.isEmpty(param.getNum())){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }

        ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
        if(null == scenePro){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }

        return ResultData.ok(this.findVideoSceneProgress(param));
    }


}
