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

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.recipeplandetail.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipeplandetail.RecipePlanDetailDO;
import cn.iocoder.yudao.module.iot.service.recipeplandetail.RecipePlanDetailService;

@Tag(name = "管理后台 - 配方计划详情表（配方库）")
@RestController
@RequestMapping("/iot/recipe-plan-detail")
@Validated
public class RecipePlanDetailController {

    @Resource
    private RecipePlanDetailService recipePlanDetailService;


    @PostMapping("/create")
    @Operation(summary = "创建配方计划详情表（配方库）")
    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:create')")
    public CommonResult<Long> createRecipePlanDetail(@Valid @RequestBody RecipePlanDetailSaveReqVO createReqVO) {
        return success(recipePlanDetailService.createRecipePlanDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配方计划详情表（配方库）")
    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:update')")
    public CommonResult<Boolean> updateRecipePlanDetail(@Valid @RequestBody RecipePlanDetailSaveReqVO updateReqVO) {
        recipePlanDetailService.updateRecipePlanDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配方计划详情表（配方库）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:delete')")
    public CommonResult<Boolean> deleteRecipePlanDetail(@RequestParam("id") Long id) {
        recipePlanDetailService.deleteRecipePlanDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配方计划详情表（配方库）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:query')")
    public CommonResult<RecipePlanDetailRespVO> getRecipePlanDetail(@RequestParam("id") Long id) {
        RecipePlanDetailDO recipePlanDetail = recipePlanDetailService.getRecipePlanDetail(id);
        return success(BeanUtils.toBean(recipePlanDetail, RecipePlanDetailRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得配方计划详情表（配方库）分页")
//    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:query')")
//    public CommonResult<PageResult<RecipePlanDetailRespVO>> getRecipePlanDetailPage(@Valid RecipePlanDetailPageReqVO pageReqVO) {
//        PageResult<RecipePlanDetailDO> pageResult = recipePlanDetailService.getRecipePlanDetailPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, RecipePlanDetailRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "分页查询配方计划详情（含关联信息）")
    public CommonResult<PageResult<RecipePlanDetailPageRespDTO>> pageRecipePlanDetail(RecipePlanDetailPageReqVO reqVO) {
        PageResult<RecipePlanDetailPageRespDTO> pageResult = recipePlanDetailService.pageRecipePlanDetail(reqVO);
        return CommonResult.success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配方计划详情表（配方库） Excel")
    @PreAuthorize("@ss.hasPermission('iot:recipe-plan-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipePlanDetailExcel(@Valid RecipePlanDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipePlanDetailDO> list = recipePlanDetailService.getRecipePlanDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配方计划详情表（配方库）.xls", "数据", RecipePlanDetailRespVO.class,
                        BeanUtils.toBean(list, RecipePlanDetailRespVO.class));
    }

}