package com.xinyilu.web.controller.scoreExport;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.custominterface.Query;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.system.domain.dto.ImportTestProjectScoresDTO;
import com.xinyilu.system.domain.dto.ScoreExportDTO;
import com.xinyilu.system.domain.vo.scoreExport.ScoreExportVO;
import com.xinyilu.system.service.scoreExport.PersonalScoreExportService;
import com.xinyilu.system.service.scoreExport.ScoreExportKindergartenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体测成绩数据导出
 * <p>
 * 成绩导出
 *
 * @author wanghao
 * @create 2023/8/8 0008
 */
@RestController
@RequestMapping("/system/scoreExport")
public class ScoreExportController extends BaseController {

    @Resource
    private PersonalScoreExportService personalScoreExportService;
    @Resource
    private ScoreExportKindergartenService scoreExportKindergartenService;

    /**
     * 个人成绩导出集合查看
     */
    @GetMapping("/list")
    public TableDataInfo list(ImportTestProjectScoresDTO scoreExport) {
        startPage();
        List<ScoreExportVO> list = personalScoreExportService.list(scoreExport);
        return getDataTable(list);
    }

    /**
     * 导出个人成绩的信息
     */
    @Log(title = "导出个人成绩的信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImportTestProjectScoresDTO scoreExport) throws IOException {
        List<ScoreExportVO> list = personalScoreExportService.export(scoreExport);
        OutputStream outputStream = response.getOutputStream();
        ClassPathResource classPathResource = new ClassPathResource("template/个人成绩导出.xlsx");
        InputStream inputStream = classPathResource.getInputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).excelType(ExcelTypeEnum.XLSX).autoCloseStream(Boolean.FALSE).build();
        // 每次都会重新生成新的一行，而不是使用下面的空行
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        //占位符替换
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).forceNewRow(Boolean.TRUE).build();
        Map<String, Object> map = new HashMap<>();
        map.put("kindergartenName", StringUtils.isBlank(scoreExport.getKindergartenName()) ? null : scoreExport.getKindergartenName());
        map.put("bachCode", StringUtils.isBlank(scoreExport.getBatchCode()) ? null : scoreExport.getBatchCode());
        map.put("className", StringUtils.isBlank(scoreExport.getClassName()) ? null : scoreExport.getClassName());
        excelWriter.fill(map, writeSheet);
        //要遍历的数据设置
        excelWriter.fill(new FillWrapper("data", list), fillConfig, writeSheet);
        excelWriter.finish();
    }

    /**
     * 导出测试分类和测试项目关系列表(班级)
     */
    //@PreAuthorize("@ss.hasPermi('system:testCategoryItem:export')")
    @Log(title = "体测(班级)成绩数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/scoreExportByClass")
    public void scoreExportByClass(HttpServletResponse response, @Validated(Query.class) ScoreExportDTO scoreExportDTO
    ) throws IOException {
        scoreExportKindergartenService.scoreExportByClass(response, scoreExportDTO);
    }

    /**
     * 查看测试分类和测试项目关系列表(班级)
     */
    //@PreAuthorize("@ss.hasPermi('system:testCategoryItem:export')")
    @GetMapping("/scoreByClassList")
    public TableDataInfo scoreByClassList(@Validated(Query.class) ScoreExportDTO scoreExportDTO) throws IOException {
        return scoreExportKindergartenService.scoreByClassList(scoreExportDTO);
    }

    /**
     * 导出测试分类和测试项目关系列表(幼儿园)
     */
    //@PreAuthorize("@ss.hasPermi('system:testCategoryItem:export')")
    @Log(title = "体测(幼儿园)成绩数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/scoreExportByKindergarten")
    public void scoreExportByKindergarten(HttpServletResponse response,
                                          @Validated(Query.class) ScoreExportDTO scoreExportDTO) throws IOException {
        scoreExportKindergartenService.scoreExportByKindergarten(response, scoreExportDTO);
    }


    /**
     * 查看测试分类和测试项目关系列表(幼儿园)
     */
    //@PreAuthorize("@ss.hasPermi('system:testCategoryItem:export')")
    @GetMapping("/scoreByKindergartenList")
    public TableDataInfo scoreByKindergartenList(@Validated(Query.class) ScoreExportDTO scoreExportDTO) throws IOException {
        return scoreExportKindergartenService.scoreByKindergartenList(scoreExportDTO);
    }
}
