<!--#include virtual="/inc/conn.asp"-->

<%
Response.CharSet="GB2312"
Response.ContentType="text/html"
  
uid=SafeRequest("uid")
act=SafeRequest("act")
istdaili=SafeRequest("istdaili")
benid=SafeRequest("benid") '下级代理ID

if act<>"alltongbu" then
response.Write "noact"
response.end
end if

if request.Cookies("aynameid")="" then
response.Write "nouid"
response.end
end if


sql="select * from aygood where aygoodsuoyouid="&request.Cookies("aynameid")&" and aygoodisydl=0 and aygoodtodaili=0 and isptdx=0" 
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
if benid<>"" then call PopErr("你有未设置代理价格的商品，请先编辑商品，设置代理价格，再进行同步操作")
response.Write "nojiage"
response.end
end if
rs.close




set rsdl=server.CreateObject("adodb.recordset")

if benid<>"" then
istdaili="true"
sqldl="select aydluserid,aydlsjuserid,aydlistongguo from aydailishang where aydlsjuserid="&request.Cookies("aynameid")&" and aydluserid="&benid&" and aydlistongguo=1" 
else
sqldl="select aydluserid,aydlsjuserid,aydlistongguo from aydailishang where aydlsjuserid="&request.Cookies("aynameid")&" and aydlistongguo=1"
end if

rsdl.open sqldl,conn,1,1
if rsdl.bof and rsdl.eof then
	if benid<>"" then call PopErr("还没有代理关系，参数错误E3，请联系管理员")
	response.Write "nodaili"
	rsdl.close
	response.end
