package com.cim.idm.service.Impl;

import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.mwmsextend.checkplan.CheckPlanServiceProxy;
import com.cim.idm.mwmsextend.checkplan.management.data.CheckPlan;
import com.cim.idm.mwmsextend.checkplan.management.data.CheckPlanKey;
import com.cim.idm.mwmsextend.checkrecord.CheckRecordServiceProxy;
import com.cim.idm.mwmsextend.checkrecord.management.data.CheckRecord;
import com.cim.idm.mwmsextend.checkrecord.management.data.CheckRecordKey;
import com.cim.idm.service.CheckPlanService;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.info.CreateInfo;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CheckPlanServiceImpl implements CheckPlanService {

	public void PDACreateCheckRecord(EventInfo eventInfo,String planDate,String checkPlanName,
			String materialPackingName,String actLocation,String sysLocation,String siteName,String erpLocation,
			String materialQuantity,String materialActQuantity,String checkTimeKey,String checkState,String lastAction,String materialSpecName,String checkMaterialType,
			String checkPackingType,String moveFlag)throws Exception{
		
		try {
			List<CheckRecord> createInfoList = new ArrayList<CheckRecord>();
			if (checkPlanName != null)
			{
				CheckRecordKey createInfo = new CheckRecordKey();
				
				Map<String, Object> bindMap = new HashMap<String, Object>();

				createInfo.setPlanDate(planDate);
				createInfo.setCheckPlanName(checkPlanName);
				createInfo.setSiteName(siteName);
				createInfo.setMaterialPackingName(materialPackingName);
				createInfo.setActLocation(actLocation);
				createInfo.setCheckMaterialType(checkMaterialType);
				createInfo.setCheckPackingType(checkPackingType);

				CheckRecord checkRecord=new CheckRecord();
				checkRecord.setKey(createInfo);
				checkRecord.setSysLocation(sysLocation);
				checkRecord.setOperatorId(eventInfo.getEventUser());
				checkRecord.setCheckTimeKey(checkTimeKey);
				checkRecord.setCheckState(checkState);
				checkRecord.setLastAction(lastAction);
				checkRecord.setMaterialQuantity(Float.parseFloat(materialQuantity));
				checkRecord.setMaterialActQuantity(Float.parseFloat(materialActQuantity));
				checkRecord.setMaterialSpecName(materialSpecName);
				checkRecord.setErpLocation(erpLocation);
				checkRecord.setActLocation(actLocation);
				checkRecord.setLastEventName("PDACreate");
				checkRecord.setLastEventUser(eventInfo.getEventUser());
				checkRecord.setLastEventTime(eventInfo.getEventTime());
				checkRecord.setLastEventComment(eventInfo.getEventComment());


				createInfoList.add(checkRecord);
				
				CheckRecordServiceProxy.getCheckRecordService().insert(createInfoList);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw new GlobalException(ex.toString());
		}
	}
	
	/*
	*PDA盘点 修复
	*/
	public AjaxResult PDACreateCheckRecord1(EventInfo eventInfo, String planDate, String checkPlanName,
											String materialPackingName, String actLocation, String sysLocation, String siteName, String erpLocation,
											String materialQuantity, String materialActQuantity, String checkTimeKey, String checkState, String lastAction, String materialSpecName, String checkMaterialType,
											String checkPackingType, String moveFlag)throws Exception{
		
		try {
			List<CheckRecord> createInfoList = new ArrayList<CheckRecord>();
			if (checkPlanName != null)
			{
				CheckRecordKey createInfo = new CheckRecordKey();
				
				Map<String, Object> bindMap = new HashMap<String, Object>();

				createInfo.setPlanDate(planDate);
				createInfo.setCheckPlanName(checkPlanName);
				createInfo.setSiteName(siteName);
				createInfo.setMaterialPackingName(materialPackingName);
				createInfo.setActLocation(actLocation);
				createInfo.setCheckMaterialType(checkMaterialType);
				createInfo.setCheckPackingType(checkPackingType);

				CheckRecord checkRecord=new CheckRecord();
				checkRecord.setKey(createInfo);
				checkRecord.setSysLocation(sysLocation);
				checkRecord.setOperatorId(eventInfo.getEventUser());
				checkRecord.setCheckTimeKey(checkTimeKey);
				checkRecord.setCheckState(checkState);
				checkRecord.setLastAction(lastAction);
				checkRecord.setMaterialQuantity(Float.parseFloat(materialQuantity));
				checkRecord.setMaterialActQuantity(Float.parseFloat(materialActQuantity));
				checkRecord.setMaterialSpecName(materialSpecName);
				checkRecord.setErpLocation(erpLocation);
				checkRecord.setActLocation(actLocation);
				checkRecord.setLastEventName("PDACreate");
				checkRecord.setLastEventUser(eventInfo.getEventUser());
				checkRecord.setLastEventTime(eventInfo.getEventTime());
				checkRecord.setLastEventComment(eventInfo.getEventComment());


				createInfoList.add(checkRecord);
				
				CheckRecordServiceProxy.getCheckRecordService().insert(createInfoList);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
//			throw new GlobalException(ex.toString());
			return AjaxResult.me().setSuccess(false).setMessage(ex.toString()).setResultObj(null).setErrorCode(0);
		}

		return AjaxResult.me().setSuccess(true).setMessage("添加成功").setErrorCode(1);
	}
	public AjaxResult CreateCheckPlanListCreate(
			EventInfo eventInfo, List<LinkedHashMap<String,String>> boxList,
			String planDate,String checkPlanName,String moveFlag, String siteName)throws Exception{
		try {
			List<CheckRecord> createInfoList = new ArrayList<CheckRecord>();
			//CheckPlan[] checkPlanArray = new CheckPlan[]();
			List<CheckPlan> checkPlanList = new ArrayList<>();
			//String erpFactory = "";
			//String planDate = "";
			//String siteName = "";
			String materialPackingName = "";
			String sysLocation = "";
			String planType = "";
			//String planTypeName = "";
			//String planCondition = "";
			String actLocation = "";
			String erpLocation = "";
			String materialQuantity = "";
			String materialSpecName = "";
			String boxState = "";
			//String moveFlag = "";
			String checkPlanState = "";
			String checkMaterialType = "";
			String checkPackingType = "";
			String lastAction = "";
			String eventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
			for (int i = 0; i < boxList.size(); i++){
				LinkedHashMap<String, String> linkedHashMap = boxList.get(i);
				materialPackingName = linkedHashMap.get("MATERIALPACKINGNAME");
				sysLocation = linkedHashMap.get("SYSLOCATION");
				erpLocation = linkedHashMap.get("ERPLOCATION");
				materialQuantity = linkedHashMap.get("MATERIALQUANTITY");
				materialSpecName = linkedHashMap.get("MATERIALSPECNAME");
				lastAction = linkedHashMap.get("LASTACTION");
				boxState = linkedHashMap.get("BOXSTATE");
				planType = linkedHashMap.get("PLANTYPE");
				if (materialPackingName != null)
				{
					CheckPlan checkPlan = new CheckPlan();
					CheckPlanKey checkPlanKey = new CheckPlanKey();
					checkPlanKey.setPlanDate(planDate);
					checkPlanKey.setCheckPlanName(checkPlanName);
					checkPlanKey.setSiteName(siteName);
					checkPlanKey.setMaterialPackingName(materialPackingName);
					checkPlan.setKey(checkPlanKey);
					checkPlan.setPlanType(planType);
					checkPlan.setPlanTypeName(planType);
					checkPlan.setSysLocation(sysLocation);
					checkPlan.setCheckPlanState("Created");
					checkPlan.setLastAction(lastAction);
					checkPlan.setMaterialQuantity(Float.parseFloat(materialQuantity));
					checkPlan.setMaterialSpecName(materialSpecName);
					checkPlan.setErpLocation(erpLocation);
					checkPlan.setBoxState(boxState);
					checkPlan.setMoveFlag(moveFlag);
					checkPlan.setcheckMPackingType("");
					checkPlan.setcheckMaterialType("");
					checkPlan.setLastEventName(eventInfo.getEventName());
					checkPlan.setLastEventUser(eventInfo.getEventUser());
					checkPlan.setLastEventTime(eventInfo.getEventTime());
					checkPlan.setLastEventComment(eventInfo.getEventComment());
					checkPlan.setLastEventTimeKey(eventTimeKey);
					checkPlanList.add(checkPlan);
				}
			}
			if(null != checkPlanList && checkPlanList.size() > 0){
				CheckPlanServiceProxy.getCheckPlanService().insert(checkPlanList);
			}
		}
		catch (Exception e) {
			//log.info("插入盘点单表数据失败，更新大数据时失败 ==========> ");
			e.printStackTrace();
			return AjaxResult.me().setSuccess(false).setMessage(e.toString()).setResultObj(null).setErrorCode(400);
		}

		return AjaxResult.me().setSuccess(true).setMessage("盘点计划创建完成").setErrorCode(200);
	}
}
