--江苏省公示系统爬虫  
--http://www.jsgsj.gov.cn:58888/province/

--引用公用包
local com=require "res.util.comm"
local ecps=require "res.util.ecps"
local json=require "json"
--名称
spiderName="江苏公示爬虫";
--代码
spiderCode="ecps_js";
--是否下载3级页
spiderDownDetailPage=true;
--开始下载页
spiderStartPage=1;
--最大下载页
spiderMaxPage=500;
--上次下载时间
spiderLastDownloadTime="2016-01-01 01:01:01";
--执行频率30分钟
spiderRunRate=1;

--下载内容写入表名
spider2Collection="ecps";
--下载页面时使用的编码
spiderPageEncoding="UTF8";
--是否使用代理
spiderUserProxy=false;
--是否是安全协议
spiderUserHttps=false;
--下载详细页线程数
spiderThread=1
--存储模式 1 直接存储，2 调用消息总线 ...
spiderStoreMode=1
spiderStoreToMsgEvent=1 --消息总线event
--延时毫秒 基本延时(spiderSleepBase)+随机延时(spiderSleepRand)
spiderSleepBase=100
spiderSleepRand=100
--判重字段 空默认不判重，spiderCoverAttr="title" 按title判重覆盖
spiderCoverAttr="title"
--取得对方网站最后发布时间 必须返回yyyy-MM-dd HH:mm:ss 格式
function getLastPublishTime()
	return os.date("%Y-%m-%d %H:%M:%S", os.time())
end



--下载分析列表页
function downloadAndParseListPage(pageno) 
	--local str="queryInfor('/ecipplatform/inner_ci/ci_queryCorpInfor_gsRelease.jsp','877','517221','10','320106000039745','','ecipplatform')"
	--print(string.gmatch(str,"queryInfor%('/(.*%.jsp)"))
--	for v1,v2,v3,v4,v5,v6,v7 in string.gmatch(str, "queryInfor%('/(.-%.jsp)','(.-)','(.-)','(.-)','(.-)','(.-)','(.-)'%)") do
--       print(v1,v2,v3,v4,v5,v6,v7)
--  end
--  os.exit()
	
	
	local entMl=getEntNames("ecps_js")
	--local entMl={}
	print("江苏企业名录长度:",tostring(table.getn(entMl)))
	if table.getn(entMl)==0 then
		print("开始下江苏异常名录")
		local head={
				["Accept"]="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
				["Accept-Encoding"]="gzip,deflate,sdch",
				["Accept-Language"]="zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
				["Cache-Control"]="no-cache",
				["Host"]="www.jsgsj.gov.cn:58888",
				["Pragma"]="no-cache",
				["Upgrade-Insecure-Requests"]="1",
				["User-Agent"]="Mozilla/.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"	
		}
		local param={}
		param["showRecordLine"]="1"
		param["tmp"]=tostring(os.time())
		param["pageNo"]=tostring(pageno)
		param["pageSize"]="10"
		local con,_=downloadAdv("http://www.jsgsj.gov.cn:58888/province/NoticeServlet.json?QueryExceptionDirectory=true","get",param,head,"")	
		if con=="" then 
			print("下载江苏异常名录出错")
			return entMl
		end
		local object=json.decode(con)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local item={}
				item["title"]=v["C1"]
				item["regno"]=v["C2"]
				item["href"]="http://www.jsgsj.gov.cn:58888/province/rand_img.jsp?type=7&temp＝"..tostring(os.time()) --验证码
				--print(item["title"])
				table.insert(entMl,item)
			end
		end
	end
return entMl
--测试代码
--	local entMl={}
--	for i=1,1 do
--				item={}
--				--item["title"]="西子奥的斯电梯有限公司长沙分公司"
--				item["title"]="testt"
--				item["href"]="http://www.jsgsj.gov.cn:58888/province/rand_img.jsp?type=7&temp＝"..tostring(os.time()); --验证码
--				table.insert(entMl,item)
				
--	end
--	return entMl
--测试代码结束
	
end

