package org.dromara.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.domain.SenMonitorPoint;
import org.dromara.domain.vo.MonitorTreeVo;
import org.dromara.system.domain.SysArea;
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.SenMonitorPointVo;
import org.dromara.domain.bo.SenMonitorPointBo;
import org.dromara.service.ISenMonitorPointService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 监控点位信息
 *
 * @author luogang
 * @date 2024-09-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/monitorPoint")
public class SenMonitorPointController extends BaseController {

    private final ISenMonitorPointService senMonitorPointService;

    /**
     * 查询监控点位信息列表
     */
    @SaCheckPermission("manage:monitorPoint:list")
    @GetMapping("/list")
    public TableDataInfo<SenMonitorPointVo> list(SenMonitorPointBo bo, PageQuery pageQuery) {
        return senMonitorPointService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询区域树下面的监控点
     */
    @SaCheckPermission("manage:monitorPoint:list")
    @GetMapping("/getListByAreaTree")
    public R<List<SenMonitorPoint>> getListByAreaTree(SenMonitorPointBo bo) {
        return R.ok(senMonitorPointService.getListByAreaId(bo));
    }
    /**
     * 导出监控点位信息列表
     */
    @SaCheckPermission("manage:monitorPoint:export")
    @Log(title = "监控点位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenMonitorPointBo bo, HttpServletResponse response) {
        List<SenMonitorPointVo> list = senMonitorPointService.queryList(bo);
        ExcelUtil.exportExcel(list, "监控点位信息", SenMonitorPointVo.class, response);
    }

    /**
     * 获取监控点位信息详细信息
     *
     * @param pointId 主键
     */
    @SaCheckPermission("manage:monitorPoint:query")
    @GetMapping("/{pointId}")
    public R<SenMonitorPointVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long pointId) {
        return R.ok(senMonitorPointService.queryById(pointId));
    }

    /**
     * 新增监控点位信息
     */
    @SaCheckPermission("manage:monitorPoint:add")
    @Log(title = "监控点位信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenMonitorPointBo bo) {
        return toAjax(senMonitorPointService.insertByBo(bo));
    }

    /**
     * 修改监控点位信息
     */
    @SaCheckPermission("manage:monitorPoint:edit")
    @Log(title = "监控点位信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenMonitorPointBo bo) {
        return toAjax(senMonitorPointService.updateByBo(bo));
    }

    /**
     * 删除监控点位信息
     *
     * @param pointIds 主键串
     */
    @SaCheckPermission("manage:monitorPoint:remove")
    @Log(title = "监控点位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pointIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] pointIds) {
        return toAjax(senMonitorPointService.deleteWithValidByIds(List.of(pointIds), true));
    }

    /**
     * 查询监控点树
     */
    @SaCheckPermission("manage:monitorPoint:list")
    @GetMapping("/getPointsTree")
    public R<MonitorTreeVo> getPointsTree(SenMonitorPointBo bo) {
        return R.ok(senMonitorPointService.selectAreaTreeList(bo));
    }
}
