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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.ThingModelCommonPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.ThingModelCommonRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.ThingModelCommonSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.ThingModelCommonDO;
import cn.iocoder.yudao.module.iot.service.thingmodel.ThingModelCommonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - IoT通用物模型")
@RestController
@RequestMapping("/iot/thing-model-common")
@Validated
public class ThingModelCommonController {

    @Resource
    private ThingModelCommonService thingModelCommonService;

    @PostMapping("/create")
    @Operation(summary = "创建IoT通用物模型")
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:create')")
    public CommonResult<Long> createThingModelCommon(@Valid @RequestBody ThingModelCommonSaveReqVO createReqVO) {
        return success(thingModelCommonService.createThingModelCommon(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IoT通用物模型")
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:update')")
    public CommonResult<Boolean> updateThingModelCommon(@Valid @RequestBody ThingModelCommonSaveReqVO updateReqVO) {
        thingModelCommonService.updateThingModelCommon(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IoT通用物模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:delete')")
    public CommonResult<Boolean> deleteThingModelCommon(@RequestParam("id") Long id) {
        thingModelCommonService.deleteThingModelCommon(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IoT通用物模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:query')")
    public CommonResult<ThingModelCommonRespVO> getThingModelCommon(@RequestParam("id") Long id) {
        ThingModelCommonDO thingModelCommon = thingModelCommonService.getThingModelCommon(id);
        return success(BeanUtils.toBean(thingModelCommon, ThingModelCommonRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得IoT通用物模型分页")
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:query')")
    public CommonResult<PageResult<ThingModelCommonRespVO>> getThingModelCommonPage(@Valid ThingModelCommonPageReqVO pageReqVO) {
        PageResult<ThingModelCommonDO> pageResult = thingModelCommonService.getThingModelCommonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ThingModelCommonRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IoT通用物模型 Excel")
    @PreAuthorize("@ss.hasPermission('iot:thing-model-common:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportThingModelCommonExcel(@Valid ThingModelCommonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ThingModelCommonDO> list = thingModelCommonService.getThingModelCommonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IoT通用物模型.xls", "数据", ThingModelCommonRespVO.class,
                        BeanUtils.toBean(list, ThingModelCommonRespVO.class));
    }

}