parseCreditXmlModel.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. #coding=utf-8
  2. import fileinput
  3. from xml.dom import minidom
  4. import base64
  5. import sys
  6. import os
  7. # import xmlSelfParser
  8. from customModel import CustomModel
  9. from datetime import datetime
  10. from ini_op import Config;
  11. base_dir = os.path.dirname(os.path.abspath(__file__)) #当前文件上一层目录的绝对路径
  12. config = Config(base_dir+"/config.ini");
  13. productNumXy = config.get("baseconf", "productNumXy")
  14. productNumJz = config.get("baseconf","productNumJz")
  15. productNumXxw = config.get("baseconf","productNumXxw")
  16. productNumFb = config.get("baseconf", "productNumFb")
  17. productNumKcd = config.get("baseconf", "productNumKcd")
  18. productNumFd = config.get("baseconf", "productNumFd")
  19. productNumKn = config.get("baseconf", "productNumKn")
  20. productNumHst = config.get("baseconf", "productNumHst")
  21. productNumQk = config.get("baseconf", "productNumQk")
  22. productNumHc = config.get("baseconf", "productNumHc")
  23. productNum500 = config.get("baseconf", "productNum500")
  24. productNumHsz = config.get("baseconf", "productNumHsz")
  25. #简营
  26. productNumJy = config.get("baseconf", "productNumJy")
  27. #新版桔子
  28. productNumJzA = config.get("baseconf", "productNumJzA")
  29. #仁东
  30. productNumRd = config.get("baseconf", "productNumRd")
  31. #恒昌新
  32. productNumHcNew = config.get("baseconf", "productNumHcNew")
  33. #非自定义列表
  34. busiNum = [productNumJz,productNumXxw,productNumFb,productNumKcd,productNumFd,productNumKn,productNumHst,productNumQk,productNumHc,productNum500,
  35. productNumJy,productNumJzA,productNumRd,productNumHcNew]
  36. isPlt = config.get("baseconf", "isPlt");
  37. import xyHttp
  38. import hst_rule_set
  39. import pboc_hc
  40. import log
  41. import shutil
  42. logger = log.logger
  43. import time
  44. from dbController import DbController
  45. dbController = DbController();
  46. import json
  47. import requests
  48. import timeit
  49. import traceback
  50. import gzip
  51. import io
  52. from xmlParser import XmlParser;
  53. from xmlParserHsz import XmlParserHsz;
  54. # from xmlSelfParser import XmlSelfParser;
  55. from pboc.invokePboc import PBOC
  56. from mailUtil import MailUtil
  57. approResult=0
  58. def gzip_str(string_):
  59. out = io.BytesIO()
  60. with gzip.GzipFile(fileobj=out, mode='w') as fo:
  61. fo.write(string_.encode())
  62. bytes_obj = out.getvalue()
  63. return bytes_obj
  64. def gunzip_bytes_obj(bytes_obj):
  65. in_ = io.BytesIO()
  66. in_.write(bytes_obj)
  67. in_.seek(0)
  68. with gzip.GzipFile(fileobj=in_, mode='rb') as fo:
  69. gunzipped_bytes_obj = fo.read()
  70. return gunzipped_bytes_obj.decode()
  71. #解析xml数据
  72. def getBusinessInfo(xmlFile):
  73. doc = minidom.parse(xmlFile)
  74. request = doc.documentElement.getElementsByTagName("request")[0]
  75. responseBody = doc.documentElement.getElementsByTagName("responseBody")[0]
  76. responseHeader = doc.documentElement.getElementsByTagName("responseHeader")[0]
  77. result = ""
  78. xmlData = ""
  79. isBaihu = "0";
  80. if len(responseBody.childNodes)==0:
  81. mailUtil = MailUtil();
  82. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56'
  83. resultMsg = getNodeData(responseHeader,"resultMsg")
  84. if resultMsg =="查询成功,无报告":
  85. isBaihu = "1"
  86. elif resultMsg == "输入的姓名与系统收录的姓名不一致":
  87. isBaihu = "3"
  88. else:
  89. isBaihu = "2"#查询失败
  90. mailUtil.dingtalk("号码:" + xmlFile.split("_")[0] + " 查询失败 ", webhook)
  91. else:
  92. responseBodyText = responseBody.childNodes[0].data
  93. decrpyt_bytes = base64.b64decode(responseBodyText)
  94. xmlData = gunzip_bytes_obj(decrpyt_bytes)
  95. #remove by chenqiwang 1105
  96. xmlPath = xmlFile+".txt"
  97. # 生成xml格式报告
  98. with open(xmlPath, 'w', encoding='utf-8') as fp:
  99. fp.write(xmlData)
  100. # 非小赢需要解析
  101. productNum = getNodeData(request, "productNum")
  102. # print("a")
  103. # print(productNum + "cc")
  104. # print("b")
  105. sqlMap = []
  106. if productNum != productNumXy:
  107. if xmlData !="":
  108. if productNum == productNumHsz:
  109. result,sqlMap = parseHsz(xmlData)
  110. # print("HSZ")
  111. else:
  112. #跑数据的时候改为parseHsz
  113. result, sqlMap = parse(xmlData)
  114. # elif productNum in busiNum:
  115. # #跑数据的时候改为parseHsz
  116. # result, sqlMap = parse(xmlData)
  117. # else :
  118. # result, sqlMap = parseSelf(xmlData)
  119. data = {
  120. "businessNum":getNodeData(request,"businessNum"),
  121. "coopBusinessNum": getNodeData(request, "coopBusinessNum"),
  122. "customerNum":getNodeData(request,"customerNum"),
  123. "certificateNum": getNodeData(request, "certificateNum"),
  124. "productNum": productNum,
  125. "creditXml":xmlData,
  126. "extend": getNodeData(request, "extend"),
  127. "result":result,
  128. "isBaihu":isBaihu,
  129. "sqlMap":sqlMap
  130. }
  131. return data
  132. #解析xml报文
  133. def parse(xmlData):
  134. xmlParse = XmlParser()
  135. result = ""
  136. sqlMap = []
  137. try:
  138. result,sqlMap = xmlParse.parse(xmlData)
  139. except:
  140. info = sys.exc_info()
  141. logger.error(info[0])
  142. logger.error(info[1])
  143. logger.error(traceback.extract_tb(info[2], 1))
  144. return result,sqlMap
  145. #解析xml报文 汇算帐
  146. def parseHsz(xmlData):
  147. xmlParseHsz = XmlParserHsz()
  148. result = ""
  149. try:
  150. result = xmlParseHsz.parse(xmlData)
  151. except:
  152. info = sys.exc_info()
  153. logger.error(info[0])
  154. logger.error(info[1])
  155. logger.error(traceback.extract_tb(info[2], 1))
  156. return result
  157. # #解析xml报文 自定义
  158. # def parseSelf(xmlData):
  159. # xmlParseSelf = XmlSelfParser()
  160. # result = ""
  161. # sqlMap = []
  162. #
  163. # try:
  164. # result,sqlMap = xmlParseSelf.parse(xmlData)
  165. # except:
  166. # info = sys.exc_info()
  167. # logger.error(info[0])
  168. # logger.error(info[1])
  169. # logger.error(traceback.extract_tb(info[2], 1))
  170. # return result,sqlMap
  171. def getNodeData(request,key):
  172. data = request.getElementsByTagName(key)[0].childNodes[0].data
  173. return data;
  174. # if __name__ == '__main__':
  175. # businessInfo = getBusinessInfo('./test.xml')
  176. # print(businessInfo)
  177. # xml = ""
  178. # data = base64.b64encode(xml.encode("UTF-8"))
  179. # print(str(data))
  180. #调用http
  181. def process(startTime,businessInfo,basePath,xml_path,file_name):
  182. startTime = str(startTime)
  183. businessInfo = businessInfo
  184. file_name = file_name
  185. xml_path = xml_path
  186. productNum = businessInfo["productNum"]
  187. # parse_result = businessInfo["result"]
  188. # print(businessInfo)
  189. # print("产品号:"+productNum)
  190. if productNumXy.find(productNum) >= 0:
  191. print("操作1")
  192. try:
  193. result = xyHttp.call_credit(businessInfo)
  194. # result = json.loads(result);
  195. # logger.info(result)
  196. #上传审批结果
  197. jsonPath = basePath + businessInfo["certificateNum"] + ".txt"
  198. logger.info(jsonPath)
  199. with open(jsonPath, 'w', encoding='utf-8') as fp:
  200. fp.write(result)
  201. uploadJsonFile(businessInfo,jsonPath)
  202. #移动xml文件
  203. try:
  204. dayStrPath = basePath+productNum+"/"+time.strftime('%Y%m%d', time.localtime(time.time()))+"/";
  205. descXmlPath = dayStrPath +"xml/"+ os.path.basename(xml_path)
  206. descJsonPath = dayStrPath+"txt/" + os.path.basename(jsonPath)
  207. if not os.path.exists(basePath+productNum):
  208. os.mkdir(basePath+productNum)
  209. if not os.path.exists(dayStrPath):
  210. os.mkdir(dayStrPath)
  211. if not os.path.exists(dayStrPath+"xml/"):
  212. os.mkdir(dayStrPath + "xml/")
  213. if not os.path.exists(dayStrPath+"txt/"):
  214. os.mkdir(dayStrPath+"txt/")
  215. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  216. if isPlt == "1":
  217. move(xml_path, descXmlPath)
  218. shutil.move(jsonPath, descJsonPath)
  219. except:
  220. logger.error(businessInfo["certificateNum"]+"#创建文件夹或者移动文件失败,不影响业务")
  221. logger.error(traceback.print_exc())
  222. logger.error(traceback.format_exc())
  223. except:
  224. logger.error(traceback.print_exc())
  225. logger.error(traceback.format_exc())
  226. mailUtil = MailUtil();
  227. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56'
  228. mailUtil.dingtalk("号码:" + businessInfo["certificateNum"] + " 调用xy服务失败 ", webhook)
  229. endTime = str(datetime.now()) # 结束时间
  230. # yuan原有商户模型入库
  231. dbController.getConn()
  232. parse_info = str(businessInfo["result"])
  233. parse_info = parse_info.replace("'", "\\'")
  234. filter_info = str(result)
  235. filter_info = filter_info.replace("'", "\\'")
  236. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info,start_time,end_time) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "', '" + endTime + "')"
  237. # print(sql)
  238. dbController.execSql(sql)
  239. # if i==1:
  240. # break
  241. dbController.commit()
  242. elif productNumQk.find(productNum) >= 0 or productNumHsz.find(productNum) >= 0 or productNumHc.find(productNum) >= 0 \
  243. or productNumJzA.find(productNum) >= 0 or productNumRd.find(productNum) >= 0 or productNumHcNew.find(productNum) >= 0:#洽客
  244. # print("操作2")
  245. txtPath = xml_path.replace(".xml", ".txt")
  246. result = ""
  247. if businessInfo["isBaihu"] != "3":
  248. with open(txtPath, 'w', encoding='utf-8') as fp:
  249. fp.write(businessInfo["result"])
  250. if productNumQk.find(productNum) >= 0:
  251. result = xyHttp.callQk(businessInfo)
  252. if productNumHc.find(productNum) >= 0:
  253. result = xyHttp.callHc(businessInfo)
  254. elif productNumHsz.find(productNum) >= 0:
  255. result = xyHttp.callHsz(businessInfo)
  256. elif productNumJzA.find(productNum) >= 0:
  257. result = xyHttp.callJz(businessInfo)
  258. elif productNumRd.find(productNum) >= 0:
  259. result = xyHttp.callRd(businessInfo)
  260. elif productNumHcNew.find(productNum) >= 0:
  261. result = pboc_hc.renhang_rules(businessInfo)
  262. # result = json.loads(result);
  263. # print(result)
  264. #上传审批结果
  265. # print(businessInfo["certificateNum"])
  266. jsonPath = basePath + businessInfo["certificateNum"] + ".txt"
  267. logger.info(jsonPath)
  268. if businessInfo["isBaihu"] == "3":
  269. result = "{'errCode':-1,errMsg:'输入的姓名与系统收录的姓名不一致'}"
  270. logger.info(result)
  271. with open(jsonPath, 'w', encoding='utf-8') as fp:
  272. fp.write(result)
  273. uploadJsonFile(businessInfo,jsonPath)
  274. endTime = str(datetime.now()) # 结束时间
  275. #yuan原有商户模型入库
  276. dbController.getConn()
  277. parse_info = str(businessInfo["result"])
  278. parse_info = parse_info.replace("'", "\\'")
  279. filter_info = str(result)
  280. filter_info = filter_info.replace("'", "\\'")
  281. sql = "INSERT INTO filter_result(report_name,product_num,filter_info,parse_info,start_time,end_time) VALUES ('" + file_name + "', '" +productNum + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "', '" + endTime + "')"
  282. dbController.execSql(sql)
  283. dbController.commit()
  284. #移动xml文件
  285. dayStrPath = basePath+productNum+"/"+time.strftime('%Y%m%d', time.localtime(time.time()))+"/";
  286. descXmlPath = dayStrPath +"xml/"+ os.path.basename(xml_path) #源文件
  287. descJsonPath = dayStrPath+"txt/" + os.path.basename(jsonPath) #解析文件
  288. descTxtPath = dayStrPath + "txt/" + os.path.basename(txtPath) #征信信息
  289. if not os.path.exists(basePath+productNum):
  290. os.mkdir(basePath+productNum)
  291. if not os.path.exists(dayStrPath):
  292. os.mkdir(dayStrPath)
  293. if not os.path.exists(dayStrPath+"xml/"):
  294. os.mkdir(dayStrPath + "xml/")
  295. if not os.path.exists(dayStrPath+"txt/"):
  296. os.mkdir(dayStrPath+"txt/")
  297. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  298. if isPlt == "1":
  299. move(xml_path, descXmlPath)
  300. shutil.move(jsonPath, descJsonPath)
  301. shutil.move(txtPath, descTxtPath)
  302. # if i==1:
  303. # break
  304. # dbController.commit()
  305. else:
  306. # print("产品号:"+productNum)
  307. #走jar包逻辑
  308. # print(businessInfo["result"])
  309. txtPath = xml_path.replace(".xml",".txt")
  310. # print("txt路径"+txtPath)
  311. with open(txtPath, 'w', encoding='utf-8') as fp:
  312. fp.write(businessInfo["result"])
  313. if productNum == productNumJz:#桔子
  314. print("桔子")
  315. # yuan原有商户模型入库
  316. dbController.getConn()
  317. parse_info = str(businessInfo["result"])
  318. parse_info = parse_info.replace("'", "\\'")
  319. filter_info = "此模型不产生结果"
  320. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  321. # print(sql)
  322. dbController.execSql(sql)
  323. # if i==1:
  324. # break
  325. dbController.commit()
  326. invokePboc(businessInfo, txtPath)
  327. #移动xml文件
  328. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  329. if not os.path.exists(basePath + "execed_new/"):
  330. os.mkdir(basePath + "execed_new/")
  331. if isPlt == "1":
  332. move(xml_path, descXmlPath)
  333. elif productNum == productNumXxw:#新希望
  334. # yuan原有商户模型入库
  335. dbController.getConn()
  336. parse_info = str(businessInfo["result"])
  337. parse_info = parse_info.replace("'", "\\'")
  338. filter_info = "此模型不产生结果"
  339. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  340. # print(sql)
  341. dbController.execSql(sql)
  342. # if i==1:
  343. # break
  344. dbController.commit()
  345. invokeXxw(businessInfo, txtPath);
  346. jsonPath = basePath + businessInfo["certificateNum"] + ".txt"
  347. #移动xml文件
  348. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  349. if not os.path.exists(basePath + "execed_new/"):
  350. os.mkdir(basePath + "execed_new/")
  351. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  352. if isPlt == "1":
  353. move(xml_path, descXmlPath)
  354. elif productNum == productNumFb or productNum== productNumKcd or productNum500.find(productNum)>=0:#没有java包 #快车道和风暴 上传500个字段
  355. logger.info(businessInfo["productNum"])
  356. # yuan原有商户模型入库
  357. dbController.getConn()
  358. parse_info = str(businessInfo["result"])
  359. parse_info = parse_info.replace("'", "\\'")
  360. filter_info = "此模型不产生结果"
  361. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  362. # print(sql)
  363. dbController.execSql(sql)
  364. # if i==1:
  365. # break
  366. dbController.commit()
  367. uploadJsonFile(businessInfo,txtPath)
  368. # 移动xml文件
  369. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  370. if not os.path.exists(basePath + "execed_new/"):
  371. os.mkdir(basePath + "execed_new/")
  372. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  373. if isPlt == "1":
  374. move(xml_path, descXmlPath)
  375. elif productNumJy.find(productNum)>=0:#简营
  376. logger.info(businessInfo["productNum"])
  377. jsonPath = txtPath.replace(".txt","_json.txt")
  378. # yuan原有商户模型入库
  379. dbController.getConn()
  380. parse_info = str(businessInfo["result"])
  381. parse_info = parse_info.replace("'", "\\'")
  382. filter_info = "此模型不产生结果"
  383. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  384. # print(sql)
  385. dbController.execSql(sql)
  386. # if i==1:
  387. # break
  388. dbController.commit()
  389. #写入数据到文件
  390. xyHttp.buildJyData(businessInfo,jsonPath)
  391. uploadJsonFile(businessInfo, jsonPath)
  392. # 移动xml文件
  393. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  394. if not os.path.exists(basePath + "execed_new/"):
  395. os.mkdir(basePath + "execed_new/")
  396. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  397. if isPlt == "1":
  398. descTxtPath = basePath + "execed_txt/" + os.path.basename(jsonPath)
  399. try:
  400. shutil.move(jsonPath, descTxtPath)
  401. except Exception:
  402. pass
  403. move(xml_path, descXmlPath)
  404. elif productNum == productNumFd:#分蛋 白户审批通过
  405. # yuan原有商户模型入库
  406. dbController.getConn()
  407. parse_info = str(businessInfo["result"])
  408. parse_info = parse_info.replace("'", "\\'")
  409. filter_info = "此模型不产生结果"
  410. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  411. # print(sql)
  412. dbController.execSql(sql)
  413. # if i==1:
  414. # break
  415. dbController.commit()
  416. if businessInfo["result"]!="":
  417. result = xyHttp.callNewModel(businessInfo)
  418. uploadAudit(result,businessInfo["businessNum"],productNum)
  419. else:
  420. result = {"approveResult":"1","rule":"白户"}
  421. uploadAudit(result, businessInfo["businessNum"], productNum)
  422. # 移动xml文件
  423. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  424. if not os.path.exists(basePath + "execed_new/"):
  425. os.mkdir(basePath + "execed_new/")
  426. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  427. if isPlt == "1":
  428. move(xml_path, descXmlPath)
  429. elif productNumKn.find(productNum)>=0 :#快牛 是本地jar包,本地jar改为逗号配置
  430. if businessInfo["result"]!="":
  431. result = xyHttp.callLocal(businessInfo)
  432. filter_info = uploadAudit(result,businessInfo["businessNum"],productNum)
  433. endTime = str(datetime.now()) # 结束时间
  434. # yuan原有商户模型入库
  435. dbController.getConn()
  436. parse_info = str(businessInfo["result"])
  437. parse_info = parse_info.replace("'", "\\'")
  438. sql = "INSERT INTO filter_result(report_name,product_num,filter_info,parse_info,start_time,end_time) VALUES ('" + file_name + "', '" + productNum + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "', '" + endTime + "')"
  439. # print(sql)
  440. dbController.execSql(sql)
  441. # if i==1:
  442. # break
  443. dbController.commit()
  444. else:
  445. result = {"approveResult":"0","rule":"白户"}
  446. uploadAudit(result, businessInfo["businessNum"], productNum)
  447. # 移动xml文件
  448. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  449. if not os.path.exists(basePath + "execed_new/"):
  450. os.mkdir(basePath + "execed_new/")
  451. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  452. if isPlt == "1":
  453. move(xml_path, descXmlPath)
  454. elif productNum == productNumHst:#海盛通
  455. # yuan原有商户模型入库
  456. dbController.getConn()
  457. parse_info = str(businessInfo["result"])
  458. parse_info = parse_info.replace("'", "\\'")
  459. filter_info = "此模型不产生结果"
  460. sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '" + filter_info + "', '" + parse_info + "')"
  461. # print(sql)
  462. dbController.execSql(sql)
  463. # if i==1:
  464. # break
  465. dbController.commit()
  466. if businessInfo["result"]!="":
  467. result = hst_rule_set.callLocal(businessInfo)
  468. uploadAudit(result,businessInfo["businessNum"],productNum)
  469. else:
  470. result = {"approveResult":"0","rule":"白户"}
  471. uploadAudit(result, businessInfo["businessNum"], productNum)
  472. # 移动xml文件
  473. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  474. if not os.path.exists(basePath + "execed_new/"):
  475. os.mkdir(basePath + "execed_new/")
  476. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  477. if isPlt == "1":
  478. move(xml_path, descXmlPath)
  479. else:
  480. # 走新增的自定义模型
  481. print("go selfdesign")
  482. customModel= CustomModel();
  483. res=customModel.doCustomModel(productNum,businessInfo,xml_path,file_name,startTime);
  484. print(res)
  485. if res==3:
  486. jsonPath = xml_path.replace(".xml", "_json.txt")
  487. dayStrPath = basePath + productNum + "/" + time.strftime('%Y%m%d', time.localtime(time.time())) + "/";
  488. descXmlPath = dayStrPath + "xml/" + os.path.basename(xml_path)
  489. descJsonPath = dayStrPath + "txt/" + os.path.basename(jsonPath)
  490. descTxtPath = dayStrPath + "txt/" + os.path.basename(txtPath)
  491. if not os.path.exists(basePath + productNum):
  492. os.mkdir(basePath + productNum)
  493. if not os.path.exists(dayStrPath):
  494. os.mkdir(dayStrPath)
  495. if not os.path.exists(dayStrPath + "xml/"):
  496. os.mkdir(dayStrPath + "xml/")
  497. if not os.path.exists(dayStrPath + "txt/"):
  498. os.mkdir(dayStrPath + "txt/")
  499. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  500. if isPlt == "1":
  501. move(xml_path, descXmlPath)
  502. shutil.move(jsonPath, descJsonPath)
  503. shutil.move(txtPath, descTxtPath)
  504. else :
  505. print("移动文件")
  506. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  507. # print("descXmlPath是"+str(businessInfo))
  508. if not os.path.exists(basePath + "execed_new/"):
  509. os.mkdir(basePath + "execed_new/")
  510. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  511. if isPlt == "1":
  512. move(xml_path, descXmlPath)
  513. # if(res):
  514. # result["approveResult"]="1"
  515. #
  516. # print(result);
  517. # 结果返回
  518. # uploadAudit(res, businessInfo["businessNum"], productNum)
  519. # 这个地方嵌进去结果
  520. i=0
  521. #本地jar
  522. if isPlt == "1":
  523. if not os.path.exists(basePath + "execed_txt/"):
  524. os.mkdir(basePath + "execed_txt/")
  525. descTxtPath = basePath + "execed_txt/" + os.path.basename(txtPath)
  526. shutil.move(txtPath, descTxtPath)
  527. #调用jar包
  528. def move(xml_path, descXmlPath):
  529. try:
  530. shutil.move(xml_path, descXmlPath)
  531. if not os.path.exists(basePath + "execed_xml/"):
  532. os.mkdir(basePath + "execed_xml/")
  533. shutil.move(xml_path+".txt", descXmlPath.replace("execed_new","execed_xml")+".txt")
  534. # os.remove(xml_path+".txt")#删除生成的xml.txt文件
  535. except:
  536. logger.info("移动文件 from " + xml_path + " to " + descXmlPath +" error")
  537. # #调用jar包
  538. # def move1(xml_path, descXmlPath):
  539. # try:
  540. # shutil.move(xml_path, descXmlPath)
  541. # # os.remove(xml_path+".txt")#删除生成的xml.txt文件
  542. # except:
  543. # logger.info("移动文件 from " + xml_path + " to " + descXmlPath +" error")
  544. def invokePboc(businessInfo,txtPath):
  545. # ===================================
  546. try:
  547. # print(businessInfo)
  548. businessNum = businessInfo["businessNum"]
  549. coopBusinessNum = businessInfo["coopBusinessNum"]
  550. pboc = PBOC()
  551. jarTxt = pboc.calc(txtPath,coopBusinessNum);
  552. result = json.loads(jarTxt)
  553. logger.info(result)
  554. if result.get("errcode")== None:
  555. uploadAudit(result,businessNum,businessInfo["productNum"])
  556. else:
  557. if businessInfo["isBaihu"]=="1":
  558. result = {"approveResult":"1"}
  559. uploadAudit(result, businessNum, businessInfo["productNum"])
  560. logger.error(result["errmsg"])
  561. except:
  562. logger.error("certificateNum=" + businessInfo["coopBusinessNum"])
  563. info = sys.exc_info()
  564. logger.error(info[0])
  565. logger.error(info[1])
  566. # logging.log(logging.ERROR, info[2])
  567. logger.error(traceback.extract_tb(info[2], 1))
  568. #调用xxwjar包
  569. def invokeXxw(businessInfo,txtPath):
  570. try:
  571. businessNum = businessInfo["businessNum"]
  572. coopBusinessNum = businessInfo["coopBusinessNum"]
  573. customerNum = businessInfo["customerNum"]
  574. certificateNum = businessInfo["certificateNum"]
  575. pboc = PBOC()
  576. jarTxt = pboc.calcXxw(coopBusinessNum,customerNum,txtPath);
  577. logger.info("jarTxt:"+jarTxt)
  578. result = json.loads(jarTxt)
  579. jsonPath = basePath+certificateNum+".txt"
  580. # logger.info(jsonPath)
  581. with open(jsonPath, 'w', encoding='utf-8') as fp:
  582. fp.write(jarTxt)
  583. uploadJsonFile(businessInfo,jsonPath)
  584. if not os.path.exists(basePath + "execed_txt/"):
  585. os.mkdir(basePath + "execed_txt/")
  586. descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath)
  587. shutil.move(jsonPath, descJsonPath)
  588. except:
  589. logger.error("certificateNum=" + businessInfo["coopBusinessNum"])
  590. info = sys.exc_info()
  591. logger.error(info[0])
  592. logger.error(info[1])
  593. logger.error(traceback.extract_tb(info[2], 1))
  594. #上传审批结果
  595. def uploadAudit(result,businessNum,productNum):
  596. global approResult
  597. approvalType = result["approveResult"]
  598. if productNum == productNumJz:
  599. if approvalType=="1":
  600. approvalOpinion = "征通信过"
  601. approvalType = "4"
  602. else:
  603. approvalOpinion = "征信拒绝"
  604. approvalType = "3"
  605. elif productNumKn.find(productNum)>=0 or productNum == productNumHst or productNum == productNumFd:
  606. if approvalType=="1":
  607. approvalOpinion = "征信通过"
  608. approvalType = "4"
  609. else:#快牛 需要添加拒绝原因
  610. approvalOpinion = "征信拒绝" + "#" + result["rule"]
  611. approvalType = "3"
  612. else:#自定义模型情况yuan
  613. if approvalType == "1":
  614. approvalOpinion = "征信通过"
  615. # print("yuan上传审批征信通过")
  616. approvalType = "4"
  617. else:
  618. approvalOpinion = "征信拒绝"
  619. approvalType = "3"
  620. taskKey = config.get("baseconf","taskKey")
  621. appoveApiUrl = config.get("baseconf","appoveApiUrl")
  622. key = config.get("baseconf", "AESKey")
  623. data = {"header":{
  624. "ticket": "2938123198320412343",
  625. "timestamp": int(int(round(time.time() * 1000+60*1000))),
  626. "nonce": config.get("baseconf", "nonce")
  627. },
  628. "body":{"approvalType": approvalType, "businessNum": businessNum,"taskKey":taskKey,"approvalOpinion":approvalOpinion}}
  629. access_token = dbController.getToken();
  630. # access_token = "3BWvQEAzX7W53UuX"
  631. if access_token == "":#获取token失败重新获取
  632. access_token = dbController.getToken();
  633. appoveApiUrl = appoveApiUrl+"?access_token="+access_token
  634. headers = {"Content-Type": "application/json"}
  635. jsonStr = json.dumps(data);
  636. jsonStr = jsonStr.replace('"',"\\\"")#必须替换才行
  637. logger.info(jsonStr)
  638. pboc = PBOC();
  639. # print("if encty")response是
  640. encryData = pboc.encrypt(jsonStr,key)
  641. # print("if encty11")
  642. encryData = encryData[0:len(encryData)-2]
  643. # print("had encry")
  644. logger.info(encryData)
  645. # print("if response")
  646. print("appoveApiUrl是: " + str(appoveApiUrl))
  647. print("encryData是: " + str(encryData))
  648. print("headers是: " + str(headers))
  649. response = requests.post(appoveApiUrl, data=encryData,headers=headers)
  650. text = response.text
  651. print("response是: " + str(response))
  652. print("text: " + str(text))
  653. pboc = PBOC();
  654. resultText = pboc.decrypt(text, config.get("baseconf", "AESKey"))
  655. print("resultText是: " + str(resultText))
  656. logger.info(businessNum + "#" + "uploadAudit upload_result:" + resultText)
  657. return str(resultText)
  658. #上传解析json 文件上传
  659. def uploadJsonFile(businessInfo,json_path):
  660. # ===================================
  661. try:
  662. fileName = os.path.basename(json_path)
  663. #上传文件逻辑
  664. logger.info(json_path+"#"+"准备上传文件")
  665. uploadApiUrl = config.get("baseconf", "uploadApiUrl");
  666. token = dbController.getToken();
  667. # token = "3BWvQEAzX7W53UuX"
  668. if token == "":#如果获取失败,重新获取
  669. token = dbController.getToken();
  670. uploadApiUrl = uploadApiUrl + "?access_token=" + token
  671. files = {'file': open(json_path, 'rb')}
  672. logger.info(fileName+"#"+"businessNum:"+businessInfo["businessNum"])
  673. data = {'docType': "23", 'businessNum': businessInfo["businessNum"]}
  674. response = requests.post(uploadApiUrl, files=files, data=data)
  675. text = response.text
  676. print("response是: " + str(response))
  677. print("text: " + str(text))
  678. logger.info("上传结果:"+text)
  679. pboc = PBOC();
  680. resultText = pboc.decrypt(text, config.get("baseconf", "AESKey"))
  681. print("resultText是: "+ str(resultText))
  682. logger.info(fileName+"#"+"uploadJsonFile:" + resultText)
  683. except:
  684. logger.error("certificateNum="+businessInfo["coopBusinessNum"])
  685. info = sys.exc_info()
  686. logger.error(info[0])
  687. logger.error(info[1])
  688. # logging.log(logging.ERROR, info[2])
  689. logger.error(traceback.extract_tb(info[2], 1))
  690. mailUtil = MailUtil();
  691. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56'
  692. # mailUtil.dingtalk("号码:" + businessInfo["certificateNum"]+"-"+businessInfo["businessNum"] + " 上传文件失败 ", webhook)
  693. if __name__ == '__main__':
  694. basePath = "D:/jin_rong/todo/"
  695. init_path = "test_data/"
  696. # basePath = "D:/mydocument/myprojects/creditreport/parse/"
  697. # file_name = sys.argv[1]
  698. # file_name = "周绍政_512925196801187190_523465671363788806-20221109114553780.xml"
  699. #
  700. file_name = "段国钊_432924196512260092_525976302319050756-20221116100213782.xml"
  701. # logger.info("Made request to REST API. [file_name={}]", file_name)
  702. # file_name = "侬志雪_532627199808160322_525990479343394824-202211161058332446.xml"
  703. # # file_name="dqy_320821199205184730_255559091387119623.xml"
  704. # file_name='zl_342425199204087911_255220233202055177.xml'
  705. # file_name="sxg_330719197909242017_257564808331673603.xml"
  706. # file_name="pxy_522401199510147022_258623031256039426.xml"
  707. # file_name = "任赞_130185199601221319_257538384191307779.xml"
  708. # file_name="朱宏辉_430521199607210734_256254065317527560.xml"
  709. # file_name="杨同山_340406197907213451_236806485030479874.xml"
  710. # # file_name = "1_210303197510273014_269873665141647369-202012091702117200.xml"
  711. # # file_name="任赞_130185199601221319_257538384191307779"
  712. # file_name="艾红平_520202198707150427_384652376306363396-202110221031275368.xml"
  713. # # file_name = "王彬彬_150429198902200911_5.xml"
  714. # file_name="裴增计_140522196602121517_331131028938695682-202105271756329018.xml"
  715. # file_name = "胡龙德_370829197506094916_389014816133485574-202111031126048397.xml"
  716. # file_name="周子彪_533024199501184513_257703742621630467.xml"
  717. xml_path = basePath + init_path + file_name
  718. start = timeit.default_timer();
  719. # if len(sys.argv) > 1:
  720. # basePath = sys.argv[1]
  721. # xml_path = basePath + sys.argv[2]
  722. # file_name = sys.argv[2]
  723. logger.info(xml_path+" 解析开始")
  724. startTime = datetime.now() #开始时间
  725. # print(xml_path+ " 解析开始")
  726. businessInfo = getBusinessInfo(xml_path)
  727. # uploadJsonFile(businessInfo,"D:/jin_rong/test_data/朱家铎_410922199111210030_255632073002334212.txt")
  728. # with open('D:/jin_rong/test_data/朱传光_371425199305027971_256979050910728199.txt', 'r', encoding='utf-8')as fp:
  729. # businessInfo['result'] = fp.read();
  730. # logger.info(businessInfo)
  731. if businessInfo["isBaihu"]!="2":#白户不上传1211
  732. try:
  733. process(startTime,businessInfo, basePath, xml_path, file_name)
  734. except:
  735. logger.error(traceback.print_exc())
  736. logger.error(traceback.format_exc())
  737. # if approResult==0:
  738. # print("征信通过")
  739. # dbController.getConn()
  740. # # print(len(sqlMap))
  741. # sql = "INSERT INTO credit_approval(approval_result,credit_name) VALUES ('征信通过', '" + file_name + "')"
  742. # print(sql)
  743. # dbController.execSql(sql)
  744. # # if i==1:
  745. # # break
  746. # dbController.commit()
  747. s = timeit.default_timer() - start;
  748. logger.info(str(s) + " 秒")
  749. logger.info(xml_path+" 解析完成")
  750. # 执行sql开始
  751. logger.info(xml_path + " 执行sql解析开始")
  752. # print("this is a result")
  753. sqlMap = businessInfo["sqlMap"]
  754. # 执行sql完成
  755. # logger.info(sqlMap)
  756. try:
  757. start = timeit.default_timer();
  758. dbController.getConn()
  759. # print(len(sqlMap))
  760. for sql in sqlMap:
  761. # i +=1
  762. dbController.execSql(sql)
  763. # if i==1:
  764. # break
  765. dbController.commit()
  766. s = timeit.default_timer() - start;
  767. logger.info(xml_path+str(s) + " 秒")
  768. except:
  769. logger.error(traceback.print_exc())
  770. logger.error(traceback.format_exc())
  771. logger.info(xml_path + " 执行sql解析失败")
  772. logger.info(xml_path + " 执行sql解析完成")