package cn.exploring.engine.server.wx.web;

import cn.exploring.engine.server.core.storage.StorageService;
import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.StudentPointInfo;
import cn.exploring.engine.server.db.domain.vo.IdVo;
import cn.exploring.engine.server.db.service.StudentInfoService;
import cn.exploring.engine.server.db.service.StudentPointService;
import cn.exploring.engine.server.db.util.StrUtil;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/wx/point")
@Validated
public class PointController {

    @Resource
    StudentPointService pointService;

    @Resource
    StudentInfoService studentInfoService;

    @Resource
    StorageService storageService;

    @GetMapping("selectPoint")
    public Object selectPoint(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId, @RequestParam String startTime, @RequestParam String endTime,
                              @RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit,
                              @RequestParam(required = false, defaultValue = "addTime") String sort, @RequestParam(required = false, defaultValue = "desc") String order) {
        studentUniqueId = StringUtils.isEmpty(studentUniqueId) ? null : studentUniqueId;
        studentName = StringUtils.isEmpty(studentName) ? null : studentName;
        startTime = StringUtils.isEmpty(startTime) ? null : startTime;
        endTime = StringUtils.isEmpty(endTime) ? null : endTime;
        return ResponseUtil.ok(pointService.selectPointByWeb(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order));
    }

    @GetMapping("export")
    public Object export(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId, @RequestParam String startTime, @RequestParam String endTime,
                              @RequestParam(required = false, defaultValue = "addTime") String sort, @RequestParam(required = false, defaultValue = "desc") String order) {
        studentUniqueId = StringUtils.isEmpty(studentUniqueId) ? null : studentUniqueId;
        studentName = StringUtils.isEmpty(studentName) ? null : studentName;
        startTime = StringUtils.isEmpty(startTime) ? null : startTime;
        endTime = StringUtils.isEmpty(endTime) ? null : endTime;

        File file = pointService.exportList(studentName, studentUniqueId, classId, startTime, endTime);
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            return ResponseUtil.ok(storageService.store(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", StrUtil.format("学生成绩-{}.xlsx", System.currentTimeMillis()), storageService.generateKey(file.getName())));

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(in)) {
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            }
        }
    }


    @PostMapping("add")
    public Object add(@RequestBody StudentPointInfo pointInfo) {
        String status = pointService.addPoint(pointInfo);
        return status.contains("OK") ? ResponseUtil.ok(status) : ResponseUtil.fail(502, status);
    }


    @PostMapping("update")
    public Object update(@RequestBody StudentPointInfo pointInfo) {
        pointService.updatePoint(pointInfo);
        return ResponseUtil.ok("OK");
    }

    @PostMapping("batchDeleted")
    public Object batchDeleted(@RequestBody List<Integer> idList) {
        return ResponseUtil.ok(pointService.batchDeleted(idList));
    }

    @PostMapping("removeAll")
    public Object removeAll() {
        pointService.removeAll();
        return ResponseUtil.ok();
    }

    @PostMapping("remove")
    public Object remove(@RequestBody IdVo req) {
        pointService.removeByStudentId(req.getId());
        return ResponseUtil.ok();
    }


//    @GetMapping("exportByClass")
//    public Object exportByClass(Integer classId) {
//        List<StudentPointInfo> list = pointService.listByClassId(classId);
//        if (CollectionUtils.isEmpty(list)) {
//            return ResponseUtil.fail(402, "暂无数据");
//        }
//
//        File file = pointService.transToExcelFile(pointService.transToExcelExportVo(list));
//        FileInputStream in = null;
//        try {
//            in = new FileInputStream(file);
//            return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName())));
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (Objects.nonNull(in)) {
//                try {
//                    in.close();
//                } catch (IOException ignored) {
//                }
//            }
//        }
//    }

}
