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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.allocation.canteen.dto.AllocRecipeStallDTO;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.menu.dto.*;
import com.bonus.canteen.core.menu.service.MenuRecipeService;
import com.bonus.canteen.core.menu.vo.*;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import com.bonus.common.houqin.i18n.I18n;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping({"/api/v2/menurecipe"})
@Api(
   value = "menurecipev2",
   tags = {"pfy_菜谱信息V2"}
)
public class MenuRecipeV2Controller {
   private static final Logger log = LoggerFactory.getLogger(MenuRecipeV2Controller.class);
   @Lazy
   @Resource
   private MenuRecipeService menuRecipeService;

   @PostMapping({"/add"})
   @ApiOperation("菜谱计划发布v2")
   public Long addMenuRecipeV2(@RequestBody @Valid MenuRecipeV2AddDTO dto) {
      Long recipeId;
      String var10000 = "yst:merchant-id:menu:recipe:edit:".replace("merchant-id", TenantContextHolder.getTenantId().toString());
      String lockKey = var10000 + dto.getRecipeId();
      boolean succ = RedisUtil.tryLock(lockKey, 0, 300);
      if (!succ) {
         throw new ServiceException(I18n.getMessage("menu_dishes_import_not_commit_repeat", new Object[0]));
      } else {
         try {
            recipeId = this.menuRecipeService.addMenuRecipeV2(dto);
         } finally {
            RedisUtil.safeUnLock(lockKey);
         }

         return recipeId;
      }
   }

   @RequiresGuest
   @PostMapping({"/add/copy"})
   @ApiOperation("发布指定日期菜谱到其他菜谱计划")
   public AjaxResult addMenuRecipeV2Copy(@RequestBody @Valid MenuRecipeV2AddCopyDTO dto) {
      this.menuRecipeService.addMenuRecipeV2Copy(dto);
      return AjaxResult.success();
   }

   @RequiresGuest
   @PostMapping({"/edit/by/applyDate"})
   @ApiOperation("修改指定日期菜谱的详情")
   public AjaxResult editMenuRecipeByApplyDate(@RequestBody @Valid MenuRecipeDetailEditDTO dto) {
      this.menuRecipeService.editMenuRecipeByApplyDate(dto);
      return AjaxResult.success();
   }

   @RequiresGuest
   @PostMapping({"/query/dishes/by/date"})
   @ApiOperation("查询指定日期各个餐次是否有指定的菜")
   public AjaxResult queryDishesByDate(@RequestBody QueryDishes2RecipeDto dto) {
      return AjaxResult.success(this.menuRecipeService.queryDishesByDate(dto));
   }

   @RequiresGuest
   @PostMapping({"/add/dishes/by/date"})
   @ApiOperation("添加或删除菜品到指定日期和餐次")
   public AjaxResult addDishesByDate(@RequestBody AddDishes2RecipeDto dto) {
      this.menuRecipeService.addDishesByDate(dto);
      return  AjaxResult.success();
   }

