package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.model.MaterialUndoDto;
import com.cim.idm.mwmsextend.materialpacking.service.MaterialPackingServiceImpl;
import com.cim.idm.service.Impl.MaterialUndoServiceImpl;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 物料冲销控制器
 * @author ZXYGY17
 *
 */
@RestController
@RequestMapping("/api/MaterialUndo")
@EnableAutoConfiguration
public class MaterialUndoController {

	CommonUtils untils=new CommonUtils();

	@Autowired
	private MaterialUndoServiceImpl materialUndoServiceImpl;
	
	@RequestMapping(value = "/commitUndo", method = RequestMethod.POST)
	public AjaxResult commitUndo(@RequestBody JSONObject in ) throws Exception {

		MaterialUndoDto undoDto = JSON.toJavaObject(in, MaterialUndoDto.class);
		String undoId = undoDto.getUndoId();
		String userId = undoDto.getUserId();
		String flag = undoDto.getFlag();
		String inv_TYPE = undoDto.getINV_TYPE() == null ? "" : undoDto.getINV_TYPE();
		String lastEventName = undoDto.getLastEventName();
		String commitDate = undoDto.getCommitDate();

		String preErpLocation = undoDto.getPreErpLocation();
		String preLocationName = undoDto.getPreLocationName();
		String locationName = undoDto.getErpLocation();
		String erpLocation = undoDto.getLocationName();

		//根据物料凭证校验物料状态是否一致
//		try {
//			Boolean checkUndoCondition = untils.CheckUndoCondition(undoId);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return AjaxResult.me().setSuccess(false).setMessage(e.toString());
//		}
		//校验单据类型，根据单据类型判断逻辑
//		switch (inv_TYPE) {
//		case "45":
//			materialUndoServiceImpl.rkUndo(undoId, userId);
//			break;
//		case "46":
//			materialUndoServiceImpl.cprkUndo(undoId, userId);
//			break;
//		case "4C":
//			materialUndoServiceImpl.ckUndo(undoId, userId);
//		case "4Y" :
//			materialUndoServiceImpl.ckUndo(undoId, userId);
//		case "4I" :
//			materialUndoServiceImpl.ckUndo(undoId, userId);
//			break;
//		default :
//			return AjaxResult.me().setSuccess(false).setMessage("单据类型不支持冲销");
//		}

		if ("Z002".equals(inv_TYPE) || "Z003".equals(inv_TYPE) || "Z004".equals(inv_TYPE) || "Z001".equals(inv_TYPE) || "Z005".equals(inv_TYPE) || "Z006".equals(inv_TYPE) || "Z007".equals(inv_TYPE)) {
//			materialUndoServiceImpl.rkUndo(undoId, userId);  // 入库
			materialUndoServiceImpl.cgrkUndo(undoId, userId); // 采购入库
		}
		else if (lastEventName.contains("FGStockIn")) {
			materialUndoServiceImpl.cprkUndo(undoId, userId);// 成品入库
		}
		else if ("Z008".equals(inv_TYPE)) { // 出库冲销
			materialUndoServiceImpl.ckUndo(undoId, userId);
		} else if (inv_TYPE.contains("ZLR")) {
			materialUndoServiceImpl.ckUndo(undoId, userId);
		}else if(inv_TYPE.contains("ZK"))  {  // 转库
			materialUndoServiceImpl.zkUndo(undoId, userId);
		}else if(lastEventName.contains("自动转库"))  {  // 自动转库
			materialUndoServiceImpl.zdZkUndo(undoId, userId,preErpLocation,preLocationName);
		}else if(lastEventName.contains("质检结果"))  {  // 质检结果
			materialUndoServiceImpl.zjJgUndo(undoId, userId);
		}else if(inv_TYPE.contains("NLCC") && lastEventName.contains("调拨出库"))  {  // 调拨出库
			materialUndoServiceImpl.orderUndo(undoId, userId,commitDate);
		} else if (inv_TYPE.contains("ZLF")) { // 销售发货出库
			materialUndoServiceImpl.orderUndo(undoId, userId,commitDate);
		} else if (lastEventName.contains("无来源转库")) { // 备货出库转现场仓，无来源转库
			materialUndoServiceImpl.wlyZkUndo(undoId, userId,preErpLocation,preLocationName);
		} else if (lastEventName.contains("生产领料出库")) { // 生产领料直接出库
			materialUndoServiceImpl.ckUndo(undoId, userId);
		} else {
			return AjaxResult.me().setSuccess(false).setMessage("单据类型不支持冲销");
		}

		return AjaxResult.me().setSuccess(true).setMessage("冲销成功");
	}
}
