package org.dromara.project.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.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.project.domain.dto.leave.BusLeaveManagerReviewReq;
import org.dromara.project.domain.dto.leave.BusLeaveQueryReq;
import org.dromara.project.domain.vo.leave.BusLeaveVo;
import org.dromara.project.service.IBusLeaveService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工人员请假申请
 *
 * @author lcj
 * @date 2025-04-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/leave")
public class BusLeaveController extends BaseController {

    private final IBusLeaveService busLeaveService;

    /**
     * 查询施工人员请假申请列表
     */
    @SaCheckPermission("project:leave:list")
    @GetMapping("/list")
    public TableDataInfo<BusLeaveVo> list(BusLeaveQueryReq req, PageQuery pageQuery) {
        return busLeaveService.queryPageList(req, pageQuery);
    }

    /**
     * 导出施工人员请假申请列表
     */
    @SaCheckPermission("project:leave:export")
    @Log(title = "施工人员请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusLeaveQueryReq req, HttpServletResponse response) {
        List<BusLeaveVo> list = busLeaveService.queryList(req);
        ExcelUtil.exportExcel(list, "施工人员请假申请", BusLeaveVo.class, response);
    }

    /**
     * 获取施工人员请假申请详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:leave:query")
    @GetMapping("/{id}")
    public R<BusLeaveVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(busLeaveService.queryById(id));
    }

    /**
     * 管理员审核施工人员请假申请
     */
    @SaCheckPermission("project:leave:edit")
    @Log(title = "施工人员请假申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/review/manager")
    public R<Void> managerReview(@Validated @RequestBody BusLeaveManagerReviewReq req) {
        return toAjax(busLeaveService.managerReview(req));
    }

    /**
     * 删除施工人员请假申请
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:leave:remove")
    @Log(title = "施工人员请假申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busLeaveService.deleteWithValidByIds(List.of(ids), true));
    }
}
