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

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.allocation.canteen.business.AllocCanteenBusiness;
import com.bonus.canteen.core.allocation.canteen.dto.AllocMobileCanteenQueryDTO;
import com.bonus.canteen.core.allocation.canteen.dto.ModifyCanteenIfReserveDTO;
import com.bonus.canteen.core.allocation.canteen.dto.ModifyCanteenStateDTO;
import com.bonus.canteen.core.allocation.canteen.dto.ModifyStallIfReserveDTO;
import com.bonus.canteen.core.allocation.canteen.model.*;
import com.bonus.canteen.core.allocation.canteen.param.AllocAreaParam;
import com.bonus.canteen.core.allocation.canteen.param.AllocCanteenNumParam;
import com.bonus.canteen.core.allocation.canteen.param.AllocCanteenStallPageParam;
import com.bonus.canteen.core.allocation.canteen.param.AllocCanteenStallParam;
import com.bonus.canteen.core.allocation.canteen.service.AllocCanteenService;
import com.bonus.canteen.core.allocation.canteen.service.AllocMealLineService;
import com.bonus.canteen.core.allocation.canteen.service.AllocStallService;
import com.bonus.canteen.core.allocation.canteen.vo.*;
import com.bonus.canteen.core.common.enums.DeliveryTypeEnum;
import com.bonus.common.core.web.domain.AjaxResult;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping({"/api/v2/alloc/canteen"})
@Api(
   tags = {"lsh_食堂档口相关控制器"}
)
public class AllocCanteenController {
   private static final Logger log = LoggerFactory.getLogger(AllocCanteenController.class);
   @Resource
   @Lazy
   private AllocCanteenService allocCanteenService;

   @Autowired
   @Lazy
   private AllocStallService allocStallService;

   @Autowired
   @Lazy
   private AllocCanteenBusiness allocCanteenBusiness;

   @Resource
   @Lazy
   private AllocMealLineService allocMealLineService;
   @ApiOperation("查询食堂所有开启的支付方式")
   @PostMapping({"/list-avail-pay-type"})
   public AjaxResult listAvailPayTypeForApp(@RequestBody AllocCanteen bean) {
      try {
         return AjaxResult.success(this.allocCanteenService.listAvailPayTypeForApp(bean.getCanteenId()));
      } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("查询食堂所有开启的支付方式失败");
      }
   }

   /**
    * @author jsk
    * @param bean
    * @return
    */
   @ApiOperation("移动端-查询预订日期列表")
   @PostMapping({"/list-reserve-date"})
   public AjaxResult listReserveDate(@RequestBody AllocMobileCanteenQueryDTO bean) {
      return AjaxResult.success(this.allocStallService.listReserveDate(bean));
   }

   @ApiOperation("集团版--权限区域食堂档口树")
   @PostMapping({"/tenant-group-area/tree"})
   public List<Tree<Long>> getTenantGroupAreaTree(@RequestBody AllocAreaParam param) {
      return this.allocCanteenService.getTenantGroupCanteenTree(param);
   }

   @ApiOperation("分页查询食堂列表")
   @PostMapping({"/page-canteen"})
   public Page<AllocCanteenVO> pageCanteen(@RequestBody AllocCanteenStallPageParam request) {
      return this.allocCanteenService.pageCanteen(request);
   }

   @ApiOperation("新增食堂")
   @PostMapping({"/add-canteen"})
//   @RequiresGuest
   public void addCanteen(@RequestBody @Valid AllocCanteenModel request) {
      this.allocCanteenBusiness.addCanteen(request);
   }

   @ApiOperation("修改食堂")
   @PostMapping({"/modify-canteen"})
