package org.dromara.controller.communityNews;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.communityNews.CommentLikes;
import org.dromara.domain.communityNews.bo.CommentLikesBo;
import org.dromara.domain.communityNews.vo.CommentLikesVo;
import org.dromara.factory.AsyncFactory;
import org.dromara.manager.AsyncManager;
import org.dromara.service.ICommentLikesService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区资讯点赞
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/commentLikes")
public class CommentLikesController extends BaseController {

    private final ICommentLikesService commentLikesService;

    /**
     * 查询社区资讯点赞列表
     */
    @SaCheckPermission("wuYe:commentLikes:list")
    @GetMapping("/list")
    public TableDataInfo<CommentLikesVo> list(CommentLikesBo bo, PageQuery pageQuery) {
        return commentLikesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出社区资讯点赞列表
     */
    @SaCheckPermission("wuYe:commentLikes:export")
    @Log(title = "社区资讯点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommentLikesBo bo, HttpServletResponse response) {
        List<CommentLikesVo> list = commentLikesService.queryList(bo);
        ExcelUtil.exportExcel(list, "社区资讯点赞", CommentLikesVo.class, response);
    }

    /**
     * 获取社区资讯点赞详细信息
     *
     * @param likesId 主键
     */
    @SaCheckPermission("wuYe:commentLikes:query")
    @GetMapping("/{likesId}")
    public R<CommentLikesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long likesId) {
        return R.ok(commentLikesService.queryById(likesId));
    }

    /**
     * 新增社区资讯点赞
     */
    @SaCheckPermission("wuYe:commentLikes:add")
    @Log(title = "社区资讯点赞", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommentLikesBo bo) {
        return toAjax(commentLikesService.insertByBo(bo));
    }

    /**
     * 修改社区资讯点赞
     */
    @SaCheckPermission("wuYe:commentLikes:edit")
    @Log(title = "社区资讯点赞", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommentLikesBo bo) {
        return toAjax(commentLikesService.updateByBo(bo));
    }

    /**
     * 删除社区资讯点赞
     *
     * @param likesIds 主键串
     */
    @SaCheckPermission("wuYe:commentLikes:remove")
    @Log(title = "社区资讯点赞", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{likesIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] likesIds) {
        return toAjax(commentLikesService.deleteWithValidByIds(List.of(likesIds), true));
    }

    /**
     * 进行点赞操作
     */
    @SaIgnore
    @PostMapping("/giveTheThumbs")
    public R<Void> giveTheThumbs(@RequestBody CommentLikes commentLikes) {
        AsyncManager.me().execute(AsyncFactory.giveTheThumbs(commentLikes));
        System.out.println("点赞返回");
        return R.ok();
    }
}
