package com.cim.idm.service.Impl;

import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.data.EventInfo;
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 org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@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);
	}
}
