package com.ynfy.app.api.v1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.app.api.v1.entity.dto.RepositoryDTO;
import com.ynfy.buss.exam.question.service.IQuestionService;
import com.ynfy.buss.exam.repository.entity.Repository;
import com.ynfy.buss.exam.repository.service.IRepositoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/repository")
public class ApiRepositoryController extends ApiBaseController {

    @Autowired
    public IRepositoryService repositoryService;

    @Autowired
    private IQuestionService questionService;


    /**
     * 获取列表
     *
     * @return
     */
    @PostMapping("/list")
    @IgnoreAuth
    public Result<IPage<Repository>> list(@RequestBody RepositoryDTO repositoryDTO) {
        Page<Repository> page = new Page<>(repositoryDTO.getPageNo(), repositoryDTO.getPageSize());
        LambdaQueryWrapper<Repository> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(repositoryDTO.getRepositoryTitle())) {
            query.like(Repository::getTitle, repositoryDTO.getRepositoryTitle());
        }
        IPage<Repository> pageList = repositoryService.page(page, query);
        //题库获取各类型题目数
        repositoryService.countQuestionNumByType(pageList.getRecords());
        return Result.OK(pageList);
    }

    /**
     * 计算指定题库的题目题型数量
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "计算指定题库的题目题型数量", notes = "计算指定题库的题目题型数量")
    @GetMapping(value = "/getById")
    public Result<Repository> getById(@RequestParam String id) {
        Repository repository = repositoryService.getById(id);
        repository.setQuestionDTOList(questionService.countQuestionNumByType(id));
        return Result.OK(repository);
    }
}
