package org.dromara.controller;

import java.util.List;
import java.util.Map;

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.domain.vo.SenMonitorTaskVo;
import org.dromara.domain.bo.SenMonitorTaskBo;
import org.dromara.service.ISenMonitorTaskService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 任务管理
 *
 * @author luogang
 * @date 2024-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/monitorTask")
public class SenMonitorTaskController extends BaseController {

    private final ISenMonitorTaskService senMonitorTaskService;

    /**
     * 查询任务管理列表
     */
    @SaCheckPermission("manage:monitorTask:list")
    @GetMapping("/list")
    public TableDataInfo<SenMonitorTaskVo> list(SenMonitorTaskBo bo, PageQuery pageQuery) {
        return senMonitorTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务管理列表
     */
    @SaCheckPermission("manage:monitorTask:export")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenMonitorTaskBo bo, HttpServletResponse response) {
        List<SenMonitorTaskVo> list = senMonitorTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务管理", SenMonitorTaskVo.class, response);
    }

    /**
     * 获取任务管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("manage:monitorTask:query")
    @GetMapping("/{id}")
    public R<SenMonitorTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(senMonitorTaskService.queryById(id));
    }

    /**
     * 新增任务管理
     */
    @SaCheckPermission("manage:monitorTask:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenMonitorTaskBo bo) {
        return toAjax(senMonitorTaskService.insertByBo(bo));
    }

    /**
     * 修改任务管理
     */
    @SaCheckPermission("manage:monitorTask:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenMonitorTaskBo bo) {
        return toAjax(senMonitorTaskService.updateByBo(bo));
    }

    /**
     * 删除任务管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manage:monitorTask:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(senMonitorTaskService.deleteWithValidByIds(List.of(ids), true));
    }
    /**
     * 根据监控点id获取任务数据
     *
     * @param id 监控点ID
     */
    @SaCheckPermission("manage:monitorTask:query")
    @GetMapping("/getTaskInfoByPointId/{id}")
    public R<Map<String,Object>> getTaskInfoByPointId(@PathVariable Long id) {
        return R.ok(senMonitorTaskService.getTaskInfoByPointId(id));
    }
}
