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.CollectCourseAddBo;
import com.zhongzheng.modules.collect.bo.CollectCourseEditBo;
import com.zhongzheng.modules.collect.bo.CollectCourseQueryBo;
import com.zhongzheng.modules.collect.domain.CollectCourse;
import com.zhongzheng.modules.collect.service.ICollectCourseService;
import com.zhongzheng.modules.collect.vo.CollectCourseVo;
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/course")
public class CollectCourseController extends BaseController {

    private final ICollectCourseService iCollectCourseService;

    private final WxTokenService wxTokenService;

    /**
     * 查询收藏课程列表
     */
    @ApiOperation("查询收藏课程列表")
    @GetMapping("/list")
    public TableDataInfo<CollectCourseVo> list(CollectCourseQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<CollectCourseVo> list = iCollectCourseService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 获取收藏课程详细信息
     */
    @ApiOperation("判断是否收藏课程")
    @GetMapping("/{courseId}")
    public AjaxResult<CollectCourse> getInfo(@PathVariable("courseId" ) Long courseId) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iCollectCourseService.queryByCourseId(loginUser.getUser().getUserId(),courseId));
    }

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



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