package cn.iocoder.yudao.module.system.controller.admin.dishesnutrition;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.dishesnutrition.vo.DishesNutritionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishesnutrition.vo.DishesNutritionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishesnutrition.vo.DishesNutritionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishesnutrition.DishesNutritionDO;
import cn.iocoder.yudao.module.system.service.dishesnutrition.DishesNutritionService;
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.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 菜品营养")
@RestController
@RequestMapping("/t/dishes-nutrition")
@Validated
public class DishesNutritionController {

    @Resource
    private DishesNutritionService dishesNutritionService;

    @PostMapping("/create")
    @Operation(summary = "创建菜品营养")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:create')")
    public CommonResult<Long> createDishesNutrition(@Valid @RequestBody DishesNutritionSaveReqVO createReqVO) {
        return success(dishesNutritionService.createDishesNutrition(createReqVO));
    }
    @PostMapping("/createbtch")
    @Operation(summary = "创建菜品营养")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:create')")
    public CommonResult<Long> createDishesNutrition(@Valid @RequestBody List<DishesNutritionSaveReqVO> createReqVO) {
        return success(dishesNutritionService.createDishesNutritionList(createReqVO));
    }
    @PutMapping("/update")
    @Operation(summary = "更新菜品营养")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:update')")
    public CommonResult<Boolean> updateDishesNutrition(@Valid @RequestBody DishesNutritionSaveReqVO updateReqVO) {
        dishesNutritionService.updateDishesNutrition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜品营养")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:delete')")
    public CommonResult<Boolean> deleteDishesNutrition(@RequestParam("id") Long id) {
        dishesNutritionService.deleteDishesNutrition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得菜品营养")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:query')")
    public CommonResult<DishesNutritionRespVO> getDishesNutrition(@RequestParam("id") Long id) {
        DishesNutritionDO dishesNutrition = dishesNutritionService.getDishesNutrition(id);
        return success(BeanUtils.toBean(dishesNutrition, DishesNutritionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜品营养分页")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:query')")
    public CommonResult<PageResult<DishesNutritionRespVO>> getDishesNutritionPage(@Valid DishesNutritionPageReqVO pageReqVO) {
        PageResult<DishesNutritionDO> pageResult = dishesNutritionService.getDishesNutritionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishesNutritionRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得菜品营养集合")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:query')")
    public CommonResult<List<DishesNutritionRespVO>> getDishesNutritionList(@Valid DishesNutritionPageReqVO pageReqVO) {
        List<DishesNutritionDO> pageResult = dishesNutritionService.getDishesNutritionList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishesNutritionRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出菜品营养 Excel")
    @PreAuthorize("@ss.hasPermission('t:dishes-nutrition:export')")
    @OperateLog(type = EXPORT)
    public void exportDishesNutritionExcel(@Valid DishesNutritionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishesNutritionDO> list = dishesNutritionService.getDishesNutritionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "菜品营养.xls", "数据", DishesNutritionRespVO.class,
                        BeanUtils.toBean(list, DishesNutritionRespVO.class));
    }

}