else
do while not rsdl.eof
	'****************************************************************************************************************************

		sid=request.Cookies("aynameid")
		userid=rsdl("aydluserid")
		'获取上级代理商品分类信息
		sql="select * from aykafenlei where aykanamesuoyou="&sid&" order by ayshangjiid asc" 
		rs.open sql,conn,1,1
		if not(rs.bof and rs.eof) then
		do while not rs.eof

					'添加本地商品分类
					fenleierji=0
					if clng(rs("ayshangjiid"))<>0 then '二级分类
					
					set rsej=server.CreateObject("adodb.recordset")
					sqlej="select aykafenleiid,aykanamesuoyou,ayflsumid from aykafenlei where aykafenleiid="&rs("ayshangjiid")&" and aykanamesuoyou="&sid&"" 
					rsej.open sqlej,conn,1,1
					if not(rsej.bof and rsej.eof) then
						'查找本地二级分类ID
						set rsejb=server.CreateObject("adodb.recordset")
						sqlejb="select aykafenleiid,ayflsumid,aykanamesuoyou from aykafenlei where ayflsumid='"&trim(rsej("ayflsumid"))&"' and aykanamesuoyou="&clng(userid)&"" 
						rsejb.open sqlejb,conn,1,1
						if not(rsejb.bof and rsejb.eof) then
						fenleierji=rsejb("aykafenleiid")
						end if
						rsejb.close
					end if
					rsej.close
					end if
					
					
					set rsfl=server.CreateObject("adodb.recordset")
					sqlfl="select * from aykafenlei where ayflsumid='"&trim(rs("ayflsumid"))&"' and aykanamesuoyou="&userid&"" 
					rsfl.open sqlfl,conn,1,3
					if rsfl.bof and rsfl.eof then
						rsfl.addnew
						rsfl("aykafenleiname")=trim(rs("aykafenleiname"))
						rsfl("ayshangjiid")=clng(fenleierji)
						rsfl("aykafenleisx")=rs("aykafenleisx")
						rsfl("aykanamesuoyou")=clng(userid)
						rsfl("ayfenleidlid")=clng(sid)
						rsfl("ayflsumid")=trim(rs("ayflsumid"))
						rsfl("ayfenleidlfid")=rs("aykafenleiid")
						rsfl("aykafenleikg")=rs("aykafenleikg")
						rsfl("aykafenleishuoming")=trim(rs("aykafenleishuoming"))
						rsfl.update
					
						'获取本地分类MD5 ID
						set rsfid=server.CreateObject("adodb.recordset")
						sqlfid="select top 1 aykafenleiid,aykanamesuoyou,ayfenleidlid,aykafenleimd5,ayfldlmd5url from aykafenlei where aykanamesuoyou="&userid&" and ayfenleidlid="&sid&" order by aykafenleiid desc" 
						rsfid.open sqlfid,conn,1,3
						if not(rsfid.bof and rsfid.eof) then
						rsfid("aykafenleimd5")=md5d(rsfid("aykafenleiid"))
						rsfid("ayfldlmd5url")=md5user(rsfid("aykafenleiid"))
						fenleiid=rsfid("aykafenleiid")
						rsfid.update
						end if
						rsfid.close
					
					else
						rsfl("aykafenleishuoming")=trim(rs("aykafenleishuoming"))
						rsfl("aykafenleikg")=rs("aykafenleikg")
						rsfl.update
					
						'获取本地分类MD5 ID
						set rsfid=server.CreateObject("adodb.recordset")
						sqlfid="select top 1 aykafenleiid,aykanamesuoyou,ayflsumid from aykafenlei where ayflsumid='"&trim(rs("ayflsumid"))&"' and aykanamesuoyou="&userid&"" 
						rsfid.open sqlfid,conn,1,3
						if not(rsfid.bof and rsfid.eof) then
						fenleiid=rsfid("aykafenleiid")
						rsfid.update
						end if
						rsfid.close
					end if
					rsfl.close
										
					
					
					
					'获取上级代理商品信息
					set rssgood=server.CreateObject("adodb.recordset")
					sqlsgood="select * from aygood where aygoodsuoyouid="&sid&" and aygoodfl="&rs("aykafenleiid")&" and aygoodisydl=0 and isptdx=0" 
					rssgood.open sqlsgood,conn,1,1
							
					if not(rssgood.bof and rssgood.eof) then
					do while not rssgood.eof
			
							'添加本地商品
							set rsg=server.CreateObject("adodb.recordset")
							sqlg="select * from aygood where aygsumid='"&trim(rssgood("aygsumid"))&"' and aygoodsuoyouid="&userid&"" 
							rsg.open sqlg,conn,1,3
							if rsg.bof and rsg.eof then

								'代充模板处理
								aymbgusum=""
								mbid=""

														
							
								rsg.addnew
								rsg("aygoodsuoyouid")=userid
								rsg("aygoodfl")=fenleiid
								rsg("aygoodsx")=rssgood("aygoodsx")
								rsg("aygoodname")=trim(rssgood("aygoodname"))
								rsg("ayjiage")=rssgood("ayjiage")
								
								rsg("ayxiangou")=rssgood("ayxiangou") '限购
								rsg("aypifayes")=rssgood("aypifayes") '批发
								
								if clng(rssgood("aypifa1"))<>0 and rssgood("dailij1")<>0 then 
								rsg("aypifa1")=rssgood("aypifa1")
								rsg("aypifaj1")=rssgood("aypifaj1")
								rsg("dailicb1")=rssgood("dailij1")
								end if
								
								if clng(rssgood("aypifa2"))<>0 and rssgood("dailij2")<>0 then 
								rsg("aypifa2")=rssgood("aypifa2")
								rsg("aypifaj2")=rssgood("aypifaj2")
								rsg("dailicb2")=rssgood("dailij2")
								end if
								if clng(rssgood("aypifa3"))<>0 and rssgood("dailij3")<>0 then 
								rsg("aypifa3")=rssgood("aypifa3")
								rsg("aypifaj3")=rssgood("aypifaj3")
								rsg("dailicb3")=rssgood("dailij3")
								end if
								if clng(rssgood("aypifa4"))<>0 and rssgood("dailij4")<>0 then 
								rsg("aypifa4")=rssgood("aypifa4")
								rsg("aypifaj4")=rssgood("aypifaj4")
								rsg("dailicb4")=rssgood("dailij4")
								end if
								if clng(rssgood("aypifa5"))<>0 and rssgood("dailij5")<>0 then 
								rsg("aypifa5")=rssgood("aypifa5")
								rsg("aypifaj5")=rssgood("aypifaj5")
								rsg("dailicb5")=rssgood("dailij5")
								end if
								
								rsg("aygoodzhuangtai")=rssgood("aygoodzhuangtai") 
								rsg("aygooddlid")=rssgood("aygoodsuoyouid")
								rsg("aygsumid")=trim(rssgood("aygsumid"))
								rsg("aysjgid")=rssgood("aygoodid")
								
								rsg("kahaoname")=trim(rssgood("kahaoname"))
								rsg("kaminame")=trim(rssgood("kaminame"))
								
								'if ccur(rsg("aysjjiage"))=0 then 
								rsg("aysjjiage")=rssgood("aygoodtodaili")
								rsg("aychenben")=rssgood("aygoodtodaili")
								'else
								'rsg("aysjjiage")=rssgood("aysjjiage")
								'rsg("aychenben")=rssgood("aysjjiage")
								'end if
								
								rsg("ayshmiaoshu")=trim(rssgood("ayshmiaoshu"))
								rsg("goodxx")=trim(rssgood("goodxx"))
								rsg("goodbuyxx")=trim(rssgood("goodbuyxx"))
								rsg("aygoodiskucun")=1	'代理商品 不显示实际库存
								rsg.update
								
								
							else
							

							
								'代充模板处理
								aymbgusum=""
								mbid=""

							
								rsg("aygoodfl")=fenleiid
								rsg("aygoodsx")=rssgood("aygoodsx")
								rsg("aygoodname")=trim(rssgood("aygoodname"))
								rsg("ayjiage")=rssgood("ayjiage")
								rsg("aygoodzhuangtai")=rssgood("aygoodzhuangtai") 
								rsg("ayshmiaoshu")=trim(rssgood("ayshmiaoshu"))
								rsg("goodxx")=trim(rssgood("goodxx"))
								rsg("goodbuyxx")=trim(rssgood("goodbuyxx"))
								
								rsg("kahaoname")=trim(rssgood("kahaoname"))
								rsg("kaminame")=trim(rssgood("kaminame"))
								
								rsg("ayxiangou")=rssgood("ayxiangou") '限购
								rsg("aypifayes")=rssgood("aypifayes") '批发
								
								if istdaili="true" then
									rsg("aysjjiage")=rssgood("aygoodtodaili")
									rsg("aychenben")=rssgood("aygoodtodaili")
									
									if clng(rssgood("aypifa1"))<>0 and rssgood("dailij1")<>0 then 
									rsg("aypifa1")=rssgood("aypifa1")
									rsg("aypifaj1")=rssgood("aypifaj1")
									rsg("dailicb1")=rssgood("dailij1")
									end if
									
									if clng(rssgood("aypifa2"))<>0 and rssgood("dailij2")<>0 then 
									rsg("aypifa2")=rssgood("aypifa2")
									rsg("aypifaj2")=rssgood("aypifaj2")
									rsg("dailicb2")=rssgood("dailij2")
									end if
									if clng(rssgood("aypifa3"))<>0 and rssgood("dailij3")<>0 then 
									rsg("aypifa3")=rssgood("aypifa3")
									rsg("aypifaj3")=rssgood("aypifaj3")
									rsg("dailicb3")=rssgood("dailij3")
									end if
									if clng(rssgood("aypifa4"))<>0 and rssgood("dailij4")<>0 then 
									rsg("aypifa4")=rssgood("aypifa4")
									rsg("aypifaj4")=rssgood("aypifaj4")
									rsg("dailicb4")=rssgood("dailij4")
									end if
									if clng(rssgood("aypifa5"))<>0 and rssgood("dailij5")<>0 then 
									rsg("aypifa5")=rssgood("aypifa5")
									rsg("aypifaj5")=rssgood("aypifaj5")
									rsg("dailicb5")=rssgood("dailij5")
									end if
								end if
								rsg("aygoodiskucun")=1	'代理商品 不显示实际库存
								rsg.update
							end if
							rsg.close
			
					rssgood.movenext 
					loop
					
					end if
					rssgood.close


		rs.movenext 
		loop
		rs.close
		

		

		'增加商品MD5数据
		set rsmd5=server.CreateObject("adodb.recordset")
		sqlmd5="select aygoodid,aygoodsuoyouid,aygooddlid,aygoodmd5,aygdlmd5url from aygood where aygoodsuoyouid="&userid&" and aygooddlid="&sid&" and aygoodmd5 is null" 
		rsmd5.open sqlmd5,conn,1,3
		if not(rsmd5.bof and rsmd5.eof) then
		do while not rsmd5.eof
		rsmd5("aygoodmd5")=md5d(rsmd5("aygoodid"))
		rsmd5("aygdlmd5url")=md5user(rsmd5("aygoodid"))
		rsmd5.update
		rsmd5.movenext 
		loop
		end if
		rsmd5.close
		
		'删除上级代理已删除的商品
		set rsd=server.CreateObject("adodb.recordset")
		set rsd2=server.CreateObject("adodb.recordset")
		sqld="select * from aygood where aygoodsuoyouid='"&userid&"' and aygooddlid='"&sid&"'" 
		rsd.open sqld,conn,1,3
		do while not rsd.eof
			sqld2="select * from aygood where aygoodid='"&rsd("aysjgid")&"' and aygoodsuoyouid='"&sid&"'" 
			rsd2.open sqld2,conn,1,1
			if rsd2.bof and rsd2.eof then
			rsd.delete
			end if
			rsd2.close
		rsd.movenext 
		loop
		rsd.close
		
		'删除上级代理已删除的商品分类
		sqld="select * from aykafenlei where aykanamesuoyou='"&userid&"' and ayfenleidlid='"&sid&"'" 
		rsd.open sqld,conn,1,3
		do while not rsd.eof
			sqld2="select * from aykafenlei where aykafenleiid='"&rsd("ayfenleidlfid")&"' and aykanamesuoyou='"&sid&"'" 
			rsd2.open sqld2,conn,1,1
			if rsd2.bof and rsd2.eof then
			rsd.delete
			end if
			rsd2.close
		rsd.movenext 
		loop
		rsd.close
		

		end if
			'**********************************************************************************************************************************************************************







rsdl.movenext 
loop
end if
rsdl.close

if benid<>"" then call PopErr2("同步成功","userdaili")
response.Write "tongbuok"
response.end
%>