package com.zd.exam.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElResources;
import com.zd.exam.service.IElResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.List;

/**
 * 课件资源Controller
 *
 * @author liubo
 * @date 2021-12-07
 */
@RestController
@Api(tags = "【课件资源】")
@RequestMapping("/el_resources")
public class ElResourcesController extends BaseController {
    @Autowired
    private IElResourcesService elResourcesService;

    /**
     * 查询课件资源列表  "exam:el_resources:list"
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_RESOURCES+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询课件资源列表",notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.LIST)
    public TableDataInfo<ElResources> list(ElResources elResources) {
        ElResources newElResources = elResourcesService.selectElResourcesCate(elResources);
        startPage();
        List<ElResources> list = elResourcesService.selectElResourcesList(newElResources);
        return getDataTable(list);
    }


    /**
     * 查询课件资源列表
     */
    @GetMapping("/listAll")
    @ApiOperation(value = "查询课件资源列表")
    public R listAll() {
        List<ElResources> list = elResourcesService.selectElResourcesListALL();
        return R.ok(list);
    }

//    /**
//     * 导出课件资源列表
//     */
//    @ApiOperation(value = "导出课件资源列表")
//    @PreAuthorize(hasPermi = "exam:el_resources:export")
//    @Log(title = "课件资源", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ElResources elResources) throws IOException {
//        List<ElResources> list = elResourcesService.selectElResourcesList(elResources);
//        ExcelUtil<ElResources> util = new ExcelUtil<ElResources>(ElResources.class);
//        util.exportExcel(response, list, "课件资源数据");
//    }

    /**
     * 获取课件资源详细信息  "exam:el_resources:query"
     */
    @ApiOperation(value = "获取课件资源详细信息" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.QUERY)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(elResourcesService.selectElResourcesById(id));
    }

    /**
     * 新增课件资源  "exam:el_resources:add"
     */
    @ApiOperation(value = "新增课件资源" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.ADD)
    @Log(title = "课件资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElResources elResources) {
        return AjaxResult.success(elResourcesService.insertElResources(elResources));
    }

    /**
     * 新增课件富文本资源  "exam:el_resources:add"
     */
    @ApiOperation(value = "新增课件富文本资源" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.ADD)
    @Log(title = "课件资源", businessType = BusinessType.INSERT)
    @PostMapping("/addText")
    public AjaxResult addText(@RequestBody ElResources elResources) {
        return AjaxResult.success(elResourcesService.insertElResources(elResources, null));
    }

    /**
     * 修改课件资源  "exam:el_resources:edit"
     */
    @ApiOperation(value = "修改课件资源" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.EDIT)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.EDIT)
    @Log(title = "课件资源", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    /*public AjaxResult edit(@ApiParam("ID") @RequestParam(value = "id") Long id,
                         @ApiParam("文件") @RequestParam(value = "file", required = false) MultipartFile file,
                         @ApiParam("分类ID")  @RequestParam(value = "cateId", required = false) Long cateId,
                         @ApiParam("资源名称")  @RequestParam(value = "title", required = false) String title,
                         @ApiParam("富文本内容")  @RequestParam(value = "content", required = false) String content) {*/
    public AjaxResult edit(@RequestBody ElResources elResources) {

        if(elResources.getId() == null){
            return error("ID 不能为空！");
        }
        return AjaxResult.success(elResourcesService.updateElResources(elResources, 1));
    }

    /**
     * 修改课件资源富文本
     */
    @ApiOperation(value = "修改课件资源富文本" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.EDIT)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.EDIT)
    @Log(title = "课件资源", businessType = BusinessType.UPDATE)
    @PostMapping("/updateText")
    public AjaxResult updateText(@RequestBody ElResources elResources) {

        if(elResources.getId() == null){
            return error("ID 不能为空！");
        }
        return AjaxResult.success(elResourcesService.updateElResources(elResources, 2));
    }

    /**
     * 删除课件资源  "exam:el_resources:remove"
     */
    @ApiOperation(value = "删除课件资源" ,notes="权限字符："+PerPrefix.EXAM_EL_RESOURCES+ PerFun.REMOVE)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_RESOURCES+ PerFun.REMOVE)
    @Log(title = "课件资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(elResourcesService.deleteElResourcesByIds(ids));
    }
}
