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

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.recipepoint.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipepoint.RecipePointDO;
import cn.iocoder.yudao.module.iot.service.recipepoint.RecipePointService;

@Tag(name = "管理后台 - 手动配置参数表（绑定配方）")
@RestController
@RequestMapping("/iot/recipe-point")
@Validated
public class RecipePointController {

    @Resource
    private RecipePointService recipePointService;

    @PostMapping("/create")
    @Operation(summary = "创建手动配置参数表（绑定配方）")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:create')")
    public CommonResult<Long> createRecipePoint(@Valid @RequestBody RecipePointSaveReqVO createReqVO) {
        return success(recipePointService.createRecipePoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新手动配置参数表（绑定配方）")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:update')")
    public CommonResult<Boolean> updateRecipePoint(@Valid @RequestBody RecipePointSaveReqVO updateReqVO) {
        recipePointService.updateRecipePoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除手动配置参数表（绑定配方）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:delete')")
    public CommonResult<Boolean> deleteRecipePoint(@RequestParam("id") Long id) {
        recipePointService.deleteRecipePoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得手动配置参数表（绑定配方）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:query')")
    public CommonResult<RecipePointRespVO> getRecipePoint(@RequestParam("id") Long id) {
        RecipePointDO recipePoint = recipePointService.getRecipePoint(id);
        return success(BeanUtils.toBean(recipePoint, RecipePointRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得手动配置参数表（绑定配方）分页")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:query')")
    public CommonResult<PageResult<RecipePointRespVO>> getRecipePointPage(@Valid RecipePointPageReqVO pageReqVO) {
        PageResult<RecipePointDO> pageResult = recipePointService.getRecipePointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecipePointRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出手动配置参数表（绑定配方） Excel")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipePointExcel(@Valid RecipePointPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipePointDO> list = recipePointService.getRecipePointPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "手动配置参数表（绑定配方）.xls", "数据", RecipePointRespVO.class,
                        BeanUtils.toBean(list, RecipePointRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得手动配置参数表（绑定配方）分页")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point:query')")
    public CommonResult<List<RecipePointRespVO>> getRecipePointList(@RequestParam("id") Long recipeId) {
        List<RecipePointDO> list = recipePointService.getRecipePointPageList(recipeId);
        return success(BeanUtils.toBean(list, RecipePointRespVO.class));
    }

}