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

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
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 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.devicemodelrules.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodelrules.DeviceModelRulesDO;
import cn.iocoder.yudao.module.iot.service.devicemodelrules.DeviceModelRulesService;

@Tag(name = "管理后台 - 点位规则关联模型")
@RestController
@RequestMapping("/iot/device-model-rules")
@Validated
public class DeviceModelRulesController {

    @Resource
    private DeviceModelRulesService deviceModelRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建点位规则关联模型")
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:create')")
    public CommonResult<Long> createDeviceModelRules(@Valid @RequestBody DeviceModelRulesSaveReqVO createReqVO) {
        return success(deviceModelRulesService.createDeviceModelRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点位规则关联模型")
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:update')")
    public CommonResult<Boolean> updateDeviceModelRules(@Valid @RequestBody DeviceModelRulesSaveReqVO updateReqVO) {
        deviceModelRulesService.updateDeviceModelRules(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点位规则关联模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:delete')")
    public CommonResult<Boolean> deleteDeviceModelRules(@RequestParam("id") Long id) {
        deviceModelRulesService.deleteDeviceModelRules(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点位规则关联模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:query')")
    public CommonResult<DeviceModelRulesRespVO> getDeviceModelRules(@RequestParam("id") Long id) {
        DeviceModelRulesDO deviceModelRules = deviceModelRulesService.getDeviceModelRules(id);
        DeviceModelRulesRespVO deviceModelRulesRespVO = BeanUtils.toBean(deviceModelRules, DeviceModelRulesRespVO.class);
        if (StringUtils.isNotBlank(deviceModelRulesRespVO.getFieldRule())){
            List<PointRulesRespVO> pointRulesVOList = JSON.parseArray(deviceModelRulesRespVO.getFieldRule(), PointRulesRespVO.class);
            deviceModelRulesRespVO.setPointRulesVOList(pointRulesVOList);
        }
        return success(deviceModelRulesRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得点位规则关联模型分页")
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:query')")
    public CommonResult<PageResult<DeviceModelRulesRespVO>> getDeviceModelRulesPage(@Valid DeviceModelRulesPageReqVO pageReqVO) {
        PageResult<DeviceModelRulesDO> pageResult = deviceModelRulesService.getDeviceModelRulesPage(pageReqVO);
        PageResult<DeviceModelRulesRespVO> modelRulesRespVOPageResult = BeanUtils.toBean(pageResult, DeviceModelRulesRespVO.class);
        for (DeviceModelRulesRespVO deviceModelRulesRespVO : modelRulesRespVOPageResult.getList()) {
            if (StringUtils.isNotBlank(deviceModelRulesRespVO.getFieldRule())) {
                List<PointRulesRespVO> pointRulesVOList = JSON.parseArray(deviceModelRulesRespVO.getFieldRule(), PointRulesRespVO.class);
                deviceModelRulesRespVO.setPointRulesVOList(pointRulesVOList);
            }
        }

        return success(modelRulesRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点位规则关联模型 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-model-rules:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceModelRulesExcel(@Valid DeviceModelRulesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceModelRulesDO> list = deviceModelRulesService.getDeviceModelRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点位规则关联模型.xls", "数据", DeviceModelRulesRespVO.class,
                        BeanUtils.toBean(list, DeviceModelRulesRespVO.class));
    }

}