package com.hotwj.platform.noticeManagerment.companyNotice.controller;

import com.hotwj.platform.noticeManagerment.companyNotice.domain.bo.HotCompanyNoticeBo;
import com.hotwj.platform.noticeManagerment.companyNotice.domain.vo.HotCompanyNoticeVo;
import com.hotwj.platform.noticeManagerment.companyNotice.service.IHotCompanyNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司公告
 *
 * @author shihongwei
 * @date 2026-01-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/noticeManagerment/companyNotice")
@Tag(name = "公司公告", description = "公司公告管理")
public class HotCompanyNoticeController extends BaseController {

    private final IHotCompanyNoticeService hotCompanyNoticeService;

    /**
     * 查询公司公告列表
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询公司公告列表")
    public TableDataInfo<HotCompanyNoticeVo> list(HotCompanyNoticeBo bo, PageQuery pageQuery) {
        return hotCompanyNoticeService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/latestUnread")
    @Operation(summary = "查询最新一条未读公司公告")
    public R<HotCompanyNoticeVo> latestUnread() {
        return R.ok(hotCompanyNoticeService.queryLatestUnread());
    }

    /**
     * 导出公司公告列表
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:export")
    @Log(title = "公司公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出公司公告列表")
    public void export(HotCompanyNoticeBo bo, HttpServletResponse response) {
        List<HotCompanyNoticeVo> list = hotCompanyNoticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "公司公告", HotCompanyNoticeVo.class, response);
    }

    /**
     * 获取公司公告详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取公司公告详细信息")
    public R<HotCompanyNoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                         @Parameter(name = "id", description = "主键", required = true, example = "1")
                                         @PathVariable Long id) {
        return R.ok(hotCompanyNoticeService.queryById(id));
    }

    /**
     * 新增公司公告
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:add")
    @Log(title = "公司公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增公司公告")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotCompanyNoticeBo bo) {
        return toAjax(hotCompanyNoticeService.insertByBo(bo));
    }

    /**
     * 修改公司公告
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:edit")
    @Log(title = "公司公告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改公司公告")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotCompanyNoticeBo bo) {
        return toAjax(hotCompanyNoticeService.updateByBo(bo));
    }

    /**
     * 删除公司公告
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("noticeManagerment:companyNotice:remove")
    @Log(title = "公司公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除公司公告")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotCompanyNoticeService.deleteWithValidByIds(List.of(ids), true));
    }

    @Log(title = "公司公告", businessType = BusinessType.UPDATE)
    @PutMapping("/read/{id}")
    @Operation(summary = "标记公司公告为已读")
    public R<Void> markRead(@NotNull(message = "主键不能为空")
                            @Parameter(name = "id", description = "主键", required = true, example = "1")
                            @PathVariable Long id) {
        return toAjax(hotCompanyNoticeService.markRead(id));
    }
}
