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.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.scene.entity.PicSceneProgress;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.mapper.IPicSceneProgressMapper;
import com.fdkankan.scene.service.IPicSceneProgressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.service.ISceneProService;
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;

/**
 * <p>
 * 视频重算进度 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-01-14
 */
@Service
public class PicSceneProgressServiceImpl extends ServiceImpl<IPicSceneProgressMapper, PicSceneProgress> implements IPicSceneProgressService {

    @Autowired
    ISceneProService sceneProService;

    @Override
    public VideoSceneProgressVO findPicSceneProgress(RebuildVedioSceneParamVO param) {
        List<PicSceneProgress> list = this.list(
                new LambdaQueryWrapper<PicSceneProgress>()
                        .eq(PicSceneProgress::getTbStatus, TbStatus.VALID.code())
                        .eq(PicSceneProgress::getNum, param.getNum()));
        VideoSceneProgressVO videoSceneProgressVO = new VideoSceneProgressVO();
        if(list == null || list.size()<=0){
            return videoSceneProgressVO;
        }
        BeanUtils.copyProperties(list.get(0), videoSceneProgressVO);
        //更新访问量
        this.updatePageView(param.getNum());

        return videoSceneProgressVO;
    }

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

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

    @Override
    public void updateProgress(String num, String panId, Integer status) {
        this.update(new LambdaUpdateWrapper<PicSceneProgress>()
                .set(PicSceneProgress::getRebuildResult, status)
                .set(PicSceneProgress::getRebuildEndTime, Calendar.getInstance().getTime())
                .eq(PicSceneProgress::getNum, num)
                .eq(PicSceneProgress::getTbStatus, RecStatus.VALID.code()));
    }

    @Override
    public void updatePageView(String sceneCode) {
        this.update(new LambdaUpdateWrapper<PicSceneProgress>()
                .set(PicSceneProgress::getRebuildResult, RebuildResult.POLLED.code())
                .eq(PicSceneProgress::getNum, sceneCode)
                .eq(PicSceneProgress::getTbStatus, RecStatus.VALID.code())
                .eq(PicSceneProgress::getRebuildResult, RebuildResult.SUCCESS.code()));
    }

    public ResultData rebuildPicSceneProgress(RebuildVedioSceneParamVO param) throws Exception {

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

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

        VideoSceneProgressVO picSceneProgress =this.findPicSceneProgress(param);
        return ResultData.ok(picSceneProgress.getRebuildResult());
    }

}
