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

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

@Tag(name = "管理后台 - 采集点分类")
@RestController
@RequestMapping("/iot/device-attribute-type")
@Validated
public class DeviceAttributeTypeController {

    @Resource
    private DeviceAttributeTypeService deviceAttributeTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建采集点分类")
    @PreAuthorize("@ss.hasPermission('iot:device-attribute-type:create')")
    public CommonResult<Long> createDeviceAttributeType(@Valid @RequestBody DeviceAttributeTypeSaveReqVO createReqVO) {
        return success(deviceAttributeTypeService.createDeviceAttributeType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采集点分类")
    @PreAuthorize("@ss.hasPermission('iot:device-attribute-type:update')")
    public CommonResult<Boolean> updateDeviceAttributeType(@Valid @RequestBody DeviceAttributeTypeSaveReqVO updateReqVO) {
        deviceAttributeTypeService.updateDeviceAttributeType(updateReqVO);
        return success(true);
    }

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

        deviceAttributeTypeService.deleteDeviceAttributeType(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采集点分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-attribute-type:query')")
    public CommonResult<DeviceAttributeTypeRespVO> getDeviceAttributeType(@RequestParam("id") Long id) {
        DeviceAttributeTypeDO deviceAttributeType = deviceAttributeTypeService.getDeviceAttributeType(id);
        return success(BeanUtils.toBean(deviceAttributeType, DeviceAttributeTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采集点分类分页")
    @PreAuthorize("@ss.hasPermission('iot:device-attribute-type:query')")
    public CommonResult<PageResult<DeviceAttributeTypeRespVO>> getDeviceAttributeTypePage(@Valid DeviceAttributeTypePageReqVO pageReqVO) {
        PageResult<DeviceAttributeTypeDO> pageResult = deviceAttributeTypeService.getDeviceAttributeTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceAttributeTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采集点分类 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-attribute-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceAttributeTypeExcel(@Valid DeviceAttributeTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceAttributeTypeDO> list = deviceAttributeTypeService.getDeviceAttributeTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采集点分类.xls", "数据", DeviceAttributeTypeRespVO.class,
                        BeanUtils.toBean(list, DeviceAttributeTypeRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得采集点分类列表")
    public CommonResult<List<DeviceAttributeTypeDO>> getDeviceModelList() {
        List<DeviceAttributeTypeDO> deviceAttributeTypeDOList = deviceAttributeTypeService.getDeviceAttributeTypeList();
        return success(deviceAttributeTypeDOList);

    }

}