package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.material.entity.MaterialVideoInfo;
import cn.com.ctop.common.module.material.service.IMaterialVideoInfoService;
import cn.com.ctop.common.module.utils.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * 视频素材基础信息
 */
@Slf4j
@RestController
@RequestMapping("/com.cn/materialVideoInfo")
public class MaterialVideoInfoController {
	@Autowired
	private IMaterialVideoInfoService materialVideoInfoService;

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

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

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

		return result;
	}

}
