package jnpf.cultivate.controller.web.learn;

import cn.hutool.core.collection.CollUtil;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateLearnTaskInfoService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.learn.FtbCultivateLearnTaskInfoExprotDto;
import jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskFinishInfoVO;
import jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskFinishStatisticsVO;
import jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskInfoVO;
import jnpf.model.cultivate.vo.learn.info.FtbCultivateLearnTaskUserFinishInfoVO;
import jnpf.model.enums.TaskLearnStatusEnums;
import jnpf.util.excel.EasyExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * web学习任务模块
 *
 * @Author: peng.hao
 * @create: 2024/9/9:11:20
 */
@RestController
@Slf4j
@RequestMapping("/web/learn-task")
public class FtbCultivateLearnTaskInfoController {

    @Resource
    private FtbCultivateLearnTaskInfoService taskInfoService;


    /**
     * 任务内容详情
     */
    @GetMapping("/getLearnTaskInfo")
    public ActionResult<FtbCultivateLearnTaskInfoVO> getLearnTaskInfo(@RequestParam("taskId") String taskId) {
        return ActionResult.success(taskInfoService.getLearnTaskInfo(taskId));
    }

    /**
     * 完成情况统计
     *
     * @param taskId 任务id
     * @return
     */
    @GetMapping("/getCompletionStatistics")
    public ActionResult<FtbCultivateLearnTaskFinishStatisticsVO> getCompletionStatistics(@RequestParam("taskId") String taskId) {
        return ActionResult.success(taskInfoService.getCompletionStatistics(taskId));
    }

    /**
     * 完成情况列表
     *
     * @param taskId 任务id
     * @return
     */
    @GetMapping("/getListOfCompletions")
    public ActionResult<PageListVO<FtbCultivateLearnTaskFinishInfoVO>> getListOfCompletions(@RequestParam("taskId") String taskId, CultivatePage page) {
        PageListVO<FtbCultivateLearnTaskFinishInfoVO> listOfCompletions = taskInfoService.getListOfCompletions(taskId, page, null);
        return ActionResult.success(listOfCompletions);
    }

    /**
     * 查看任务完成情况
     *
     * @param taskId 任务id
     * @param userId 用户id
     */
    @GetMapping("/viewTaskCompletion")
    public ActionResult<FtbCultivateLearnTaskUserFinishInfoVO> viewTaskCompletion(@RequestParam("taskId") String taskId,
                                                                                  @RequestParam("userId") String userId) {
        return ActionResult.success(taskInfoService.viewTaskCompletion(taskId, userId));
    }

    /**
     * 导出完成情况列表
     *
     * @return
     */
    @PostMapping("/exportListOfCompletions")
    public void exportListOfCompletions(@RequestBody FtbCultivateLearnTaskInfoExprotDto dto,
                                        HttpServletResponse response) throws IOException {
        CultivatePage page = new CultivatePage();
        page.setPageSize(-1);
        PageListVO<FtbCultivateLearnTaskFinishInfoVO> listOfCompletions = taskInfoService.getListOfCompletions(dto.getTaskId(), page,dto.getIds());
        List<FtbCultivateLearnTaskFinishInfoVO> list = listOfCompletions.getList();
        dealExportExcelData(list);
        EasyExcelUtils.exportExcel(response, "任务完成情况", list, FtbCultivateLearnTaskFinishInfoVO.class);
    }

    /**
     * 处理导入数据
     * @param list
     */
    private void dealExportExcelData(List<FtbCultivateLearnTaskFinishInfoVO> list) {
        if(CollUtil.isEmpty(list)){
            return;
        }
        for (FtbCultivateLearnTaskFinishInfoVO vo : list) {
            //处理状态 0未开始，1进行中，2已完成，3已逾期
            String completionStatusStr="";
            TaskLearnStatusEnums taskLearnStatusEnums = TaskLearnStatusEnums.fromCode(vo.getCompletionStatus());
            if(taskLearnStatusEnums!=null){
                completionStatusStr=taskLearnStatusEnums.getMsg();
            }
            vo.setCompletionStatusStr(completionStatusStr);
        }
    }
}
