package com.ruoyi.web.controller.ie;

import com.google.common.collect.Lists;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.sy.domain.SyTestExaminee;
import com.ruoyi.util.PageUtil;
import com.ruoyi.web.domain.TestMarjorDto;
import com.ruoyi.web.service.SyTestMajorService;
import com.ruoyi.web.util.MentalHealthExporter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "测评-健康测试")
@RestController
@RequestMapping("front/mentalHealth/")
public class FrontMentalHealthController extends BaseController {
    private final SyTestMajorService syTestMajorService;

    public FrontMentalHealthController(SyTestMajorService syTestMajorService) {
        this.syTestMajorService = syTestMajorService;
    }

    @GetMapping("list")
    @ApiOperation("01 Get MentalHealth form history list")
    public TableDataInfo getList(@ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                              @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        startPage();
        List<SyTestExaminee> examineeList = syTestMajorService.getTestListWithStatus(4, new Integer[]{6, 7});
        List<TestMarjorDto.MentalHealth> mentalHealthList = Lists.newArrayList();
        for(SyTestExaminee e: examineeList) {
            mentalHealthList.add(new TestMarjorDto.MentalHealth(e.getExamineeId(), e.getBeginTime()));
        }
        return getDataTable(PageUtil.getDtoListWithPageable(examineeList, mentalHealthList));
    }

    @GetMapping("report")
    @ApiOperation("02 Returns result")
    public AjaxResult getReport(@RequestParam Long examineeId) {
        TestMarjorDto.MentalHealthResult result = new TestMarjorDto.MentalHealthResult();
        SyTestExaminee examinee = syTestMajorService.getTestExaminee(4, examineeId);
        if(null != examinee) {
            result.setEndTime(examinee.getEndTime());
            for (TestMarjorDto.TestMatchStat stat : syTestMajorService.toTestMatchStats(examinee)) {
                result.setValue(stat.getCode(), stat.getCount());
            }
        }
        return AjaxResult.success("查询成功!", result);
    }

    @GetMapping("/export")
    @ApiOperation("03 export result")
    public void export(@RequestParam Long examineeId, HttpServletResponse response)
    {
        SyTestExaminee examinee = syTestMajorService.getTestExaminee(4, examineeId);
        List<TestMarjorDto.TestMatchStat> examineeStatList = syTestMajorService.toTestMatchStats(examinee);
        Map<String, Integer> categoryScoreMap = examineeStatList.stream().collect(Collectors.toMap(TestMarjorDto.TestMatchStat::getCode, TestMarjorDto.TestMatchStat::getCount));
        MentalHealthExporter.export(response, examineeId, categoryScoreMap);
    }
}
