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

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
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.dto.TtTrainDto;
import com.ruoyi.project.business.domain.dto.TtTrainSearchDto;
import com.ruoyi.project.business.domain.train.TtTrain;
import com.ruoyi.project.business.domain.train.TtTrainTarget;
import com.ruoyi.project.business.domain.vo.train.*;
import com.ruoyi.project.business.service.ITtTrainService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
 * 培训考核Controller
 *
 * @author ruoyi
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/train/train")
@RequiredArgsConstructor
public class TtTrainController extends BaseController {
    private final ITtTrainService ttTrainService;

    /**
     * 查询培训考核列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TtTrainSearchDto ttTrainSearchDto) {
        startPage();
        List<TtTrainVo> list = ttTrainService.selectTtTrainList(ttTrainSearchDto);
        return getDataTable(list);
    }


    /**
     * 导出培训考核列表
     */
    @Log(title = "培训考核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TtTrainSearchDto ttTrain) {
        List<TtTrainVo> list = ttTrainService.selectTtTrainList(ttTrain);
        List<TtTrainExcelVo> excelVos = new ArrayList<>();
        list.forEach(item -> {
            TtTrainExcelVo ttTrainExcelVo = new TtTrainExcelVo();
            BeanUtils.copyProperties(item, ttTrainExcelVo);
            excelVos.add(ttTrainExcelVo);
        });
        ExcelUtil<TtTrainExcelVo> util = new ExcelUtil<TtTrainExcelVo>(TtTrainExcelVo.class);
        util.exportExcel(response, excelVos, "培训考核数据");
    }

    /**
     * 获取培训考核详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ttTrainService.selectTtTrainById(id));
    }

    /**
     * 新增培训考核
     */
    @Log(title = "培训考核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TtTrainDto ttTrainDto) {
        return toAjax(ttTrainService.insertTtTrain(ttTrainDto));
    }

    /**
     * 修改培训考核 todo 多表 如何校验
     */
    @Log(title = "培训考核", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key="id",tableName = "tt_train")
    public AjaxResult edit(@RequestBody TtTrainDto ttTrainDto) {
        return toAjax(ttTrainService.updateTtTrain(ttTrainDto));
    }

    /**
     * 删除培训考核
     */
    @Log(title = "培训考核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @DataUpdateCheck(key="id",tableName = "tt_train")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ttTrainService.deleteTtTrainByIds(ids));
    }

    /**
     * 删除培训考核
     */
    @Log(title = "培训考核", businessType = BusinessType.DELETE)
    @PutMapping("/terminateTrain/{ids}")
    @DataUpdateCheck(key="id",tableName = "tt_train")
    public AjaxResult terminateTrain(@PathVariable Long[] ids) {
        return toAjax(ttTrainService.updateTtTrainStatusByIds(ids));
    }

    /**
     * 查询当前用户培训考核一览
     */
    @GetMapping("/participantTrainPage")
    public TableDataInfo pageByCurrentUser(TrainParticipantReq req) {
        startPage();
        List<TrainParticipantRes> list = ttTrainService.selectParticipantTrainListCur(req);
        return getDataTable(list);
    }

    /**
     * 获取该培训 试卷详细
     */
    @GetMapping("/getTestPaperDetail")
    public AjaxResult getTestPaperDetail(TtTrainDto ttTrainDto) {
        TtTestPaperVo result = ttTrainService.selectTestPaperDetail(ttTrainDto);
        return AjaxResult.success(result);
    }

    /**
     * 获取该培训 试卷详细
     */
    @GetMapping("/getTestResult")
    public AjaxResult getTestResult(TtTrainDto ttTrainDto) {
        TtTestPaperVo result = ttTrainService.selectTestResult(ttTrainDto);
        return AjaxResult.success(result);
    }

    /**
     * 获取该培训 试卷详细
     */
    @GetMapping("/getTrainResult")
    public AjaxResult getTrainResult(TtTrainDto ttTrainDto) {
        List<TtTrainTargetVo> result = ttTrainService.selectTrainResult(ttTrainDto);
        return AjaxResult.success(result);
    }


}
