<!--#include file="../Include/Const.Asp" -->
<!--#include file="../Include/ConnSiteData.Asp" -->
<!--#include file="../CheckAdmin.Asp"-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../Images/Admin_style.css">
<script language="javascript" src="../JavaScript/Admin.js"></script>
<script type="text/javascript">
//==========================================


</script>

<%
'获取参数
dim Realname
Types=Clng(Request.QueryString("Type"))

Select Case Types
	Case 1
		Call FileAdd() '执行导入
	Case Else
	    Call showu() '
End Select
%>
<%
'==============================
'函 数 名：showu
'作    用：初始
'参    数：
'==============================
Sub showu()
%>
<br />
<table class="tableBorder" width="95%" border="0" align="center" cellpadding="5" cellspacing="1">
    <tr>
      <th height="22" sytle="line-height:150%">&nbsp;</th>
    </tr>
        <tr>
            <td height="30" colspan="6" style="padding-bottom:5px;">
     <form name="AddFile" id="AddFile" onsubmit="return false;">
           
&nbsp;&nbsp;&nbsp;&nbsp;沟通记录上传&nbsp;<input name="XlsFile" value="" type="hidden" id="XlsFile" />&nbsp;<iframe frameborder="0" width="290" height="30" scrolling="No" id="XlsFiles" name="XlsFiles" src="../Kehu/XlsUpLoad.asp?Form=AddFile&Input=XlsFile"></iframe>&nbsp;<input type="button" value="导入沟通记录" class="Button2" onclick="if(document.all.XlsFile.value!=''){window.location.href='?Type=1&XlsFile='+document.all.XlsFile.value;}else{alert('请先点浏览选择文件，再点上传，上传导入文件！');}" style="color:#063" />
</form>
<div style="padding-left:20px;">
<a href="../Kehu/导入例子v1.csv" target="_blank">下载导入格式示例.csv</a><br>
<font color="#FF0000">注意事项：
<br>
1、请严格按格式填写，不得改变表格样式。<br>
2、系统暂不提供重复检测，已上传导入的记录请不要再次上传及导入。
</font>
      </div>      </td>
        </tr>

	<tr>
      
    </tr>
    	
    <tr>
      <td class="forumRowHighlight" align="center">&nbsp;</td>
    </tr>
</table>
<%
End Sub
%>


