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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.menu.dto.MenuMaterialCategoryAddDTO;
import com.bonus.canteen.core.menu.dto.MenuMaterialCategoryEditDTO;
import com.bonus.canteen.core.menu.dto.MenuMaterialCategoryPageDTO;
import com.bonus.canteen.core.menu.dto.CategoryTreeListDTO;
import com.bonus.canteen.core.menu.service.MenuMaterialCategoryService;
import com.bonus.canteen.core.menu.vo.MenuCategoryTreeVO;
import com.bonus.canteen.core.menu.vo.MenuMaterialCategoryPageVO;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping({"/api/v1/menumaterialcategory"})
@Api(
   value = "货品类别",
   tags = {"pfy_库存原料类别"}
)
public class MenuMaterialCategoryController {
   private static final Logger log = LoggerFactory.getLogger(MenuMaterialCategoryController.class);
   @Autowired
   @Lazy
   private MenuMaterialCategoryService menuMaterialCategoryService;

   @PostMapping({"/add"})
   @ApiOperation("新增原料类别")
   public AjaxResult addMenuMaterialCategory(@RequestBody @Valid MenuMaterialCategoryAddDTO dto) {
      this.menuMaterialCategoryService.addMenuMaterialCategory(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/page"})
   @ApiOperation("分页获取原料类别")
   public AjaxResult getMenuMaterialCategoryPage(@RequestBody MenuMaterialCategoryPageDTO dto) {
      Page<MenuMaterialCategoryPageVO> resultPage = this.menuMaterialCategoryService.getMenuMaterialCategoryPage(dto);
      return AjaxResult.success(resultPage);
   }

   @PostMapping({"/edit"})
   @ApiOperation("修改原料类别")
   public AjaxResult editMenuMaterialCategory(@RequestBody @Valid MenuMaterialCategoryEditDTO dto) {
      this.menuMaterialCategoryService.editMenuMaterialCategory(dto);
      return AjaxResult.success();
   }

   @ApiOperation("删除原料类别")
   @PostMapping({"/remove"})
   @RequiresGuest
   public AjaxResult removeMenuMaterialCategory(@RequestBody MenuMaterialCategoryEditDTO dto) {
      this.menuMaterialCategoryService.removeMenuMaterialCategory(dto.getCategoryId());
      return AjaxResult.success();
   }

   @PostMapping({"/tree/list"})
   @ApiOperation("获取所有类别的树形结构")
   public AjaxResult getCategoryTreeList(@RequestBody CategoryTreeListDTO dto) {
      List<MenuCategoryTreeVO> resultList = this.menuMaterialCategoryService.getCategoryTreeList(dto);
      return AjaxResult.success(resultList);
   }

   @PostMapping({"/init"})
   @RequiresGuest
   @ApiOperation("初始化原料类别")
   public AjaxResult initMenuCategory() {
      this.menuMaterialCategoryService.initMenuCategory();
      return AjaxResult.success();
   }

   @PostMapping({"/area/init"})
   @RequiresGuest
   @ApiOperation("初始化原料类别")
   public AjaxResult areaInitMenuCategory(@RequestBody Long dto) {
      this.menuMaterialCategoryService.areaInitMenuCategory(dto);
      return AjaxResult.success();
   }
}
