package com.miaxis.app.controller.question;

import com.miaxis.common.annotation.Log;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.question.domain.QuestionCollection;
import com.miaxis.question.domain.QuestionError;
import com.miaxis.question.dto.QuestionCollectionDto;
import com.miaxis.question.service.IQuestionCollectionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 【题目收藏】Controller
 *
 * @author miaxis
 * @date 2021-03-18
 */
@RestController
@RequestMapping(Constants.STUDENT_PREFIX+"/question/collection")
@Api(tags={"【小程序-题目收藏】"})
public class QuestionCollectionController extends BaseController{
    @Autowired
    private IQuestionCollectionService questionCollectionService;





    /**
     * 新增题目收藏
     */
    @Log(title = "题目收藏", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增题目收藏")
    public Response<Integer> add(@RequestBody QuestionCollection questionCollection){
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        questionCollection.setUserId(student.getId());
        return toResponse(questionCollectionService.save(questionCollection) ? 1 : 0);
    }

    @GetMapping("/getQuestionCollectionList")
    @ApiOperation("查询题目收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<QuestionError> getQuestionCollectionList(@ModelAttribute QuestionCollectionDto questionCollectionDto){
        startPage();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        questionCollectionDto.setUserId(student.getId());
        List<QuestionError> list = questionCollectionService.getQuestionCollectionList(questionCollectionDto);
        return toResponsePageInfo(list);
    }



    /**
     * 删除题目收藏
     */
    @Log(title = "题目收藏", businessType = BusinessTypeEnum.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除题目收藏")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "题目收藏ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(questionCollectionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