   @RequiresGuest
   @PostMapping({"/edit/day/meal/template"})
   @ApiOperation("修改指定单个餐次菜谱的模版")
   public AjaxResult editRecipeByTemplate(@RequestBody @Valid MenuRecipeDetailEditDTO dto) {
      this.menuRecipeService.editRecipeByTemplate(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/page"})
   @ApiOperation("菜谱计划发布记录分页列表")
   public Page<MenuRecipeV2PageVO> getMenuRecipeV2Page(@RequestBody MenuRecipeV2PageDTO dto) {
      return this.menuRecipeService.getMenuRecipeV2Page(dto);
   }

   @RequiresGuest
   @PostMapping({"/detail/page"})
   @ApiOperation("获取指定菜谱发布详情分页列表")
   public Page<MenuRecipeDetailV2PageVO> getMenuRecipeDetailV2Page(@RequestBody MenuRecipeDetailV2PageDTO dto) {
      return this.menuRecipeService.getMenuRecipeDetailV2Page(dto);
   }

   @PostMapping({"/date/detail"})
   @ApiOperation("获取指定日期菜谱计划菜品详情")
   public List<MenuRecipeDetailDateVO> getMenuRecipeDateDetail(@RequestBody MenuRecipeDetailDateDTO dto) {
      return this.menuRecipeService.getMenuRecipeDateDetail(dto);
   }

   @PostMapping({"/template/detail"})
   @ApiOperation("获取指定菜谱模版详情")
   public List<MenuRecipeDetailDateVO> queryRecipeTemplateDate(@RequestBody QueryRecipeTemplateDateDTO dto) {
      return this.menuRecipeService.queryRecipeTemplateDate(dto);
   }

   @RequiresGuest
   @PostMapping({"/detail/by/id"})
   @ApiOperation("获取指定菜谱详情")
   public List<MenuRecipeDetailV2VO> getMenuRecipeDetailById(@RequestBody MenuRecipeDetailV2DTO dto) {
      return this.menuRecipeService.getMenuRecipeDetailById(dto);
   }

   @RequiresGuest
   @ApiOperation("删除菜品计划")
   @PostMapping({"/remove/recipes"})
   public AjaxResult removeByRecipeId(@RequestBody List<Long> dto) {
      this.menuRecipeService.removeByRecipeId(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/remove/detail"})
   @ApiOperation("删除菜品计划详情")
   public AjaxResult removeDetailByRecipeId(@RequestBody MenuRecipeDetailRemoveDTO dto) {
      this.menuRecipeService.removeDetailByRecipeId(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/edit/base"})
   @ApiOperation("编辑修改菜谱基础信息")
   public AjaxResult editRecipeBase(@RequestBody @Valid MenuRecipeV2EditDTO dto) {
      this.menuRecipeService.editRecipeBase(dto);
      return AjaxResult.success();
   }

   @RequiresGuest
   @PostMapping({"/app/bind"})
   @ApiOperation("菜谱发布到移动端或解绑")
   public AjaxResult bindAppRecipe(@RequestBody @Valid MenuRecipeBindDTO dto) {
      this.menuRecipeService.bindAppRecipe(dto);
      return AjaxResult.success();
   }

   @RequiresGuest
   @PostMapping({"/app/bind/list"})
   @ApiOperation("获取移动端菜谱绑定详情")
   public List<MenuAppBindRecipeVO> getBindAppRecipeList(@RequestBody @Valid MenuAppBindRecipeDTO dto) {
      return this.menuRecipeService.getBindAppRecipeList(dto);
   }

   @PostMapping({"/recipe/import/dishes"})
   @ApiOperation("批量导入菜谱中的菜品信息")
   public AjaxResult recipeImportDishes(@RequestParam("file") MultipartFile excel, @RequestParam("recipeId") Long recipeId) {
      return ObjectUtil.isNull(excel) ? AjaxResult.error("请选择导入文件 ^_^") :
              AjaxResult.success(this.menuRecipeService.recipeImportDishes(excel, recipeId));
   }

   @RequiresGuest
   @PostMapping({"/recipe/delete/byInterval"})
   @ApiOperation("根据餐次批量删除菜谱中的菜品信息")
   public AjaxResult deletebyInterval(@RequestBody @Valid MenuDeleteByDateAndIntervalDTO dto) {
      this.menuRecipeService.deletebyInterval(dto);
      return AjaxResult.success();
   }

   @ApiOperation("根据时间查询菜谱")
   @PostMapping({"/recipe/get/between/date"})
   public List<MenuRecipeDateVO> getBetweenDate(@RequestBody @Valid MenuSelectByDateDTO dto) {
      return this.menuRecipeService.getBetweenDate(dto);
   }

   @ApiOperation("根据时间查询菜谱")
   @PostMapping({"/recipe/get/week"})
   public List<MenuRecipeWeekVO> getWeek(@RequestBody @Valid Long dto) {
      return this.menuRecipeService.getWeek(dto);
   }

   @PostMapping({"/copy-recipe-to-stall"})
   @ApiOperation("复制菜谱到其他档口")
   public AjaxResult copyRecipeToStall(@RequestBody @Valid MenuCopyRecipeStallDTO dto) {
      this.menuRecipeService.copyRecipeToStall(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/query/current/recipe"})
   @ApiOperation("查询档口的当前发布菜谱")
   public AjaxResult queryCurrentRecipe(@RequestBody @Valid AllocRecipeStallDTO dto) {
      return AjaxResult.success(this.menuRecipeService.queryCurrentRecipe(dto));
   }
}
