package com.xhkjedu.slive.service.liveplay;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionPlaybackMapper;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionPlaybackSeeMapper;
import com.xhkjedu.slive.model.liveplay.LCourseSectionPlayback;
import com.xhkjedu.slive.model.liveplay.LCourseSectionPlaybackSee;
import com.xhkjedu.slive.utils.LiveUtil;
import com.xhkjedu.slive.vo.liveplay.*;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName CourseSectionPlaybackService
 * Description TODO 课节回放
 * Author WN
 * Date 2021/6/16 10:20
 **/
@Service
@Slf4j
public class CourseSectionPlaybackService {

    @Resource
    private CourseSectionPlaybackMapper courseSectionPlaybackMapper;
    @Resource
    private CourseSectionPlaybackSeeMapper courseSectionPlaybackSeeMapper;

    //新增
    public ResultVo add(LCourseSectionPlayback playback) {
        int timestamp = N_Utils.getSecondTimestamp();
        playback.setBegintime(timestamp);
        playback.setCreatetime(timestamp);
        playback.setRn(0);
        playback.setDn(0);

        courseSectionPlaybackMapper.insertSelective(playback);
        return new ResultVo(0,"保存成功");

    }

    //修改回放信息
    public ResultVo updatePlayback(LCourseSectionPlayback playback) {
        int timestamp = N_Utils.getSecondTimestamp();
        playback.setEndtime(timestamp);
        playback.setMc("h264");
        if(playback.getDuration() == null){
            playback.setDuration(0);
        }
        Integer num = courseSectionPlaybackMapper.updatePlayback(playback);
        if (num > 0) {
            return new ResultVo(0, "修改成功");
        } else {
            return new ResultVo(1, "修改失败");
        }
    }

    //课节下所有回放
    public List<Map> listSectionPlaybacks(Integer sectionid){
       return courseSectionPlaybackMapper.listSectionPlaybacks(sectionid);
    }

    //视频回放观看次数
    public ResultVo updateReadnum(LCourseSectionPlaybackSee playbackSee) {
        courseSectionPlaybackMapper.updateReadnum(playbackSee.getPbid());//修改回放观看次数

        //添加观看记录
        playbackSee.setCreatetime(N_Utils.getSecondTimestamp());
        courseSectionPlaybackSeeMapper.insertUseGeneratedKeys(playbackSee);

        return new ResultVo(0,"成功更新视频观看次数");
    }

    //视频回放下载次数
    public void updateDownnum(LCourseSectionPlayback playback) {
        courseSectionPlaybackMapper.updateDownnum(playback.getPbid());//修改回放观看次数
    }

    /**
     * @Description 根据课节名称删除回放地址
     * @Date 2023/11/20 16:36
     * @Author YWX
     * @Param [pb]
     * @Return void
     **/
    public void deletePb(CSPlaybackVo pb) {
        List<Map> list = courseSectionPlaybackMapper.listPb(pb.getSectionnames());
        for (Map map : list) {
            String rspath = map.get("rspath").toString();
            List<String> pbs = (List<String>) map.get("pbs");
            Map<String, Object> params = new HashMap<>();
            Integer batchnum = pb.getBatchnum();
            for (int i = 0; i < pbs.size(); i += batchnum) {
                List<String> filelist = pbs.stream().skip(i).limit(batchnum).collect(Collectors.toList());
                params.put("fileList", filelist);
                params.put("sign", N_Utils.getMD5Pwd(JSON.toJSONString(filelist)));
                ResultVo vo = LiveUtil.sendJsonPost(rspath + "/recorder/v1/delfiles", JSON.toJSONString(params));
                if (vo.getCode() == 0) {
                    Map<String, List<String>> result = (Map<String, List<String>>) vo.getObj();
                    List<String> successList = result.get("successList");
                    courseSectionPlaybackMapper.deletePb(successList);
                    List<String> failList = result.get("failList");
                    if (N_Utils.isListNotEmpty(failList)) {
                        log.error("删除回放失败：{}", failList);
                    }
                } else {
                    log.error("删除回放失败：{}", vo.getMsg());
                }
            }
        }
    }
}
