package com.fuyuanshen.web.controller.device;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.bo.DeviceGroupBo;
import com.fuyuanshen.equipment.domain.vo.DeviceGroupVo;
import com.fuyuanshen.equipment.service.IDeviceGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备分组
 *
 * @author Lion Li
 * @date 2025-08-08
 */
@Tag(name = "web:设备分组", description = "web:设备分组")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/device/group")
public class DeviceGroupController extends BaseController {

    private final IDeviceGroupService deviceGroupService;


    /**
     * 查询设备分组列表
     */
    @Operation(summary = "查询设备分组列表")
    @SaCheckPermission("fys-equipment:group:list")
    @GetMapping("/list")
    public R<List<DeviceGroupVo>> list(DeviceGroupBo bo) {
        List<DeviceGroupVo> list = deviceGroupService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 查询设备分组列表(分页)
     */
    @Operation(summary = "查询设备分组列表(分页)")
    @SaCheckPermission("fys-equipment:group:list")
    @GetMapping("/listPage")
    public TableDataInfo<DeviceGroupVo> listPage(DeviceGroupBo bo) {
        List<DeviceGroupVo> list = deviceGroupService.queryList(bo);
        // return R.ok(list);
        return null;
    }

    // @GetMapping("/list")
    // public TableDataInfo<DeviceAlarmVo> list(DeviceAlarmBo bo, PageQuery pageQuery) {
    //     return deviceAlarmService.queryPageList(bo, pageQuery);
    // }


    /**
     * 导出设备分组列表
     */
    @SaCheckPermission("fys-equipment:group:export")
    @Log(title = "设备分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceGroupBo bo, HttpServletResponse response) {
        List<DeviceGroupVo> list = deviceGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备分组", DeviceGroupVo.class, response);
    }


    /**
     * 获取设备分组详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取设备分组详细信息")
    @SaCheckPermission("fys-equipment:group:query")
    @GetMapping("/{id}")
    public R<DeviceGroupVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(deviceGroupService.queryById(id));
    }


    /**
     * 新增设备分组
     */
    @Operation(summary = "新增设备分组")
    @SaCheckPermission("fys-equipment:group:add")
    @Log(title = "设备分组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceGroupBo bo) {
        return toAjax(deviceGroupService.insertByBo(bo));
    }


    /**
     * 修改设备分组
     */
    @Operation(summary = "修改设备分组")
    @SaCheckPermission("fys-equipment:group:edit")
    @Log(title = "设备分组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceGroupBo bo) {
        return toAjax(deviceGroupService.updateByBo(bo));
    }

    /**
     * 删除设备分组
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除设备分组")
    @SaCheckPermission("fys-equipment:group:remove")
    @Log(title = "设备分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(deviceGroupService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 绑定设备分组
     *
     * @param groupId  分组id
     * @param deviceId 设备id
     */
    @Operation(summary = "绑定设备分组")
    // @SaCheckPermission("fys-equipment:group:remove")
    @Log(title = "绑定设备分组", businessType = BusinessType.DELETE)
    @GetMapping("/groupId/{groupId}/{deviceId}")
    public R<Void> bindingDevice(@NotNull(message = "分组id 不能为空") @PathVariable Long groupId,
                                 @NotNull(message = "设备id 不能为空") @PathVariable Long[] deviceId) {
        return toAjax(deviceGroupService.bindingDevice(groupId, deviceId));
    }


    /**
     * 解绑设备分组
     *
     * @param deviceId 设备id
     */
    @Operation(summary = "解绑设备分组")
    // @SaCheckPermission("fys-equipment:group:remove")
    @Log(title = "解绑设备分组", businessType = BusinessType.DELETE)
    @GetMapping("/groupUnbind/{deviceId}")
    public R<Void> groupUnbind(@NotNull(message = "设备id 不能为空") @PathVariable Long[] deviceId) {
        return toAjax(deviceGroupService.groupUnbind(deviceId));
    }


}
