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.CollectBankAddBo;
import com.zhongzheng.modules.collect.bo.CollectBankEditBo;
import com.zhongzheng.modules.collect.bo.CollectBankQueryBo;
import com.zhongzheng.modules.collect.domain.CollectBank;
import com.zhongzheng.modules.collect.service.ICollectBankService;
import com.zhongzheng.modules.collect.vo.CollectBankVo;
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-23
 */
@Api(value = "收藏题库控制器", tags = {"收藏题库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/collect/bank")
public class CollectBankController extends BaseController {

    private final ICollectBankService iCollectBankService;

    private final WxTokenService wxTokenService;

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

    /**
     * 获取收藏题库详细信息
     */
    @ApiOperation("判断是否收藏题库")
    @GetMapping("/{collectBankId}")
    public AjaxResult<CollectBank> getInfo(@PathVariable("collectBankId" ) Long collectBankId) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iCollectBankService.queryByBankId(loginUser.getUser().getUserId(),collectBankId));
    }

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


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