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

import cn.iocoder.yudao.module.iot.controller.admin.devicemodelrules.vo.PointRulesRespVO;
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.devicepointrules.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicepointrules.DevicePointRulesDO;
import cn.iocoder.yudao.module.iot.service.devicepointrules.DevicePointRulesService;

@Tag(name = "管理后台 - 点位规则")
@RestController
@RequestMapping("/iot/device-point-rules")
@Validated
public class DevicePointRulesController {

    @Resource
    private DevicePointRulesService devicePointRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建点位规则")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:create')")
    public CommonResult<Long> createDevicePointRules(@Valid @RequestBody DevicePointRulesSaveReqVO createReqVO) {
        return success(devicePointRulesService.createDevicePointRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点位规则")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:update')")
    public CommonResult<Boolean> updateDevicePointRules(@Valid @RequestBody DevicePointRulesSaveReqVO updateReqVO) {
        devicePointRulesService.updateDevicePointRules(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得点位规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:query')")
    public CommonResult<DevicePointRulesRespVO> getDevicePointRules(@RequestParam("id") Long id) {
        DevicePointRulesDO devicePointRules = devicePointRulesService.getDevicePointRules(id);
        return success(BeanUtils.toBean(devicePointRules, DevicePointRulesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点位规则分页")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:query')")
    public CommonResult<PageResult<DevicePointRulesRespVO>> getDevicePointRulesPage(@Valid DevicePointRulesPageReqVO pageReqVO) {
        PageResult<DevicePointRulesDO> pageResult = devicePointRulesService.getDevicePointRulesPage(pageReqVO);
        PageResult<DevicePointRulesRespVO> rulesRespVOPageResult = BeanUtils.toBean(pageResult, DevicePointRulesRespVO.class);
        for (DevicePointRulesRespVO devicePointRulesRespVO : rulesRespVOPageResult.getList()) {
            if (StringUtils.isNotBlank(devicePointRulesRespVO.getFieldRule())) {
                List<PointRulesRespVO> pointRulesVOList = JSON.parseArray(devicePointRulesRespVO.getFieldRule(), PointRulesRespVO.class);
                devicePointRulesRespVO.setPointRulesVOList(pointRulesVOList);
            }

        }

        return success(rulesRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点位规则 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevicePointRulesExcel(@Valid DevicePointRulesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DevicePointRulesDO> list = devicePointRulesService.getDevicePointRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点位规则.xls", "数据", DevicePointRulesRespVO.class,
                        BeanUtils.toBean(list, DevicePointRulesRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得点位规则列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-point-rules:query')")
    public CommonResult<List<DevicePointRulesDO>> getList(@RequestParam("id") Long id) {
        List<DevicePointRulesDO> devicePointRules = devicePointRulesService.getList(id);
        return success(devicePointRules);
    }

}