package com.uas.eis.serviceImpl;

import com.uas.eis.core.config.SpObserver;
import com.uas.eis.dao.BaseDao;
import com.uas.eis.dao.SqlRowList;
import com.uas.eis.entity.ErrorMessage;
import com.uas.eis.entity.HelperType;
import com.uas.eis.exception.ApiSystemException;
import com.uas.eis.sdk.entity.ApiResult;
import com.uas.eis.service.MESDataService;
import com.uas.eis.service.MESHelperService;
import com.uas.eis.utils.*;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.awt.TimedWindowEvent;

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

@Service
public class MESHelperServiceImpl implements MESHelperService {

	@Autowired
	private BaseDao baseDao;

	@Value("${spring.datasource.username}")
	private String username;

	String Master;

	String accessKey1="";

	@Override
	public Map<Object,Object> DLLMain(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		accessKey1=accessKey;
		SpObserver.putSp(AE_MASTER);
		Master=AE_MASTER;
		Map<Object, Object> map = BaseUtil.parseFormStoreToMap(data);
		//解析map
		if(!StringUtil.hasText(map.get("type"))){
			throw new ApiSystemException(new ApiResult(ErrorMessage.BUSINESS_TYPENULL,requestId));
		}
		if(!StringUtil.hasText(map.get("detail"))){
			throw new ApiSystemException(new ApiResult(ErrorMessage.BUSINESS_DATAILLEGAL,requestId));
		}
		String type = map.get("type").toString();
		JSONObject json = JSONObject.fromObject(map.get("detail"));
		HelperType helperType = HelperType.getTypeByCode(type);
		if(null == helperType){
			throw new ApiSystemException(new ApiResult(ErrorMessage.BUSINESS_TYPEILLEGAL,requestId));
		}
		int err = 0;
		//返回值
		Map<Object, Object> remap = new HashMap<Object, Object>();
		switch(helperType){
			case CheckRoutePassed:
				remap = CheckRoutePassed(json);
				break;
			case CheckUserAndResourcePassed:
				remap = CheckUserAndResourcePassed(json);
				break;
			case GetAddressRangeByMO:
				remap = GetAddressRangeByMO(json);
				break;
			case GetMEIOrNetCodeRange:
				remap = GetMEIOrNetCodeRange(json);
				break;
			case GetRcardMOInfo:
				remap = GetRcardMOInfo(json);
				break;
			case GetMobileAllInfo:
				remap = GetMobileAllInfo(json);
				break;
			case SetAddressInfo:
				remap = SetAddressInfo(json);
				break;
			case GetMaster:
				remap = GetMaster(json);
				break;
			case SetMobileData:
				remap = SetMobileData(json);
				break;
			case SetIMEIInfo:
				remap = SetIMEIInfo(json);
				break;
			case SetTestDetail:
				remap = SetTestDetail(json);
				break;
			case SetPcbaData:
				remap = SetPcbaData(json);
				break;
			case GetInfoByMaterial:
				remap = GetInfoByMaterial(json);
				break;
			case GoMo:
				remap = GoMo(json);
				break;
			case GetMakeInfoBySN:
				remap = GetMakeInfoBySN(json);
				break;
			case GetMakeInfoByProd:
				remap = GetMakeInfoByProd(json);
				break;
			case GetProdInfo:
				remap = GetProdInfo(json);
				break;
			case GetPSN:
				remap = GetPSN(json);
				break;
			case GetMakeRelation:
				remap = GetMakeRelation(json);
				break;
			case GetFeeReason:
				remap = GetFeeReason(json);
				break;
			case GetMakeSN:
				remap = GetMakeSN(json);
				break;
			case SetFeeReason:
				remap = SetFeeReason(json);
				break;
			case WriteMakeInfo:
				remap = WriteMakeInfo(json);
				break;
			case GetMakeInfo:
				remap = GetMakeInfo(json);
				break;
			default:
				err = -1;
				break;
		}
		if(err == -1){
			throw new ApiSystemException(new ApiResult(ErrorMessage.BUSINESS_TYPEILLEGAL,requestId));
		}
		SpObserver.putSp(username);
		return remap;
	}

