package org.dromara.controller.communityNews;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.CommentStars;
import org.dromara.domain.communityNews.bo.CommentStarsBo;
import org.dromara.domain.communityNews.vo.CommentStarsVo;
import org.dromara.factory.AsyncFactory;
import org.dromara.manager.AsyncManager;
import org.dromara.service.ICommentStarsService;
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/commentStars")
public class CommentStarsController extends BaseController {

    private final ICommentStarsService commentStarsService;

    /**
     * 查询社区资讯收藏列表
     */
    @SaCheckPermission("wuYe:commentStars:list")
    @GetMapping("/list")
    public TableDataInfo<CommentStarsVo> list(CommentStarsBo bo, PageQuery pageQuery) {
        return commentStarsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出社区资讯收藏列表
     */
    @SaCheckPermission("wuYe:commentStars:export")
    @Log(title = "社区资讯收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommentStarsBo bo, HttpServletResponse response) {
        List<CommentStarsVo> list = commentStarsService.queryList(bo);
        ExcelUtil.exportExcel(list, "社区资讯收藏", CommentStarsVo.class, response);
    }

    /**
     * 获取社区资讯收藏详细信息
     *
     * @param starsId 主键
     */
    @SaCheckPermission("wuYe:commentStars:query")
    @GetMapping("/{starsId}")
    public R<CommentStarsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long starsId) {
        return R.ok(commentStarsService.queryById(starsId));
    }

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

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

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

    /**
     * 社区资讯收藏或取消收藏
     */
    @Log(title = "社区资讯收藏", businessType = BusinessType.INSERT)
    @PostMapping("/getStars")
    public R<Void> getStars(@RequestBody CommentStars commentStars) {
        AsyncManager.me().execute(AsyncFactory.getStars(commentStars));
        return R.ok();
    }

    /**
     * 党建收藏或取消收藏
     */
    @PostMapping("/getPartyNewsStars")
    public R<Void> getPartyNewsStars(@RequestBody CommentStars commentStars) {
        AsyncManager.me().execute(AsyncFactory.getPartyNewsStars(commentStars));
        return R.ok();
    }
}
