package com.qxgmat.controller.admin;

import com.nuliji.tools.*;
import com.qxgmat.data.dao.entity.HomeworkPreview;
import com.qxgmat.data.dao.entity.User;
import com.qxgmat.data.relation.entity.UserHomeworkPreviewRelation;
import com.qxgmat.dto.admin.response.UserHomeworkPreviewListDto;
import com.qxgmat.service.UsersService;
import com.qxgmat.service.HomeworkPreviewService;
import com.qxgmat.service.inline.ManagerLogService;
import com.qxgmat.service.UserPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@RestController("AdminUserPreviewController")
@RequestMapping("/admin/user/preview")
@Api(tags = "用户预习作业接口", description = "用户预习作业", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserPreviewController {
    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private UserPaperService userPaperService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private HomeworkPreviewService homeworkPreviewService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "预习作业列表", httpMethod = "GET")
    public Response<PageMessage<UserHomeworkPreviewListDto>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false, defaultValue = "0") int category,
            @RequestParam(required = false, name = "user_id") Integer userId,
            @RequestParam(required = false, name = "preview_id") Integer previewId,
            @RequestParam(required = false, name="start_time") String startTime,
            @RequestParam(required = false, name="end_time") String endTime,
            HttpSession session) {
        PageResult<UserHomeworkPreviewRelation> p = homeworkPreviewService.listAdmin(page, size, category, userId, previewId, startTime, endTime);

        // 绑定用户
        Collection userIds = Transform.getIds(p, UserHomeworkPreviewRelation.class, "userId");
        List<User> userList = usersService.select(userIds);
        Transform.combine(p, userList, UserHomeworkPreviewRelation.class, "userId", "user", User.class, "id");

        // 绑定预习作业
        Collection previewIds = Transform.getIds(p, UserHomeworkPreviewRelation.class, "moduleId");
        List<HomeworkPreview> homeworkPreviewList = homeworkPreviewService.select(previewIds);
        Transform.combine(p, homeworkPreviewList, UserHomeworkPreviewRelation.class, "moduleId", "preview", HomeworkPreview.class, "id");

        List<UserHomeworkPreviewListDto> pr = Transform.convert(p, UserHomeworkPreviewListDto.class);

        return ResponseHelp.success(pr, page, size, p.getTotal());
    }
}
