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

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.recipe.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipe.RecipeDO;
import cn.iocoder.yudao.module.iot.service.recipe.RecipeService;

@Tag(name = "管理后台 - 配方管理主")
@RestController
@RequestMapping("/iot/recipe")
@Validated
public class RecipeController {

    @Resource
    private RecipeService recipeService;

    @PostMapping("/create")
    @Operation(summary = "创建配方管理主")
    @PreAuthorize("@ss.hasPermission('iot:recipe:create')")
    public CommonResult<Long> createRecipe(@Valid @RequestBody RecipeSaveReqVO createReqVO) {
        return success(recipeService.createRecipe(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配方管理主")
    @PreAuthorize("@ss.hasPermission('iot:recipe:update')")
    public CommonResult<Boolean> updateRecipe(@Valid @RequestBody RecipeSaveReqVO updateReqVO) {
        recipeService.updateRecipe(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配方管理主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:recipe:delete')")
    public CommonResult<Boolean> deleteRecipe(@RequestParam("id") Long id) {
        recipeService.deleteRecipe(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配方管理主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe:query')")
    public CommonResult<RecipeRespVO> getRecipe(@RequestParam("id") Long id) {
        RecipeDO recipe = recipeService.getRecipe(id);
        return success(BeanUtils.toBean(recipe, RecipeRespVO.class));
    }

    //    @GetMapping("/page")
//    @Operation(summary = "获得配方管理主分页")
//    @PreAuthorize("@ss.hasPermission('iot:recipe:query')")
//    public CommonResult<PageResult<RecipeRespVO>> getRecipePage(@Valid RecipePageReqVO pageReqVO) {
//        PageResult<RecipeDO> pageResult = recipeService.getRecipePage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, RecipeRespVO.class));
//    }
    @GetMapping("/page")
    @Operation(summary = "获得配方管理主分页")
    @PreAuthorize("@ss.hasPermission('iot:recipe:query')")
    public CommonResult<PageResult<RecipeRespVO>> getRecipePage(@Valid RecipePageReqVO pageReqVO) {

        // 直接调用实现类的方法，无类型转换
        PageResult<RecipeRespVO> pageResult = recipeService.getRecipePageWithDeviceId(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配方管理主 Excel")
    @PreAuthorize("@ss.hasPermission('iot:recipe:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipeExcel(@Valid RecipePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipeDO> list = recipeService.getRecipePage(pageReqVO).getList();
        PageResult<RecipeDO> pageResult = recipeService.getRecipePage(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "配方管理主.xls", "数据", RecipeRespVO.class,
                        BeanUtils.toBean(list, RecipeRespVO.class));
    }

}