package org.dromara.personnel.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.dromara.common.core.exception.ServiceException;
import org.dromara.personnel.domain.bo.OpsSchedulingReq;
import org.dromara.personnel.domain.dto.SchedulingUserGroupDTO;
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.personnel.domain.vo.OpsSchedulingVo;
import org.dromara.personnel.domain.bo.OpsSchedulingBo;
import org.dromara.personnel.service.IOpsSchedulingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维-人员排班
 * 前端访问路由地址为:/personnel/scheduling
 *
 * @author LionLi
 * @date 2025-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/personnel/scheduling")
public class OpsSchedulingController extends BaseController {

    private final IOpsSchedulingService opsSchedulingService;

    /**
     * 查询运维-人员排班列表
     */
    @SaCheckPermission("personnel:scheduling:list")
    @GetMapping("/list")
    public TableDataInfo<OpsSchedulingVo> list(OpsSchedulingBo bo, PageQuery pageQuery) {
        return opsSchedulingService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询运维-人员排班列表
     */
    @SaCheckPermission("personnel:scheduling:list")
    @GetMapping("/getRiLiList")
    public R<List<SchedulingUserGroupDTO>> getRiLiList(OpsSchedulingReq req) {
        return R.ok(opsSchedulingService.getRiLiList(req));
    }

    /**
     * 导出运维-人员排班列表
     */
    @SaCheckPermission("personnel:scheduling:export")
    @Log(title = "运维-人员排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsSchedulingBo bo, HttpServletResponse response) {
        List<OpsSchedulingVo> list = opsSchedulingService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-人员排班", OpsSchedulingVo.class, response);
    }

    /**
     * 获取运维-人员排班详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("personnel:scheduling:query")
    @GetMapping("/{id}")
    public R<OpsSchedulingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(opsSchedulingService.queryById(id));
    }

    /**
     * 新增运维-人员排班
     */
    @SaCheckPermission("personnel:scheduling:add")
    @Log(title = "运维-人员排班", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/all")
    public R<Void> addAll(@Validated(AddGroup.class) @RequestBody OpsSchedulingBo bo) {
        return toAjax(opsSchedulingService.insertByBo(bo));
    }
    /**
     * 新增运维-人员排班
     */
    @SaCheckPermission("personnel:scheduling:add")
    @Log(title = "运维-人员排班", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OpsSchedulingBo bo) {
        return toAjax(opsSchedulingService.insertOneByBo(bo));
    }

    /**
     * 修改运维-人员排班
     */
    @SaCheckPermission("personnel:scheduling:edit")
    @Log(title = "运维-人员排班", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/all")
    public R<Void> allEdit(@RequestBody OpsSchedulingBo bo) {
        if (bo.getProjectId() == null) {
            throw new ServiceException("项目id不能为空");
        }
        if (bo.getSchedulingDate() == null) {
            throw new ServiceException("排班日期不能为空");
        }
        return toAjax(opsSchedulingService.allEdit(bo));
    }
    /**
     * 修改运维-人员排班
     */
    @SaCheckPermission("personnel:scheduling:edit")
    @Log(title = "运维-人员排班", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OpsSchedulingBo bo) {
        return toAjax(opsSchedulingService.updateByBo(bo));
    }

    /**
     * 删除运维-人员排班
     *
     * @param ids 主键串
     */
    @SaCheckPermission("personnel:scheduling:remove")
    @Log(title = "运维-人员排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(opsSchedulingService.deleteWithValidByIds(List.of(ids), true));
    }
}
