package org.jeecg.modules.ctop.controller;

import javax.servlet.http.HttpServletRequest;

import cn.com.ctop.common.module.annotation.AutoLog;
import cn.com.ctop.common.module.material.entity.MaterialVideoProject;
import cn.com.ctop.common.module.material.service.IMaterialVideoProjectService;
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.*;
import io.swagger.annotations.ApiOperation;

 /**
 * 视频素材关联项目信息
 */
@Slf4j
@RestController
@RequestMapping("/com.cn/materialVideoProject")
public class MaterialVideoProjectController {

	@Autowired
	private IMaterialVideoProjectService materialVideoProjectService;

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

	/**
	 *   添加
	 */
	@AutoLog(value = "视频素材关联项目信息-添加")
	@ApiOperation(value="视频素材关联项目信息-添加", notes="视频素材关联项目信息-添加")
	@PostMapping(value = "/add")
	public Result<MaterialVideoProject> add(@RequestBody MaterialVideoProject materialVideoProject) {
		Result<MaterialVideoProject> result = new Result<>();
		try {
			materialVideoProjectService.save(materialVideoProject);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

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

		return result;
	}
}