function downloadDetailPage(data)
	data["href"]="http://www.jsgsj.gov.cn:58888/province/rand_img.jsp?type=7&temp＝"..tostring(os.time())
	print("开始下载  ",data["title"],"  公示数据")
	for i=1,30 do
		--local downloadId=changeDownloader()
		for j=1,1 do
			local headIndex={
				["Accept"]="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
				["Accept-Encoding"]="gzip,deflate,sdch",
				["Accept-Language"]="zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
				["Upgrade-Insecure-Requests"]="1",
				["Host"]="www.jsgsj.gov.cn:58888",
				["User-Agent"]="Mozilla/.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"	
			}
			local content,cookies = downloadAdv("http://www.jsgsj.gov.cn:58888/province/","get",{},headIndex,"")
			if content=="" then 
				print("访问江苏首页出错")
				timeSleep(8)
				break
			end
			--print("开始下验证码: "..data["title"])
			local code,_= getCode(data["href"],cookies)
			if code=="error" then
				timeSleep(8)
				break
			end
			--print("获取到验证码,开始校验"..code,"  ",data["title"])
			code=com.trim(code)
			data["title"]=com.trim(data["title"])
			local isSuccess=codeCheck(cookies,data["title"],code)
			if isSuccess=="true" then
				local entDetailUrl,org,id,seq_id,reg_no=getEntTmp(cookies,code,data["title"])
				if entDetailUrl=="" then
					break
				elseif entDetailUrl=="error" then
					break
				elseif entDetailUrl==nil then
					print(data["title"].."  无此企业!")
					saveErrLog(spiderCode,spiderName.."_title",data["title"],err)
					return nil
				else
					local entInfo={}
					local errorMsg=getEntDetail(entDetailUrl,org,id,seq_id,reg_no,cookies,entInfo)
					--去冗余字段
					entInfo["pripid"]=nil
					entInfo["href"]=nil
					entInfo["publishtime"]=nil
					data["href"]=nil
					local b=checkData(data["title"],entInfo)
					--printf(entInfo)
						if  b	then
							--printf(entInfo)
							return entInfo
						else
							break
						end
				end
			elseif isSuccess=="error" then
				timeSleep(8)
				break
			else
				--print("验证码错误,重新请求.....")
				break
			end
		end
	end
end

function getCode(href,cookies)
	local head={
			["Accept"]="image/webp,image/*,*/*;q=0.8",
			["Accept-Encoding"]="gzip,deflate,sdch",
			["Accept-Language"]="zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
			["Cache-Control"]="no-cache",
			["Host"]="www.jsgsj.gov.cn:58888",
			["Pragma"]="no-cache",
			["Referer"]="http://www.jsgsj.gov.cn:58888/province/",
			["User-Agent"]="Mozilla/.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"	
	}
	local content,_= downloadAdv(href,"get",{},head,cookies)
	if content=="" then
		print("获取验证码图片出错")
		return "error"
	end
	local code= getEcpsCode("js",content)
	return code
end

