package com.bonus.canteen.core.supply.purchase.controller;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.supply.purchase.domain.param.*;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionPlanPageVO;
import com.bonus.canteen.core.supply.purchase.service.DrpProductionPlanService;
import com.bonus.canteen.core.supply.purchase.service.DrpPurchasePlanDisassemblyService;
import com.bonus.common.core.exception.ServiceException;
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.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/drp-production-plan"})
@Api(
   value = "gyp-生产计划",
   tags = {"gyp-生产计划"}
)
public class DrpProductionPlanController extends BaseController {
   private static final Logger log = LoggerFactory.getLogger(DrpProductionPlanController.class);
   @Autowired
   @Lazy
   private DrpProductionPlanService drpProductionPlanService;
   @Autowired
   @Lazy
   private DrpPurchasePlanDisassemblyService drpPurchasePlanDisassemblyService;

   @PostMapping({"/page"})
   @ApiOperation("分页查询生产计划")
   public TableDataInfo pageProductionPlan(@RequestBody @Valid DrpProductionPlanPageDTO request) {
      startPage();
      List<DrpProductionPlanPageVO> resultList = this.drpProductionPlanService.pageProductionPlan(request);
      return getDataTable(resultList);
   }

   @PostMapping({"/materialCount"})
   @ApiOperation("生产计划货品数量")
   public AjaxResult materialCount(@RequestBody @Valid DrpProductionPlanPageDTO request) {
      return AjaxResult.success(this.drpProductionPlanService.materialCount(request));
   }

//   @PostMapping({"/preOrderInfo"})
//   @ApiOperation("根据预订单创建生产计划")
//   public DrpProductionPlanDetailVO preOrderInfo(@RequestBody @Valid LeRequest<OrderDrpDishesSumDTO> leRequest) {
//      return this.drpProductionPlanService.preOrderInfo((OrderDrpDishesSumDTO)leRequest.getContent());
//   }

   @PostMapping({"/add"})
   @ApiOperation("新增修改生产计划")
   public AjaxResult addProductionPlan(@RequestBody @Valid DrpProductionPlanAddDTO request) {
      return AjaxResult.success(this.drpProductionPlanService.addProductionPlan(request));
   }

   @PostMapping({"/delete/{productionId}"})
   @ApiOperation("删除生产计划")
   public AjaxResult deleteProductionPlan(@PathVariable("productionId") String productionId) {
      drpProductionPlanService.deleteProductionPlan(productionId);
      return AjaxResult.success();
   }

   @PostMapping({"/generate/pur-plan"})
   @ApiOperation("生产计划生成采购计划")
   public AjaxResult generatePurPlan(@RequestBody @Valid List<DrpGeneratePurPlanDetailDTO> request) {
      if (CollUtil.isEmpty(request)) {
         throw new ServiceException("采购明细不能为空");
      } else {
         String productionId = this.drpProductionPlanService.generatePurPlan(request);
         return AjaxResult.success(productionId);
      }
   }

//   @PostMapping({"/generate/batch/pur-plan"})
//   @ApiOperation("批量生产计划生成采购计划")
//   public AjaxResult batchGeneratePurPlan(@RequestBody @Valid DrpGeneratePurPlanDTO content) {
//      if (content == null) {
//         throw new ServiceException(I18n.getMessage("drp.generate-pur-plan-detail-is-null", new Object[0]));
//      } else {
//         String productionId = this.drpProductionPlanService.batchGeneratePurPlan(content);
//         return AjaxResult.success(productionId);
//      }
//   }

   @PostMapping({"/generate/batch/pur-plan"})
   @ApiOperation("生产计划生成采购计划")
   public AjaxResult generateBatchPurPlan(@RequestBody @Valid DrpProductionGeneratePurPlanDTO request) {
      return AjaxResult.success(this.drpPurchasePlanDisassemblyService.generateBatchPurPlan(request));
   }

   /** @deprecated */
   @PostMapping({"/list/detail-direct/{productionPlanId}"})
   @ApiOperation("查询生产计划详情-菜品")
   public AjaxResult listDetailDirect(@PathVariable("productionPlanId") String productionPlanId) {
      return AjaxResult.success(this.drpProductionPlanService.listDetailDirect(productionPlanId));
   }

   @PostMapping({"/list/detail-recipe-order"})
   @ApiOperation("获取生产计划详情-菜谱/预订单")
   public AjaxResult listDetailRecipeOrder(@RequestBody @Valid String productionPlanId) {
      return AjaxResult.success(drpProductionPlanService.listDetailRecipeOrder(productionPlanId));
   }

   @PostMapping({"/detail/{productionPlanId}"})
   @ApiOperation("获取生产计划详情")
   public AjaxResult detail(@PathVariable("productionPlanId") String productionPlanId) {
      return AjaxResult.success(drpProductionPlanService.detail(productionPlanId));
   }

   @PostMapping({"/list/production-dishes/sum/{productionPlanId}"})
   @ApiOperation("获取生产计划菜品详情汇总")
   public AjaxResult listProductionDishesSum(@PathVariable("productionPlanId") String productionPlanId) {
      return AjaxResult.success(drpProductionPlanService.listProductionDishesSum(productionPlanId));
   }

   @PostMapping({"/list/production-dishes/sumByPlanIds"})
   @ApiOperation("获取生产计划菜品详情汇总")
   public AjaxResult listProductionDishesSumByPlanIds(@RequestBody @Valid List<String> productionPlanIdList) {
      return AjaxResult.success(drpProductionPlanService.listProductionDishesSumByPlanIds(productionPlanIdList));
   }

   @PostMapping({"/save/reserve-rate"})
   @ApiOperation("存储预留比例值")
   public AjaxResult saveReserveRate(@RequestBody @Valid List<DrpReserveRateSaveDTO> content) {
      this.drpProductionPlanService.saveReserveRate(content);
      return AjaxResult.success();
   }

//   @PostMapping({"/merge/generate/pur-plan"})
//   @ApiOperation("生产计划合并生成采购计划")
//   public LeResponse<String> mergeGeneratePurPlan(@RequestBody @Valid LeRequest<List<String>> leRequest) {
//      if (CollUtil.isEmpty((Collection)leRequest.getContent())) {
//         throw new LeException("请选择生产计划！");
//      } else {
//         String purPlanId = this.drpProductionPlanService.mergeGeneratePurPlan((List)leRequest.getContent());
//         return LeResponse.succ(purPlanId);
//      }
//   }

   @PostMapping({"/getMenuMaterialByPlanIdList"})
   @ApiOperation("获取生产计划菜品原料重量及预留比例")
   public AjaxResult getMenuMaterialByPlanIdList(@RequestBody @Valid List<String> planIdList) {
      return AjaxResult.success(drpProductionPlanService.getMenuMaterialByPlanIdList(planIdList));
   }
}
