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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishes.DishesDO;
import cn.iocoder.yudao.module.system.service.dishes.DishesService;
import cn.iocoder.yudao.module.system.service.dishesnutrition.DishesNutritionService;
import cn.iocoder.yudao.module.system.service.dishesraw.DishesRawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


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

    @Resource
    private DishesService dishesService;
    @Resource
    private DishesNutritionService dishesNutritionService;
    @Resource
    private DishesRawService dishesRawService;

    @PostMapping("/create")
    @Operation(summary = "创建菜品管理")
    @PreAuthorize("@ss.hasPermission('t:dishes:create')")
    public CommonResult<Long> createDishes(@Valid @RequestBody DishesSaveReqVO createReqVO) {
        return success(dishesService.createDishes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜品管理")
    @PreAuthorize("@ss.hasPermission('t:dishes:update')")
    public CommonResult<Boolean> updateDishes(@Valid @RequestBody DishesSaveReqVO updateReqVO) {
        dishesService.updateDishes(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得菜品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:dishes:query')")
    public CommonResult<DishesRespVO> getDishes(@RequestParam("id") Long id) {
        return success(dishesService.getDishes1(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜品管理分页")
    @PreAuthorize("@ss.hasPermission('t:dishes:query')")
    public CommonResult<PageResult<DishesRespVO>> getDishesPage(@Valid DishesPageReqVO pageReqVO) {
        PageResult<DishesDO> pageResult = dishesService.getDishesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出菜品管理 Excel")
    @PreAuthorize("@ss.hasPermission('t:dishes:export')")
    @OperateLog(type = 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));
    }

}