package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.annotation.AutoLog;
import cn.com.ctop.common.module.entity.MaterialCutFrame;
import cn.com.ctop.common.module.service.IMaterialCutFrameService;
import cn.com.ctop.common.module.service.IMaterialCutFrameTaskService;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 截屏
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-12-25
 */
@Slf4j
@Api(tags = "截屏")
@RestController
@RequestMapping("/cutFrame/material")
public class MaterialCutFrameController {
    @Autowired
    private IMaterialCutFrameService materialCutFrameService;
    @Autowired
    private IMaterialInfoService materialInfoService;
    @Autowired
    private IMaterialCutFrameTaskService materialCutFrameTaskServiceTask;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "截屏-分页列表查询")
    @ApiOperation(value = "截屏-分页列表查询", notes = "截屏-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MaterialCutFrame>> queryPageList(MaterialCutFrame materialCutFrame,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<MaterialCutFrame>> result = new Result<>();
        QueryWrapper<MaterialCutFrame> queryWrapper = QueryGenerator.initQueryWrapper(materialCutFrame, req.getParameterMap());
        queryWrapper.orderByAsc("image_index");
        Page<MaterialCutFrame> page = new Page<>(pageNo, pageSize);
        //TODO 这边代码有bug,先临时紧急处理，后续优化截帧任务
        IPage<MaterialCutFrame> pageList = materialCutFrameService.page(page, queryWrapper);
        List<MaterialCutFrame> list = materialCutFrameService.getListByVideoSignature(materialCutFrame.getVideoSignature());
        pageList.setRecords(list);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "截屏-查询")
    @ApiOperation(value = "截屏-查询", notes = "截屏-查询")
    @GetMapping(value = "/queryByPhotoId")
    public Result<Object> queryByPhotoId(Long photoId) {
        try {
            KuaiShouVideoGet videoGet = videoGetService.getVideoByPhotoId(photoId);
            List<MaterialCutFrame> list = materialCutFrameService.getListByVideoSignature(videoGet.getSignature());
            return Result.ok(list);
        } catch (Exception e) {
            log.error("queryByPhotoId error", e);
            return Result.error(e.getMessage());
        }
    }
}
