package com.bonus.canteen.core.cook.controller;

import com.bonus.canteen.core.cook.dto.AppletDishesDetailDTO;
import com.bonus.canteen.core.cook.dto.CookH5WeekDetailsDTO;
import com.bonus.canteen.core.cook.dto.UserInfoDTO;
import com.bonus.canteen.core.cook.service.ICookH5Service;
import com.bonus.canteen.core.cook.dto.CookH5ReserveRecipeDTO;
import com.bonus.canteen.core.cook.vo.AppletDishesDetailVO;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;

/**
 * @author xliu
 * @date 2025/5/28 9:20
 */
@Api(tags = "H5菜品计划信息")
@RestController
@RequestMapping("/cook/h5")
public class CookH5Controller extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CookH5Controller.class);

    @Resource
    private ICookH5Service cookH5Service;

    @GetMapping({"/list/week/canteen"})
    @ApiOperation("获取一周菜谱食堂列表")
    public AjaxResult getWeekMealList( UserInfoDTO dto) {
        startPage();
        try {
            return AjaxResult.success(this.cookH5Service.getWeekCanteenList(dto));
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("获取一周菜谱食堂列表失败");
        }
    }

    @ApiOperation("获取一周菜谱详情")
    @GetMapping({"/week/recipe/detail"})
    public TableDataInfo getWeekRecipeDetail(@Valid CookH5WeekDetailsDTO dto) {
        try {
            startPage();
            dto.setApplyDate(LocalDate.parse(dto.getDate()));
            return getDataTable(this.cookH5Service.getWeekRecipeDetail(dto));
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }

    }

    @ApiOperation("获取预定餐食堂列表")
    @GetMapping({"/list/reserve/canteen"})
    public TableDataInfo getReserveMealCanteenList(@Valid UserInfoDTO dto) {
        try {
            startPage();
            return getDataTable(this.cookH5Service.getReserveCanteenList(dto));
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }


    @ApiOperation("获取预定餐菜谱详情")
    @PostMapping({"/reserve/recipe/detail"})
    public TableDataInfo getReserveRecipeDetailList(@RequestBody CookH5ReserveRecipeDTO content) {
        try {
            return getDataTable(this.cookH5Service.getReserveRecipeDetailList(content));
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping({"/scheduledTasks"})
    @ApiOperation("定时任务")
    @Transactional
    public AjaxResult scheduledTasks() {
        this.cookH5Service.scheduledTask();
        return AjaxResult.success();
    }

    @PostMapping({"/detail"})
    @ApiOperation("根据菜品id获取菜品详情")
    public AjaxResult getRecipeDetail(@RequestBody AppletDishesDetailDTO dto) {
        try {
            AppletDishesDetailVO dishesDetailVO = this.cookH5Service.getDishesDetailByDishesId(dto);
            return success(dishesDetailVO);
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
