package org.dromara.work.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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.work.domain.vo.TpNewsVo;
import org.dromara.work.domain.bo.TpNewsBo;
import org.dromara.work.service.ITpNewsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 新闻活动
 *
 * @author Maosw
 * @date 2025-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/news")
public class TpNewsController extends BaseController {

    private final ITpNewsService tpNewsService;

    /**
     * 查询新闻活动列表
     */
    @SaCheckPermission("work:news:list")
    @GetMapping("/list")
    public TableDataInfo<TpNewsVo> list(TpNewsBo bo, PageQuery pageQuery) {
        return tpNewsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出新闻活动列表
     */
    @SaCheckPermission("work:news:export")
    @Log(title = "新闻活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpNewsBo bo, HttpServletResponse response) {
        List<TpNewsVo> list = tpNewsService.queryList(bo);
        ExcelUtil.exportExcel(list, "新闻活动", TpNewsVo.class, response);
    }

    /**
     * 获取新闻活动详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:news:query")
    @GetMapping("/{id}")
    public R<TpNewsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpNewsService.queryById(id));
    }

    /**
     * 新增新闻活动
     */
    @SaCheckPermission("work:news:add")
    @Log(title = "新闻活动", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpNewsBo bo) {
        return toAjax(tpNewsService.insertByBo(bo));
    }

    /**
     * 修改新闻活动
     */
    @SaCheckPermission("work:news:edit")
    @Log(title = "新闻活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpNewsBo bo) {
        return toAjax(tpNewsService.updateByBo(bo));
    }

    /**
     * 删除新闻活动
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:news:remove")
    @Log(title = "新闻活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpNewsService.deleteWithValidByIds(List.of(ids), true));
    }
}
