package com.cim.idm.controller;

import com.alibaba.fastjson.JSONObject;
import com.cim.idm.constants.delivery.DeliveryStateEnums;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.model.dto.delivery.DeliveryDelDto;
import com.cim.idm.model.dto.delivery.DeliveryDto;
import com.cim.idm.model.dto.delivery.StoreDetailDto;
import com.cim.idm.model.po.MaterialReceiveRequest;
import com.cim.idm.model.dto.PageDto;
import com.cim.idm.model.po.delivery.MaterialDelivery;
import com.cim.idm.model.po.delivery.MaterialDeliveryReceive;
import com.cim.idm.service.IDeliveryService;
import com.cim.idm.utils.AjaxResult;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/delivery")
@EnableAutoConfiguration
public class DeliveryController {

	@Resource
	private IDeliveryService deliveryService;

	/**
	 * 创建入库单
	 */
	@PostMapping(value = "/create")
	public AjaxResult createDelivery(@RequestBody DeliveryDto dto) {
		if (deliveryService.createDelivery(dto) > 0) {
			return AjaxResult.me().setSuccess(true).setMessage("创建成功");
		}
		return AjaxResult.me().setErrorCode(-1).setMessage("创建失败");
	}

	/**
	 * 获取采购单
	 */
	@PostMapping(value = "/getReceiveList")
	public AjaxResult getReceiveList(@RequestBody PageDto in) {
		return AjaxResult.me().setSuccess(true).setResultObj(deliveryService.getReceiveList(in));
	}

	/**
	 * 获取入库详情
	 * 到货单/销售采购单，根据物料清单，创建批次，进行入库
	 */
	@PostMapping(value = "/getStoreDetail")
	public AjaxResult getStoreDetail(@RequestBody JSONObject in) {
		Map<String, String> params = (Map<String, String>) in.get("params");
		// siteName
		StoreDetailDto storeDetailDto = new StoreDetailDto();
		storeDetailDto.setSiteName(params.get("SITENAME"));
		storeDetailDto.setDocumentName(params.get("RECEIVEREQUESTNAME"));
		// 单据名称集合
		List<String> nameList = new ArrayList<>();
		String deliveryName = null;
		MaterialDelivery byKey = deliveryService.getByKey(storeDetailDto);
		// 到货单
		if (Objects.nonNull(byKey)) {
			deliveryName = byKey.getDeliveryName();
			// 到库单（找出该单据下的所有的采购单）
			nameList = deliveryService.getReceiveByDelivery(storeDetailDto)
					.stream().map(MaterialDeliveryReceive::getReceiveRequestName).collect(Collectors.toList());
		} else {
			// 采购单
			MaterialReceiveRequest receiveByKey = deliveryService.getReceiveByKey(storeDetailDto);
			if (Objects.nonNull(receiveByKey)) {
				nameList.add(storeDetailDto.getDocumentName());
			}
		}
		// 异常
		if (nameList.isEmpty()) {
			throw new GlobalException("不存在该单据");
		}
		// 获取采购单明细及批次
		return AjaxResult.me().setSuccess(true).setResultObj(
				deliveryService.getStoreDetail(storeDetailDto.getSiteName(), nameList, deliveryName));
	}

	/**
	 * 获取采购订单详情
	 */
	@PostMapping(value = "/getReceiveDetail")
	public AjaxResult getReceiveDetail(@RequestBody JSONObject in) {
		List<String> nameList = (List<String>) in.get("nameList");
		return AjaxResult.me().setSuccess(true).setResultObj(deliveryService.getReceiveDetail(nameList));
	}

	/**
	 * 获取到货单
	 */
	@PostMapping(value = "/getDeliveryList")
	public AjaxResult getDeliveryList(@RequestBody PageDto in) {
		return AjaxResult.me().setSuccess(true).setResultObj(deliveryService.getDeliveryList(in));
	}

	/**
	 * 删除到货单
	 */
	@PostMapping(value = "/delDelivery")
	public AjaxResult delDelivery(@RequestBody DeliveryDelDto deliveryDelDto) {
		StoreDetailDto storeDetailDto = new StoreDetailDto();
		storeDetailDto.setDocumentName(deliveryDelDto.getDeliveryName());
		storeDetailDto.setSiteName(deliveryDelDto.getSiteName());
		// 校验
		MaterialDelivery byKey = deliveryService.getByKey(storeDetailDto);
		if (Objects.isNull(byKey)) {
			throw new GlobalException("不存在到货单");
		}
		if (!byKey.getDeliveryState().equals(DeliveryStateEnums.CREATE.getCode())) {
			throw new GlobalException("到货单进行中或已结束，不允许删除");
		}
		if (deliveryService.delDelivery(deliveryDelDto) > 0) {
			return AjaxResult.me().setSuccess(true).setResultObj("删除成功");
		}
		return AjaxResult.me().setErrorCode(-1).setMessage("删除失败");
	}
}
