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

import com.bonus.canteen.core.supply.purchase.domain.param.*;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpPurMaterialPageVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpPurchasePlanAddVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpPurchasePlanPageVO;
import com.bonus.canteen.core.supply.purchase.service.DrpPurchasePlanDisassemblyService;
import com.bonus.canteen.core.supply.purchase.service.DrpPurchasePlanService;
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/drppurchaseplan"})
@Api(
   value = "ffc_采购计划",
   tags = {"ffc_采购计划"}
)
public class DrpPurchasePlanController extends BaseController {
   private static final Logger log = LoggerFactory.getLogger(DrpPurchasePlanController.class);
   @Autowired
   @Lazy
   private DrpPurchasePlanService drpPurchasePlanService;
   @Autowired
   @Lazy
   private DrpPurchasePlanDisassemblyService drpPurchasePlanDisassemblyService;

   @PostMapping({"/page/material-info"})
   @ApiOperation("后台通用-获取货品信息")
   public TableDataInfo pageMaterialInfo(@RequestBody DrpPurMaterialInfoPageDTO content) {
      startPage();
      return getDataTable(this.drpPurchasePlanService.pageMaterialInfo(content));
   }

   @PostMapping({"/add/save"})
   @ApiOperation("新增/编辑采购计划保存")
   public AjaxResult addSaveDrpPurchasePlan(@RequestBody @Valid DrpPurchasePlanSaveDTO content) {
      DrpPurchasePlanAddVO result = this.drpPurchasePlanService.addSaveDrpPurchasePlan(content);
      return AjaxResult.success(result);
   }

   @PostMapping({"/add/commit"})
   @ApiOperation("新增/编辑采购计划提交")
   public AjaxResult addCommitDrpPurchasePlan(@RequestBody @Valid DrpPurchasePlanSaveDTO content) {
      DrpPurchasePlanAddVO result = this.drpPurchasePlanService.addCommitDrpPurchasePlan(content);
      return AjaxResult.success(result);
   }

   @PostMapping({"/page"})
   @ApiOperation("分页获取采购计划")
   public TableDataInfo getDrpPurchasePlanPage(@RequestBody DrpPurchasePlanPageDTO content) {
      startPage();
      List<DrpPurchasePlanPageVO> resultPage = this.drpPurchasePlanService.getDrpPurchasePlanPage(content);
      return getDataTable(resultPage);
   }

   @PostMapping({"/materialCount"})
   @ApiOperation("采购计划货品数量")
   public AjaxResult materialCount(@RequestBody @Valid DrpPurchasePlanPageDTO content) {
      return AjaxResult.success(this.drpPurchasePlanService.materialCount(content));
   }

   @PostMapping({"/detail/{planId}"})
   @ApiOperation("采购计划详情")
   public AjaxResult detail(@PathVariable("planId") String planId) {
      return AjaxResult.success(this.drpPurchasePlanService.detail(planId));
   }

//   @PostMapping({"/mergeDetail"})
//   @ApiOperation("采购计划合并详情")
//   public DrpPurchasePlanMergeVO mergeDetail(@RequestBody @Valid LeRequest<List<String>> leRequest) {
//      return this.drpPurchasePlanService.mergeDetail((List)leRequest.getContent());
//   }

   @PostMapping({"/syncPurchasePrice"})
   @ApiOperation("同步货品价格")
   public AjaxResult syncPurchasePrice(@RequestBody @Valid DrpSyncPurchasePriceDTO content) {
      return AjaxResult.success(this.drpPurchasePlanService.syncPurchasePrice(content.getMaterialIdList(), content.getCanteenId()));
   }

   @PostMapping({"/remove/{planId}"})
   @ApiOperation("删除采购计划")
   public AjaxResult removeByPlanId(@PathVariable String planId) {
      this.drpPurchasePlanService.removeByPlanId(planId);
      return AjaxResult.success();
   }

//   @PostMapping({"/send/email"})
//   @ApiOperation("发送邮件")
//   public AjaxResult sendEmail(@RequestBody @Valid DrpPurSendEmailDTO content) {
//      return AjaxResult.success(this.drpPurchasePlanService.sendEmail(content));
//   }

   @PostMapping({"/page/material"})
   @ApiOperation("采购单分页获取原料")
   public TableDataInfo pageMaterial(@RequestBody DrpPurMaterialPageDTO content) {
      startPage();
      List<DrpPurMaterialPageVO> resultPage = this.drpPurchasePlanService.pageMaterial(content);
      return getDataTable(resultPage);
   }

   @PostMapping({"/generate/order-goods"})
   @ApiOperation("采购生成采购订单")
   public AjaxResult generateOrderGoods(@RequestBody DrpPurGenerateOrderGoodsDTO content) {
      String orderGoodsId = this.drpPurchasePlanService.generateOrderGoods(content);
      return AjaxResult.success(orderGoodsId);
   }

