package org.dromara.system.controller.notice;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.notice.XiaoyuanNotice;
import org.dromara.system.domain.notice.bo.XiaoyuanNoticeBo;
import org.dromara.system.domain.notice.vo.XiaoyuanNoticeVo;
import org.dromara.system.service.notice.IXiaoyuanNoticeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 校园新闻
 *
 * @author boman
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xiaoYuan/notice")
public class XiaoyuanNoticeController extends BaseController {

    private final IXiaoyuanNoticeService xiaoyuanNoticeService;

    /**
     * 查询校园新闻列表
     */
    @SaCheckPermission("notice:notice:list")
    @GetMapping("/list")
    public TableDataInfo<XiaoyuanNoticeVo> list(XiaoyuanNoticeBo bo, PageQuery pageQuery) {
        return xiaoyuanNoticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 我的校园新闻列表
     */
    @GetMapping("/myList")
    public TableDataInfo<XiaoyuanNoticeVo> myList(XiaoyuanNoticeBo bo, PageQuery pageQuery)
    {
        List<XiaoyuanNoticeVo> list = xiaoyuanNoticeService.selectXiaoyuanNoticeMyList(bo, pageQuery);
        return TableDataInfo.build(list);
    }

    /**
     * 导出校园新闻列表
     */
    @SaCheckPermission("notice:notice:export")
    @Log(title = "校园新闻", businessType = BusinessType.EXPORT)
   // @PostMapping("/export")
    public void export(XiaoyuanNoticeBo bo, HttpServletResponse response) {
        List<XiaoyuanNoticeVo> list = xiaoyuanNoticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "校园新闻", XiaoyuanNoticeVo.class, response);
    }

    /**
     * 获取校园新闻详细信息
     *
     * @param noticeId 主键
     */
    @SaCheckPermission("notice:notice:query")
    @GetMapping("/{noticeId}")
    public R<XiaoyuanNoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long noticeId) {
        return R.ok(xiaoyuanNoticeService.queryById(noticeId));
    }

    /**
     * 新增校园新闻
     */
    @SaCheckPermission("notice:notice:add")
    @Log(title = "校园新闻", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XiaoyuanNoticeBo bo) {
        return toAjax(xiaoyuanNoticeService.insertByBo(bo));
    }

    /**
     * 修改校园新闻
     */
    @SaCheckPermission("notice:notice:edit")
    @Log(title = "校园新闻", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XiaoyuanNoticeBo bo) {
        return toAjax(xiaoyuanNoticeService.updateByBo(bo));
    }

    /**
     * 删除校园新闻
     *
     * @param noticeIds 主键串
     */
    @SaCheckPermission("notice:notice:remove")
    @Log(title = "校园新闻", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{noticeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] noticeIds) {
        return toAjax(xiaoyuanNoticeService.deleteWithValidByIds(List.of(noticeIds), true));
    }

    /**
     * 我的收藏
     */
    @GetMapping("/collect")
    public TableDataInfo<XiaoyuanNoticeVo> myCollect(XiaoyuanNoticeBo bo, PageQuery pageQuery)
    {

        return xiaoyuanNoticeService.myCollect(bo,pageQuery);
    }
}
