package com.ruoyi.project.business.controller.train;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.train.TtTrainFile;
import com.ruoyi.project.business.service.ITtTrainFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训资料Controller
 *
 * @author ruoyi
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/train/file")
@RequiredArgsConstructor
public class TtTrainFileController extends BaseController {

    private final ITtTrainFileService ttTrainFileService;

    /**
     * 查询培训资料列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TtTrainFile ttTrainFile) {
        startPage();
        List<TtTrainFile> list = ttTrainFileService.selectTtTrainFileList(ttTrainFile);
        return getDataTable(list);
    }

    /**
     * 导出培训资料列表
     */
    @Log(title = "培训资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TtTrainFile ttTrainFile) {
        List<TtTrainFile> list = ttTrainFileService.selectTtTrainFileList(ttTrainFile);
        ExcelUtil<TtTrainFile> util = new ExcelUtil<TtTrainFile>(TtTrainFile.class);
        util.exportExcel(response, list, "培训资料数据");
    }

    /**
     * 获取培训资料详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ttTrainFileService.selectTtTrainFileById(id));
    }
    /**
     * 获取培训资料详细信息 通过培训id
     */
    @GetMapping("fileByTrainId")
    public AjaxResult fileByTrainId(Long trainId) {
        return success(ttTrainFileService.fileByTrainId(trainId));
    }

    /**
     * 新增培训资料
     */
    @Log(title = "培训资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TtTrainFile ttTrainFile) {
        return toAjax(ttTrainFileService.insertTtTrainFile(ttTrainFile));
    }

    /**
     * 修改培训资料
     */
    @Log(title = "培训资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TtTrainFile ttTrainFile) {
        return toAjax(ttTrainFileService.updateTtTrainFile(ttTrainFile));
    }

    /**
     * 删除培训资料
     */
    @Log(title = "培训资料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ttTrainFileService.deleteTtTrainFileByIds(ids));
    }
}
