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

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.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.app.dishes.vo.AppSaveVO;
import cn.iocoder.yudao.module.system.controller.app.dishes.vo.AppUpdateVO;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

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


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

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

    @PostMapping("/create")
    @Operation(summary = "创建菜品管理")
    public CommonResult<Long> createDishes(@Valid @RequestBody AppSaveVO vo) {
        return success(dishesService.createDishesApp(vo));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜品管理")
    public CommonResult<Boolean> updateDishes(@RequestBody AppUpdateVO vo) {
        dishesService.updateDishesApp(vo);
        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) {
        DishesDO dishes = dishesService.getDishes(id);
        return success(BeanUtils.toBean(dishes, DishesRespVO.class));
    }

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

//    @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));
//    }

    @GetMapping("/getToday")
    @Operation(summary = "获取今日菜品")
    public CommonResult<List<DishesRespVO>> getTodayDishes() {
        List<DishesDO> todayDishes = dishesService.getTodayDishes();
        return success(BeanUtils.toBean(todayDishes, DishesRespVO.class));
    }
}