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.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.communityNews.bo.CommunityNewsBo;
import org.dromara.domain.communityNews.vo.CommentInteractionVo;
import org.dromara.domain.communityNews.vo.CommunityNewsVo;
import org.dromara.service.ICommunityNewsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static org.dromara.common.core.constant.Constants.SHARE_COMMUNITY;

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

    private final ICommunityNewsService communityNewsService;

    /**
     * 查询社区资讯列表
     */
    @SaCheckPermission("wuYe:news:list")
    @GetMapping("/list")
    public TableDataInfo<CommunityNewsVo> list(CommunityNewsBo bo, PageQuery pageQuery) {
        return communityNewsService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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

    /**
     * 获取未读互动数量
     */
    @SaCheckPermission("wuYe:news:getCommentInteractionCount")
    @PostMapping("/getCommentInteractionCount")
    public R<Integer> getCommentInteractionCount(@RequestBody CommentInteractionVo commentInteractionVo) {
        return communityNewsService.getCommentInteractionCount(commentInteractionVo);
    }

    /**
     * 获取未读互动列表
     */
    @SaCheckPermission("wuYe:news:getCommentInteraction")
    @PostMapping("/getCommentInteraction")
    public R<Map<String, Object>> getCommentInteraction(@RequestBody CommentInteractionVo commentInteractionVo) {
        return communityNewsService.getCommentInteraction(commentInteractionVo);
    }

    /**
     * 分享
     * @param bo
     * @return
     */
    @PostMapping("/setShare")
    public R<Void> setShare(@RequestBody CommunityNewsBo bo) {
        Long communityId = bo.getCommunityId();
        String key = SHARE_COMMUNITY + communityId;
        if (RedisUtils.hasKey(key)) {
            long atomicValue = RedisUtils.getAtomicValue(key);
            RedisUtils.setAtomicValue(SHARE_COMMUNITY, atomicValue + 1);
        } else {
            RedisUtils.setAtomicValue(SHARE_COMMUNITY, 1);
        }
        return R.ok();
    }
}
