<% Option Explicit %>
<!--#include file="inc/Cls_DB.asp" -->
<!--#include file="../../Inc/Const.asp" -->
<!--#include file="inc/PublicFun.asp" -->
<%
'==============================================================================
'软件名称：风讯新闻采集系统V1.0
'最新更新：2004.12
'==============================================================================
'商业注册联系：028-85098980-601,602 技术支持：028-85098980-606、607,客户支持：608
'产品咨询QQ：159410,655071,66252421
'技术支持:所有程序使用问题，请提问到bbs.foosun.net我们将及时回答您
'程序开发：风讯开发组 & 风讯插件开发组
'论坛支持：风讯在线论坛(http://bbs.foosun.net)
'官方网站：www.Foosun.net  演示站点：test.cooin.com    
'网站建设专区：www.cooin.com
'==============================================================================
'免费版本请在新闻首页保留版权信息，并做上本站LOGO友情连接
'==============================================================================
Dim DBC,UpdateConn,Conn
Set DBC = New DataBaseClass
DBC.ConnStr = "DBQ=" + server.mappath("FooSunData/Update.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
Set UpdateConn = DBC.OpenConnection()
DBC.ConnStr = "DBQ=" + Server.MapPath(DataBaseConnectStr) + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
Set Conn = DBC.OpenConnection()
Set DBC = Nothing
'判断权限
%>
<!--#include file="../../Inc/Session.asp" -->
<!--#include file="../../Inc/CheckPopedom.asp" -->
<%
if Not JudgePopedomTF(Session("Name"),"P1405") then Call ReturnError1()
'判断权限结束
Dim Submit,MainID,TableCorrInfo,i
Submit = Request("Submit")
MainID = Request("MainID")
TableCorrInfo = Request("TableCorrInfo")
if MainID = "" then
	Set UpdateConn = Nothing
	Response.Write("<script>alert('参数传递出错');window.location.href='UpDateManage.asp'</script>")  
	Response.End
end if

Dim SourceDBType,SServer,SourceDB,SUser,SPassWord,ObjectDBType,OServer,ObjectDB,OUser,OPassWord
	
if Submit = "Add" then
	Dim TableSOSql,RSTableSOObj,TableCorrInfoArray,TableCorrArray,TableSOLoopVar,AlreadyExistIDStr
	TableCorrInfoArray = Split(TableCorrInfo,"$$$")
	TableSOSql = "Select * from TableSO where DBSOID='" & MainID & "'"
	Set RSTableSOObj = Server.CreateObject("ADODB.RecordSet")
	RSTableSOObj.Open TableSOSql,UpdateConn,3,3
	do while Not RSTableSOObj.Eof
		AlreadyExistIDStr = AlreadyExistIDStr & "$$$" & RSTableSOObj("ID") & "---" & RSTableSOObj("STable") & "---" & RSTableSOObj("OTable")
		RSTableSOObj.MoveNext
	Loop
	RSTableSOObj.Close
	for TableSOLoopVar = LBound(TableCorrInfoArray) to UBound(TableCorrInfoArray)
		TableCorrArray = Split(TableCorrInfoArray(TableSOLoopVar),"---")
		if TableCorrArray(0) = "" then '添加
			RSTableSOObj.Open "Select * from TableSO where 1=0"
			RSTableSOObj.AddNew
			RSTableSOObj("DBSOID") = MainID
			RSTableSOObj("STable") = TableCorrArray(1)
			RSTableSOObj("OTable") = TableCorrArray(2)
			RSTableSOObj.UpDate
			RSTableSOObj.Close
		else 
			if InStr(AlreadyExistIDStr,TableCorrInfoArray(TableSOLoopVar)) <> 0 then '修改
				RSTableSOObj.Open "Select * from TableSO where ID=" & TableCorrArray(0) & ""
				RSTableSOObj("DBSOID") = MainID
				RSTableSOObj("STable") = TableCorrArray(1)
				RSTableSOObj("OTable") = TableCorrArray(2)
				RSTableSOObj.UpDate
				RSTableSOObj.Close
				AlreadyExistIDStr = Replace(AlreadyExistIDStr,"$$$" & TableCorrInfoArray(TableSOLoopVar),"")
			else '添加
				RSTableSOObj.Open "Select * from TableSO where 1=0"
				RSTableSOObj.AddNew
				RSTableSOObj("DBSOID") = MainID
				RSTableSOObj("STable") = TableCorrArray(1)
				RSTableSOObj("OTable") = TableCorrArray(2)
				RSTableSOObj.UpDate
				RSTableSOObj.Close
			end if
		end if
	Next
	if AlreadyExistIDStr <> "" then '删除
		Dim DeleteSql,TempExistIDArray,TempArray,TempLoopVar,TempExistIDStr
		TempExistIDStr = ""
		if Left(AlreadyExistIDStr,3) = "$$$" then '取掉头部
			AlreadyExistIDStr = Right(AlreadyExistIDStr,Len(AlreadyExistIDStr)-3)
		end if
		TempExistIDArray = Split(AlreadyExistIDStr,"$$$")
		for TempLoopVar = LBound(TempExistIDArray) to UBound(TempExistIDArray)
			TempArray = Split(TempExistIDArray(TempLoopVar),"---")
			if TempExistIDStr = "" then
				TempExistIDStr = TempArray(0)
			else
				TempExistIDStr = TempExistIDStr & "," & TempArray(0)
			end if
		Next
		DeleteSql = "Delete from FieldSO where TableSOID in (" & TempExistIDStr & ")"
		UpdateConn.Execute(DeleteSql) '删除FieldSO表中的垃圾
		DeleteSql = "Delete from TableSO where ID in (" & TempExistIDStr & ")"
		UpdateConn.Execute(DeleteSql) '删除TableSO表中的垃圾
	end if
	if Err.Number <> 0 then
		Response.Write("<script>alert('数据出错');window.location.href='UpdateAddThreeStep.asp?MainID=" & MainID & "';</script>")  
		Response.End
	end if
end if

Dim RSTableCorrObj,TableCorrTextStr,TableCorrValueStr,TableCorrTextArray,TableCorrValueArray
Dim RSFieldCorrObj,FieldCorrStr,TempFieldCorrStr,FieldCorrTextArray,FieldCorrValueArray
TableCorrTextStr = ""
TableCorrValueStr = ""
Set RSTableCorrObj = UpdateConn.Execute("Select * from TableSO where DBSOID='" & MainID & "'")
do while Not RSTableCorrObj.Eof
	if TableCorrTextStr = "" then
		TableCorrTextStr = RSTableCorrObj("STable") & "---" & RSTableCorrObj("OTable")
	else
		TableCorrTextStr = TableCorrTextStr & "$$$" & RSTableCorrObj("STable") & "---" & RSTableCorrObj("OTable")
	end if
	if TableCorrValueStr = "" then
		TableCorrValueStr = RSTableCorrObj("ID")
	else
		TableCorrValueStr = TableCorrValueStr & "$$$" & RSTableCorrObj("ID")
	end if
	TempFieldCorrStr = ""
	Set RSFieldCorrObj = UpdateConn.Execute("Select * from FieldSO where TableSOID=" & RSTableCorrObj("ID") & "")
	do while Not RSFieldCorrObj.Eof
		if TempFieldCorrStr = "" then
			TempFieldCorrStr = RSFieldCorrObj("ID") & "---" & RSFieldCorrObj("SField") & "---" & RSFieldCorrObj("OField")
		else
			TempFieldCorrStr = TempFieldCorrStr & "$$$" & RSFieldCorrObj("ID") & "---" & RSFieldCorrObj("SField") & "---" & RSFieldCorrObj("OField")
		end if
		RSFieldCorrObj.MoveNext
	Loop
	Set RSFieldCorrObj = Nothing
	if FieldCorrStr = "" then
		FieldCorrStr = RSTableCorrObj("ID") & "###" & TempFieldCorrStr
	else
		FieldCorrStr = FieldCorrStr & "***" & RSTableCorrObj("ID") & "###" & TempFieldCorrStr
	end if
	RSTableCorrObj.MoveNext
Loop
Set RSTableCorrObj = Nothing
if (TableCorrTextStr = "") OR (TableCorrValueStr = "") then
	TableCorrTextArray = Array()
	TableCorrValueArray = Array()
else
	TableCorrTextArray = Split(TableCorrTextStr,"$$$")
	TableCorrValueArray = Split(TableCorrValueStr,"$$$")
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据库转换规则</title>
</head>
<link href="inc/Collect.css" rel="stylesheet">
<body topmargin="0" leftmargin="0">
<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
  <form name="UpdateForm" method="get" action="UpdateAddFiveStep.asp">
    <tr bgcolor="#E4E4E4"> 
      <td height="30" background="images/Collect_bg.gif"><font color="#FF0000"><strong>数据转移规则---第五步---数据库表字段的对应</strong></font></td>
    </tr>
    <tr>
      <td bgcolor="#F3F3F3"><div align="center">数据库表对应关系</div></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><select onChange="GetTableFieldStr(this);" name="select" size="4" multiple style="width:100%;">
          <%
		  for i = LBound(TableCorrTextArray) to UBound(TableCorrTextArray)
		  %>
          <option value="<% = TableCorrValueArray(i) %>">
          <% = TableCorrTextArray(i) %>
          </option>
          <%
		  Next
		  %>
        </select></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
          <tr bgcolor="#FFFFFF"> 
            <td> <div align="center">源数据库表字段</div></td>
            <td> <div align="center">目标数据库表字段</div></td>
          </tr>
          <tr bgcolor="#FFFFFF"> 
            <td height="30"><div align="center">自定义数据
				<select name="UserDataType" onChange="SetUserData(this);">
                  <option selected>选择数据类型</option>
                  <option value="varChar">字符</option>
                  <option value="Number">数字</option>
                  <option value="GetRandomID18">18位随机数</option>
                </select>
                <input name="UserData" type="text" size="20">
              </div></td>
            <td rowspan="2" valign="bottom"> 
              <div align="left"> 
                <select name="ObjectTableFieldList" style="width:100%;" size="8">
                </select>
              </div></td>
          </tr>
          <tr bgcolor="#FFFFFF"> 
            <td width="50%" valign="bottom"> 
              <div align="left"> 
                <select style="width:100%;" name="SourceTableFieldList" size="6">
                </select>
              </div></td>
          </tr>
        </table></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td><div align="center"> 
                <input type="button" name="BtnAdd" onClick="AddTableFieldCorr();" value=" 添 加 ">
              </div></td>
            <td><div align="center"> 
                <input type="button" onClick="DelTableFieldCorr();" name="BtnDel" value=" 删 除 ">
              </div></td>
          </tr>
        </table></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><div align="center"> 
          <select name="TableFieldCorrList" size="10" multiple style="width:100%;">
          </select>
        </div></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td><div align="center"> 
                <input name="BtnUp" onClick="window.location.href='UpDateAddThreeStep.asp?MainID=<% = MainID %>';" type="button" id="Submitttt" value="上 一 步">
                <input type="hidden" name="MainID" value="<% = MainID %>">
                <input type="hidden" name="Submit" value="Add">
                <input type="hidden" name="TableFieldCorrInfo">
              </div></td>
            <td><div align="center"> 
                <input name="BtnNext" onClick="SubmitData();" type="button" id="Submit2dfsd" value="下 一 步">
              </div></td>
          </tr>
        </table></td>
    </tr>
  </form>
</table>
<iframe id="ListTableFieldFrame" src="ListTableAllField.asp" width="0" height="0"></iframe>
</body>
</html>
<%
Set UpdateConn = Nothing
%>
<script language="JavaScript">
var FieldCorrStr='<% = FieldCorrStr %>';
var FieldCorrObjArray=new Array();
var CurrOperateTableSOID='';
function FieldCorrObj(TableSOID,TableCorr)
{
	this.TableSOID=TableSOID;
	this.TableCorr=TableCorr;
}
InitialFieldCorrObjArray();
function InitialFieldCorrObjArray()
{
	var TempArray=null,TempTempArray,i=0,TempFieldCorrObj=null;
	if (FieldCorrStr!='')
	{
		TempArray=FieldCorrStr.split('***');
		for (i=0;i<TempArray.length;i++)
		{
			TempTempArray=TempArray[i].split('###');
			TempFieldCorrObj=new FieldCorrObj(TempTempArray[0],TempTempArray[1]);
			FieldCorrObjArray[FieldCorrObjArray.length]=TempFieldCorrObj;
		}
	}
}
function AddTableFieldCorr()
{
	var ErrorStr='',i=0,AddOption=null,AddTF=false;
	var TableFieldCorrListObj=document.UpdateForm.TableFieldCorrList;
	var SourceTableFieldListObj=document.UpdateForm.SourceTableFieldList;
	var ObjectTableFieldListObj=document.UpdateForm.ObjectTableFieldList;
	var SelectedSourceTable=SourceTableFieldListObj.value;
	var SelectedObjectTable=ObjectTableFieldListObj.value;
	if (SelectedSourceTable=='')
	{
		if (document.UpdateForm.UserDataType.value=='')
		{
			ErrorStr='请选择源数据库表字段';
			alert(ErrorStr);
			SourceTableFieldListObj.focus();
			return;
		}
		else
		{
			if (document.UpdateForm.UserData.value=='')
			{
				alert('请填写自定义数据');
				document.UpdateForm.UserData.focus();
				return;
			}
		}
	}
	if (SelectedObjectTable=='') {ErrorStr='请选择目标数据库表字段';alert(ErrorStr);ObjectTableFieldListObj.focus();return;}
	if (document.UpdateForm.UserDataType.value=='')
	{
		if (!SearchUserPosition(TableFieldCorrListObj,SelectedObjectTable))
		{
			if (SourceTableFieldListObj.options(SourceTableFieldListObj.selectedIndex).Type!=ObjectTableFieldListObj.options(ObjectTableFieldListObj.selectedIndex).Type)
			{
				if (confirm('数据类型不一致，如果添加在数据转移的时候可能发生错误。\n\n                                     确定要添加吗？')) AddTF=true;
				else AddTF=false;
			}
			else AddTF=true;
			if (AddTF)
			{
				AddOption=document.createElement("OPTION");
				AddOption.text=SelectedSourceTable+'---'+SelectedObjectTable;
				AddOption.value='---'+SelectedSourceTable+'---'+SelectedObjectTable;
				TableFieldCorrListObj.add(AddOption);
				UpDateTableFieldCorrList();
			}
		}
		else alert('目标字段'+SelectedObjectTable+'已经设置了值');
	}
	else
	{
		if (!SearchUserPosition(TableFieldCorrListObj,SelectedObjectTable))
		{
			AddOption=document.createElement("OPTION");
			AddOption.text='!'+document.UpdateForm.UserData.value+'!'+'---'+SelectedObjectTable;
			AddOption.value='---'+'!'+document.UpdateForm.UserData.value+'!'+'---'+SelectedObjectTable;
			TableFieldCorrListObj.add(AddOption);
			document.UpdateForm.UserDataType.options(0).selected=true;
			document.UpdateForm.UserData.value='';
			UpDateTableFieldCorrList();
		}
		else alert('目标字段'+SelectedObjectTable+'已经设置了值');
	}
}
function DelTableFieldCorr()
{
	var TableFieldCorrListObj=document.UpdateForm.TableFieldCorrList,i=0;
	while (i<TableFieldCorrListObj.options.length)
	{
		if (TableFieldCorrListObj.options(i).selected) {TableFieldCorrListObj.options.remove(i);}
		else i=i+1;
	}
	UpDateTableFieldCorrList();
}
function SearchUserPosition(ObjectObj,Str)
{
	var i=0,TempText='',TextArray=null;
	for(i=0;i<ObjectObj.length;i++)
	{
		TempText=ObjectObj.options(i).text;
		TextArray=TempText.split('---');
		if (TextArray[1]==Str) return true;
	}
	return false;
}
function SearchPosition(ObjectObj,Str)
{
	var i=0;
	for(i=0;i<ObjectObj.length;i++)
	{
		if (ObjectObj.options(i).text.indexOf(Str)!=-1) return true;
	}
	return false;
}
function SubmitData()
{
	var i=0,ReturnStr='';
	for (i=0;i<FieldCorrObjArray.length;i++)
	{
		if (ReturnStr=='') ReturnStr=FieldCorrObjArray[i].TableSOID+'###'+FieldCorrObjArray[i].TableCorr;
		else ReturnStr=ReturnStr+'***'+FieldCorrObjArray[i].TableSOID+'###'+FieldCorrObjArray[i].TableCorr;
	}
	document.UpdateForm.TableFieldCorrInfo.value=ReturnStr;
	document.UpdateForm.submit();
	//alert(ReturnStr);
}
//////////////////////////////////////////////////////////////////////////////////////
function SetBtnStatus(Flag)
{
	document.UpdateForm.BtnAdd.disabled=Flag;
	document.UpdateForm.BtnDel.disabled=Flag;
	document.UpdateForm.BtnUp.disabled=Flag;
	document.UpdateForm.BtnNext.disabled=Flag;
}
function GetTableFieldStr(Obj)
{
	SetBtnStatus(true);
	CurrOperateTableSOID=Obj.value;
	if (Obj.value!='')
	{
		frames["ListTableFieldFrame"].location='ListTableAllField.asp?TableSOID='+Obj.value;
	}
	InitialTableFieldCorrList(Obj.value);
}
function InitialTableFieldCorrList(ID)
{
	var i=0,OperateObj=document.UpdateForm.TableFieldCorrList,OperateStr='',OperateArray=null,AddOption=null;
	var j=0;
	DeleteAllOption(OperateObj);
	for (i=0;i<FieldCorrObjArray.length;i++)
	{
		if (FieldCorrObjArray[i].TableSOID==ID)
		{
			OperateStr=FieldCorrObjArray[i].TableCorr;
			OperateArray=OperateStr.split('$$$');
			for (j=0;j<OperateArray.length;j++)
			{
				if (OperateArray[j]!='')
				{
					AddOption=document.createElement("OPTION");
					AddOption.text=OperateArray[j].substr(OperateArray[j].indexOf('---')+3);
					AddOption.value=OperateArray[j];
					OperateObj.add(AddOption);
				}
			}
		}
	}
}
function UpDateTableFieldCorrList()
{
	var i=0,OperateObj=document.UpdateForm.TableFieldCorrList,ReturnStr='';
	for (i=0;i<OperateObj.length;i++)
	{
		if (ReturnStr=='') ReturnStr=OperateObj.options(i).value;
		else ReturnStr=ReturnStr+'$$$'+OperateObj.options(i).value;
	}
	for (i=0;i<FieldCorrObjArray.length;i++)
	{
		if (FieldCorrObjArray[i].TableSOID==CurrOperateTableSOID) FieldCorrObjArray[i].TableCorr=ReturnStr;
	}
}
function SetTableField(FieldStr,Flag)
{
	var FieldArray=FieldStr.split('$$$'),FieldAttrubute='';
	var OperateObj=null,i=0,AddOption=null;
	switch (Flag)
	{
		case 'Source':
			OperateObj=document.UpdateForm.SourceTableFieldList;
			break;
		case 'Object':
			OperateObj=document.UpdateForm.ObjectTableFieldList;
			break;
		default :
			return;
	}
	DeleteAllOption(OperateObj);
	for (i=0;i<FieldArray.length;i++)
	{
		FieldAttrubute=FieldArray[i].substr(FieldArray[i].lastIndexOf('<'));
		FieldAttrubute=FieldAttrubute.replace('<','');
		FieldAttrubute=FieldAttrubute.replace('>','');
		AddOption=document.createElement("OPTION");
		AddOption.text=FieldArray[i];
		AddOption.value=FieldArray[i].slice(0,FieldArray[i].lastIndexOf('<'));
		AddOption.IsRequested=FieldAttrubute.split('|')[0];
		AddOption.Type=FieldAttrubute.split('|')[1];
		OperateObj.add(AddOption);
	}
}
function DeleteAllOption(SelectObj)
{
	var OptionLength=SelectObj.length;
	for(var i=0;i<OptionLength;i++)
	{
		SelectObj.options.remove(SelectObj.length-1);
	}
}
function SetUserData(Obj)
{
	document.UpdateForm.UserData.value='';
	switch (Obj.value)
	{
		case 'GetRandomID18':
			document.UpdateForm.UserData.value='GetRandomID18';
			break;
		default :
		
	}
}
</script>