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

import cn.iocoder.yudao.module.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
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.constraints.*;
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.devicemodel.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodel.DeviceModelDO;
import cn.iocoder.yudao.module.iot.service.devicemodel.DeviceModelService;

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

    @Resource
    private DeviceModelService deviceModelService;

    @PostMapping("/create")
    @Operation(summary = "创建采集设备模型")
    @PreAuthorize("@ss.hasPermission('iot:device-model:create')")
    public CommonResult<Long> createDeviceModel(@Valid @RequestBody DeviceModelSaveReqVO createReqVO) {
        return success(deviceModelService.createDeviceModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采集设备模型")
    @PreAuthorize("@ss.hasPermission('iot:device-model:update')")
    public CommonResult<Boolean> updateDeviceModel(@Valid @RequestBody DeviceModelSaveReqVO updateReqVO) {
        deviceModelService.updateDeviceModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采集设备模型")
    @Parameter(name = "ids", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-model:delete')")
    public CommonResult<Boolean> deleteDeviceModel(@RequestParam("ids") String ids) {
        // 将逗号分隔的字符串转换为Long类型的List
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());
        deviceModelService.deleteDeviceModel(idList);

        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采集设备模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-model:query')")
    public CommonResult<DeviceModelRespVO> getDeviceModel(@RequestParam("id") Long id) {
        DeviceModelDO deviceModel = deviceModelService.getDeviceModel(id);
        return success(BeanUtils.toBean(deviceModel, DeviceModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采集设备模型分页")
    @PreAuthorize("@ss.hasPermission('iot:device-model:query')")
    public CommonResult<PageResult<DeviceModelRespVO>> getDeviceModelPage(@Valid DeviceModelPageReqVO pageReqVO) {
        PageResult<DeviceModelDO> pageResult = deviceModelService.getDeviceModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceModelRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出采集设备模型 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceModelExcel(@Valid DeviceModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceModelDO> list = deviceModelService.getDeviceModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采集设备模型.xls", "数据", DeviceModelRespVO.class,
                        BeanUtils.toBean(list, DeviceModelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得采集设备模型列表")
    public CommonResult<List<DeviceModelDO>> getDeviceModelList() {
        List<DeviceModelDO> deviceModelDOList = deviceModelService.getDeviceModelList();
        return success(deviceModelDOList);

    }


    @PostMapping("/copy")
    @Operation(summary = "复制设备模型")
    @PreAuthorize("@ss.hasPermission('iot:device-model:create')")
    public CommonResult<Long> copyDevice(@RequestParam("id") Long id) {
        return success(deviceModelService.copyDevice(id));
    }


}