package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.model.MaterialUndoDto;
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;

/**
 * 物料冲销控制器
 * @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();
		//根据物料凭证校验物料状态是否一致
		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 ("Z001".equals(inv_TYPE) || "Z005".equals(inv_TYPE) || "Z006".equals(inv_TYPE) || "Z007".equals(inv_TYPE) || "Z008".equals(inv_TYPE)) {
			materialUndoServiceImpl.rkUndo(undoId, userId);  // 入库
		} else if ("Z002".equals(inv_TYPE) || "Z003".equals(inv_TYPE) || "Z004".equals(inv_TYPE)) {
			materialUndoServiceImpl.cprkUndo(undoId, userId);// 成品入库
		} else if (inv_TYPE.contains("ZLF")) {
			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 {
			return AjaxResult.me().setSuccess(false).setMessage("单据类型不支持冲销");
		}

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