package com.fs.course.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsCourseWatchComment;
import com.fs.course.param.FsCourseWatchCommentPageParam;
import com.fs.course.service.IFsCourseWatchCommentService;
import com.fs.course.vo.FsCourseWatchCommentListVO;
import com.fs.qw.service.IQwExternalContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 看课评论Controller
 *
 * @author fs
 * @date 2025-05-26
 */
@RestController
@RequestMapping("/course/courseWatchComment")
public class FsCourseWatchCommentController extends BaseController
{
    @Autowired
    private IFsCourseWatchCommentService fsCourseWatchCommentService;

    @Autowired
    private IQwExternalContactService qwExternalContactService;

    /**
     * 查询看课评论列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:list')")
    @GetMapping("/list")
    public R list(FsCourseWatchCommentPageParam fsCourseWatchCommentPageParam)
    {
//        startPage();
        PageHelper.startPage(fsCourseWatchCommentPageParam.getPageNum(), fsCourseWatchCommentPageParam.getPageSize());
        List<FsCourseWatchCommentListVO> list = fsCourseWatchCommentService.selectFsCourseWatchCommentList(fsCourseWatchCommentPageParam);
        PageInfo<FsCourseWatchCommentListVO> pageInfo = new PageInfo<>(list);
        return R.ok().put("rows", pageInfo);
    }

    /**
     * 导出看课评论列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:export')")
    @Log(title = "看课评论", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseWatchCommentPageParam fsCourseWatchCommentPageParam)
    {
        List<FsCourseWatchCommentListVO> list = fsCourseWatchCommentService.selectFsCourseWatchCommentList(fsCourseWatchCommentPageParam);
        ExcelUtil<FsCourseWatchCommentListVO> util = new ExcelUtil<FsCourseWatchCommentListVO>(FsCourseWatchCommentListVO.class);
        return util.exportExcel(list, "看课评论数据");
    }

    /**
     * 获取看课评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return AjaxResult.success(fsCourseWatchCommentService.selectFsCourseWatchCommentByCommentId(commentId));
    }

    /**
     * 新增看课评论
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:add')")
    @Log(title = "看课评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCourseWatchComment fsCourseWatchComment)
    {
        return toAjax(fsCourseWatchCommentService.insertFsCourseWatchComment(fsCourseWatchComment));
    }

    /**
     * 修改看课评论
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:edit')")
    @Log(title = "看课评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCourseWatchComment fsCourseWatchComment)
    {
        return toAjax(fsCourseWatchCommentService.updateFsCourseWatchComment(fsCourseWatchComment));
    }

    /**
     * 删除看课评论
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchComment:remove')")
    @Log(title = "看课评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(fsCourseWatchCommentService.deleteFsCourseWatchCommentByCommentIds(commentIds));
    }

    @Log(title = "手动拉黑外部联系人用户", businessType = BusinessType.UPDATE)
    @PutMapping("/addBlack")
    public R addBlack(Integer commentStatus, Long fsUserId)
    {
        int i = qwExternalContactService.updateQwExternalContactByFsUserId(commentStatus, fsUserId);
        if (i > 0){
            return R.ok();
        } else {
            return R.error();
        }
    }

}
