package org.dromara.app.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.app.domain.vo.AppSchedulingDeviceVo;
import org.dromara.app.domain.bo.AppSchedulingDeviceBo;
import org.dromara.app.service.IAppSchedulingDeviceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 排程与设备关联
 *
 * @author Lion Li
 * @date 2026-04-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/schedulingDevice")
public class AppSchedulingDeviceController extends BaseController {

    private final IAppSchedulingDeviceService appSchedulingDeviceService;

    /**
     * 查询排程与设备关联列表
     */
    @SaCheckPermission("app:schedulingDevice:list")
    @GetMapping("/list")
    public TableDataInfo<AppSchedulingDeviceVo> list(AppSchedulingDeviceBo bo, PageQuery pageQuery) {
        return appSchedulingDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出排程与设备关联列表
     */
    @SaCheckPermission("app:schedulingDevice:export")
    @Log(title = "排程与设备关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppSchedulingDeviceBo bo, HttpServletResponse response) {
        List<AppSchedulingDeviceVo> list = appSchedulingDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "排程与设备关联", AppSchedulingDeviceVo.class, response);
    }

    /**
     * 获取排程与设备关联详细信息
     *
     * @param schedulingId 主键
     */
    @SaCheckPermission("app:schedulingDevice:query")
    @GetMapping("/{schedulingId}")
    public R<AppSchedulingDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long schedulingId) {
        return R.ok(appSchedulingDeviceService.queryById(schedulingId));
    }

    /**
     * 新增排程与设备关联
     */
    @SaCheckPermission("app:schedulingDevice:add")
    @Log(title = "排程与设备关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppSchedulingDeviceBo bo) {
        return toAjax(appSchedulingDeviceService.insertByBo(bo));
    }

    /**
     * 修改排程与设备关联
     */
    @SaCheckPermission("app:schedulingDevice:edit")
    @Log(title = "排程与设备关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppSchedulingDeviceBo bo) {
        return toAjax(appSchedulingDeviceService.updateByBo(bo));
    }

    /**
     * 删除排程与设备关联
     *
     * @param schedulingIds 主键串
     */
    @SaCheckPermission("app:schedulingDevice:remove")
    @Log(title = "排程与设备关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schedulingIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] schedulingIds) {
        return toAjax(appSchedulingDeviceService.deleteWithValidByIds(List.of(schedulingIds), true));
    }
}
