package org.dromara.system.controller.project;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.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.dromara.system.domain.dto.projectnews.BusProjectNewsCreateReq;
import org.dromara.system.domain.dto.projectnews.BusProjectNewsGisReq;
import org.dromara.system.domain.dto.projectnews.BusProjectNewsQueryReq;
import org.dromara.system.domain.dto.projectnews.BusProjectNewsUpdateReq;
import org.dromara.system.domain.vo.projectnews.BusProjectNewsGisVo;
import org.dromara.system.domain.vo.projectnews.BusProjectNewsVo;
import org.dromara.system.service.IBusProjectNewsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目新闻
 *
 * @author lilemy
 * @date 2025-04-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/projectNews")
public class BusProjectNewsController extends BaseController {

    private final IBusProjectNewsService busProjectNewsService;

    /**
     * 查询项目新闻列表
     */
    @SaCheckPermission("project:projectNews:list")
    @GetMapping("/list")
    public TableDataInfo<BusProjectNewsVo> list(BusProjectNewsQueryReq req, PageQuery pageQuery) {
        return busProjectNewsService.queryPageList(req, pageQuery);
    }

    /**
     * 导出项目新闻列表
     */
    @SaCheckPermission("project:projectNews:export")
    @Log(title = "项目新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusProjectNewsQueryReq req, HttpServletResponse response) {
        List<BusProjectNewsVo> list = busProjectNewsService.queryList(req);
        ExcelUtil.exportExcel(list, "项目新闻", BusProjectNewsVo.class, response);
    }

    /**
     * 查询大屏项目新闻列表
     */
    @GetMapping("/list/gis")
    public R<List<BusProjectNewsGisVo>> listGis(BusProjectNewsGisReq req) {
        return R.ok(busProjectNewsService.queryGisList(req));
    }

    /**
     * 获取项目新闻详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:projectNews:query")
    @GetMapping("/{id}")
    public R<BusProjectNewsVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(busProjectNewsService.queryById(id));
    }

    /**
     * 新增项目新闻
     */
    @SaCheckPermission("project:projectNews:add")
    @Log(title = "项目新闻", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated @RequestBody BusProjectNewsCreateReq req) {
        return R.ok(busProjectNewsService.insertByBo(req));
    }

    /**
     * 修改项目新闻
     */
    @SaCheckPermission("project:projectNews:edit")
    @Log(title = "项目新闻", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody BusProjectNewsUpdateReq req) {
        return toAjax(busProjectNewsService.updateByBo(req));
    }

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