package com.qmth.qrzk.web.controller;


import com.qmth.qrzk.repository.constants.WebConstants;
import com.qmth.qrzk.repository.service.query.PaperAnalysisParams;
import com.qmth.qrzk.repository.user.model.PaperAnalysis;
import com.qmth.qrzk.repository.user.model.Role;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.user.service.impl.PaperAnalysisService;
import com.qmth.qrzk.repository.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("papers/analysis")
public class PaperAnalysisController extends BaseAjaxController{

    @Autowired
    private PaperAnalysisService paperAnalysisService;

    @RequestMapping("/page")
    @ResponseBody
    public Map<String, Object> page(HttpSession session ,@RequestBody final PaperAnalysisParams params) {
        Role role=getCurRole(session);
        if(role.getCode().equalsIgnoreCase(Consts.UserRoleCodes.NORMAL_USER)){
            User user = getCurUser(session);
            params.setCurUserId(user.getId());
        }
        Map<String, Object> result = new HashMap<String, Object>();
        List<PaperAnalysis> rows = paperAnalysisService.queryPaperAnalysis(params);
        result.put("rows", rows);
        result.put("total", params.getTotalNum());
        return result;
    }

    @RequestMapping(value = "/upload/{batch}")
    @ResponseBody
    public Map<String, Object> upload(@PathVariable String batch, @RequestParam("file") MultipartFile file) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (!file.isEmpty()) {
                paperAnalysisService.upload(file.getInputStream(), batch);
                resultMap.put("flag", "success");
            } else {
                resultMap.put("flag", "failed");
                resultMap.put("msg", "上传的是空文件！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("flag", "failed");
            resultMap.put("msg", e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value = "/list/{batch}/{courseCode}", method = RequestMethod.GET)
    @ResponseBody
    public List<PaperAnalysis> list(@PathVariable String courseCode,@PathVariable String batch) {
        List<PaperAnalysis> list = paperAnalysisService.findByBatchAndCourseCode(batch, courseCode);
        return list;
    }
    @RequestMapping(value = "/queryAllBatch")
    @ResponseBody
    public List<String> queryAllBatch(HttpSession session) {
        List<String> rows = paperAnalysisService.queryAllBatch();
        return rows;
    }
}
