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.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.question.domain.QuestionCollection;
import com.miaxis.question.dto.QuestionCollectionDTO;
import com.miaxis.question.service.IQuestionCollectionService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

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

    /**
     * 查询collection列表
     */

    @GetMapping("/list")
    @ApiOperation("查询collection列表")
        @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<QuestionCollection> list(@ModelAttribute QuestionCollection questionCollection){
        startPage();
        List<QuestionCollection> list = questionCollectionService.selectQuestionCollectionList(questionCollection);
        return toResponsePageInfo(list);
    }

    /**
     * 导出collection列表
     */
//    @PreAuthorize("@ss.hasPermi('question:collection:export')")
//    @Log(title = "collection", businessType = BusinessTypeEnum.EXPORT)
//    @GetMapping("/export")
//    @ApiOperation("导出collection列表Excel")
//    public Response<String> export(@ModelAttribute QuestionCollection questionCollection){
//        List<QuestionCollection> list = questionCollectionService.selectQuestionCollectionList(questionCollection);
//        ExcelUtil<QuestionCollection> util = new ExcelUtil<QuestionCollection>(QuestionCollection.class);
//        return util.exportExcel(list, "collection");
//    }

    /**
     * 获取collection详细信息
     */
//    @PreAuthorize("@ss.hasPermi('question:collection:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取collection详细信息")
    public Response<QuestionCollection> getInfo(
            @ApiParam(name = "id", value = "collection参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(questionCollectionService.getById(id));
    }

    /**
     * 新增collection
     */
    @Log(title = "collection", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增collection")
    public Response<Integer> add(@RequestBody QuestionCollectionDTO questionCollectionDTO){
        QuestionCollection questionCollection = new QuestionCollection();
        BeanUtils.copyProperties(questionCollectionDTO,questionCollection);
        questionCollection.setUserId(SecurityUtils.getLoginUser().getStudent().getId());
        return toResponse(questionCollectionService.save(questionCollection) ? 1 : 0);
    }

    @PostMapping("collections")
    @ApiOperation("批量新增collection")
    public Response<Integer> collections(@RequestBody List<QuestionCollectionDTO> list){
        List<QuestionCollection> qlist = new ArrayList<QuestionCollection>();
        for (QuestionCollectionDTO questionCollectionDTO: list) {
            QuestionCollection questionCollection = new QuestionCollection();
            BeanUtils.copyProperties(questionCollectionDTO,questionCollection);
            questionCollection.setUserId(SecurityUtils.getLoginUser().getStudent().getId());
            qlist.add(questionCollection);
        }
        return toResponse(questionCollectionService.saveBatch(qlist) ? 1 : 0);
    }


    /**
     * 修改collection
     */
//    @PreAuthorize("@ss.hasPermi('question:collection:edit')")
    @Log(title = "collection", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("修改collection")
    public Response<Integer> edit(@RequestBody QuestionCollection questionCollection){
        return toResponse(questionCollectionService.updateById(questionCollection) ? 1 : 0);
    }

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