package org.jeecg.modules.ctop.controller;

import javax.servlet.http.HttpServletRequest;

import cn.com.ctop.common.module.material.entity.MaterialVideoFrame;
import cn.com.ctop.common.module.material.service.IMaterialVideoFrameService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

 /**
 * 视频素材截帧信息
 */
@Slf4j
@RestController
@RequestMapping("/com.cn/materialVideoFrame")
public class MaterialVideoFrameController {
	@Autowired
	private IMaterialVideoFrameService materialVideoFrameService;

	/**
	  * 分页列表查询
	 */
	@GetMapping(value = "/list")
	public Result<IPage<MaterialVideoFrame>> queryPageList(MaterialVideoFrame materialVideoFrame,
														   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														   HttpServletRequest req) {
		Result<IPage<MaterialVideoFrame>> result = new Result<>();
		QueryWrapper<MaterialVideoFrame> queryWrapper = QueryGenerator.initQueryWrapper(materialVideoFrame, req.getParameterMap());
		Page<MaterialVideoFrame> page = new Page<>(pageNo, pageSize);
		IPage<MaterialVideoFrame> pageList = materialVideoFrameService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 *   添加
	 */
	@PostMapping(value = "/add")
	public Result<MaterialVideoFrame> add(@RequestBody MaterialVideoFrame materialVideoFrame) {
		Result<MaterialVideoFrame> result = new Result<>();
		try {
			materialVideoFrameService.save(materialVideoFrame);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 *  编辑
	 */
	@PutMapping(value = "/edit")
	public Result<MaterialVideoFrame> edit(@RequestBody MaterialVideoFrame materialVideoFrame) {
		Result<MaterialVideoFrame> result = new Result<MaterialVideoFrame>();
		MaterialVideoFrame materialVideoFrameEntity = materialVideoFrameService.getById(materialVideoFrame.getId());
		if(materialVideoFrameEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = materialVideoFrameService.updateById(materialVideoFrame);
			if(ok) {
				result.success("修改成功!");
			}
		}

		return result;
	}

	/**
	 * 通过id查
	 */
	@GetMapping(value = "/queryById")
	public Result<MaterialVideoFrame> queryById(@RequestParam(name="id",required=true) String id) {
		Result<MaterialVideoFrame> result = new Result<>();
		MaterialVideoFrame materialVideoFrame = materialVideoFrameService.getById(id);
		if(materialVideoFrame==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(materialVideoFrame);
			result.setSuccess(true);
		}
		return result;
	}

}
