package org.eco.system.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eco.system.domain.bo.SysNoticeBo;
import org.eco.system.domain.vo.SysNoticeVo;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.service.DictService;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.eco.common.websocket.utils.WebSocketUtils;
import org.eco.system.service.ISysNoticeService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公告 信息操作处理
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    @Resource
    private ISysNoticeService noticeService;
    @Resource
    private DictService dictService;

    /**
     * 获取通知公告列表
     */
    @SaCheckPermission("system:notice:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysNoticeVo>> list(SysNoticeBo noticeBo) {
        return CommonResult.success(noticeService.selectPage(noticeBo));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @SaCheckPermission("system:notice:query")
    @GetMapping(value = "/{noticeId}")
    public CommonResult<SysNoticeVo> getInfo(@PathVariable Long noticeId) {
        return CommonResult.success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @SaCheckPermission("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SysNoticeBo noticeBo) {
        boolean inserted = noticeService.insertNotice(noticeBo);
        if (!inserted) {
            return CommonResult.fail("新增通知公告记录失败！");
        }
        String type = dictService.getDictLabel("sys_notice_type", noticeBo.getNoticeType());
        WebSocketUtils.publishAll("[" + type + "] " + noticeBo.getNoticeTitle());
        return CommonResult.success();
    }

    /**
     * 修改通知公告
     */
    @SaCheckPermission("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysNoticeBo noticeBo) {
        boolean updated = noticeService.updateNotice(noticeBo);
        if (!updated) {
            return CommonResult.fail("修改通知公告记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除通知公告
     */
    @SaCheckPermission("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public CommonResult<Void> remove(@PathVariable Long[] noticeIds) {
        boolean deleted = noticeService.deleteNoticeByIds(noticeIds);
        if (!deleted) {
            return CommonResult.fail("删除通知公告记录失败!");
        }
        return CommonResult.success();
    }
}