<%
'==============================
'函 数 名：FileAdd
'作    用：文件导入
'参    数：
'==============================
Sub FileAdd()
    Realname=genjinText(session("UserName"))
	On Error Resume Next
	Dim Rs2,sql,XlsFile,Conn3,Sqlstr3
	Dim Etext '导入失败
	XlsFile=Trim(Request.QueryString("XlsFile"))
	If XlsFile="" Then
		Response.Write("<div style=""margin:30px;""><a href=""?"">请先上传导入文件</a></div>")
		Response.End()
	End If
	Response.Write("<div style=""margin:30px;line-height:22px;"">")
	Response.Write("导入过程开始<br />")
	Response.Flush()
	Response.Clear()
	Set Rs2=Server.Createobject("Adodb.RecordSet")
	Set Conn3=CreateObject("ADODB.Connection")     
	Conn3.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended properties=Excel 8.0;Data Source="&Server.MapPath(""&XlsFile&"")
	Set Rs=Server.Createobject("Adodb.RecordSet")
	Sqlstr3="Select * From [沟通内容表$]"
	Rs.Open Sqlstr3,Conn3,1,3
	If Err Then
		Response.Write("<span style=""color:red;"">未找到表名为【沟通内容表】的Excel工作簿！</span><br />")
		Response.Write("<a href=""?"">返回</a></div>")
		Err.Clear
		Response.End()
	End If
	Application.Lock()
	While Not Rs.Eof
	        '---------------------------------查公司ID和联系人ID开始
			Dim rs4,Sqlstr4,rs5,Sqlstr5,rs6,sqlstr6
			if Rs(1)<>"" then
			    Set rs5 = server.CreateObject("adodb.recordset")
	            Sqlstr5="Select * from Zqj_Company where Co_name='"&trim(Rs(1))&"' and Memname='"&session("UserName")&"'"
			    rs5.Open Sqlstr5,Conn,1,3
			    if rs5.bof and rs5.eof then
			        Co_ID=0
					Cp_ID=0
					Etext="公司不存在!"
			    else
				     Co_ID=rs5("Co_ID")	
				     '通过联系人表找出联系人ID，否则导入出错
					  set rs6=server.CreateObject("adodb.recordset")
					  sqlstr6="Select * from Zqj_Contactperson where Co_ID="&Co_ID&" and Cp_name='"&trim(Rs(2))&"' and Memname='"&session("UserName")&"'"
					  rs6.open sqlstr6,conn,1,3
					  if rs6.bof and rs6.eof then
					     Co_ID=0
						 Cp_ID=0
						 Etext="联系人不存在!"
					  else
					     Cp_ID=rs6("Cp_ID")
					  end if
					  rs6.close
					  set rs6=nothing
			    end if	
			    rs5.close
			    set rs5=nothing
			else
			    Set rs4 = server.CreateObject("adodb.recordset")
	            Sqlstr4="Select * from Zqj_Contactperson where Cp_name='"&trim(Rs(2))&"' and Memname='"&session("UserName")&"'"
			     Rs4.Open Sqlstr4,Conn,1,3
			     if rs4.bof and rs4.eof then
			        Cp_ID=0
					Etext="联系人不存在!"
			     else
			       Cp_ID=rs4("Cp_ID")
			     end if
				 Co_ID=0
			     rs4.close
			     set rs4=nothing
			end if
			
			
			
			'--------------------------------查公司ID和联系人ID结束
			if Etext<>"" then
			    response.Write(""&Rs(0)&","&Rs(1)&","&Rs(2)&"&nbsp;失败!原因：[<font color='#FF0000'>"&Etext&"</font>]<br>")
				Response.Flush()
				Response.Clear()
			else
			Sqlstr="Select * from Zqj_Records"
			Rs2.Open Sqlstr,Conn,1,3
				Rs2.AddNew()
				If Trim(Rs(0))<>"" and IsDate(Rs(0)) Then
				    Rs2("Re_time")=Rs(0)
				else
				    Rs2("Re_time")=now()
				end if
				Rs2("Re_company")=trim(Rs(1))
				Rs2("Re_Contactname")=trim(Rs(2))
				Rs2("Re_Content")=Rs(3)
				Rs2("Re_status")=Rs(4)
				Rs2("Cp_ID")=Cp_ID
				Rs2("Co_ID")=Co_ID			
				If Trim(Rs(5))<>"" Then
				    Rs2("RealName")=Rs(5)
				else
				    Rs2("RealName")=Realname
				end if
				Rs2("MemName")=session("UserName")
				Response.Write("<span style=""color:green;"">"&Rs(0)&_
				","&Rs(1)&_
				"["&Rs(2)&_
				"] 成功导入！</span><br />")
				Response.Flush()
				Response.Clear()
			    Rs2.Update()
			    Rs2.Close
			end if
		Rs.MoveNext
		Etext=""
	Wend
	Application.UnLock()
	Rs.Close
	Set Rs2=Nothing
	Conn3.Close
	Set Conn3=Nothing
	
	Response.Write("导入过程完成<br />")
	Response.Write("<a href=""KhRecords.Asp"">返回咨询记录列表</a></div>")
End Sub

Function genjinText(MemName)
    Dim rs, sql
    Set rs = server.CreateObject("adodb.recordset")
    sql = "Select * From Zqj_Members where MemName='"&MemName&"'"
    rs.Open sql, conn, 1, 1
	if rs.bof and rs.eof then
	genjinText=""
	else
    genjinText = rs("RealName")
	end if
    rs.Close
    Set rs = Nothing
End Function


%>