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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.menu.dto.MenuNutritionAllDTO;
import com.bonus.canteen.core.menu.dto.MenuNutritionDTO;
import com.bonus.canteen.core.menu.entity.MenuMaterialDishes;
import com.bonus.canteen.core.menu.entity.MenuNutrition;
import com.bonus.canteen.core.menu.service.MenuNutritionService;
import com.bonus.canteen.core.menu.utils.NutritionEntity;
import com.bonus.canteen.core.menu.vo.MenuNutritionAllVO;
import com.bonus.canteen.core.menu.vo.MenuNutritionPageVO;
import com.bonus.canteen.core.menu.vo.MenuNutritionVO;
import com.bonus.canteen.core.menu.vo.NutritionTypeVO;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import com.bonus.common.houqin.utils.LeBeanUtil;
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 javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping({"/api/v1/menunutrition"})
@Api(
   value = "menunutrition",
   tags = {"pfy_食材营养基础信息管理"}
)
public class MenuNutritionController {
   private static final Logger log = LoggerFactory.getLogger(MenuNutritionController.class);
   @Resource
   @Lazy
   private MenuNutritionService menuNutritionService;

   @PostMapping({"/all/type/list"})
   @ApiOperation("获取所有食材类别")
   public AjaxResult getNutritionTypeList() {
      List<NutritionTypeVO> nutritionTypeList = this.menuNutritionService.getNutritionTypeList();
      return AjaxResult.success(nutritionTypeList);
   }

   @PostMapping({"/all/list"})
   @ApiOperation("获取所有食材营养信息")
   public AjaxResult getMenuNutritionAllList(@RequestBody MenuNutritionAllDTO dto) {
      List<MenuNutritionAllVO> resultList = this.menuNutritionService.getMenuNutritionAllList(dto);
      return AjaxResult.success(resultList);
   }

   @PostMapping({"/one"})
   @RequiresGuest
   @ApiOperation("根据菜品id获取该菜品所有原材料营养素信息")
   public AjaxResult getMenuNutritionByMaterialId(@RequestBody MenuMaterialDishes dto) {
      NutritionEntity nutritionEntity = this.menuNutritionService.getMenuNutritionByMaterialId(dto.getDishesId());
      return AjaxResult.success(nutritionEntity);
   }

   @PostMapping({"/oneMaterial"})
   @RequiresGuest
   @ApiOperation("获取单个原材料营养素信息")
   public AjaxResult getByNutritionId(@RequestBody MenuNutritionDTO dto) {
      MenuNutrition menuNutrition = this.menuNutritionService.getByNutritionId(dto.getNutritionId());
      return AjaxResult.success((MenuNutritionVO) LeBeanUtil.copyCreateProperties(menuNutrition, MenuNutritionVO.class));
   }

   @PostMapping({"/addMenuNutrition"})
   @ApiOperation("食物成分原材料录入")
   public AjaxResult addMenuNutrition(@RequestBody @Valid MenuNutritionDTO dto) {
      this.menuNutritionService.addMenuNutrition(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/updateMenuNutrition"})
   @ApiOperation("食物成分原材料修改")
   public AjaxResult updateMenuNutrition(@RequestBody MenuNutritionDTO dto) {
      this.menuNutritionService.updateMenuNutrition(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/getMenuNutritionPage"})
   @ApiOperation("分页获取原材料信息")
   public AjaxResult getMenuNutritionPage(@RequestBody MenuNutritionDTO dto) {
      Page<MenuNutritionPageVO> resultPage = this.menuNutritionService.getMenuNutritionPage(dto);
      return AjaxResult.success(resultPage);
   }

   @PostMapping({"/remove"})
   @RequiresGuest
   @ApiOperation("删除食物成分原材料")
   public AjaxResult removeByNutritionId(@RequestBody MenuNutritionDTO dto) {
      this.menuNutritionService.removeByNutritionCode(dto.getNutritionId());
      return AjaxResult.success();
   }
}
