package com.zhongzheng.controller.collect;

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

import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.bank.vo.QuestionVo;
import com.zhongzheng.modules.collect.bo.CollectQuestionAddBo;
import com.zhongzheng.modules.collect.bo.CollectQuestionEditBo;
import com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo;
import com.zhongzheng.modules.collect.domain.CollectQuestion;
import com.zhongzheng.modules.collect.service.ICollectQuestionService;
import com.zhongzheng.modules.collect.vo.CollectQuestionVo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 收藏题目Controller
 *
 * @author ruoyi
 * @date 2021-06-24
 */
@Api(value = "收藏题目控制器", tags = {"收藏题目管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/collect/question")
public class CollectQuestionController extends BaseController {

    private final ICollectQuestionService iCollectQuestionService;

    private final WxTokenService wxTokenService;

    /**
     * 查询收藏题目列表
     */
    @ApiOperation("查询收藏题目列表")
    @GetMapping("/list")
    public TableDataInfo<QuestionVo> list(CollectQuestionQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<QuestionVo> list = iCollectQuestionService.selectList(bo);
        return getDataTable(list);
    }



    /**
     * 获取收藏题目详细信息
     */
    @ApiOperation("获取收藏题目详细信息")
    @GetMapping("/{questionId}")
    public AjaxResult<CollectQuestion> getInfo(@PathVariable("questionId" ) Long questionId) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iCollectQuestionService.queryQuestionById(loginUser.getUser().getUserId(),questionId));
    }

    /**
     * 新增收藏题目
     */
    @ApiOperation("新增收藏题目")
    @Log(title = "收藏题目", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody CollectQuestionAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iCollectQuestionService.insertByAddBo(bo) ? 1 : 0);
    }



    /**
     * 删除收藏题目
     */
    @ApiOperation("删除收藏题目")
    @Log(title = "收藏题目" , businessType = BusinessType.DELETE)
    @PostMapping("/delete/{collectQuestionIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] collectQuestionIds) {
        return toAjax(iCollectQuestionService.deleteWithValidByIds(Arrays.asList(collectQuestionIds), true) ? 1 : 0);
    }
}
