package vip.xiaonuo.exam.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.exam.base.BaseApiController;
import vip.xiaonuo.exam.base.RestResponse;
import vip.xiaonuo.exam.domain.ExamPaperAnswer;
import vip.xiaonuo.exam.domain.Subject;
import vip.xiaonuo.exam.service.ExamPaperAnswerService;
import vip.xiaonuo.exam.service.SubjectService;
import vip.xiaonuo.exam.utility.DateTimeUtil;
import vip.xiaonuo.exam.utility.ExamUtil;
import vip.xiaonuo.exam.utility.PageInfoHelper;
import vip.xiaonuo.exam.viewmodel.admin.paper.ExamPaperAnswerPageRequestVM;
import vip.xiaonuo.exam.viewmodel.student.exampaper.ExamPaperAnswerPageResponseVM;

@RestController("AdminExamPaperAnswerController")
@RequestMapping(value = "/api/admin/examPaperAnswer")
public class ExamPaperAnswerController extends BaseApiController {

    private final ExamPaperAnswerService examPaperAnswerService;
    private final SubjectService subjectService;


    @Autowired
    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, SubjectService subjectService) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.subjectService = subjectService;

    }


    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperAnswerPageResponseVM>> pageJudgeList(@RequestBody ExamPaperAnswerPageRequestVM model) {
        PageInfo<ExamPaperAnswer> pageInfo = examPaperAnswerService.adminPage(model);
        PageInfo<ExamPaperAnswerPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
            Subject subject = subjectService.selectById(vm.getSubjectId());
            vm.setDoTime(ExamUtil.secondToVM(e.getDoTime()));
            vm.setSystemScore(ExamUtil.scoreToVM(e.getSystemScore()));
            vm.setUserScore(ExamUtil.scoreToVM(e.getUserScore()));
            vm.setPaperScore(ExamUtil.scoreToVM(e.getPaperScore()));
            vm.setSubjectName(subject.getName());
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
         //   User user = userService.selectById(Integer.valueOf(e.getCreateUser()));
          //  vm.setUserName(user.getUserName());
            return vm;
        });
        return RestResponse.ok(page);
    }


}
