Commit a22d18be by 黄静

hj

parent 7bb854ad
# coding:utf-8
import traceback
from IndexItem import IndexItem
import json
#计算风控结果
import re
class IndexComputer:
funs=[]#公式集合
def Init(self):
return ""
def Compute(self,indexItem):#计算单一指标
#如果所有因子有一个为空,返回数据不存在
i=0
X=[]
Y=[]
V={}
S=[]
print(indexItem.Name)
print(indexItem.Funs)
print(indexItem.FactorsValue)
# 如果所有因子有一个为空,返回数据不存在
i = 0
X = []
Y = []
V = {}
S = []
for factorkey in indexItem.FactorsValue:
tempval=indexItem.FactorsValue[factorkey]
tempval = indexItem.FactorsValue[factorkey]
if indexItem.FactorsValue[factorkey] is None:
print("is None")
return "0"
......@@ -28,24 +24,22 @@ class IndexComputer:
except:
print("is isNumeric", indexItem.FactorsValue[factorkey])
return "0"
Y.append(float(indexItem.FactorsValue[factorkey]))
V[factorkey]=str("Y["+str(i)+"]")
V[factorkey] = str("Y[" + str(i) + "]")
S.append(factorkey)
i=i+1
#print(Y,V)
S=sorted(S, key=lambda i: len(i), reverse=True)
# print(S)
j=0
i = i + 1
S = sorted(S, key=lambda i: len(i), reverse=True)
j = 0
for fun in indexItem.Funs:
funstr=fun
print(indexItem.id,"===============gsid")
funstr = fun
for s in S:
funstr=funstr.replace(s,str(V[s]))
funstr = funstr.replace(s, str(V[s]))
strinfo = re.compile(r'X\[\d*\]=')
funstr = strinfo.sub("", funstr)
#funstr=funstr.replace("/X[d]=/","")
# funstr=funstr.replace("/X[d]=/","")
try:
print(funstr,"======funstr")
val = eval(funstr)
X.append(val)
j = j + 1
......@@ -53,7 +47,7 @@ class IndexComputer:
print('Handling run-time error:', err)
return "0"
except(ValueError):
print('other:',ValueError)
print('other:', ValueError)
return "0"
# try:
......@@ -61,5 +55,48 @@ class IndexComputer:
return "2"
else:
return "1"
# except:
# return "0"
\ No newline at end of file
def finance_Compute(self, indexItem):
finance_result = {}
i = 0
X = []
Y = []
V = {}
S = []
for factorkey in indexItem.FactorsValue:
tempval = indexItem.FactorsValue[factorkey]
if type(tempval) == str:
eval(tempval)
else:
tempval = tempval
Y.append(float(indexItem.FactorsValue[factorkey]))
V[factorkey] = str("Y[" + str(i) + "]")
S.append(factorkey)
i = i + 1
S = sorted(S, key=lambda i: len(i), reverse=True)
j = 0
for fun in indexItem.Funs:
funstr = fun
for s in S:
funstr = funstr.replace(s, str(V[s]))
strinfo = re.compile(r'X\[\d*\]=')
funstr = strinfo.sub("", funstr)
try:
if type(funstr) == str:
val = eval(funstr)
else:
val = funstr
X.append(val)
j = j + 1
except ZeroDivisionError as err:
print('finance error:', err)
return "∞"
except(ValueError):
print('other:', ValueError)
return "—"
Finance_result = int(X[j - 1])
if Finance_result ==0.0:
Finance_result= int(Finance_result)
else:
Finance_result=Finance_result
return Finance_result
\ No newline at end of file
......@@ -17,18 +17,21 @@ FK_DB_NAME = env_dist.get('FK_DB_NAME') #获取mysql库
class IndexGet:
conn = pymysql.connect(host=FK_DB_HOST,port=int(FK_DB_PORT),user=FK_DB_USER,password=FK_DB_PWD,db=FK_DB_NAME,charset="utf8mb4")
def Subscribe(self,span,type):
print("查询条件",span,type)
# conn = pymysql.connect(host='47.105.186.2',port=3307,user='caishui',password='jvmfTVDuG5YE(*Z',db='fktaxctl',charset="utf8mb4")
#检索财税03的情况:
def Subscribe(self,span,company_type):
cursor = self.conn.cursor()
list = []
sql = "SELECT * from gongshi WHERE span = %s and type = %s;"
sql = "SELECT * from gongshi2 WHERE span = %s and vat_type = %s order by indexName;"
try:
# 执行SQL语句
cursor.execute(sql,[span,type])
cursor.execute(sql,[span,company_type])
# 获取所有记录列表
results = cursor.fetchall()
# for row in results[1:2]:
for row in results:
indexItem=IndexItem()
indexItem.id = row[0]
indexItem.Name = row[1]
indexItem.IndexName = row[2]
funs = str(row[3])
......@@ -40,9 +43,10 @@ class IndexGet:
for fun in funs:
funsGrp.append(fun)
indexItem.Funs=funsGrp
indexItem.Factors = row[4]
indexItem.Span = row[5]
indexItem.Factors=row[4]
indexItem.Type = row[6]
indexItem.Category = row[8]
list.append(indexItem)
except:
print("Error: unable to fetch data")
......
......@@ -7,6 +7,4 @@ class IndexItem:
FactorsValue={}
Span=""
Type=""
Description=""
Description=""
\ No newline at end of file
......@@ -12,14 +12,16 @@ FK_DB_NAME = env_dist.get('FK_DB_NAME') #获取mysql库
class RiskComputer:
IndexRes={}
IndexRes1={}
indexs=[]
RCFactorsValue={}
# batch_No=0
def IndexsGet(self,indexs):
self.indexs=indexs
def FactorGet(self,factors):
self.RCFactorsValue=factors
def Compute(self):#计算所有指标
def Compute(self,Previous_Date):#计算所有指标
Curr_Date = Previous_Date['Curr_Date']
Previous_Date = Previous_Date['Previous_Date']
for indexItem in self.indexs:
factors=indexItem.Factors.split(":")
flag=True
......@@ -30,30 +32,61 @@ class RiskComputer:
else:
flag=False
break
if flag:
ic=IndexComputer()
ret=ic.Compute(indexItem)
self.IndexRes[indexItem.Name]=ret
ic=IndexComputer()
if indexItem.Category == 'tax_risk':
if flag:
ret=ic.Compute(indexItem)
self.IndexRes[indexItem.Name]=ret
else:
self.IndexRes[indexItem.Name]="0"
else:
self.IndexRes[indexItem.Name]="0"
if flag:
ret = ic.finance_Compute(indexItem)
else:
ret = "—"
a=indexItem.Name
if(a not in self.IndexRes1):
self.IndexRes1[a]={Curr_Date:ret}
else:
if(Previous_Date not in self.IndexRes1[a]):
self.IndexRes1[a][Previous_Date]=ret
else:
self.IndexRes1[a]["change_rate"]=ret
def Ret2Json(self):#把指标结果转化为樊辉需要的Json
#json写入数据库
# db = pymysql.connect(host='47.105.186.2',port=3307,user='caishui',password='jvmfTVDuG5YE(*Z',db='fktaxctl',charset="utf8mb4")
db = pymysql.connect(host=FK_DB_HOST,port=int(FK_DB_PORT),user=FK_DB_USER,password=FK_DB_PWD,db=FK_DB_NAME,charset="utf8mb4")
cursor = db.cursor()
for key in self.IndexRes:
sql = """INSERT INTO result_set(batchno,indexName,status) VALUES ('"""+str(self.batch_No)+"""', '"""+str(key)+"""','"""+str(self.IndexRes[key])+"""')"""
try:
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
db.rollback()
# 关闭数据库连接
cursor = db.cursor()
arr = [self.IndexRes,self.IndexRes1]
ss = ['ri','fi']
for i,index_results in enumerate(arr): #索引,值
print(i,"=============i")
print(index_results, "=============qq")
for key in index_results:
# print(self.indexs['id'],"=======公式id")
#'factors_name':'指标状态'
ri1 = {}
ri1[key]= index_results[key]
insert_batch_No = "'"+str(self.batch_No)+"'"
fi_dict={"营业总收入":"Total_trade_income","利润总额":"Total_profit","净利润":"Net_profit","负债总额":"Total_Liabilities","所有人权益":"Owner_rights","销售总额":"Total_sales",
"资产总额":"Total_assets","负债及所有者权益合计":"Total_liabilities_and_Owner_rights","销售费用":"sales_expense","销售净利率":"Net_interest_rate","产权比率":"Equity_ratio",
"销售增长率":"sales_growth_rate","净利润增长率":"Net_profit_growth_rate","资产负债比率":"Gearing_ratio","总资产增长率":"total_assets_growth_rate"
}
if key in fi_dict:
insert_key = str(fi_dict[key])
else:
insert_key = str(key)
insert_status = str(index_results[key])
sql='INSERT INTO result_set(Batchno,indexName,status,biztype) VALUES (%s,"%s","%s","%s")'%(insert_batch_No,insert_key,insert_status,ss[i])
# print(sql,"======风控sql")
try:
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except Exception as e:
print(e)
db.rollback()
# 关闭数据库连接
db.close()
jsonStr=json.dumps(self.IndexRes,ensure_ascii=False, indent=4, separators=(',', ':'))
# #写入信息到Redis的队列中
# redisPool = redis.ConnectionPool(host=FK_REDIS_HOST, port=8967, password='Gongsibao2018',db=2)
# client = redis.Redis(connection_pool=redisPool)
# client.lpush('funcmq','{"key":'+str(datetime.datetime.now())+',"content":'+str(self.batch_No)+'}')
jsonStr=json.dumps(self.IndexRes1,ensure_ascii=False, indent=4, separators=(',', ':'))
return jsonStr
\ No newline at end of file
......@@ -13,6 +13,7 @@ FK_DB_PWD = env_dist.get('FK_DB_PWD') #获取mysql密码
FK_DB_NAME = env_dist.get('FK_DB_NAME') #获取mysql库
class UpdateId:
conn = pymysql.connect(host=FK_DB_HOST,port=int(FK_DB_PORT),user=FK_DB_USER,password=FK_DB_PWD,db=FK_DB_NAME,charset="utf8mb4")
# conn = pymysql.connect(host='47.105.186.2', port=3307, user='caishui', password='jvmfTVDuG5YE(*Z', db='fktaxctl',charset="utf8mb4")
def Update(self,BatchNo):
cursor = self.conn.cursor()
sql = "UPDATE upload_batch SET batchstatuscode = 'rptover' WHERE id = %s;"
......
......@@ -5,7 +5,7 @@
# @File : getInfoByBatchNo.py
# @Software: PyCharm
import requests,json,redis,re,os
from RiskComputer import RiskComputer
from RiskComputer import RiskComputer #计算并将结果写入mysql数据库
from IndexGet import IndexGet
from Update import UpdateId
......@@ -27,16 +27,17 @@ def getInfo_ByBatchNo(BatchNo):
name = {"batchCode":BatchNo} # 根据任务号进行接口访问参数
taskInfo = requests.get(url,name) # 访问接口返回的信息
taskInfo_New = json.loads(taskInfo.text)
print("任务信息获取:",taskInfo_New,BatchNo)
# print("任务信息获取:",taskInfo_New,BatchNo)
if taskInfo_New['data'] is None:
print("taskInfo_New['data'] is None")
client.rpop('notifyRule')
# client.rpop('notifyRule')
quit()
Info_Task = taskInfo_New['data']
AccountInfo = {}
AccountInfo["taxCode"]=Info_Task['taxCode'] # 获取纳税人识别号
AccountInfo["batchCode"]=Info_Task['batchCode'] # 获取批次号
AccountInfo["timeType"] = Info_Task['timeType'] # 获取时间类型
AccountInfo["timeGet"] = Info_Task['timeGet'] # 获取当期时间
if Info_Task["timeType"] =="Y":
currentIssue = Info_Task["checkYear"]
elif Info_Task["timeType"] =="Q":
......@@ -55,13 +56,11 @@ def customBatch(custom_currentIssue,offset,timeType):
batchInfo = requests.post(url,name) # 访问接口返回的信息
batchInfo_New = json.loads(batchInfo.text)
currentIssue = batchInfo_New['data']
print("接口计算的期数为:",currentIssue)
# print("接口计算的期数为:",currentIssue)
return currentIssue
#获取L期数信息
def Transformer(currentYear,YearyearInfo,issueNum):
currentIssue=""
year=0
if "L" in YearyearInfo:
year=currentYear-1
else:
......@@ -76,19 +75,45 @@ def Transformer(currentYear,YearyearInfo,issueNum):
if(len(issueNum))<2:
issueNum="0"+str(issueNum)
currentIssue=str(year)+str(issueNum)
print("自己计算期数为:",currentIssue)
# print("自己计算期数为:",currentIssue)
return currentIssue
# 调用映射接口,转化为位置编码
def posmap(talbe,cellPos):
talbe = str(talbe)
cellPos = str(cellPos)
url = 'https://fkctlapi.gongsibao.com/api/rpt/rptApi/queryPosMap'
positionInfo = requests.post(url)
positionInfo_New = json.loads(positionInfo.text)
if positionInfo_New['status'] == 0:
posmap_result = {}
posmap_lists = positionInfo_New['data']['data']
for posmap_list in posmap_lists: #cellPos:坐标值;cellDesc:单元格中文编码
if (posmap_list["rpttypecode"] == talbe and posmap_list["nposition"] == cellPos):
posmap_result['cellDesc'] = posmap_list["position"]
posmap_result['cellPos'] = ""
# print(posmap_result['cellDesc'],"===============因子中文编码位置")
else:
posmap_result['cellDesc'] = ""
posmap_result['cellPos'] = cellPos
else:
posmap_result = '因子中文编码查询错误'
return posmap_result
#获取本期指定报表的单元格值
def getValue(batchCode,cellPos,currentIssue,fileCode,taxCode,timeType):
def getValue(batchCode,cellDesc,cellPos,currentIssue,fileCode,taxCode,timeType):
batchCode = str(batchCode)
cellPos =str(cellPos)
cellDesc =str(cellDesc)
cellPos = str(cellPos)
currentIssue =str(currentIssue)
fileCode = str(fileCode)
taxCode = str(taxCode)
timeType =str(timeType)
url = 'https://risk-api.gongsibao.com/risk-api/getCellValuesByIssue' #获取信息接口
name = {"batchCode":batchCode,"cellPos":cellPos,"currentIssue":currentIssue,"fileCode":fileCode,"taxCode":taxCode,"timeType":timeType} # 接口访问参数
if cellDesc == "":
name = {"batchCode":batchCode,"cellPos":cellPos,"currentIssue":currentIssue,"fileCode":fileCode,"taxCode":taxCode,"timeType":timeType} # 接口访问参数
else:
name = {"batchCode":batchCode,"cellDesc":cellDesc,"currentIssue":currentIssue,"fileCode":fileCode,"taxCode":taxCode,"timeType":timeType} # 接口访问参数
contentInfo = requests.post(url,name) # 访问接口返回的信息
batchInfo_New = json.loads(contentInfo.text)
index_Values = batchInfo_New['data']
......@@ -97,11 +122,10 @@ def getValue(batchCode,cellPos,currentIssue,fileCode,taxCode,timeType):
else:
index = index_Values[0]
index_Value = index["cellContent"]
print("单元格数据为:",index_Value)
# print("单元格数据为:",index_Value)
return index_Value
def fun(str):
print(str,"ooo")
fun_list=[]
indexdict={}
if len(str.split("_")[3])<=2 :
......@@ -109,17 +133,100 @@ def fun(str):
table=str.split("_")[0]
row=str.split("_")[1]
cloumn=str.split("_")[2]
coordinate = row+"-"+cloumn
times=str.split("_")[3]
nums=re.findall(r'([A-Z]+)([0-9]+)',times)[0][1]
indexdict["table"]=table
indexdict["row"] = row
indexdict["cloumn"]= cloumn
indexdict["position"] = coordinate
indexdict["times"] = times
indexdict["nums"] = "-"+nums
indexdict["issueNum"] = nums
fun_list.append(indexdict)
return fun_list
def Previous(tasks):
global Previous_month, Previous_Date
Date_Info={}
Curr_Get = tasks['timeGet']
Curr_year = Curr_Get[0:4]
Curr_month = Curr_Get[4:7]
Curr_Date = Curr_year + '-' + Curr_month
print(Curr_month, "=================Curr_month")
#转化当前时间
if Curr_month in ("01", "03", "05", "07", "08", "10", "12"):
Curr_Day = "31"
elif Curr_month in ("04", "06", "09", "11"):
Curr_Day = "30"
else:
if Curr_year % 4 == 0 and Curr_year % 400 == 0:
Curr_Day = "29"
elif Curr_year % 4 == 0 and Curr_year % 100 != 0:
Curr_Day = "29"
else:
Curr_Day = "28"
Curr_Date = Curr_Date+'-'+Curr_Day
#打印出上一期时间
if tasks["timeType"] == "Y":
Previous_year = int(Curr_year)-1
Previous_Date = str(Previous_year)+"-"+Curr_month
elif tasks["timeType"] == "Q":
if int(Curr_month)<= 3 :
Previous_year = int(Curr_year) - 1
Previous_month = 12
else:
Previous_year = Curr_year
str_month = str(int(Curr_month)-3)
if len(str_month)>2:
Previous_month = int(Curr_month)-3
else:
Previous_month = "0"+str_month
Previous_Date = str(Previous_year)+ '-' +str(Previous_month)
else:
if int(Curr_month) == 1:
Previous_year = int(Curr_year) - 1
Previous_month = 12
else:
Previous_year = Curr_year
str_month = str(int(Curr_month) - 1)
if len(str_month)>2:
Previous_month = str_month
else:
Previous_month = "0"+str_month
Previous_Date = str(Previous_year) + '-' + str(Previous_month)
if Previous_month in ("01", "03", "05", "07", "08", "10", "12"):
Previous_Day = "31"
elif Previous_month in ("04", "06", "09", "11"):
Previous_Day = "30"
else:
if Previous_year % 4 == 0 and Previous_year % 400 == 0:
Previous_Day = "29"
elif Previous_year % 4 == 0 and Previous_year % 100 != 0:
Previous_Day = "29"
else:
Previous_Day = "28"
Previous_Date = Previous_Date + '-' + Previous_Day
Date_Info["Curr_Date"] = Curr_Date
Date_Info["Previous_Date"] = Previous_Date
return Date_Info
def report(BatchNo):
print(BatchNo,"=============触发生成报告接口")
url = 'https://fkctlapi.gongsibao.com/api/rpt/rptApi/buildWordReport'
name = {"batchid": BatchNo}
reportInfo = requests.post(url,name)
reportInfo_New = json.loads(reportInfo.text)
print(reportInfo_New, "reportInfo_New")
if reportInfo_New['status'] == 0:
report_result = '触发生成报告接口成功'
else:
report_result = '触发生成报告接口失败'
return report_result
redisPool = redis.ConnectionPool(host=FK_REDIS_HOST, port=int(FK_REDIS_PORT), password=FK_REDIS_PWD,db=FK_REDIS_DB)
client = redis.Redis(connection_pool=redisPool)
a = client.lrange('notifyRule',-1,-1)
......@@ -140,18 +247,23 @@ content=result["content"]
print(content,"content")
taxTypeCode=content["taxTypeCode"]#企业性质
accountTypeCode=content["accountTypeCode"]#会计准则
# accountTypeCode="S"#会计准则
BatchNo=content["batchCode"]
# BatchNo=226
# taxTypeCode = "ST"
if taxTypeCode=="ST":
taxTypeCode="S"
else:
taxTypeCode="G"
type=taxTypeCode+"-"+accountTypeCode
indexsGet=IndexGet()#获取所有指标和所有因子
indexsGet=IndexGet()#获取所有指标信息
#根据任务号获取任务信息
Info_task=getInfo_ByBatchNo(BatchNo)
tasks=Info_task[0]
Previous_Date=Previous(tasks)
indexs=indexsGet.Subscribe(tasks['timeType'],type)#根据时间类型和企业类型获取特征s
print(tasks['timeType'],type,"==============获取查询公式条件")
# indexs=indexsGet.Subscribe('Q','G-N')#根据时间类型和企业类型获取特征
factors={}
for indexItem in indexs:
factorsStr=indexItem.Factors
......@@ -163,9 +275,13 @@ for indexItem in indexs:
factorsnew={}
for factor in factors:#获取所有因子
fun_list=fun(str(factor))
index=fun_list[0]
index = fun_list[0]
cellPos=index['position']
talbe = index['table']
batchCode = BatchNo
cellPos = index['row'] + "-" +index['cloumn']
posmap_result=posmap(talbe,cellPos)
position_data = posmap_result['cellPos']
cellDesc = posmap_result['cellDesc']
offset = index['nums']
times = index['times']
issueNum = index['issueNum']
......@@ -175,26 +291,25 @@ for factor in factors:#获取所有因子
taxCode = tasks["taxCode"]
timeType = tasks["timeType"]
currentYear = int(currentIssue[0:4])
#获取任务信息
indexsGet = IndexGet() # 获取所有指标和所有因子
# 获取L期数信息
Info_LBatch=0
if "L" in YearyearInfo:
Info_LBatch = Transformer(currentYear, YearyearInfo, issueNum)
else:
# 获取自定义期数
Info_LBatch=customBatch(currentIssue,offset,timeType)
# 获取单元格值
index_Value = getValue(batchCode,cellPos,Info_LBatch,fileCode,taxCode,timeType)
index_Value = getValue(batchCode,cellDesc,cellPos,currentIssue,fileCode,taxCode,timeType)
factorsnew[factor] = index_Value
factors=factorsnew
rc=RiskComputer() #构建指标计算对象
rc.batch_No = batchCode
rc.batch_No = BatchNo
rc.indexs = indexs
rc.FactorGet(factors)#把因子装入对象
rc.IndexsGet(indexs)#把指标装入对象
rc.Compute()#指标计算
rc.Compute(Previous_Date)#指标计算
jsonstr = rc.Ret2Json()#指标输出成樊辉要的格式
report_result = report(BatchNo) #触发生成报告接口
print(jsonstr)
client.rpop('notifyRule')
Updqtestatus=UpdateId()
......
安装环境:3.7.2
安装包:pip install redis
pip install pymysql
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment