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

import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportResultVo;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportVo;
import cn.exploring.engine.server.db.service.ClassService;
import cn.exploring.engine.server.db.service.StudentInfoService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/wx/student")
@Validated
public class StudentController {

    @Resource
    StudentInfoService studentInfoService;

    @Resource
    ClassService classService;


    @GetMapping("selectStudent")
    public Object selectClass(@RequestParam String studentName, @RequestParam String studentUniqueId, @Nullable @RequestParam Integer classId,
                              @Nullable @RequestParam Integer minCall, @Nullable @RequestParam Integer maxCall,
                              @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) {
        return ResponseUtil.ok(studentInfoService.selectStudentInfoByWeb(studentName, studentUniqueId, null, classId,  page, limit, sort, order, minCall, maxCall));
    }

    @GetMapping("randomSelect")
    public Object randomSelect(@RequestParam Integer classId, @Nullable @RequestParam Integer maxCall) {
        return ResponseUtil.ok(studentInfoService.selectStudentInfo(classId, maxCall));
    }

    @PostMapping("add")
    public Object add(@RequestBody StudentInfo studentInfo) {
        String status = studentInfoService.addStudentInfo(studentInfo);
        return status.contains("OK") ? ResponseUtil.ok(status) : ResponseUtil.fail(301, status);
    }


    @PostMapping("update")
    public Object update(@RequestBody StudentInfo studentInfo) {
        String err = studentInfoService.updateStudentInfo(studentInfo);
        return StringUtils.isNotBlank(err) ? ResponseUtil.fail(301, err) : ResponseUtil.ok("修改成功");
    }

    @PostMapping("emptyCall/all")
    public Object emptyAllCall() {
        String err = studentInfoService.handleEmptyAllCall();
        return StringUtils.isNotBlank(err) ? ResponseUtil.fail(301, err) : ResponseUtil.ok("修改成功");
    }

    @PostMapping("batchDeleted")
    public Object batchDeleted(@RequestBody List<Integer> idList) {
        String err = studentInfoService.batchDeleted(idList);
        return StringUtils.isNotBlank(err) ? ResponseUtil.fail(301, err) : ResponseUtil.ok("删除成功");
    }

    @PostMapping("import")
    public Object handleImport(@RequestParam("file") MultipartFile file, @RequestParam Integer classId) {

        if (Objects.isNull(classService.selectByPrimaryKey(classId))) {
            return ResponseUtil.fail(407, "班级不存在");
        }

        List<StudentInfoExcelImportVo> voList;
        try {
             voList = studentInfoService.importByExcel(file);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(407, "文件打开失败");
        }

        if (CollectionUtils.isEmpty(voList)) {
            return ResponseUtil.fail(407, "未能读取到有效的数据");
        }

        List<StudentInfoExcelImportResultVo> errors = studentInfoService.checkImportByExcel(voList, classId);
        if (!CollectionUtils.isEmpty(errors)) {
            return ResponseUtil.fail(408, "文件存在错误，请检查", errors);
        }

        List<StudentInfoExcelImportResultVo> list = studentInfoService.handleImport(voList, classId);

        return ResponseUtil.ok(ImmutableMap.of("total", list.size(), "list", list));
    }
}
