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

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.recipepointrecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipepointrecord.RecipePointRecordDO;
import cn.iocoder.yudao.module.iot.service.recipepointrecord.RecipePointRecordService;

@Tag(name = "管理后台 - IoT配方点位记录")
@RestController
@RequestMapping("/iot/recipe-point-record")
@Validated
public class RecipePointRecordController {

    @Resource
    private RecipePointRecordService recipePointRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建IoT配方点位记录")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:create')")
    public CommonResult<Long> createRecipePointRecord(@Valid @RequestBody RecipePointRecordSaveReqVO createReqVO) {
        return success(recipePointRecordService.createRecipePointRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IoT配方点位记录")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:update')")
    public CommonResult<Boolean> updateRecipePointRecord(@Valid @RequestBody RecipePointRecordSaveReqVO updateReqVO) {
        recipePointRecordService.updateRecipePointRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IoT配方点位记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:delete')")
    public CommonResult<Boolean> deleteRecipePointRecord(@RequestParam("id") Long id) {
        recipePointRecordService.deleteRecipePointRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IoT配方点位记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:query')")
    public CommonResult<RecipePointRecordRespVO> getRecipePointRecord(@RequestParam("id") Long id) {
        RecipePointRecordDO recipePointRecord = recipePointRecordService.getRecipePointRecord(id);
        return success(BeanUtils.toBean(recipePointRecord, RecipePointRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得IoT配方点位记录分页")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:query')")
    public CommonResult<PageResult<RecipePointRecordRespVO>> getRecipePointRecordPage(@Valid RecipePointRecordPageReqVO pageReqVO) {
        PageResult<RecipePointRecordDO> pageResult = recipePointRecordService.getRecipePointRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecipePointRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IoT配方点位记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipePointRecordExcel(@Valid RecipePointRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipePointRecordDO> list = recipePointRecordService.getRecipePointRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IoT配方点位记录.xls", "数据", RecipePointRecordRespVO.class,
                        BeanUtils.toBean(list, RecipePointRecordRespVO.class));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量创建IoT配方点位记录")
    @PreAuthorize("@ss.hasPermission('iot:recipe-point-record:create')")
    public CommonResult<Boolean> createRecipePointRecordBatch(@Valid @RequestBody List<RecipePointRecordSaveReqVO> createReqVOList) {
        for (RecipePointRecordSaveReqVO createReqVO :  createReqVOList) {
            createReqVO.setId(null);
            recipePointRecordService.createRecipePointRecord(createReqVO);
        }
        return success(true);
    }

}