package com.zehong.web.controller.train;

import com.alibaba.fastjson.JSON;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.core.domain.model.LoginUser;
import com.zehong.common.core.page.TableDataInfo;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.ServletUtils;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.framework.web.service.TokenService;
import com.zehong.system.domain.train.StatisticsTrainCourse;
import com.zehong.system.domain.train.TNews;
import com.zehong.system.domain.train.TTrainCourse;
import com.zehong.system.domain.train.TTrainUserCourse;
import com.zehong.system.domain.vo.UserCourseVo;
import com.zehong.system.service.train.ITTrainCourseService;
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.util.List;
import java.util.Map;

/**
 * 培训课程Controller
 *
 * @author zehong
 * @date 2022-09-19
 */
@Api("课程管理")
@RestController
@RequestMapping("/train/course")
public class TTrainCourseController extends BaseController
{
    @Autowired
    private ITTrainCourseService tTrainCourseService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询培训课程列表
     */
    @ApiOperation("课程列表")
    @GetMapping("/list")
    public TableDataInfo list(TTrainCourse tTrainCourse)
    {
        startPage();
        tTrainCourse.setIsDel(0);
        List<TTrainCourse> list = tTrainCourseService.selectTTrainCourseList(tTrainCourse);
        return getDataTable(list);
    }

    @ApiOperation("用户课程列表")
    @GetMapping("/userCourseList")
    public TableDataInfo userCourseList(Integer type){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        startPage();
        List<UserCourseVo> list = tTrainCourseService.userCourseList(user.getUserId(),type);
        return getDataTable(list);
    }

    @ApiOperation("用户课程考试")
    @GetMapping("/examination")
    public AjaxResult examination(Long userCourseId, String answers){
        String n="["+answers+']';
        //字符串转换成二维数组
        Integer[][] integers = JSON.parseObject(n, Integer[][].class);
        Map<String,Object> map = tTrainCourseService.examination(userCourseId,integers);
        return AjaxResult.success(map);
    }
    /**
     * 导出培训课程列表
     */
    //@PreAuthorize("@ss.hasPermi('system:course:export')")
    @Log(title = "培训课程", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTrainCourse tTrainCourse)
    {
        List<TTrainCourse> list = tTrainCourseService.selectTTrainCourseList(tTrainCourse);
        ExcelUtil<TTrainCourse> util = new ExcelUtil<TTrainCourse>(TTrainCourse.class);
        return util.exportExcel(list, "培训课程数据");
    }

    /**
     * 获取培训课程详细信息
     */
    @ApiOperation("课程详情")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return AjaxResult.success(tTrainCourseService.selectTTrainCourseById(courseId));
    }
    /**
     * 获取培训课程详细信息
     */
    @ApiOperation("用户课程详情")
    @GetMapping(value = "/userCourse")
    public AjaxResult getUserCourse(Long userCourseId)
    {
        return AjaxResult.success(tTrainCourseService.getUserCourse(userCourseId));
    }

    /**
     * 获取培训课程详细信息
     */
    @ApiOperation("课程发布")
    @GetMapping("/release")
    public AjaxResult release(Long courseId)
    {
        return toAjax(tTrainCourseService.selectTTrainCourseRelease (courseId));
    }

    /**
     * 新增培训课程
     */
    @ApiOperation("新增课程")
    @Log(title = "培训课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTrainCourse tTrainCourse)
    {
        return AjaxResult.success(tTrainCourseService.insertTTrainCourse(tTrainCourse));
    }

    /**
     * 修改培训课程
     */
    @ApiOperation("修改课程")
    @Log(title = "培训课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTrainCourse tTrainCourse)
    {
        return toAjax(tTrainCourseService.updateTTrainCourse(tTrainCourse));
    }
    /**
     * 修改用户课程
     */
    @ApiOperation("修改用户课程")
    @Log(title = "修改用户课程", businessType = BusinessType.UPDATE)
    @PutMapping("/editUserCourse")
    public AjaxResult editUserCourse(@RequestBody TTrainUserCourse tTrainUserCourse)
    {
        return toAjax(tTrainCourseService.updateTTrainUserCourse(tTrainUserCourse));
    }

    /**
     * 删除培训课程
     */
    @ApiOperation("删除课程")
    @Log(title = "培训课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseId}")
    public AjaxResult remove(@PathVariable Long courseId)
    {
        TTrainCourse tTrainCourse = new TTrainCourse();
        tTrainCourse.setCourseId(courseId);
        tTrainCourse.setIsDel(1);
        return toAjax(tTrainCourseService.updateTTrainCourse(tTrainCourse));
    }

    /**
     * 考试发布
     * @param tTrainCourse
     * @return
     */
    @PostMapping(value = "testPublish")
    public AjaxResult testPublish(@RequestBody TTrainCourse tTrainCourse){
        return toAjax(tTrainCourseService.testPublish(tTrainCourse));
    }

    /**
     * 考试统计接口
     * @param statisticsTrainCourse 统计实体
     * @return TableDataInfo
     */
    @GetMapping(value = "/statisticsTrainCourse")
    public TableDataInfo statisticsTrainCourse(StatisticsTrainCourse statisticsTrainCourse){
        startPage();
        List<StatisticsTrainCourse> statisticsTrainCourses = tTrainCourseService.statisticsTrainCourse(statisticsTrainCourse);
        return getDataTable(statisticsTrainCourses);
    }

    /**
     * 根据考试查询人员考试详情
     * @param courseId 考试id
     * @return List<TTrainUserCourse>
     */
    @GetMapping(value = "testPersonDetailByCourseId")
    public TableDataInfo testPersonDetailByCourseId(@RequestParam(value = "courseId") Long courseId){
        startPage();
        List<TTrainUserCourse> persons = tTrainCourseService.testPersonDetailByCourseId(courseId);
        return getDataTable(persons);
    }

    /**
     * 导出所有考试详细数据项
     */
    @Log(title = "设备信息管理", businessType = BusinessType.EXPORT)
    @GetMapping("/examDetails")
    public AjaxResult examDetails(@RequestParam(value = "courseId") Long courseId)
    {
        List<TTrainUserCourse> persons = tTrainCourseService.examDetails(courseId);
        ExcelUtil<TTrainUserCourse> util = new ExcelUtil<TTrainUserCourse>(TTrainUserCourse.class);
        return util.exportExcel(persons, "考试详细数据");
    }

    /**
     * 手机端 最新消息查询
     */
    @GetMapping("/latestNews")
    public TNews latestNews(Long recipientId){
        //最新消息查询
        return tTrainCourseService.selectLatestNews(recipientId);
    }

    /**
     * 手机端 查询历史消息
     * @param recipientId
     * @return
     */
    @GetMapping("/historicalMessages")
    public List<TNews> historicalMessages(Long recipientId){
        return tTrainCourseService.historicalMessages(recipientId);
    }

    /**
     * 修改消息状态为已读
     */
    @GetMapping("/updateReadStatus")
    public AjaxResult updateReadStatus(Long newsId){
        return toAjax(tTrainCourseService.updateReadStatus(newsId));
    }


}
