package cn.iocoder.yudao.module.iot.controller.admin.devicecontactmodel;

import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.devicecontactmodel.vo.*;
import cn.iocoder.yudao.module.iot.service.devicecontactmodel.DeviceContactModelService;

@Tag(name = "管理后台 - 采集设备-模型关联")
@RestController
@RequestMapping("/iot/device-contact-model")
@Validated
public class DeviceContactModelController {

    @Resource
    private DeviceContactModelService deviceContactModelService;

    @PostMapping("/create")
    @Operation(summary = "创建采集设备模型-点位管理")
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:create')")
    public CommonResult<Long> createDeviceContactModel(@Valid @RequestBody DeviceContactModelSaveReqVO createReqVO) {
        return success(deviceContactModelService.createDeviceContactModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采集设备模型-点位管理")
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:update')")
    public CommonResult<Boolean> updateDeviceContactModel(@Valid @RequestBody DeviceContactModelSaveReqVO updateReqVO) {
        deviceContactModelService.updateDeviceContactModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采集设备模型-点位管理")
    @Parameter(name = "ids", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:delete')")
    public CommonResult<Boolean> deleteDeviceContactModel(@RequestParam("ids") String ids) {

        // 将逗号分隔的字符串转换为Long类型的List
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());

        deviceContactModelService.deleteDeviceContactModel(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采集设备模型-点位管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:query')")
    public CommonResult<DeviceContactModelRespVO> getDeviceContactModel(@RequestParam("id") Long id) {
        DeviceContactModelDO deviceContactModel = deviceContactModelService.getDeviceContactModel(id);
        return success(BeanUtils.toBean(deviceContactModel, DeviceContactModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采集设备模型-点位管理分页")
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:query')")
    public CommonResult<PageResult<DeviceContactModelRespVO>> getDeviceContactModelPage(@Valid DeviceContactModelPageReqVO pageReqVO) {
        PageResult<DeviceContactModelDO> pageResult = deviceContactModelService.getDeviceContactModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceContactModelRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得采集设备模型-点位管理 列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-model-attribute:query')")
    public CommonResult<List<DeviceContactModelDO>> getDeviceContactModelList(@RequestParam("id") Long id) {
        List<DeviceContactModelDO> deviceModelAttribute = deviceContactModelService.getDeviceContactModelList(id);
        return success(deviceModelAttribute);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出采集设备模型-点位管理 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-contact-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceContactModelExcel(@Valid DeviceContactModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceContactModelDO> list = deviceContactModelService.getDeviceContactModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采集设备模型-点位管理.xls", "数据", DeviceContactModelRespVO.class,
                        BeanUtils.toBean(list, DeviceContactModelRespVO.class));
    }

}