//   @RequiresGuest
   public void modifyCanteen(@RequestBody @Valid AllocCanteenModifyModel request) {
      this.allocCanteenBusiness.modifyCanteen(request);
   }

   @ApiOperation("删除食堂")
   @PostMapping({"/remove-canteen"})
   public void removeCanteen(@RequestBody Map<String, Object> request) {
      Long canteenId = Long.valueOf((String) request.get("canteenId"));
      this.allocCanteenBusiness.removeCanteen(canteenId);
   }

   @ApiOperation("修改食堂 是否支持预订餐")
   @PostMapping({"/modify-canteen-if-reserve"})
   public void modifyCanteenIfReserve(@RequestBody @Valid ModifyCanteenIfReserveDTO request) {
      this.allocCanteenService.modifyCanteenIfReserve(request);
   }

   @ApiOperation("修改食堂/档口 营业状态")
   @PostMapping({"/modify-business-state"})
   public void modifyBusinessState(@RequestBody @Valid ModifyCanteenStateDTO request) {
      this.allocCanteenBusiness.modifyBusinessState(request);
   }

   @ApiOperation("校验食堂是否可以修改为休息状态")
   @PostMapping({"/check-canteen-rest"})
   public Integer checkCanteenRest(@RequestBody Map<String, Object> request) {
      Long canteenId = Long.valueOf((String) request.get("canteenId"));
      return this.allocCanteenService.checkCanteenRest(canteenId);
   }

   @ApiOperation("分页查询档口列表")
   @PostMapping({"/page-stall"})
   public Page<AllocStallVO> pageStall(@RequestBody AllocCanteenStallPageParam request) {
      return this.allocStallService.pageStall(request);
   }

   @ApiOperation("查询单个档口(聚合查询)")
   @PostMapping({"/get-stall-for-modify"})
   public AllocStallModifyModel getStallForModify(@RequestBody Map<String, Object> request) {
      Long stallId = Long.valueOf((String) request.get("stallId"));
      return this.allocCanteenBusiness.getStallForModify(stallId);
   }

   @ApiOperation("新增档口")
   @PostMapping({"/add-stall"})
   public void addStall(@RequestBody @Valid AllocStallModel request) {
      this.allocCanteenBusiness.addStall(request);
   }

   @ApiOperation("修改档口")
   @PostMapping({"/modify-stall"})
   public void modifyStall(@RequestBody @Valid AllocStallModifyModel request) {
      this.allocCanteenBusiness.modifyStall(request);
   }

   @ApiOperation("删除档口")
   @PostMapping({"/remove-stall"})
   public void removeStall(@RequestBody Map<String, Object> request) {
      Long stallId = Long.valueOf((String) request.get("stallId"));
      this.allocCanteenBusiness.removeStall(stallId);
   }

   @ApiOperation("修改档口 是否支持预订餐")
   @PostMapping({"/modify-stall-if-reserve"})
   public void modifyStallIfReserve(@RequestBody @Valid ModifyStallIfReserveDTO request) {
      this.allocStallService.modifyStallIfReserve(request);
   }

   @ApiOperation("查询区域食堂档口餐线编号")
   @PostMapping({"/get-canteen-num"})
   public AjaxResult getCanteenNum(@RequestBody @Valid AllocCanteenNumParam request) {
      return AjaxResult.success("操作成功", this.allocCanteenBusiness.getCanteenNum(request));
   }

   @ApiOperation("查询单个食堂(聚合查询)")
   @PostMapping({"/get-canteen-for-modify"})
   public AllocCanteenModifyModel getCanteenForModify(@RequestBody Map<String, Object> request) {
      Long canteenId = Long.valueOf((String) request.get("canteenId"));
      return this.allocCanteenBusiness.getCanteenForModify(canteenId);
   }


   @ApiOperation("分页查询餐线列表")
   @PostMapping({"/page-meal-line"})
   public AjaxResult pageMealLine(@RequestBody AllocCanteenStallPageParam dto) {
      return AjaxResult.success(this.allocMealLineService.pageMealLine(dto));
   }

   @ApiOperation("查询带权限的食堂列表")
   @PostMapping({"/list-all-auth-canteen"})
   public List<AllocCanteenAuthVO> listAllAuthCanteen(@RequestBody AllocCanteenStallParam request) {
      return this.allocCanteenService.listAllAuthCanteen(request);
   }

   @PostMapping({"/list-delivery-type-enum"})
   @ApiOperation("获取配送方式枚举")
   public List<Map<String, Object>> listDeliveryTypeEnum() {
      return DeliveryTypeEnum.getAllEnumsList();
   }

   @ApiOperation("查询多食堂带权限的档口列表")
   @PostMapping({"/list-multiple-auth-stall"})
   public List<AllocStallAuthVO> listMultipleAuthStall(@RequestBody AllocCanteenStallParam request) {
      return this.allocStallService.listMultipleAuthStall(request);
   }

   @ApiOperation("查询多档口的餐线列表")
   @PostMapping({"/list-multiple-meal-line"})
   public List<AllocMealLineVO> listMultipleMealLine(@RequestBody AllocCanteenStallParam param) {
      return this.allocMealLineService.listMultipleMealLine(param);
   }
}
