package com.yonge.cooleshow.website.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.search.CourseCoursewareSearch;
import com.yonge.cooleshow.biz.dal.entity.CourseCourseware;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.CourseCoursewareService;
import com.yonge.cooleshow.biz.dal.vo.CourseCoursewareVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("${app-config.url.website:}/courseCourseware")
@Api(value = "课件表", tags = "课件表")
public class CourseCoursewareController extends BaseController {

	@Autowired
	private CourseCoursewareService courseCoursewareService;

	/**
	 * 查询分页
	 */
	@PostMapping("/page")
	@ApiOperation(value = "查询分页", notes = "传入courseCoursewareSearch")
	public HttpResponseResult<PageInfo<CourseCoursewareVo>> page(@RequestBody CourseCoursewareSearch query) {
		IPage<CourseCoursewareVo> pages = courseCoursewareService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入courseCourseware")
	public HttpResponseResult<CourseCourseware> submit(@Valid @RequestBody CourseCourseware courseCourseware) {
		if (courseCourseware.getId() != null) {
			courseCourseware.setUpdateTime(new Date());
		} else {
			courseCourseware.setUpdateTime(new Date());
			courseCourseware.setCreateTime(new Date());
		}
		courseCourseware.setClientType(ClientEnum.TEACHER);
		return succeed(courseCoursewareService.submit(courseCourseware));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove/{id}")
	@ApiOperation(value = "删除", notes = "传入id")
	public HttpResponseResult remove(@PathVariable Long id ) {
		return status(courseCoursewareService.removeById(id));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "批量删除", notes = "传入id")
	public HttpResponseResult remove(@RequestParam String ids ) {
		return status(courseCoursewareService.removeByIds(StringUtil.toLongList(ids)));
	}
}