package org.dromara.safety.controller;

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.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.web.core.BaseController;
import org.dromara.safety.domain.req.teammeeting.TeamMeetingCreateReq;
import org.dromara.safety.domain.req.teammeeting.TeamMeetingQueryReq;
import org.dromara.safety.domain.req.teammeeting.TeamMeetingUpdateReq;
import org.dromara.safety.domain.vo.BusTeamMeetingVo;
import org.dromara.safety.service.IBusTeamMeetingService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 站班会
 *
 * @author lcj
 * @date 2025-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/safety/teamMeeting")
public class BusTeamMeetingController extends BaseController {

    private final IBusTeamMeetingService busTeamMeetingService;

    /**
     * 查询站班会列表
     */
    @SaCheckPermission("safety:teamMeeting:list")
    @GetMapping("/list")
    public TableDataInfo<BusTeamMeetingVo> list(TeamMeetingQueryReq req, PageQuery pageQuery) {
        return busTeamMeetingService.queryPageList(req, pageQuery);
    }

    /**
     * 导出站班会列表
     */
    @SaCheckPermission("safety:teamMeeting:export")
    @Log(title = "站班会", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TeamMeetingQueryReq req, HttpServletResponse response) {
        List<BusTeamMeetingVo> list = busTeamMeetingService.queryList(req);
        ExcelUtil.exportExcel(list, "站班会", BusTeamMeetingVo.class, response);
    }

    /**
     * 获取站班会详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("safety:teamMeeting:query")
    @GetMapping("/{id}")
    public R<BusTeamMeetingVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(busTeamMeetingService.queryById(id));
    }

    /**
     * 新增站班会
     */
    @SaCheckPermission("safety:teamMeeting:add")
    @Log(title = "站班会", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody TeamMeetingCreateReq req) {
        return R.ok(busTeamMeetingService.insertByBo(req));
    }

    /**
     * 修改站班会
     */
    @SaCheckPermission("safety:teamMeeting:edit")
    @Log(title = "站班会", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TeamMeetingUpdateReq req) {
        return toAjax(busTeamMeetingService.updateByBo(req));
    }

    /**
     * 删除站班会
     *
     * @param ids 主键串
     */
    @SaCheckPermission("safety:teamMeeting:remove")
    @Log(title = "站班会", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busTeamMeetingService.deleteWithValidByIds(List.of(ids), true));
    }
}
