package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.yonge.cooleshow.biz.dal.vo.CourseCoursewareVo;
import com.yonge.cooleshow.biz.dal.dto.search.CourseCoursewareSearch;
import com.yonge.cooleshow.biz.dal.entity.CourseCourseware;
import com.yonge.cooleshow.biz.dal.service.CourseCoursewareService;

import java.util.Date;

@RestController
@RequestMapping("/courseCourseware")
@Api(value = "课件表", tags = "课件表")
public class CourseCoursewareController extends BaseController {

	@Autowired
	private CourseCoursewareService courseCoursewareService;


	@Autowired
	private SysUserFeignService sysUserFeignService;

	/**
	 * 查询分页
	 */
	@PostMapping("/page")
	@ApiOperation(value = "查询分页", notes = "传入courseCoursewareSearch")
	public HttpResponseResult<PageInfo<CourseCoursewareVo>> page(@RequestBody CourseCoursewareSearch query) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null) {
			throw new BizException("请重新登录");
		}
		query.setClientType(ClientEnum.TEACHER);
		query.setUserId(user.getId());
		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) {

		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null) {
			throw new BizException("请重新登录");
		}

		courseCourseware.setClientType(ClientEnum.TEACHER);
		courseCourseware.setUserId(user.getId());
		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)));
	}
}