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.collect.bo.CollectNoteAddBo;
import com.zhongzheng.modules.collect.bo.CollectNoteQueryBo;
import com.zhongzheng.modules.collect.domain.CollectNote;
import com.zhongzheng.modules.collect.service.ICollectNoteService;
import com.zhongzheng.modules.collect.vo.CollectNoteVo;
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 hjl
 * @date 2021-06-22
 */
@Api(value = "收藏考试重点控制器", tags = {"收藏考试重点管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/collect/note")
public class CollectNoteController extends BaseController {

    private final ICollectNoteService iCollectNoteService;

    private final WxTokenService wxTokenService;

    /**
     * 查询收藏考试重点列表
     */
    @ApiOperation("查询收藏考试重点列表")
    @GetMapping("/list")
    public TableDataInfo<CollectNoteVo> list(CollectNoteQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<CollectNoteVo> list = iCollectNoteService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 获取收藏考试重点详细信息
     */
    @ApiOperation("判断是否收藏重点")
    @GetMapping("/{noteId}")
    public AjaxResult<CollectNote> getInfo(@PathVariable("noteId" ) Long noteId) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iCollectNoteService.queryByNoteId(loginUser.getUser().getUserId(),noteId));
    }

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


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