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

import com.fasterxml.jackson.core.JsonProcessingException;
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 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.recipedeviceattribute.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipedeviceattribute.RecipeDeviceAttributeDO;
import cn.iocoder.yudao.module.iot.service.recipedeviceattribute.RecipeDeviceAttributeService;
import cn.iocoder.yudao.module.iot.service.device.TDengineService;

@Tag(name = "管理后台 - 配方配置（关联采集设备模型-点位管理）")
@RestController
@RequestMapping("/iot/recipe-device-attribute")
@Validated
public class RecipeDeviceAttributeController {

    @Resource
    private RecipeDeviceAttributeService recipeDeviceAttributeService;
    @Resource
    private TDengineService tDengineService;

    @PostMapping("/create")
    @Operation(summary = "创建配方配置（关联采集设备模型-点位管理）")
    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:create')")
    public CommonResult<Boolean> createRecipeDeviceAttribute(@Valid @RequestBody RecipeDeviceAttributeSaveReqVO createReqVO) {
        return success(recipeDeviceAttributeService.createRecipeDeviceAttributeBatch(createReqVO));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新配方配置（关联采集设备模型-点位管理）")
//    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:update')")
//    public CommonResult<Boolean> updateRecipeDeviceAttribute(@Valid @RequestBody RecipeDeviceAttributeSaveReqVO updateReqVO) {
//        recipeDeviceAttributeService.updateRecipeDeviceAttribute(updateReqVO);
//        return success(true);
//    }
    @PutMapping("/update")
    @Operation(summary = "更新配方设备属性（先删后增）")
    public CommonResult<Boolean> update(@Valid @RequestBody RecipeDeviceAttributeUpdateReqVO reqVO) {
        Boolean success = recipeDeviceAttributeService.updateRecipeDeviceAttribute(reqVO);
        return success(success); // 返回是否成功（和create接口返回格式一致）
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配方配置（关联采集设备模型-点位管理）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:delete')")
    public CommonResult<Boolean> deleteRecipeDeviceAttribute(@RequestParam("id") Long id) {
        recipeDeviceAttributeService.deleteRecipeDeviceAttribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配方配置（关联采集设备模型-点位管理）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:query')")
    public CommonResult<RecipeDeviceAttributeRespVO> getRecipeDeviceAttribute(@RequestParam("id") Long id) {
        RecipeDeviceAttributeDO recipeDeviceAttribute = recipeDeviceAttributeService.getRecipeDeviceAttribute(id);
        return success(BeanUtils.toBean(recipeDeviceAttribute, RecipeDeviceAttributeRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得配方配置（关联采集设备模型-点位管理）分页")
//    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:query')")
//    public CommonResult<PageResult<RecipeDeviceAttributeRespVO>> getRecipeDeviceAttributePage(@Valid RecipeDeviceAttributePageReqVO pageReqVO) {
//        PageResult<RecipeDeviceAttributeDO> pageResult = recipeDeviceAttributeService.getRecipeDeviceAttributePage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, RecipeDeviceAttributeRespVO.class));
//    }
    @GetMapping("/page")
    @Operation(summary = "分页查询配方配置")
    public CommonResult<PageResult<RecipeDeviceAttributePageRespDTO>> page(RecipeDeviceAttributePageReqVO reqVO) {
        // 替换为关联查询方法
        PageResult<RecipeDeviceAttributePageRespDTO> pageResult = recipeDeviceAttributeService.selectPageWithAttribute(reqVO);
        return success(pageResult);
    }

    @GetMapping("/getList")
    @Operation(summary = "查询配方配置列表")
    public CommonResult<List<RecipeDeviceAttributePageRespDTO>> getList(RecipeDeviceAttributePageReqVO reqVO) {
        // 替换为关联查询方法
        List<RecipeDeviceAttributePageRespDTO> recipeDeviceAttributeServiceList = recipeDeviceAttributeService.getList(reqVO);
        return success(recipeDeviceAttributeServiceList);
    }

    @GetMapping("/list")
    @Operation(summary = "查询配方配置列表")
    public CommonResult<PageResult<RecipeDeviceAttributePageRespDTO>> list(RecipeDeviceAttributePageReqVO reqVO) {
        // 替换为关联查询方法
        PageResult<RecipeDeviceAttributePageRespDTO> pageResult = recipeDeviceAttributeService.selectPageWithAttribute(reqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配方配置（关联采集设备模型-点位管理） Excel")
    @PreAuthorize("@ss.hasPermission('iot:recipe-device-attribute:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipeDeviceAttributeExcel(@Valid RecipeDeviceAttributePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipeDeviceAttributeDO> list = recipeDeviceAttributeService.getRecipeDeviceAttributePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配方配置（关联采集设备模型-点位管理）.xls", "数据", RecipeDeviceAttributeRespVO.class,
                        BeanUtils.toBean(list, RecipeDeviceAttributeRespVO.class));
    }


    @GetMapping("/singleDevice")
    @Operation(summary = "设备运行参数最新值")
    @PreAuthorize("@ss.hasPermission('iot:device-model-attribute:query')")
// 修正返回值类型为Service方法对应的Map结构
    public CommonResult<Map<String, List<Map<String, Object>>>> operationAnalysisDetails(
            @RequestParam("deviceId") Long deviceId) throws JsonProcessingException {

        // 1. 注入RecipeDeviceAttributeService（需确保类名和注入名称正确）
        // 注意：这里假设你的Service类名为RecipeDeviceAttributeService，需和实际类名一致
        // 如果是通过@Autowired注入，需在Controller类中先声明：
        // @Autowired
        // private RecipeDeviceAttributeService recipeDeviceAttributeService;

        // 2. 调用Service方法（变量类型改为Map，匹配返回值）
        Map<String, List<Map<String, Object>>> recipeDeviceAttribute = recipeDeviceAttributeService.singleDevice(deviceId);

        // 3. 返回结果（参数类型匹配）
        return success(recipeDeviceAttribute);
    }

}