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

import cn.dev33.satoken.annotation.SaIgnore;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationBo;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationGroupBo;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.vo.HotSystemNotificationVo;
import com.hotwj.platform.noticeManagerment.systemNotification.service.IHotSystemNotificationService;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
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.domain.model.LoginUser;
import org.dromara.common.core.utils.StringUtils;
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.satoken.utils.LoginHelper;
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("/noticeSignDocument/systemNotification")
@Tag(name = "系统通知", description = "系统通知管理")
public class HotSystemNotificationController extends BaseController {

    private final IHotSystemNotificationService hotSystemNotificationService;

    /**
     * 查询系统通知列表
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询系统通知列表")
    public TableDataInfo<HotSystemNotificationVo> list(HotSystemNotificationBo bo, PageQuery pageQuery) {
        return hotSystemNotificationService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询系统通知列表
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:list")
    @GetMapping("/listByType")
    @Operation(summary = "分页查询系统通知列表")
    public TableDataInfo<HotSystemNotificationVo> listByType(HotSystemNotificationBo bo, PageQuery pageQuery) {
        return hotSystemNotificationService.queryPageListByType(bo, pageQuery);
    }

    /**
     * 导出系统通知列表
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:export")
    @Log(title = "系统通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出系统通知列表")
    public void export(HotSystemNotificationBo bo, HttpServletResponse response) {
        List<HotSystemNotificationVo> list = hotSystemNotificationService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统通知", HotSystemNotificationVo.class, response);
    }

    /**
     * 获取系统通知详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取系统通知详细信息")
    public R<HotSystemNotificationVo> getInfo(@NotNull(message = "主键不能为空")
                                              @Parameter(name = "id", description = "主键", required = true, example = "1")
                                              @PathVariable Long id) {
        return R.ok(hotSystemNotificationService.queryById(id));
    }

    /**
     * 新增系统通知
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:add")
    @Log(title = "系统通知", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增系统通知")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotSystemNotificationGroupBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        assert loginUser != null;
        //手动发放消息，只有企业端人员和政府端人员可以发放
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.ENTERPRISE_PORT)) {
            bo.setSenderType("COMPANY");
            return toAjax(hotSystemNotificationService.insertByBo(bo));
        }
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.GOVERNMENT_PORT)) {
            bo.setSenderType("GOVERNMENT");
            return toAjax(hotSystemNotificationService.insertByBo(bo));
        }
        return toAjax(false);
    }

    /**
     * 修改系统通知
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification:edit")
    @Log(title = "系统通知", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改系统通知")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotSystemNotificationBo bo) {
        return toAjax(hotSystemNotificationService.updateByBo(bo));
    }

    /**
     * 删除系统通知
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("noticeSignDocument:systemNotification: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(hotSystemNotificationService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaIgnore
    @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(hotSystemNotificationService.markRead(id));
    }
}
