package com.xunmei.core.edu.controller;

import com.xunmei.common.core.domain.edu.domain.SysLearningMaterials;
import com.xunmei.common.core.domain.edu.dto.SysLearningMaterialsPageDto;
import com.xunmei.core.edu.service.ISysLearningMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xunmei.common.log.annotation.Log;
import com.xunmei.common.log.enums.BusinessType;
import com.xunmei.common.security.annotation.RequiresPermissions;
import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xunmei.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 学习资料Controller
 *
 * @author xunmei
 * @date 2023-08-21
 */
@Api(tags = {"SysLearningMaterials"})
@RestController
@RequestMapping("/materials")
public class SysLearningMaterialsController extends BaseController {
    @Autowired
    private ISysLearningMaterialsService sysLearningMaterialsService;

    /**
     * 查询学习资料列表
     */
    @ApiOperation(value = "查询SysLearningMaterials列表")
    @RequiresPermissions("core:materials:list")
    @GetMapping("/list")
    public TableDataInfo list(SysLearningMaterialsPageDto sysLearningMaterials) {
        return sysLearningMaterialsService.selectPage(sysLearningMaterials);
    }


    /**
     * 获取学习资料详细信息
     */
    @ApiOperation(value = "获取SysLearningMaterials详细信息")
    @RequiresPermissions("core:materials:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysLearningMaterialsService.selectSysLearningMaterialsById(id));
    }

    /**
     * 新增学习资料
     */
    @ApiOperation(value = "新增SysLearningMaterials")
    @RequiresPermissions("core:materials:add")
    @Log(title = "学习资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLearningMaterials sysLearningMaterials) {
        return toAjax(sysLearningMaterialsService.insertSysLearningMaterials(sysLearningMaterials));
    }

    /**
     * 修改学习资料
     */
    @ApiOperation(value = "修改SysLearningMaterials")
    @RequiresPermissions("core:materials:edit")
    @Log(title = "学习资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLearningMaterials sysLearningMaterials) {
        return toAjax(sysLearningMaterialsService.updateSysLearningMaterials(sysLearningMaterials));
    }

    /**
     * 删除学习资料
     */
    @ApiOperation(value = "删除SysLearningMaterials")
    @RequiresPermissions("core:materials:remove")
    @Log(title = "学习资料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysLearningMaterialsService.deleteSysLearningMaterialsByIds(ids));
    }

    /**
     * 导出excel
     */
    @ApiOperation(value = "修改SysLearningMaterials")
    @RequiresPermissions("core:materials:export")
    @Log(title = "学习资料", businessType = BusinessType.UPDATE)
    @PostMapping("/export")
    public void export(SysLearningMaterialsPageDto sysLearningMaterials, HttpServletResponse response) throws IOException {
        sysLearningMaterialsService.export(sysLearningMaterials,response);
    }

    /**
     * 修改学习资料,设置未公开/公开
     */
    @ApiOperation(value = "修改SysLearningMaterials")
    @RequiresPermissions("core:materials:edit")
    @Log(title = "学习资料", businessType = BusinessType.UPDATE)
    @GetMapping("/{id}/{isOpen}")
    public AjaxResult editOpen(@PathVariable Long id, @PathVariable Integer isOpen) {
        return toAjax(sysLearningMaterialsService.editOpen(id, isOpen));
    }
}
