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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.DishesPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.DishesDO;
import cn.iocoder.yudao.module.system.service.restaurant.DishesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 餐厅菜品")
@RestController
@RequestMapping("/restaurant/dishes")
@Validated
public class DishesController {

    @Resource
    private DishesService dishesService;

    @PostMapping("/create")
    @Operation(summary = "创建菜品")
    public CommonResult<Boolean> createDishes(@Valid @RequestBody DishesDO createReqVO) {
        dishesService.createDishes(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜品")
    public CommonResult<Boolean> updateDishes(@Valid @RequestBody DishesDO updateReqVO) {
        dishesService.updateDishes(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除菜品")
    public CommonResult<Boolean> deleteDishes(@RequestBody Long[] ids) {
        dishesService.deleteDishes(ids);
        return success(true);
    }

    @GetMapping("/getDishes")
    @Operation(summary = "获得菜品详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DishesDO> getDishes(@RequestParam("id") Long id) {
        return success(dishesService.getDishes(id));
    }

    @GetMapping("/getDishesPage")
    @Operation(summary = "菜品分页")
    @TenantIgnore
    public CommonResult<PageResult<DishesDO>> getDishesPage(@Valid DishesPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<DishesDO> pageResult = dishesService.getDishesPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/getDishesList")
    @Operation(summary = "查询菜品列表")
    public CommonResult<List<DishesDO>>  getDishesList(@RequestParam(value = "foodName",required = false) String foodName,@RequestParam(value = "restaurantId") Long restaurantId){
        return success(dishesService.getDishesList(foodName,restaurantId));
    }


//    @GetMapping("/export-excel")
//    @Operation(summary = "导出餐厅菜品 Excel")
//    @PreAuthorize("@ss.hasPermission('restaurant:dishes:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportDishesExcel(@Valid DishesPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<DishesDO> list = dishesService.getDishesPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "餐厅菜品.xls", "数据", DishesRespVO.class,
//                        BeanUtils.toBean(list, DishesRespVO.class));
//    }

}