function codeCheck(cookies,title,code)
	local codeCheckUrl="http://www.jsgsj.gov.cn:58888/province/infoQueryServlet.json?checkCode=true"
	local head={
			["Accept"]="application/json, text/javascript, */*; q=0.01",
			["Accept-Encoding"]="gzip, deflate",
			["Accept-Language"]="zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
			["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8",
			["Host"]="www.jsgsj.gov.cn:58888",
			["Origin"]="http://www.jsgsj.gov.cn:58888",
			["Referer"]="http://www.jsgsj.gov.cn:58888/province/",
			["Pragma"]="no-cache",
			["Cache-Control"]="no-cache",
			["X-Requested-With"]="XMLHttpRequest",
			["User-Agent"]="Mozilla/.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36",	
	}
	local param={["verifyCode"]=code}
	local content,_=downloadAdv(codeCheckUrl,"post",param,head,cookies);
	if content=="" then
		print("验证码校验请求失败")
		return "error"
	end
	--print(content)
	local object=com.eval(content)
	--printf(object["bean"]["mark"])
	local isSuccessFul="true"
	if object["bean"]["mark"]=="0" then 
		isSuccessFul="false"
	end
	--print("是否成功:"..isSuccessFul)
	return isSuccessFul
end


function getEntTmp(cookies,code,title)	
	local head={
			["Accept"]="application/json, text/javascript, */*; q=0.01",
			["Accept-Encoding"]="gzip, deflate",
			["Accept-Language"]="zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
			["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8",
			["Host"]="www.jsgsj.gov.cn:58888",
			["Origin"]="http://www.jsgsj.gov.cn:58888",
			["Referer"]="http://www.jsgsj.gov.cn:58888/province/queryResultList.jsp",
			["Pragma"]="no-cache",
			["Cache-Control"]="no-cache",
			["X-Requested-With"]="XMLHttpRequest",
			["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",	
	}
	local param={["verifyCode"]=code,["name"]=title}
	local entListContent,_=downloadAdv("http://www.jsgsj.gov.cn:58888/province/infoQueryServlet.json?query_info=true","post",param,head,cookies)
	
	if string.find(entListContent,"没有符合查询条件的结果")~=nil then
		return nil
	end
	
	--print(entListContent)
	--os.exit()
	local object=json.decode(entListContent)
	if object~=nil then 
		local infoStr=object[1]["INFO"]
		--print(infoStr)
		if string.find(infoStr,"queryInfor")~=nil then
			local tmp=findOneText("a:eq(0):attr(onclick)",infoStr)
			--print(tmp)
			for v1,v2,v3,v4,v5,v6,v7 in string.gmatch(tmp, "queryInfor%('/(.-%.jsp)','(.-)','(.-)','(.-)','(.-)','(.-)','(.-)'%)") do
     			local detailUrl=com.trim(v1)
				local org=com.trim(v2)
				local id=com.trim(v3)
				local seq_id=com.trim(v4)
				local reg_no=com.trim(v5)
				--print(v1,v2,v3,v4,v5,v6,v7)
				return detailUrl,org,id,seq_id,reg_no
			end
			
		end
	end
	return ""
	
end



function getEntDetail(entDetailUrl,org,id,seq_id,reg_no,cookies,entInfo)
	local head={
		["Accept"]="application/json, text/javascript, */*; q=0.01",
		["Accept-Encoding"]="gzip, deflate",
		["Accept-Language"]="zh-CN,zh;q=0.8",
		["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8",
		["Host"]="www.jsgsj.gov.cn:58888",
		["Origin"]="http://www.jsgsj.gov.cn:58888",
		["Referer"]="http://www.jsgsj.gov.cn:58888/ecipplatform/inner_ci/ci_queryCorpInfor_gsRelease.jsp",
		["Upgrade-Insecure-Requests"]="1",
		["User-Agent"]="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"
	}
	
	entInfo["Area"]="JS"
	--基本信息
	entInfo=getBaseInfo(entInfo,org,id,seq_id,cookies,head)
	--print("基本信息完成")
	--股东信息
	local invertors=getInvertor(org,id,seq_id,cookies,head)
	entInfo["invertor"]=invertors
	--print("股东信息完成")
	--变更信息
	local alterInfos=getAlterInfo(org,id,seq_id,cookies,head)
	entInfo["alterInfo"]=alterInfos
	--print("变更信息完成")
	--主要人员
	local staffinfos=getStaffinfo(org,id,seq_id,cookies,head)
	entInfo["staffinfo"]=staffinfos
	--print("主要人员完成")
	--分支机构
	local childents=getChildent(org,id,seq_id,cookies,head)
	entInfo["childent"]=childents
	--print("分支机构完成")
	--行政处罚
	local punishInfos=getPunishInfo(org,id,seq_id,cookies,head)
	entInfo["punishInfo"]=punishInfos
	--print("行政处罚完成")
	--经营异常
	local excDirecs=getExcDirec(org,id,seq_id,cookies,head)
	entInfo["excDirec"]=excDirecs
	--print("经营异常完成")
	--年报
	local nbtab=getNbList(reg_no,cookies,head)
	if nbtab=="error" then
		print("查询年报列表信息请求出错")
		return "error"
	end
	entInfo["qynb"]=nbtab
	--print("年报完成")
	--os.exit()
	
	
end


function getBaseInfo(entInfo,org,id,seq_id,cookies,head)
	local param={
		["org"]=org,
		["id"]=id,
		["seq_id"]=seq_id,
		["specificQuery"]="basicInfo",
	}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/ciServlet.json?ciEnter=true","post",param,head,cookies)
	
	if content~="" then
		local object=json.decode(content)
		--printf(object)
		entInfo["RegNo"]=object[1]["C1"]
		entInfo["EntName"]=object[1]["C2"]
		entInfo["EntTypeName"]=object[1]["C3"]
		entInfo["RegCap"]=object[1]["C6"]
		entInfo["EstDate"]=object[1]["C4"]
		entInfo["IssBLicDate"]=object[1]["C12"]
		entInfo["OpFrom"]=object[1]["C9"]
		entInfo["OpTo"]=object[1]["C10"]
		entInfo["Dom"]=object[1]["C7"]
		entInfo["OpLoc"]=""
		entInfo["OpScope"]=object[1]["C8"]
		entInfo["RegOrg"]=object[1]["C11"]
		entInfo["OpState"]=object[1]["C13"]
	end
	
	
	return entInfo
end

function getInvertor(org,id,seq_id,cookies,head)
	local param={
		["CORP_ORG"]=org,
		["CORP_ID"]=id,
		["CORP_SEQ_ID"]=seq_id,
		["specificQuery"]="investmentInfor",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local investors={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/ciServlet.json?ciEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		--printf(object)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local inverstor={}
				inverstor["Inv"]=v["C2"]
				inverstor["InvTypeName"]=v["C1"]
				--print(inverstor["Inv"],inverstor["InvTypeName"])
				table.insert(investors,inverstor)
			end
		end
	end
	
	return investors
end

function getAlterInfo(org,id,seq_id,cookies,head)
	local param={
		["corp_org"]=org,
		["corp_id"]=id,
		--["CORP_SEQ_ID"]=seq_id,
		["specificQuery"]="commonQuery",
		["propertiesName"]="biangeng",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["tmp"]=tostring(os.time()),
		["pageSize"]="100"
	}
	local alterInfos={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/commonServlet.json?commonEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local alterInfo={}
				alterInfo["AltItemName"]=v["C1"]
				alterInfo["AltAf"]=v["C3"]
				alterInfo["AltBe"]=v["C2"]
				alterInfo["AltDate"]=v["C4"]
				--print(alterInfo["AltItemName"],alterInfo["AltBe"],alterInfo["AltAf"])
				table.insert(alterInfos, alterInfo)
			end
		end
	end
	return alterInfos
end


function getStaffinfo(org,id,seq_id,cookies,head)
	local param={
		["CORP_ORG"]=org,
		["CORP_ID"]=id,
		["CORP_SEQ_ID"]=seq_id,
		["specificQuery"]="personnelInformation",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local staffinfos={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/ciServlet.json?ciEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local staffinfo1={}
				staffinfo1["Name"]=v["PERSON_NAME1"]
				staffinfo1["Position"]=v["POSITION_NAME1"]
				--print(staffinfo1["Name"],staffinfo1["Position"])
				table.insert(staffinfos,staffinfo)
				if v["PERSON_NAME2"]~=nil and v["PERSON_NAME2"]~="" then
					local staffinfo2={}
					staffinfo2["Name"]=v["PERSON_NAME2"]
					staffinfo2["Position"]=v["POSITION_NAME2"]
					--print(staffinfo2["Name"],staffinfo2["Position"])
					table.insert(staffinfos,staffinfo2)
				end
			end
		end
	end
	return staffinfos
end


function getChildent(org,id,seq_id,cookies,head)
	local param={
		["CORP_ORG"]=org,
		["CORP_ID"]=id,
		["CORP_SEQ_ID"]=seq_id,
		["specificQuery"]="branchOfficeInfor",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local childents={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/ciServlet.json?ciEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local childent={}
				childent["BrName"]=v["C2"]--分支名称
				childent["RegNO"]=v["C1"]--注册号
				childent["RegOrgName"]=v["C3"]--分支机构登记机关
				--print(childent["BrName"],childent["RegNO"],childent["RegOrgName"])
				table.insert(childents, childent)
			end
		end
	end
	return  childents
end


function getPunishInfo(org,id,seq_id,cookies,head)
	local param={
		["corp_org"]=org,
		["corp_id"]=id,
		["corp_seq_id"]=seq_id,
		["specificQuery"]="commonQuery",
		["propertiesName"]="chufa",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["tmp"]=tostring(os.time()),
		["pageSize"]="100"
	}
	local punishInfos={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/commonServlet.json?commonEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local punishInfo={}
				punishInfo["PenDecNo"]=v["C1"]--行政处罚号
				punishInfo["IllegActTypeName"]=v["C2"]--行政处罚类型名称
				punishInfo["PenResult"]=v["C3"]--处罚结果
				punishInfo["OrgName"]=v["C4"]--机构
				punishInfo["PenDecIssDate"]=v["C5"]--日期
				--print(punishInfo["PenDecNo"],punishInfo["IllegActTypeName"],punishInfo["PenResult"],punishInfo["OrgName"],punishInfo["PenDecIssDate"])
				table.insert(punishInfos, punishInfo)
			end
		end
	end
	return punishInfos
end

function getExcDirec(org,id,seq_id,cookies,head)
	local param={
		["corp_org"]=org,
		["corp_id"]=id,
		["corp_seq_id"]=seq_id,
		["specificQuery"]="commonQuery",
		["propertiesName"]="abnormalInfor",
		["showRecordLine"]="1",
		["pageNo"]="1",
		["tmp"]=tostring(os.time()),
		["pageSize"]="100"
	}
	local excDirecs={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/commonServlet.json?commonEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local excDirec={}
				excDirec["DecOrg"]=v["C5"]
				excDirec["SpeCause"]=v["C1"]
				excDirec["AbnTime"]=v["C2"]
				excDirec["OutSpeTim"]=v["C4"]
				excDirec["OutSpeCause"]=v["C3"]
				--print(excDirec["DecOrg"],excDirec["SpeCause"],excDirec["AbnTime"],excDirec["OutSpeTim"],excDirec["OutSpeCause"])
				table.insert(excDirecs, excDirec)
			end
		end
	end
	return excDirecs
end

function getNbList(reg_no,cookies,head)
	local param={
		["REG_NO"]=reg_no,
		["specificQuery"]="gs_pb",
		["propertiesName"]="query_report_list",
		["showRecordLine"]="0",
		["tmp"]=tostring(os.time())
	}
	local nbinfos={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/nbServlet.json?nbEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object)
		if len>0 then
			for k,v in pairs (object) do
				local year=string.sub(v["REPORT_RESULT"],1,4)
				--print(year)
				local nbinfo=getExYear(v["ID"],reg_no,year,cookies,head)
				nbinfo["ancheyear"]=year
				table.insert(nbinfos, 1, nbinfo)
				--os.exit()
			end
		end
	end
	return nbinfos
end

function getExYear(id,reg_no,year,cookies,head)
	
	local param={
		["ID"]=tostring(id),
		["OPERATE_TYPE"]="2",
		["specificQuery"]="gs_pb",
		["propertiesName"]="query_basicInfo",
		["showRecordLine"]="0",
		["tmp"]=tostring(os.time())
	}
	local nb={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/nbServlet.json?nbEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local nbbase={}
		nbbase["RegNo"]=object[1]["REG_NO"]
		nbbase["EntName"]=object[1]["CORP_NAME"]
		nbbase["address"]=object[1]["ADDR"]
		nbbase["Tel"]=object[1]["TEL"]
		nbbase["postcode"]=object[1]["ZIP"]
		nbbase["email"]=object[1]["E_MAIL"]
		nbbase["equityTransfer"]=object[1]["IF_EQUITY"]
		nbbase["state"]=object[1]["PRODUCE_STATUS"]
		nbbase["hasWebsite"]=object[1]["IF_WEBSITE"]
		nbbase["hasInv"]=object[1]["IF_INVEST"]
		nbbase["hasGuarantee"]=""
		nbbase["numPeople"]=object[1]["PRAC_PERSON_NUM"]
		nb["base"]=nbbase
		
		local qyzc={}
		qyzc["assets"]=object[1]["NET_AMOUNT"]--资产总额
		qyzc["equity"]=object[1]["TOTAL_EQUITY"]--所有者权益合计
		qyzc["incometotal"]=object[1]["SALE_INCOME"]--营业总收入
		qyzc["profitotal"]=object[1]["PROFIT_TOTAL"]--利润总额	
		qyzc["mainincome"]=object[1]["SERV_FARE_INCOME"]--营业总收入中主营业务收入
		qyzc["netprofit"]=object[1]["PROFIT_RETA"]--净利润
		qyzc["taxtotal"]=object[1]["TAX_TOTAL"]--纳税总额
		qyzc["debtstotal"]=object[1]["DEBT_AMOUNT"]--负债总额
		nb["assetstatus"]=qyzc
	end
	--printf(nb["assetstatus"])
	
	--股东及出资信息
	local param={
		["MAIN_ID"]=tostring(id),
		["OPERATE_TYPE"]="1",
		["TYPE"]="NZGS",
		["showRecordLine"]="1",
		["specificQuery"]="gs_pb",
		["propertiesName"]="query_stockInfo",
		["ID"]=tostring(id),
		["ADMIT_MAIN"]="08",
		["temp"]=tostring(os.time()),
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local gdczs={}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/nbServlet.json?nbEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local gdcz={}
				gdcz["inv"]=v["STOCK_NAME"]--股东
				gdcz["acConAm"]=v["REAL_CAPI"] --实缴出资额
				gdcz["realConDate"]=v["REAL_CAPI_DATE"]--实缴出资时间
				gdcz["realConForm"]=v["REAL_CAPI_TYPE_NAME"]--实缴出资方式
				gdcz["subConAm"]=v["SHOULD_CAPI"]--认缴出资额
				gdcz["subConAmForm"]=v["SHOULD_CAPI_TYPE_NAME"]--认缴出资方式
				gdcz["subConAmDate"]=v["SHOULD_CAPI_DATE"]--认缴出资时间
				--print(gdcz["inv"],gdcz["acConAm"],gdcz["realConForm"],gdcz["subConAm"],gdcz["subConAmForm"])
				table.insert(gdczs,gdcz)
			end
		end
	end
	nb["invs"]=gdczs
	
	
	--对外投资信息
	local tzs={}
	local param={
		["REPORT_YEAR"]=year,
		["ID"]=tostring(id),
		["showRecordLine"]="1",
		["specificQuery"]="gs_pb",
		["propertiesName"]="query_investInfo",
		["temp"]=tostring(os.time()),
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/nbServlet.json?nbEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local tz={}
				tz["name"]=v["INVEST_NAME"]--名称
				tz["regno"]=v["INVEST_REG_NO"]--注册号
				--print(tz["name"],tz["regno"])
				table.insert(tzs,tz)
			end
		end
	end
	nb["investment"]=tzs
	
	
	--股权变更信息
	local bgs={}
	nb["equitychange"]=bgs
	local param={
		["REPORT_YEAR"]=year,
		["ID"]=tostring(id),
		["showRecordLine"]="1",
		["specificQuery"]="gs_pb",
		["propertiesName"]="query_EquityTransfer",
		["temp"]=tostring(os.time()),
		["pageNo"]="1",
		["pageSize"]="100"
	}
	local content,_=downloadAdv("http://www.jsgsj.gov.cn:58888/ecipplatform/nbServlet.json?nbEnter=true","post",param,head,cookies)
	if content~="" then
		local object=json.decode(content)
		local len=table.getn(object["items"])
		if len>0 then
			for k,v in pairs (object["items"]) do
				local bg={}
				bg["inv"]=v["STOCK_NAME"] --股东
				bg["transbmpr"]=v["CHANGE_BEFORE"] --变更前股权比例
				bg["transampr"]=v["CHANGE_AFTER"] --变更后股权比例				
				bg["altdate"]=v["CHANGE_DATE"] --股权变更日期
				--print(bg["inv"],bg["transbmpr"],bg["transampr"],bg["altdate"])
				table.insert(bgs, bg)
			end
		end
	end
	
	
	return nb
end
function strDel(str)
	str=string.gsub(str,"\"","")
	return str
end



function checkData(title,entInfo)
	local entName=entInfo["EntName"]
	local regNo=entInfo["RegNo"]
	if entName=="" or entName==nil or title~=entName then
		print(title.."江苏数据下载出错或者不完整重新下载")
		return false
	end
	if regNo=="" or regNo==nil then
		print(title.."江苏数据下载出错或者不完整重新下载")
		return false
	end
	--print(entInfo["EntName"].."数据完整")
	return true
end