package com.qxgmat.controller.admin;

import com.github.pagehelper.Page;
import com.nuliji.tools.PageMessage;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.dto.admin.extend.UserExtendDto;
import com.qxgmat.dto.admin.response.UserFeedbackErrorDetailDto;
import com.qxgmat.dto.admin.response.UserFeedbackErrorListDto;
import com.qxgmat.dto.admin.response.UserAskListDto;
import com.qxgmat.service.UsersService;
import com.qxgmat.service.inline.UserFeedbackErrorService;
import com.qxgmat.service.inline.ManagerLogService;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@RestController("AdminFeedbackErrorController")
@RequestMapping("/admin/user/feedback_error")
@Api(tags = "用户勘误接口", description = "用户勘误相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserFeedbackErrorController {
    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private UserFeedbackErrorService userFeedbackErrorService;

    @Autowired
    private UsersService usersService;


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "修改提问排序", httpMethod = "GET")
    public Response<UserFeedbackErrorDetailDto> detail(@RequestParam int id, HttpServletRequest request) {
        UserFeedbackError entity = userFeedbackErrorService.get(id);
        UserFeedbackErrorDetailDto dto = Transform.convert(entity, UserFeedbackErrorDetailDto.class);

        return ResponseHelp.success(dto);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "勘误列表", httpMethod = "GET")
    public Response<PageMessage<UserFeedbackErrorListDto>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            HttpSession session) {
        Page<UserFeedbackError> p = userFeedbackErrorService.select(page, size);
        List<UserFeedbackErrorListDto> pr = Transform.convert(p, UserFeedbackErrorListDto.class);

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

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