	private Map<Object,Object> GetMakeInfo(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iSN=null;
		String oErrMessage=null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		Object makecode=baseDao.getFieldDataByCondition(Master+".makeserial","ms_makecode","ms_id=(select max(ms_id) from "+Master+".makeserial where ms_sncode='"+iSN+"')");
		if(makecode==null){
			oErrMessage = "SN不存在";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		SqlRowList rs=baseDao.queryForRowSet("select mid_item,mid_value from "+Master+".makeitem left join "+Master+".makeitemdetail on mi_id=mid_miid " +
				"where mi_makecode='"+makecode+"' and mid_item is not null");
		while(rs.next()){
			rmap.put(rs.getString("mid_item"),rs.getString("mid_value"));
		}
		return rmap;
    /*if(rs.next()){

    }else{
      oErrMessage = "SN未查询到关联信息";
      rmap.put("oErrMessage",oErrMessage);
      return rmap;
    }*/
	}

	private Map<Object,Object> WriteMakeInfo(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String oErrMessage=null;
		if(map.get("iMakeCode")==null){
			oErrMessage = "工单号不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String MO=map.get("iMakeCode").toString();
		String Item=map.get("Item").toString();

		SqlRowList rs=baseDao.queryForRowSet("select * from "+Master+".makeitem where mi_makecode='"+MO+"'");
		//如果已经存在工单了
		if(rs.next()){
			String[] str=Item.split("\\|");
			String mi_id=rs.getString("mi_id");
			baseDao.execute("delete from "+Master+".makeitemdetail where mid_miid='"+mi_id+"'");
			for (int i=0;i<str.length;i++) {
				Object key = str[i].split("#")[0];
				Object value = str[i].split("#")[1];
				baseDao.execute("insert into "+Master+".makeitemdetail(mid_id,mid_miid,mid_item,mid_value)" +
						"values("+Master+".makeitemdetail_seq.nextval,'"+mi_id+"','"+key.toString()+"','"+value.toString()+"')");
			}
		}else {
			int mi_id = baseDao.getSeqId(Master+".makeitem_seq");
			String[] str=Item.split("\\|");
			baseDao.execute("insert into "+Master+".makeitem(mi_id,mi_makecode,mi_indate)values("+mi_id+",'"+MO+"',sysdate)");
			for (int i=0;i<str.length;i++) {
				Object key = str[i].split("#")[0];
				Object value = str[i].split("#")[1];
				baseDao.execute("insert into "+Master+".makeitemdetail(mid_id,mid_miid,mid_item,mid_value)" +
						"values("+Master+".makeitemdetail_seq.nextval,'"+mi_id+"','"+key.toString()+"','"+value.toString()+"')");
			}
		}
		return rmap;
	}

	private Map<Object,Object> GetMakeSN(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		List<Map<String, Object>> maps = new ArrayList<>();
		String ma_custname="";
		String TIME_BEGIN="";
		String TIME_END="";
		String oErrMessage="";
		if(map.get("MA_CUSTNAME")==null){
			oErrMessage = "客户名称不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if(map.get("TIME_BEGIN")==null){
			oErrMessage = "开始时间不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if(map.get("TIME_END")==null){
			oErrMessage = "结束时间不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		ma_custname=map.get("MA_CUSTNAME").toString();
		TIME_BEGIN=map.get("TIME_BEGIN").toString();
		TIME_END=map.get("TIME_END").toString();
		SqlRowList rs=baseDao.queryForRowSet("select ms_sncode,to_char(ms_enddate,'yyyy-mm-dd hh24:mi:ss')ms_enddate from makeserial left join make on ma_code=ms_makecode where ms_enddate between to_date('"+TIME_BEGIN+"','yyyy-mm-dd') " +
				"and to_date('"+TIME_END+"','yyyy-mm-dd') and ma_custname='"+ma_custname+"'");
		rmap.put("data",rs.getResultList());
		return rmap;
	}

	private Map<Object,Object> GetFeeReason(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String oErrMessage="";
		if(map.get("CT_ID")==null){
			oErrMessage = "单据ID不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		SqlRowList rs=baseDao.queryForRowSet("select ct_code,ct_number_3,ct_varchar50_18,ct_varchar500_1,ct_varchar500_2,ct_varchar500_3,ct_varchar500_4,ct_varchar500_5,ct_number_2,ct_varchar50_27,ct_number_19,ct_varchar50_30,ct_varchar50_25,ct_number_12,ct_number_23,ct_varchar50_34,ct_varchar50_39,ct_varchar50_22,ct_statuscode,ct_date_1,ct_status,ct_recorder,ct_varchar50_10,ct_date_2,ct_id,ct_varchar50_1,ct_varchar50_2,ct_varchar50_3,ct_varchar50_4,ct_varchar50_5,ct_varchar50_6,ct_varchar50_7,ct_varchar50_8,ct_varchar50_11,ct_number_1,ct_varchar50_9,ct_varchar50_12,ct_varchar50_13,ct_varchar50_14,ct_varchar50_15,ct_varchar50_16,ct_caller,ct_date_3,ct_varchar50_17" +
				" from "+Master+".CUSTOMTABLE where ct_id="+map.get("CT_ID") +" and ct_caller='MakeFee'");
		if(rs.next()){
			for(String key:rs.getCurrentMap().keySet()){
				rmap.put(key,rs.getCurrentMap().get(key));
			}
			return rmap;
		}else {
			oErrMessage = "单据ID不存在";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
	}

	private Map<Object,Object> SetFeeReason(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String formstore=map.get("data").toString();
		Map<Object, Object> gstore = BaseUtil.parseFormStoreToMap(formstore);
		String gridSql = SqlUtil.getUpdateSqlByFormStore(gstore,Master+".CUSTOMTABLE", "CT_ID");
		baseDao.execute(gridSql);
		return null;
	}

	private Map<Object,Object> GoMo(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iSN=null;
		String iResCode=null;
		String iMo=null;
		String oErrMessage=null;
		if(map.get("iMo")==null){
			oErrMessage = "工单号不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if(map.get("iResCode")==null){
			oErrMessage = "岗位资源不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		iResCode=map.get("iResCode").toString();
		iMo=map.get("iMo").toString();
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_CHECKSTEPSNANDMACODE", new Object[]{
				String.valueOf(iMo),
				String.valueOf(iResCode),
				String.valueOf(iSN),
				String.valueOf("")
		}, new Integer[]{1, 2, 3, 4}, new Integer[]{5, 6, 7});
		oErrMessage=list.get(2);

		SqlRowList rs = baseDao.queryForRowSet("select ms_status,ms_stepcode,ms_nextstepcode from "+Master+".makeserial where ms_id=(  select max(ms_id) from "+Master+".makeserial where ms_sncode  in ( select '" + iSN + "' from dual union select sn from "+Master+".makesnrelation where beforesn='" + iSN + "' and sn<>' '  union select beforesn from "+Master+".makesnrelation where sn='" + iSN + "' and beforesn<>' '))");
		String ms_status = "";
		String ms_stepcode = "";
		String ms_nextstepcode = "";
		if(rs.next()){
			ms_status= rs.getString("ms_status");
			ms_stepcode=rs.getString("ms_stepcode");
			ms_nextstepcode=rs.getString("ms_nextstepcode");
		}
		String stepcode = GetStepCodeBySource(iResCode);
		if ( oErrMessage == null || (ms_status == "3" && ms_stepcode == stepcode))
		{
			if (ms_status == "3")
			{
				oErrMessage =  null;
			}
		}
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> GetInfoByMaterial(JSONObject map){
		Object AE_MASTER = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_MASTER", "AE_KEY='" + accessKey1 + "'");
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iPCBA=null;
		String oErrMessage=null;
		if(map.get("iPCBA")==null){
			oErrMessage = "主板SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iPCBA=map.get("iPCBA").toString();
		SqlRowList rs=baseDao.queryForRowSet("select nvl(max(ms_id),0)ms_id from "+AE_MASTER+".CRAFTMATERIAL left join "+AE_MASTER+".makeserial on ms_firstsn=cm_firstsn  where cm_barcode='"+iPCBA+"' and nvl(cm_status,0)<>-1 order by cm_id desc");
		if(rs.next()){
			if(!rs.getString("ms_id").equals("0")){
				rs=baseDao.queryForRowSet("select ma_bios,ma_ec,ms_sncode,ma_hardware from "+AE_MASTER+".makeserial left join "+AE_MASTER+".make on ms_makecode=ma_code where ms_id='"+rs.getString("ms_id")+"'");
				if (rs.next()){
					rmap.put("oSN",rs.getString("ms_sncode"));
					rmap.put("oBIOS",rs.getString("ma_bios"));
					rmap.put("oEC",rs.getString("ma_ec"));
					rmap.put("oHardWare",rs.getString("ma_hardware"));
					rmap.put("oMaster",AE_MASTER);
				}
			}else{
				oErrMessage = "主板SN未查询到关联信息";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
			}
		}
		return rmap;
	}

	private Map<Object,Object> CheckRoutePassed(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String iSN=map.get("iSN").toString();
		String iResCode=map.get("iResCode").toString();
		String oErrMessage="";
		if (iSN ==null)
		{
			oErrMessage = "SN不能为空";
		}
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_CHECKSTEPSNANDMACODE", new Object[]{
				String.valueOf(""),
				String.valueOf(iResCode),
				String.valueOf(iSN),
				String.valueOf("")
		}, new Integer[]{1, 2, 3, 4}, new Integer[]{5, 6, 7});
		oErrMessage=list.get(2);
		if(oErrMessage!=null){
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		SqlRowList rs = baseDao.queryForRowSet("select ms_status,ms_stepcode,ms_nextstepcode from "+Master+".makeserial where ms_id=(  select max(ms_id) from "+Master+".makeserial where ms_sncode  in ( select '" + iSN + "' from dual union select sn from "+Master+".makesnrelation where beforesn='" + iSN + "' and sn<>' '  union select beforesn from "+Master+".makesnrelation where sn='" + iSN + "' and beforesn<>' '))");
		String ms_status = "";
		String ms_stepcode = "";
		String ms_nextstepcode = "";
		if(rs.next()){
			ms_status= rs.getString("ms_status");
			ms_stepcode=rs.getString("ms_stepcode");
			ms_nextstepcode=rs.getString("ms_nextstepcode");
		}
		String stepcode = GetStepCodeBySource(iResCode);
		if (ms_nextstepcode != "" && !ms_nextstepcode.equals(stepcode))
		{
			oErrMessage = "当前序列号下一工序" + ms_nextstepcode;
		}
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> CheckUserAndResourcePassed(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage = null;
		if (map.get("iUserCode") == null || map.get("iResCode") == null || map.get("iPassWord") == null)
		{
			oErrMessage = "用户名,密码,岗位资源必须填写";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iUserCode=map.get("iUserCode").toString();
		String iPassWord=map.get("iPassWord").toString();
		String iResCode=map.get("iResCode").toString();
		if (CheckUserLogin(iUserCode, iPassWord))
		{
			String SQL = "select em_code,em_type,em_name from "+Master+".employee where em_code=? ";
			SqlRowList rs = baseDao.queryForRowSet(SQL,  iUserCode);
			if (rs.next())
			{
				String em_name = rs.getString("em_name");
				String em_type = rs.getString("em_type");
				if (iResCode ==null)
				{
					oErrMessage = "岗位资源不允许为空";
					rmap.put("oErrMessage",oErrMessage);
					return rmap;
				}
				if (em_type.equals("admin"))
				{
					if (!baseDao.checkIf(""+Master+".Source", "sc_code='" + iResCode + "' and sc_statuscode='AUDITED'"))
					{
						oErrMessage = "岗位资源编号错误或者未审核！";
					}
				}
				else
				{
					SqlRowList rs1=baseDao.queryForRowSet("select ur_resourcecode from "+Master+".cs$empgroup left join "+Master+".cs$userresource on ur_groupcode=eg_groupcode left join "+Master+".source on ur_resourcecode=sc_code where eg_emcode = '"+ iUserCode + "' and sc_statuscode='AUDITED'");
					//如果存在该编号
					if (rs1.next())
					{
						//判断如果多个岗位资源存在，用户输入的只要在其中就行
						while (rs.next())
						{
							if (!rs.getString("ur_resourcecode").equals("iResCode")){
								oErrMessage = "用户不处于当前资源所属分组!";
							}else
							{
								oErrMessage=null;
								continue;
							}
						}
					}
					else
						oErrMessage = "岗位资源编号错误或者未审核！";
				}
			}
			else
				oErrMessage = "用户不存在!";
		}else{
			oErrMessage = "用户名密码不正确!";
		}
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}
	private Map<Object,Object> GetAddressRangeByMO(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String iSN="";
		String oErrMessage = null;
		String oWIFI = "";
		String oBT = "";
		String oNetCard = "";
		String oCode1 = "";
		String oCode2 = "";
		String oCode3 = "";
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		//判断工单是否归属
		JSONObject map3=new JSONObject();
		map3.put("iSN",iSN);
		Map<Object, Object> map2=GetRcardMOInfo(map3);
		if(map2.get("oErrMessage")!=null){
			oErrMessage=map2.get("oErrMessage").toString();
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_GETADDRESSBYMAKECODE_TOOL", new Object[]{
				String.valueOf(iSN),
				String.valueOf(map2.get("oMoCode").toString()),
		}, new Integer[]{1,2}, new Integer[]{3,4,5,6,7,8,9});
		oWIFI = list.get(0);
		oBT =  list.get(1);
		oNetCard =  list.get(2);
		oCode1 =  list.get(3);
		oCode2 = list.get(4);
		oCode3 =  list.get(5);
		oErrMessage= list.get(6);
		if (oErrMessage == "" || oErrMessage == null || oErrMessage == "null"){
			rmap.put("oWIFI",oWIFI);
			rmap.put("oBT",oBT);
			rmap.put("oNetCard",oNetCard);
			rmap.put("oCode1",oCode1);
			rmap.put("oCode2",oCode2);
			rmap.put("oCode3",oCode3);
		}
		else
			rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> GetMEIOrNetCodeRange(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String iSN="";
		String oErrMessage = null;
		String oIMEI1 = "";
		String oIMEI2 = "";
		String oMEID = "";
		String oNetCode = "";
		String oPSN = "";
		String oID1 = "";
		String oID2 = "";
		String oID3 = "";
		String oID4 = "";
		String oID5 = "";
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if(map.get("iIMEI1")!=null){
			oIMEI1=map.get("iIMEI1").toString();
		}
		if(map.get("iNetCode")!=null){
			oNetCode=map.get("iNetCode").toString();
		}
		iSN=map.get("iSN").toString();
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_GETIMEIORNETCODERANGE", new Object[]{
				String.valueOf(iSN),
				String.valueOf(""),
				String.valueOf(oIMEI1),
				String.valueOf(oNetCode),
		}, new Integer[]{1,2,3,4}, new Integer[]{5,6,7,8,9,10,11,12,13,14});
		oIMEI1 = list.get(0);
		oIMEI2 =  list.get(1);
		oMEID =  list.get(2);
		oNetCode = list.get(3);
		oID1 =  list.get(4);
		oID2 =  list.get(5);
		oID3 =  list.get(6);
		oID4 =  list.get(7);
		oID5 =  list.get(8);
		oErrMessage= list.get(9);
		if (oErrMessage == "" || oErrMessage == null || oErrMessage == "null"){
			rmap.put("oIMEI1",oIMEI1);
			rmap.put("oIMEI2",oIMEI2);
			rmap.put("oMEID",oMEID);
			rmap.put("oNetCode",oNetCode);
			rmap.put("oPSN",oPSN);
			rmap.put("oID1",oID1);
			rmap.put("oID2",oID2);
			rmap.put("oID3",oID3);
			rmap.put("oID4",oID4);
			rmap.put("oID5",oID5);
		}
		else
			rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> GetRcardMOInfo(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		//取MakeProcess表中的执行记录ID最大的一个工单的号码
		String oErrMessage = null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		StringBuffer sql=new StringBuffer();
		sql.append("select max(ms_id)ms_id from "+Master+".makeserial where ms_sncode in (select '" + iSN + "' from dual union select sn from ");
		sql.append(""+Master+".makesnrelation where beforesn='" + iSN + "' and sn<>' '  union select beforesn from "+Master+".makesnrelation where sn='" + iSN + "' and beforesn<>' ')");
		SqlRowList rs = baseDao.queryForRowSet(sql.toString());
		String ms_id="";
		if(rs.next()){
			if(rs.getString("ms_id")==null){
				oErrMessage = "序列号:" + iSN + " 未归属工单";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
			}
			else{
				ms_id= rs.getString("ms_id");
			}
		}
		Object[] oMoCode = baseDao.getFieldsDataByCondition(Master+".MakeSerial", "ms_makecode,ms_prodcode", "ms_id='" + ms_id + "'");
		if (oMoCode != null){
			rmap.put("oMoCode",oMoCode[0]);
			rmap.put("oPrCode",oMoCode[1]);
			oErrMessage= null;
		}
		else
		{
			oErrMessage = "序列号:" + iSN + " 未归属工单";
		}
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}



	private Map<Object,Object> GetMobileAllInfo(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String iSN="";
		String oErrMessage = null;
		String oIMEI1 = "";
		String oIMEI2 = "";
		String oMEID = "";
		String oNetCode = "";
		String oPSN = "";
		String oID1 = "";
		String oID2 = "";
		String oID3 = "";
		String oID4 = "";
		String oID5 = "";
		String oWIFI = "";
		String oBT = "";
		String oCode1 = "";
		String oCode2 = "";
		String oCode3 = "";
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		Object ms_id =baseDao.getFieldDataByCondition(Master+".makeserial", "max(ms_id)", "ms_sncode='" + iSN + "'");
		if(ms_id!=null)
		{
			SqlRowList rs=baseDao.queryForRowSet("select ms_id, ms_mac, ms_bt, ms_meid, ms_netcode, ms_psn, ms_imei1, ms_imei2, ms_imei3, ms_othcode1, ms_othcode2, ms_othcode3, ms_othid1, ms_othid2, ms_othid3 from "+Master+".MakeSerial where ms_id="+ms_id.toString());
			if(rs.next()){
				oWIFI = rs.getString("ms_mac");
				oBT = rs.getString("ms_bt");
				oPSN = rs.getString("ms_psn");
				oNetCode = rs.getString("ms_netcode");
				oMEID = rs.getString("ms_meid");
				oIMEI1 = rs.getString("ms_imei1");
				oIMEI2 = rs.getString("ms_imei2");
				oCode1 = rs.getString("ms_othcode1");
				oCode2 = rs.getString("ms_othcode2");
				oCode3 = rs.getString("ms_othcode3");
				oID1 = rs.getString("ms_othid3");
				oID2 = rs.getString("ms_othid3");
				oID3 = rs.getString("ms_othid3");
			}
		}else
		{
			oErrMessage = "序列号" + iSN + "不存在";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		if (oErrMessage == "" || oErrMessage == null || oErrMessage == "null"){
			rmap.put("oIMEI1",oIMEI1);
			rmap.put("oIMEI2",oIMEI2);
			rmap.put("oMEID",oMEID);
			rmap.put("oNetCode",oNetCode);
			rmap.put("oPSN",oPSN);
			rmap.put("oID1",oID1);
			rmap.put("oID2",oID2);
			rmap.put("oID3",oID3);
			rmap.put("oID4",oID4);
			rmap.put("oID5",oID5);
			rmap.put("oWIFI",oWIFI);
			rmap.put("oBT",oBT);
			rmap.put("oCode1",oCode1);
			rmap.put("oCode2",oCode2);
			rmap.put("oCode3",oCode3);
		}
		else
			rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> SetAddressInfo(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage = null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		String iWIFI = null;
		String iBT = null;
		String iNetCard=null;
		String iCode1 = null;
		String iCode2 = null;
		String iCode3 = null;
		if(map.get("iMAC")!=null){
			iWIFI=map.get("iMAC").toString();
		}
		if(map.get("iWIFI")!=null){
			iWIFI=map.get("iWIFI").toString();
		}
		if(map.get("iBT")!=null){
			iBT=map.get("iBT").toString();
		}
		if(map.get("iNetCard")!=null){
			iNetCard=map.get("iNetCard").toString();
		}
		if(map.get("iCode1")!=null){
			iCode1=map.get("iCode1").toString();
		}
		if(map.get("iCode2")!=null){
			iCode2=map.get("iCode2").toString();
		}
		if(map.get("iCode3")!=null){
			iCode3=map.get("iCode3").toString();
		}
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_SETADDRESSINFO_TOOL", new Object[]{
				String.valueOf(iSN),
				String.valueOf(iWIFI),
				String.valueOf(iBT),
				String.valueOf(iNetCard),
				String.valueOf(iCode1),
				String.valueOf(iCode2),
				String.valueOf(iCode3)
		}, new Integer[]{1, 2, 3, 4, 5, 6,7}, new Integer[]{8});
		oErrMessage = list.get(0);
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> GetMaster(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		SqlRowList rs=baseDao.queryForRowSet("select ma_user,AE_KEY,AE_SECRET from mes.master left join MES.APIEMPLOYEE B  on ma_user=ae_master");
		while(rs.next())
		{
			rmap.put(rs.getString("ma_user"),rs.getString("AE_KEY")+"|"+rs.getString("AE_SECRET"));
		}
		return rmap;
	}

	private Map<Object,Object> SetMobileData(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage = null;
		if(map.get("iSN")==null||map.get("iTSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		String iTSN=map.get("iTSN").toString();
		String iSourceCode = null;
		String iOperater = null;
		String iResult = null;
		String flag = null;
		String iErrCode = null;
		if(map.get("iSourceCode")!=null){
			iSourceCode=map.get("iSourceCode").toString();
		}
		if(map.get("iResCode")!=null){
			iSourceCode=map.get("iResCode").toString();
		}
		if(map.get("iOperater")!=null){
			iOperater=map.get("iOperater").toString();
		}
		if(map.get("iResult")!=null){
			iResult=map.get("iResult").toString();
		}
		if(map.get("flag")!=null){
			flag=map.get("flag").toString();
		}
		if(map.get("iErrCode")!=null){
			iErrCode=map.get("iErrCode").toString();
		}
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_DLLSNCHANGE_TOOL", new Object[]{
				String.valueOf(iTSN),
				String.valueOf(iSN),
				String.valueOf(iSourceCode),
				String.valueOf(iOperater),
				String.valueOf(iResult),
				String.valueOf(iErrCode),
		}, new Integer[]{1, 2, 3, 4, 5, 6}, new Integer[]{7});
		oErrMessage = list.get(0);
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> SetIMEIInfo(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage = null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		String iIMEI1=null;
		String iIMEI2 = null;
		String iIMEI3 = null;
		String iMEID = null;
		String iNET = null;
		String iID1 = null;
		String iID2 = null;
		String iID3 = null;
		if(map.get("iIMEI1")!=null){
			iIMEI1=map.get("iIMEI1").toString();
		}
		if(map.get("iIMEI2")!=null){
			iIMEI2=map.get("iIMEI2").toString();
		}
		if(map.get("iIMEI3")!=null){
			iIMEI3=map.get("iIMEI3").toString();
		}
		if(map.get("iMEID")!=null){
			iMEID=map.get("iMEID").toString();
		}
		if(map.get("iNET")!=null){
			iNET=map.get("iNET").toString();
		}
		if(map.get("iID1")!=null){
			iID1=map.get("iID1").toString();
		}
		if(map.get("iID2")!=null){
			iID2=map.get("iID2").toString();
		}
		if(map.get("iID3")!=null){
			iID3=map.get("iID3").toString();
		}
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_SETIMEIINFO", new Object[]{
				String.valueOf(iSN),
				String.valueOf(iIMEI1),
				String.valueOf(iIMEI2),
				String.valueOf(iIMEI3),
				String.valueOf(iMEID),
				String.valueOf(iNET),
				String.valueOf(""),
				String.valueOf(iID1),
				String.valueOf(iID2),
				String.valueOf(iID3),
		}, new Integer[]{1, 2, 3, 4, 5, 6,7,8,9,10}, new Integer[]{11});
		oErrMessage = list.get(0);
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> SetTestDetail(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage=null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		String iTestResult= "";
		String iResCode= "";
		String iClassName="";
		String iTestDetail= "";
		String iMaxValue= "";
		String iMinValue= "";
		if(map.get("iTestResult")!=null){
			iTestResult=map.get("iTestResult").toString();
		}
		if(map.get("iClassName")!=null){
			iClassName=map.get("iClassName").toString();
		}
		if(map.get("iResCode")!=null){
			iResCode=map.get("iResCode").toString();
		}
		if(map.get("iTestDetail")!=null){
			iTestDetail=map.get("iTestDetail").toString();
		}
		if(map.get("iMaxValue")!=null){
			iMaxValue=map.get("iTestDetail").toString();
		}
		if(map.get("iMinValue")!=null){
			iMinValue=map.get("iTestDetail").toString();
		}
		JSONObject map3=new JSONObject();
		map3.put("iSN",iSN);
		Map<Object, Object> map2=GetRcardMOInfo(map3);
		String omakeCode = "";
		StringBuffer sql=new StringBuffer();
		String[] name=iClassName.split("\\|");
		String[] result=iTestResult.split("\\|");
		String[] value=iTestDetail.split("\\|");
		if(name.length!=value.length||name.length!=result.length){
			rmap.put("oErrMessage","测试项目名称和值的项次不一致");
			return rmap;
		}
		for (int i=0;i<name.length;i++){
			sql.setLength(0);
			sql.append("Insert into STEPTESTDETAIL (STD_ID,STD_CLASS,STD_SN,STD_MAKECODE,STD_ACTUALVALUE,STD_TESTRESULT,STD_INDATE,STD_RESCODE,STD_MAXVALUE, STD_MINVALUE)");
			sql.append("values(STEPTESTDETAIL_SEQ.nextval,'"+name[i]+"','" + iSN + "','" + map2.get("oMoCode") + "','" + value[i] + "','" + result[i] + "',sysdate,'" + iResCode + "','"+iMaxValue+"','"+iMinValue+"')");
			baseDao.execute(sql.toString());
		}
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}

	private Map<Object,Object> SetPcbaData(JSONObject map){
		//实际业务数据
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage = null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		String iResCode = null;
		String iOperator = null;
		String iResult = null;
		String iErrCode = null;
		if(map.get("iResCode")!=null){
			iResCode=map.get("iResCode").toString();
		}
		if(map.get("iOperater")!=null){
			iOperator=map.get("iOperater").toString();
		}
		if(map.get("iResult")!=null){
			iResult=map.get("iResult").toString();
		}
		if(map.get("iErrCode")!=null){
			iErrCode=map.get("iErrCode").toString();
		}
		SqlRowList rs=baseDao.queryForRowSet("select nvl(st_ifinput,0)st_ifinput,nvl(st_ifoqc,0)st_ifoqc,nvl(st_ifweigh,0)st_ifweigh from "+Master+".source left join "+Master+".step on sc_stepcode=st_code where sc_code='"+iResCode+"'");
		if(rs.next()){
			String st_ifinput=rs.getString("st_ifinput");
			String st_ifoqc=rs.getString("st_ifinput");
			String st_ifweigh=rs.getString("st_ifweigh");
			if(st_ifinput.equals("-1")){
				oErrMessage = "上料工序必须在客户端进行过站";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
			}
			if(st_ifoqc.equals("-1")){
				oErrMessage = "OQC必须在客户端进行过站";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
			}
			if(st_ifweigh.equals("-1")){
				oErrMessage = "称重工序必须在客户端进行过站";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
			}
		}
		JSONObject map3=new JSONObject();
		map3.put("iSN",iSN);
		Map<Object, Object> map2=GetRcardMOInfo(map3);
		String oMoCode=null;
		if(map2.get("oMoCode")!=null){
			oMoCode=map2.get("oMoCode").toString();
		}
		if (map2.get("oErrMessage")==null)
			return SetStepFinish(oMoCode, iResCode, iSN, "", iResult, iOperator, iErrCode);
		return rmap;
	}

	/// <summary>
	/// 获取步骤代码
	/// </summary>
	/// <param name="Source"></param>
	/// <returns></returns>
	private String GetStepCodeBySource(String Source)
	{
		Object o=baseDao.getFieldDataByCondition(Master+".source", "sc_stepcode", "sc_code='" + Source + "'");
		if(o!=null)
			return o.toString();
		else
			return "";
	}

	private Boolean CheckUserLogin(String iUserCode, String iPassWord)
	{
		SqlRowList rs =baseDao.queryForRowSet("select em_code from "+Master+".employee where em_code='"+iUserCode+"' and em_password =?",iPassWord);
		if (rs.next())
			return true;
		else
		{
			return false;
		}
	}

	private Map<Object,Object> GetMakeInfoBySN(JSONObject map)
	{
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iSN=null;
		String oErrMessage=null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		SqlRowList rs=baseDao.queryForRowSet("select ma_remark1,ma_remark2,ma_remark3,ma_remark4,ma_remark5,ma_remark6,ma_remark7,ma_remark8,ma_remark9,ma_remark10,ma_remark11,ma_remark12,ma_remark13,ma_remark14,ma_remark15,ma_remark16,ma_remark17,ma_remark18 from  "+Master+".make  " +
				"left join "+Master+".makeserial on ms_makecode=ma_code  where ms_sncode='"+iSN+"' order by ms_id desc");
		if(rs.next()){
			rmap.put("oMaremark1",rs.getString("ma_remark1"));
			rmap.put("oMaremark2",rs.getString("ma_remark2"));
			rmap.put("oMaremark3",rs.getString("ma_remark3"));
			rmap.put("oMaremark4",rs.getString("ma_remark4"));
			rmap.put("oMaremark5",rs.getString("ma_remark5"));
			rmap.put("oMaremark6",rs.getString("ma_remark6"));
			rmap.put("oMaremark7",rs.getString("ma_remark7"));
			rmap.put("oMaremark8",rs.getString("ma_remark8"));
			rmap.put("oMaremark9",rs.getString("ma_remark9"));
			rmap.put("oMaremark10",rs.getString("ma_remark10"));
			rmap.put("oMaremark11",rs.getString("ma_remark11"));
			rmap.put("oMaremark12",rs.getString("ma_remark12"));
			rmap.put("oMaremark13",rs.getString("ma_remark13"));
			rmap.put("oMaremark14",rs.getString("ma_remark14"));
			rmap.put("oMaremark15",rs.getString("ma_remark15"));
			rmap.put("oMaremark16",rs.getString("ma_remark16"));
			rmap.put("oMaremark17",rs.getString("ma_remark17"));
			rmap.put("oMaremark18",rs.getString("ma_remark18"));
			return rmap;
		}else{
			oErrMessage = "SN未查询到关联信息";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
	}

	private Map<Object,Object> GetPSN(JSONObject map){
		String oErrMessage="";
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
		SqlRowList rs=baseDao.queryForRowSet("select li_upperline,ms_firstsn,ms_sncode from "+Master+".makeserial left join "+Master+".line on ms_linecode=li_code where ms_id=(select max(ms_id) from "+Master+".makeserial where ms_sncode='"+iSN+"')");
		if(rs.next()){
			String li_upperline=rs.getString("li_upperline");
			if(li_upperline.equals("7F")||li_upperline.equals("6F")){
				rmap.put("msl_sncode",iSN);
				return rmap;
			}
		}
		Object iMakecode=baseDao.getFieldDataByCondition(""+Master+".makeserial","ms_makecode","ms_id=(select max(ms_id) from "+Master+".makeserial where ms_sncode='"+iSN+"' or ms_firstsn='"+iSN+"')");
		if(iMakecode==null){
			oErrMessage = iSN+"无关联工单";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		//之前有获取过的
		rs=baseDao.queryForRowSet("select msl_sncode from "+Master+".makesnlist where msl_beforesn='"+iSN+"'");
		if(rs.next()){
			rmap.put("msl_sncode",rs.getString("msl_sncode"));
			return rmap;
		}
		rs=baseDao.queryForRowSet("select msl_sncode,msl_id from "+Master+".makesnlist where msl_makecode='"+iMakecode+"' and msl_type='after' and nvl(msl_status,0)=0 order by msl_sncode");
		if(rs.next()){
			String msl_id=rs.getString("msl_id");
			String msl_sncode=rs.getString("msl_sncode");
			baseDao.execute("update "+Master+".makesnlist set msl_status=-1,msl_beforesn='"+iSN+"' where msl_id="+msl_id+" and nvl(msl_status,0)=0");
			rmap.put("msl_sncode",msl_sncode);
			return rmap;
		}else{
			oErrMessage = "无可分配号段";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
	}

	private Map<Object,Object> GetMakeRelation(JSONObject map){
		String oErrMessage="";
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		String iSN=map.get("iSN").toString();
//		Object iMakecode=baseDao.getFieldDataByCondition(Master+".makeserial","ms_makecode","ms_id=(select max(ms_id) from "+Master+".makeserial where ms_sncode='"+iSN+"')");
//		if(iMakecode==null){
//			oErrMessage = iSN+"无关联工单";
//			rmap.put("oErrMessage",oErrMessage);
//			return rmap;
//		}
		//之前有获取过的
		SqlRowList rs=baseDao.queryForRowSet("select MID_ID, MID_MIID, MID_ORDERCODE, MID_SN, MID_WIFIMAC, MID_IMEI, MID_BTMAC, MID_OSVERSION, MID_OSPRODUCTKEY, MID_OSPRODUCTID, MID_DRIVERVERSION, MID_BIOSVERSION, MID_PROCESSID, MID_HARDDISKSN, MID_HARDDISKMODEL, MID_CREATETIME, MID_DETNO, MID_EC, ORDERNUMBER, MODEL, ORDERQUANTITY, BARCODE, CPU, RAM, ROM, GENERATION, WIFI, LCD, DPI, TP, NFC, CAMERA, BAT, GPS, FINGERPRINT, MAINBOARDVER, SUBBOARDVER, OSTYPE, MACHINECODE, MCU, GYROSCOPE, ACCELEROMETER, MAGNETOMETER, LIGHTSENSOR, BRIDGEIC, REMARK, IDENTITYMODULE, ASD_ID, ASD_ASID, ASD_MACHINETYPE, ASD_RECIVETIME, ASD_SALEER, ASD_CUSTNAME, ASD_QTY, ASD_SENDMAN, ASD_CONFIG, ASD_CONTENT, ASD_KIND, ASD_DEALMAN, ASD_FINISHTIME, ASD_STATUS, ASD_REMARK, ASD_ACTREMAKETIME, ASD_BIOS, ASD_OS, ASD_DETNO, ASD_SN, ASD_IMEI, ASD_INDATE, DeliveryIn.SN IN_SN, DeliveryIn.IMEI IN_IMEI, DeliveryIn.SALES IN_SALES, DeliveryIn.CUSTOMER IN_CUSTOMER, DeliveryIn.DELIVERY IN_DELIVERY, Deliveryout.SN OUT_SN, Deliveryout.IMEI OUT_IMEI, Deliveryout.SALES OUT_SALES, Deliveryout.CUSTOMER OUT_CUSTOMER, Deliveryout.DELIVERY OUT_DELIVERY, RMD_ID, RMD_RMID, RMD_MACHINETYPE, RMD_SN, RMD_REMARK, RMD_RECIVETIME, RMD_SALEER, RMD_CUSTNAME, RMD_QTY, RMD_ASSISTANT, RMD_BADNAME, RMD_BADCONFRM, RMD_BADTYPE, RMD_BADREASON, RMD_DEALMAN, RMD_FINISHTIME, RMD_STATUS, RMD_ANAREASON, RMD_REPAIR, RMD_REPAIRKIND, RMD_DAYS, RMD_MDM, RMD_TYPE, RMD_DETNO from "+Master+".makeinfodet left join "+Master+".MakeRemark on MID_ORDERCODE=ordernumber " +
				"left join "+Master+".aftersaledet on asd_sn=mid_sn left join "+Master+".DeliveryIn on "+Master+".DeliveryIn.sn=mid_sn " +
				"left join "+Master+".Deliveryout on "+Master+".Deliveryout.sn=mid_sn left join "+Master+".remakedet  on mid_sn=RMD_SN where mid_sn='"+iSN+"' and rownum=1");
		if(rs.next()){
			rmap.put("MID_ID","");
			rmap.put("MID_MIID",rs.getString("MID_MIID"));
			rmap.put("MID_ORDERCODE",rs.getString("MID_ORDERCODE"));
			rmap.put("MID_SN",rs.getString("MID_SN"));
			rmap.put("MID_WIFIMAC",rs.getString("MID_WIFIMAC"));
			rmap.put("MID_IMEI",rs.getString("MID_IMEI"));
			rmap.put("MID_BTMAC",rs.getString("MID_BTMAC"));
			rmap.put("MID_OSVERSION",rs.getString("MID_OSVERSION"));
			rmap.put("MID_OSPRODUCTKEY",rs.getString("MID_OSPRODUCTKEY"));
			rmap.put("MID_OSPRODUCTID",rs.getString("MID_OSPRODUCTID"));
			rmap.put("MID_DRIVERVERSION",rs.getString("MID_DRIVERVERSION"));
			rmap.put("MID_BIOSVERSION",rs.getString("MID_BIOSVERSION"));
			rmap.put("MID_PROCESSID",rs.getString("MID_PROCESSID"));
			rmap.put("MID_HARDDISKSN",rs.getString("MID_HARDDISKSN"));
			rmap.put("MID_HARDDISKMODEL",rs.getString("MID_HARDDISKMODEL"));
			rmap.put("MID_CREATETIME",rs.getString("MID_CREATETIME"));
			rmap.put("MID_DETNO",rs.getString("MID_DETNO"));
			rmap.put("MID_EC",rs.getString("MID_EC"));
			rmap.put("ORDERNUMBER",rs.getString("ORDERNUMBER"));
			rmap.put("CUSTOMER",rs.getString("CUSTOMER"));
			rmap.put("MODEL",rs.getString("MODEL"));
			rmap.put("ORDERQUANTITY",rs.getString("ORDERQUANTITY"));
			rmap.put("BARCODE",rs.getString("BARCODE"));
			rmap.put("CPU",rs.getString("CPU"));
			rmap.put("RAM",rs.getString("RAM"));
			rmap.put("ROM",rs.getString("ROM"));
			rmap.put("GENERATION",rs.getString("GENERATION"));
			rmap.put("WIFI",rs.getString("WIFI"));
			rmap.put("LCD",rs.getString("LCD"));
			rmap.put("DPI",rs.getString("DPI"));
			rmap.put("TP",rs.getString("TP"));
			rmap.put("NFC",rs.getString("NFC"));
			rmap.put("CAMERA",rs.getString("CAMERA"));
			rmap.put("BAT",rs.getString("BAT"));
			rmap.put("GPS",rs.getString("GPS"));
			rmap.put("FINGERPRINT",rs.getString("FINGERPRINT"));
			rmap.put("MAINBOARDVER",rs.getString("MAINBOARDVER"));
			rmap.put("SUBBOARDVER",rs.getString("SUBBOARDVER"));
			rmap.put("OSTYPE",rs.getString("OSTYPE"));
			rmap.put("MACHINECODE",rs.getString("MACHINECODE"));
			rmap.put("MCU",rs.getString("MCU"));
			rmap.put("GYROSCOPE",rs.getString("GYROSCOPE"));
			rmap.put("ACCELEROMETER",rs.getString("ACCELEROMETER"));
			rmap.put("MAGNETOMETER",rs.getString("MAGNETOMETER"));
			rmap.put("LIGHTSENSOR",rs.getString("LIGHTSENSOR"));
			rmap.put("BRIDGEIC",rs.getString("BRIDGEIC"));
			rmap.put("REMARK",rs.getString("REMARK"));
			rmap.put("IDENTITYMODULE",rs.getString("IDENTITYMODULE"));
			rmap.put("ASD_MACHINETYPE",rs.getString("ASD_MACHINETYPE"));
			rmap.put("ASD_RECIVETIME",rs.getString("ASD_RECIVETIME"));
			rmap.put("ASD_SALEER",rs.getString("ASD_SALEER"));
			rmap.put("ASD_CUSTNAME",rs.getString("ASD_CUSTNAME"));
			rmap.put("ASD_QTY",rs.getString("ASD_QTY"));
			rmap.put("ASD_SENDMAN",rs.getString("ASD_SENDMAN"));
			rmap.put("ASD_CONFIG",rs.getString("ASD_CONFIG"));
			rmap.put("ASD_CONTENT",rs.getString("ASD_CONTENT"));
			rmap.put("ASD_KIND",rs.getString("ASD_KIND"));
			rmap.put("ASD_DEALMAN",rs.getString("ASD_DEALMAN"));
			rmap.put("ASD_FINISHTIME",rs.getString("ASD_FINISHTIME"));
			rmap.put("ASD_STATUS",rs.getString("ASD_STATUS"));
			rmap.put("ASD_REMARK",rs.getString("ASD_REMARK"));
			rmap.put("ASD_ACTREMAKETIME",rs.getString("ASD_ACTREMAKETIME"));
			rmap.put("ASD_BIOS",rs.getString("ASD_BIOS"));
			rmap.put("ASD_OS",rs.getString("ASD_OS"));
			rmap.put("ASD_DETNO",rs.getString("ASD_DETNO"));
			rmap.put("ASD_SN",rs.getString("ASD_SN"));
			rmap.put("ASD_IMEI",rs.getString("ASD_IMEI"));
			rmap.put("ASD_INDATE",rs.getString("ASD_INDATE"));
			rmap.put("IN_SN",rs.getString("IN_SN"));
			rmap.put("IN_IMEI",rs.getString("IN_IMEI"));
			rmap.put("IN_CUSTOMER",rs.getString("IN_CUSTOMER"));
			rmap.put("IN_SALES",rs.getString("IN_SALES"));
			rmap.put("IN_DELIVERY",rs.getString("IN_DELIVERY"));
			rmap.put("OUT_SN",rs.getString("OUT_SN"));
			rmap.put("OUT_IMEI",rs.getString("OUT_IMEI"));
			rmap.put("OUT_CUSTOMER",rs.getString("OUT_CUSTOMER"));
			rmap.put("OUT_SALES",rs.getString("OUT_SALES"));
			rmap.put("OUT_DELIVERY",rs.getString("OUT_DELIVERY"));
			rmap.put("RMD_MACHINETYPE",rs.getString("RMD_MACHINETYPE"));
			rmap.put("RMD_SN",rs.getString("RMD_SN"));
			rmap.put("RMD_REMARK",rs.getString("RMD_REMARK"));
			rmap.put("RMD_RECIVETIME",rs.getString("RMD_RECIVETIME"));
			rmap.put("RMD_SALEER",rs.getString("RMD_SALEER"));
			rmap.put("RMD_CUSTNAME",rs.getString("RMD_CUSTNAME"));
			rmap.put("RMD_QTY",rs.getString("RMD_QTY"));
			rmap.put("RMD_ASSISTANT",rs.getString("RMD_ASSISTANT"));
			rmap.put("RMD_BADNAME",rs.getString("RMD_BADNAME"));
			rmap.put("RMD_BADCONFRM",rs.getString("RMD_BADCONFRM"));
			rmap.put("RMD_BADTYPE",rs.getString("RMD_BADTYPE"));
			rmap.put("RMD_BADREASON",rs.getString("RMD_BADREASON"));
			rmap.put("RMD_DEALMAN",rs.getString("RMD_DEALMAN"));
			rmap.put("RMD_FINISHTIME",rs.getString("RMD_FINISHTIME"));
			rmap.put("RMD_STATUS",rs.getString("RMD_STATUS"));
			rmap.put("RMD_ANAREASON",rs.getString("RMD_ANAREASON"));
			rmap.put("RMD_REPAIR",rs.getString("RMD_REPAIR"));
			rmap.put("RMD_REPAIRKIND",rs.getString("RMD_REPAIRKIND"));
			rmap.put("RMD_DAYS",rs.getString("RMD_DAYS"));
			rmap.put("RMD_MDM",rs.getString("RMD_MDM"));
			rmap.put("RMD_TYPE",rs.getString("RMD_TYPE"));
			rmap.put("RMD_DETNO",rs.getString("RMD_DETNO"));
			return rmap;
		}else{
			oErrMessage = "无对应配置信息";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
	}

	private Map<Object,Object> GetProdInfo(JSONObject map){
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iSN=null;
		String oErrMessage=null;
		if(map.get("iSN")==null){
			oErrMessage = "SN不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		String prcode=baseDao.getFieldDataByCondition(Master+".makeserial","ms_prodcode","ms_id=(select max(ms_id) from "+Master+".makeserial where ms_sncode='"+iSN+"')").toString();
		SqlRowList rs=baseDao.queryForRowSet("select pid_item,pid_value from "+Master+".productitem left join "+Master+".productitemdetail on pi_id=pid_piid " +
				"where pi_prodcode='"+prcode+"' and pid_item is not null");
		while(rs.next()){
			rmap.put(rs.getString("pid_item"),rs.getString("pid_value"));
		}
		return rmap;
		/*if(rs.next()){

		}else{
			oErrMessage = "SN未查询到关联信息";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}*/
	}

	private Map<Object,Object> GetMakeInfoByProd(JSONObject map)
	{
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		String iSN=null;
		String oErrMessage=null;
		if(map.get("iSN")==null){
			oErrMessage = "产品编号不能为空";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
		iSN=map.get("iSN").toString();
		SqlRowList rs=baseDao.queryForRowSet("select pr_remark1,pr_remark2,pr_remark3,pr_remark4,pr_remark5" +
				",pr_remark6,pr_remark7,pr_remark8 from  "+Master+".Makeserial left join  "+Master+".product on pr_code=ms_prodcode where ms_sncode='"+iSN+"' order by ms_id desc");
		if(rs.next()){
			rmap.put("oProdmark1",rs.getString("pr_remark1"));
			rmap.put("oProdmark2",rs.getString("pr_remark2"));
			rmap.put("oProdmark3",rs.getString("pr_remark3"));
			rmap.put("oProdmark4",rs.getString("pr_remark4"));
			rmap.put("oProdmark5",rs.getString("pr_remark5"));
			rmap.put("oProdmark6",rs.getString("pr_remark6"));
			rmap.put("oProdmark7",rs.getString("pr_remark7"));
			rmap.put("oProdmark8",rs.getString("pr_remark8"));
			return rmap;
		}else{
			oErrMessage = "产品未查询到关联信息";
			rmap.put("oErrMessage",oErrMessage);
			return rmap;
		}
	}

	public Map<Object,Object> SetStepFinish(String iMakeCode, String iSourceCode, String iSN, String iMPKind, String iResult, String iUserCode, String iErrCode)
	{
		String oErrMessage = null;
		Object StepCode = baseDao.getFieldDataByCondition(Master+".Makeserial", "ms_stepcode", "ms_sncode='" + iSN + "' and ms_makecode='" + iMakeCode + "'");
		String CurrentStep = GetStepCodeBySource(iSourceCode);
		Object BgCode = baseDao.getFieldDataByCondition(Master+".step", "nvl(st_badgroupcode,' ')", "st_code='" + CurrentStep + "'");
		Map<Object, Object> rmap = new HashMap<>();
		switch (iResult)
		{
			case "OK":
				break;
			case "NG":
				if (iErrCode.equals("")||iErrCode==null)
				{
					oErrMessage = "测试结果为NG时必须传递不良代码";
					rmap.put("oErrMessage",oErrMessage);
					return rmap;
				}
				else
				{
					baseDao.updateByCondition(""+Master+".makebad", "mb_status=-1", "mb_sncode='" + iSN + "' and mb_makecode='" + iMakeCode + "' and mb_stepcode='" + CurrentStep + "' and mb_status=0");
					String[] BadCode = iErrCode.split(",");
					StringBuffer sql=new StringBuffer();
					for (int i = 0; i < BadCode.length; i++)
					{
						if(!baseDao.checkIf(Master+".badcode","bc_code='"+ BadCode[i]+"'")){
							oErrMessage = "不良代码"+BadCode[i]+"不存在";
							rmap.put("oErrMessage",oErrMessage);
							return rmap;
						}
						int mb_id = baseDao.getSeqId("makebad_seq");
						sql.setLength(0);
						sql.append("insert into "+Master+".makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
						sql.append(",mb_sourcecode,mb_badcode,mb_badtable,mb_bgcode,mb_soncode,mb_status) select '" + mb_id + "'");
						sql.append(",ma_code,ms_code,ms_sncode,'" + iUserCode + "',sysdate,'" + CurrentStep + "','" + iSourceCode + "','" + BadCode[i] + "','',");
						sql.append("'" + BgCode + "',sp_soncode,'0' from "+Master+".make left join "+Master+".makeSerial on ms_makecode=ma_code left join "+Master+".stepProduct on ");
						sql.append("sp_mothercode=ma_prodcode and sp_stepcode=ms_nextstepcode where ms_sncode='" + iSN + "' and ms_makecode='" + iMakeCode + "'");
						baseDao.execute(sql.toString());
					}
					//将不良的序列号的状态码设为3
					baseDao.execute("update "+Master+".makeserial set ms_status='3' where ms_sncode='" + iSN + "' and ms_makecode='" + iMakeCode + "'");
				}
				break;
			default:
				oErrMessage = "测试结果必须为NG或者OK";
				rmap.put("oErrMessage",oErrMessage);
				return rmap;
		}
		//不良采集为良品是更新
		if (StepCode!=null &&StepCode.toString().equals(CurrentStep) && iResult == "OK")
		{
			Object[] ob = baseDao.getFieldsDataByCondition(Master+".makeserial", "ms_status,ms_craftcode,ms_prodcode", "ms_sncode='" + iSN + "' and ms_makecode='" + iMakeCode + "'");
			if (ob!=null)
			{
				String ms_status =ob[0].toString();
				String ms_craftcode= ob[1].toString();
				String ms_prodcode = ob[2].toString();
				if (ms_status == "3")
				{
					String nextstepcode =baseDao.getFieldDataByCondition(Master+".craft left join "+Master+".craftdetail on cr_id=cd_crid ", "cd_nextstepcode", "cr_code='" + ms_craftcode + "' and cr_prodcode='" + ms_prodcode + "' and cd_stepcode='" + CurrentStep + "'").toString();
					baseDao.updateByCondition(Master+".makeserial", "ms_status=1,ms_nextstepcode='" + nextstepcode + "'", "ms_sncode='" + iSN + "' and ms_makecode='" + iMakeCode + "'");
					baseDao.updateByCondition(Master+".makebad", "mb_status=-1", "mb_sncode='" + iSN + "' and mb_makecode='" + iMakeCode + "'");
				}
			}
		}
		return CS_SetFinish(iMakeCode, iSourceCode, iSN, iUserCode, iResult);
	}

	private Map<Object,Object> CS_SetFinish(String iMakeCode, String iSourceCode, String iSN, String iUserCode, String iResult)
	{
		Map<Object, Object> rmap = new HashMap<>();
		String oErrMessage=null;
		List<String> list = new ArrayList<String>();
		list = baseDao.callProcedureWithOut(Master+".CS_SETSTEPRESULT_TOOL", new Object[]{
				String.valueOf(iMakeCode),
				String.valueOf(iSourceCode),
				String.valueOf(iSN),
				String.valueOf(iUserCode),
				String.valueOf(iResult),
		}, new Integer[]{1, 2, 3, 4, 5}, new Integer[]{6});
		oErrMessage = list.get(0);
		rmap.put("oErrMessage",oErrMessage);
		return rmap;
	}



	private String checkAccessKey(String accessKey,String requestId){
		Object accessSecret_O = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_SECRET", "AE_KEY='" + accessKey + "'");
		Object AE_MASTER = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_MASTER", "AE_KEY='" + accessKey + "'");
		String accessSecret = accessSecret_O == null ? "" : accessSecret_O.toString();
		// 检查KEY是否合理
		if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(accessSecret) || AE_MASTER== null || "".equals(AE_MASTER.toString())) {
			ApiResult apiResult = new ApiResult(ErrorMessage.ACCESSKEY_ILLEGAL.getCode(),ErrorMessage.ACCESSKEY_ILLEGAL.getMessage(),requestId);
			throw new ApiSystemException(apiResult);
		}
		return AE_MASTER.toString();
	}



}