   @PostMapping({"/generate/inquiry"})
   @ApiOperation("采购生成询价单")
   public AjaxResult generateInquiry(@RequestBody DrpPurGenerateInquiryDTO content) {
      String inquiryId = this.drpPurchasePlanService.generateInquiry(content);
      return AjaxResult.success(inquiryId);
   }

//   @PostMapping({"/apply/approval"})
//   @ApiOperation("采购审批申请")
//   public void applyApproval(@RequestBody @Valid LeRequest<String> leRequest) {
//      String planId = (String)leRequest.getContent();
//      this.drpPurchasePlanService.applyApproval(planId);
//   }

   @PostMapping({"/list/pur-detail/inventory"})
   @ApiOperation("获取采购明细的仓库库存")
   public AjaxResult listPurDetailInventory(@RequestBody @Valid MaterialInventoryDTO content) {
      return AjaxResult.success(this.drpPurchasePlanService.listPurDetailInventory(content));
   }

   @PostMapping({"/list/detail"})
   @ApiOperation("根据采购单号获取采购明细")
   public AjaxResult listPurDetail(@RequestBody @Valid String planId) {
      return AjaxResult.success(this.drpPurchasePlanService.listPurDetail(planId));
   }

   @PostMapping({"/generate/order"})
   @ApiOperation("采购计划拆解生成采购订单")
   public AjaxResult purGenerateOrder(@RequestBody @Valid List<DrpPurGenerateOrderDTO> content) {
      this.drpPurchasePlanService.purGenerateOrder(content);
      return AjaxResult.success();
   }

   @PostMapping({"/generate/dismantle/inquiry"})
   @ApiOperation("采购计划拆解生成采购询价单")
   public AjaxResult purGenerateInquiry(@RequestBody @Valid List<DrpPurDismantleGenerateInquiryDTO> content) {
      this.drpPurchasePlanService.purGenerateInquiry(content);
      return AjaxResult.success();
   }

   @PostMapping({"/generate/dismantle/inquiryByGoods"})
   @ApiOperation("采购计划拆解生成采购询价单")
   public AjaxResult purGenerateInquiryByGoods(@RequestBody @Valid DrpPurDismantleGenerateInquiryDTO content) {
      this.drpPurchasePlanService.purGenerateInquiryByGoods(content);
      return AjaxResult.success();
   }

//   @PostMapping({"/approve/agree"})
//   @ApiOperation("采购计划-审批同意")
//   public void purPlanApproveAgree(@RequestBody @Valid LeRequest<PurPlanApproveDTO> leRequest) {
//      PurPlanApproveDTO content = (PurPlanApproveDTO)leRequest.getContent();
//      this.drpPurchasePlanService.purPlanApproveAgree(content);
//   }

//   @PostMapping({"/approve/reject"})
//   @ApiOperation("采购计划-审批拒绝")
//   public void purPlanApproveReject(@RequestBody @Valid LeRequest<PurPlanApproveDTO> leRequest) {
//      PurPlanApproveDTO content = (PurPlanApproveDTO)leRequest.getContent();
//      this.drpPurchasePlanService.purPlanApproveReject(content);
//   }

//   @PostMapping({"/more-pur-plan/merge-one"})
//   @ApiOperation("采购计划-多条采购计划合并生成一个采购计划")
//   public DrpPurchasePlanAddVO morePurPlanMergeOne(@RequestBody @Valid LeRequest<DrpMorePurPlanMergeOneDTO> leRequest) {
//      DrpMorePurPlanMergeOneDTO content = (DrpMorePurPlanMergeOneDTO)leRequest.getContent();
//      return this.drpPurchasePlanService.morePurPlanMergeOne(content);
//   }

//   @PostMapping({"/import/excel"})
//   @RequiresAuthentication
//   @ApiOperation("excel导采购计划功能")
//   public LeResponse<String> importExcel(@RequestParam("file") MultipartFile excel) {
//      if (ObjectUtil.isNull(excel)) {
//         return LeResponse.fail(I18n.getMessage("drp.select-import-file", new Object[0]));
//      } else {
//         this.drpPurchasePlanService.importExcel(excel);
//         return LeResponse.succ();
//      }
//   }

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

   @PostMapping({"/mergeDetailByPlanId"})
   @ApiOperation("采购计划详情")
   public AjaxResult planMergeDetail(@RequestBody @Valid List<String> content) {
      return AjaxResult.success(this.drpPurchasePlanDisassemblyService.planMergeDetail(content));
   }

   @PostMapping({"/deduction/inventory/{areaId}"})
   @ApiOperation("是否可以扣减库存")
   public AjaxResult deductionInventory(@PathVariable("areaId") Long areaId) {
      return AjaxResult.success(this.drpPurchasePlanService.deductionInventory(areaId));
   }

   @PostMapping({"/direct/generate/inquiry/{planId}"})
   @ApiOperation("直接生成采购询价单")
   public AjaxResult directGenerateInquiry(@PathVariable("planId") String planId) {
      String inquiryId = this.drpPurchasePlanDisassemblyService.directGenerateInquiry(planId);
      return AjaxResult.success(inquiryId);
   }

   @PostMapping({"/direct/generate/order/{planId}"})
   @ApiOperation("直接生成采购订单")
   public AjaxResult directGenerateOrder(@PathVariable String planId) {
      String inquiryId = this.drpPurchasePlanDisassemblyService.directGenerateOrder(planId);
      return AjaxResult.success(inquiryId);
   }
}
