1859 lines
74 KiB
Python
1859 lines
74 KiB
Python
# 基础函数配置
|
|
import pandas as pd
|
|
import pandas as pd
|
|
import requests
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
import json
|
|
import numpy as np
|
|
import time
|
|
|
|
ROOT = Path('.').absolute() # 当前工作目录
|
|
|
|
def generateToken() -> str:
|
|
""" 生成 token """
|
|
|
|
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
|
|
|
|
# 该信息在钉钉开放应用中
|
|
data = {
|
|
"appKey": "ding5kqocon5s9oph5uq",
|
|
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
|
|
}
|
|
|
|
res = requests.post(token_api, json=data)
|
|
token = res.json()['accessToken']
|
|
|
|
return token
|
|
def read_instances_pt(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
def read_instances(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_delete(token, formInstanceId):
|
|
""" 函数功能:调用本接口删除表单数据。 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formInstanceId" : formInstanceId
|
|
}
|
|
|
|
res = requests.delete(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_new(FORMID,formData):
|
|
""" 通过实例id 获取表单内容 """
|
|
api = f'https://api.dingtalk.com/v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
print(res.json())
|
|
|
|
return res.json()
|
|
|
|
|
|
def component(FORMID,TOKEN):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/formFields'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
# "language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.get(api, headers=headers, json=payload)
|
|
|
|
return res.json()
|
|
def Batch_creation(FORMID,TOKEN,ALL_formData):
|
|
""" 批量新建 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchSave'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"noExecuteExpression" : "true",
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"keepRunningAfterException" : "true",
|
|
"userId" : "yida_pub_account",
|
|
"formDataJsonList" : json.dumps(ALL_formData, cls=NpEncoder),
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in_batches(FORMID,TOKEN,ALL_DATA_instance):
|
|
""" 批量删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchRemove'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"formInstanceIdList" : json.dumps(ALL_DATA_instance, cls=NpEncoder),
|
|
"userId" : "yida_pub_account",
|
|
"executeExpression" : "false" # 不触发
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in(TOKEN,formInstanceIdList):
|
|
""" 逐条删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances?appType=APP_UYZ0KG6L0CCNV80GZ66O&systemToken=XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2&userId=yida_pub_account&language=zh_CN&formInstanceId={formInstanceIdList}'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
res = requests.delete(api, headers=headers)
|
|
return res.json()
|
|
def Retrieve_employee_id(TOKEN,offset):
|
|
""" 获取在职员工列表 """
|
|
api = f'https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob?access_token={TOKEN}'
|
|
|
|
payload = {
|
|
"offset":offset,
|
|
"size":50,
|
|
"status_list":"2,3,5,-1"
|
|
}
|
|
|
|
res = requests.post(api, json=payload)
|
|
return res.json()
|
|
|
|
def Retrieve_employee_list(TOKEN,userid):
|
|
""" 获取员工花名册字段信息 """
|
|
api = f'https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list?access_token={TOKEN}'
|
|
|
|
payload = {
|
|
"userid_list":userid,
|
|
"field_filter_list":"sys00-name",
|
|
"agentid":"1405052868"
|
|
}
|
|
|
|
res = requests.post(api, json=payload)
|
|
return res.json()
|
|
|
|
|
|
class NpEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, np.integer):
|
|
return int(obj)
|
|
elif isinstance(obj, np.floating):
|
|
return float(obj)
|
|
elif isinstance(obj, np.ndarray):
|
|
return obj.tolist()
|
|
else:
|
|
return super(NpEncoder, self).default(obj)
|
|
TOKEN = generateToken()
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6" # 宜搭员工-ID对应表 FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6
|
|
|
|
'''批量删除NGV数据'''
|
|
for i in range(0,10):
|
|
default = True
|
|
while default:
|
|
try:
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 +1
|
|
print(form_data.get('totalCount'))
|
|
if form_data.get('totalCount')<=0:
|
|
default = False
|
|
for i in range(PAGES, 1,-1):
|
|
ALL_DATA_instance = []
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_instance.append(data['formInstanceId'])
|
|
print(f'读取到表单中 {len(ALL_DATA_instance)} 条数据!')
|
|
red_delete = delete_in_batches(FORMID,TOKEN,ALL_DATA_instance)
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
formInstanceId = data['formInstanceId']
|
|
delete_in(TOKEN,formInstanceId)
|
|
print(f'单条删除 {formInstanceId} 数据!')
|
|
except:
|
|
pass
|
|
|
|
'''遍历数据进行新建'''
|
|
offset = 0
|
|
while offset != 10088208820:
|
|
try:
|
|
ALL_formData = []
|
|
res_list = Retrieve_employee_id(TOKEN,offset)
|
|
try:
|
|
offset = res_list["result"]["next_cursor"]
|
|
except:
|
|
offset = 10088208820
|
|
res_list["result"]["data_list"]
|
|
for a in range(0,len(res_list["result"]["data_list"])):
|
|
if res_list["result"]["data_list"][a] !="69365621842352":
|
|
formData = {
|
|
'textField_lfrw3u59':res_list["result"]["data_list"][a],# 员工ID
|
|
'textField_lfrw3u58':Retrieve_employee_list(TOKEN,res_list["result"]["data_list"][a])["result"][0]['field_data_list'][0]['field_value_list'][0]['label']# 员工名称
|
|
}
|
|
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
|
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
|
print(res_new)
|
|
except:
|
|
pass
|
|
|
|
# -*- coding: utf-8 -*-
|
|
import psycopg2
|
|
import pandas as pd
|
|
import calendar
|
|
import datetime
|
|
# 获得连接
|
|
conn = psycopg2.connect(database="f6_bi", user="BASIC$ro_caowei", password="!ro_caowei123", host="hgprecn-cn-nif1vnv0y002-cn-shanghai.hologres.aliyuncs.com", port="80")
|
|
# 获得游标对象,一个游标对象可以对数据库进行执行操作
|
|
cursor = conn.cursor()
|
|
|
|
# 获取当前日期
|
|
today = datetime.date.today()
|
|
# 获取当月的第一天
|
|
first_day_of_month = int(today.replace(day=1).strftime('%Y%m%d'))
|
|
# 获取当月的最后一天
|
|
last_day_of_month = int(today.replace(day=calendar.monthrange(today.year, today.month)[1]).strftime('%Y%m%d'))
|
|
|
|
# sql语句 建表
|
|
sql =f"""SELECT * FROM "public"."holo_ads_report_sales_saas_to_renew_org_regular_income_d" WHERE "pt" >= '{first_day_of_month}' AND "pt" <= '{last_day_of_month}';"""
|
|
# 执行语句
|
|
cursor.execute(sql)
|
|
# 获取结果集的每一行
|
|
rows = cursor.fetchall()
|
|
# 获取所有字段名
|
|
all_fields = cursor.description
|
|
#执行结果转化为dataframe
|
|
col = []
|
|
for i in all_fields:
|
|
col.append(i[0])
|
|
data_details = pd.DataFrame(list(rows),columns=col)
|
|
data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
|
# 关闭数据库连接
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
# 基础函数配置
|
|
import pandas as pd
|
|
import pandas as pd
|
|
import requests
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
import json
|
|
import numpy as np
|
|
import time
|
|
|
|
ROOT = Path('.').absolute() # 当前工作目录
|
|
|
|
def generateToken() -> str:
|
|
""" 生成 token """
|
|
|
|
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
|
|
|
|
# 该信息在钉钉开放应用中
|
|
data = {
|
|
"appKey": "ding5kqocon5s9oph5uq",
|
|
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
|
|
}
|
|
|
|
res = requests.post(token_api, json=data)
|
|
token = res.json()['accessToken']
|
|
|
|
return token
|
|
|
|
def read_instances(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_delete(token, formInstanceId):
|
|
""" 函数功能:调用本接口删除表单数据。 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formInstanceId" : formInstanceId
|
|
}
|
|
|
|
res = requests.delete(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_new(FORMID,formData):
|
|
""" 通过实例id 获取表单内容 """
|
|
api = f'https://api.dingtalk.com/v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
print(res.json())
|
|
|
|
return res.json()
|
|
|
|
|
|
def component(FORMID,TOKEN):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/formFields'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
# "language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.get(api, headers=headers, json=payload)
|
|
|
|
return res.json()
|
|
def Batch_creation(FORMID,TOKEN,ALL_formData):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchSave'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"noExecuteExpression" : "true",
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"keepRunningAfterException" : "true",
|
|
"userId" : "yida_pub_account",
|
|
"formDataJsonList" : json.dumps(ALL_formData, cls=NpEncoder),
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in_batches(FORMID,TOKEN,ALL_DATA_instance):
|
|
""" 批量删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchRemove'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"formInstanceIdList" : json.dumps(ALL_DATA_instance, cls=NpEncoder),
|
|
"userId" : "yida_pub_account",
|
|
"executeExpression" : "false" # 不触发
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in(TOKEN,formInstanceIdList):
|
|
""" 逐条删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances?appType=APP_UYZ0KG6L0CCNV80GZ66O&systemToken=XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2&userId=yida_pub_account&language=zh_CN&formInstanceId={formInstanceIdList}'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
res = requests.delete(api, headers=headers)
|
|
return res.json()
|
|
|
|
class NpEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, np.integer):
|
|
return int(obj)
|
|
elif isinstance(obj, np.floating):
|
|
return float(obj)
|
|
elif isinstance(obj, np.ndarray):
|
|
return obj.tolist()
|
|
else:
|
|
return super(NpEncoder, self).default(obj)
|
|
TOKEN = generateToken()
|
|
'''读取员工对应关系:宜搭员工-ID对应表 '''
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6" # 宜搭员工-ID对应表 FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6
|
|
# 读取流程表单数据
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 + 1
|
|
ALL_DATA_staff = {}
|
|
""" 获取全量数据 """
|
|
for i in range(1, PAGES+1):
|
|
# form_data = read_processes_instances(token=TOKEN, formUuid=FORMID, createFromTimeGMT=CREATE_FROM, createToTimeGMT=CREATE_TO, page=i, n=100, searchField={'textField_l7if5ff9': '否'})
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=i, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_staff[data['formData']['textField_lfrw3u58']]=data['formData']['textField_lfrw3u59']
|
|
|
|
'''获取表内控件信息 '''
|
|
FORMID = "FORM-XN966G71FGWAWFJU6P9MA5MPMC6829BG110ILU"
|
|
component_list = component(FORMID,TOKEN)
|
|
for i in range(len(component_list['result'])):
|
|
componentName = component_list['result'][i]['componentName']
|
|
name_value = component_list['result'][i]['label']['value']
|
|
fieldId = component_list['result'][i]['fieldId']
|
|
print("'",fieldId,"':","data_details[",name_value,"][i], # ",name_value)
|
|
|
|
|
|
# '''批量删除NGV数据'''
|
|
for i in range(0,10):
|
|
default = True
|
|
while default:
|
|
FORMID = "FORM-XN966G71FGWAWFJU6P9MA5MPMC6829BG110ILU"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 +1
|
|
print(form_data.get('totalCount'))
|
|
if form_data.get('totalCount')<=0:
|
|
default = False
|
|
for i in range(PAGES, 1,-1):
|
|
ALL_DATA_instance = []
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_instance.append(data['formInstanceId'])
|
|
print(f'读取到表单中 {len(ALL_DATA_instance)} 条数据!')
|
|
red_delete = delete_in_batches(FORMID,TOKEN,ALL_DATA_instance)
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
formInstanceId = data['formInstanceId']
|
|
delete_in(TOKEN,formInstanceId)
|
|
print(f'单条删除 {formInstanceId} 数据!')
|
|
|
|
'''遍历数据进行新建'''
|
|
data_details = data_details.astype('string')
|
|
data_details = data_details.fillna('',inplace=False)
|
|
for a in range(0,len(data_details["date_fmt"]),100):
|
|
ALL_formData = []
|
|
for i in range(a,a+100): # for i in range(len(data_NGV["date_fmt"])):
|
|
try:
|
|
formData = {
|
|
'textField_05ktjv3':data_details["date_fmt"][i],#"date_fmt"
|
|
'textField_sjt4g4u':data_details["org_crm_id"][i],#"org_crm_id"
|
|
'textField_dninbhg':data_details["org_id"][i],#"org_id"
|
|
'textField_v4p3wpf':data_details["org_code"][i],#"org_code"
|
|
'textField_rt77wej':data_details["org_name"][i],#"org_name"
|
|
'textField_iwoh6hs':data_details["is_main_org"][i],#"is_main_org"
|
|
'textField_x43bdsp':data_details["group_id"][i],#"group_id"
|
|
'textField_ciih4ya':data_details["group_name"][i],#"group_name"
|
|
'dateField_d9qfuns':int(time.mktime(time.strptime(data_details["saas_create_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["saas_create_time"][i] !='' else '',#"saas_create_time"
|
|
'dateField_5ypoy57':int(time.mktime(time.strptime(data_details["to_renew_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["to_renew_time"][i] !='' else '',#"to_renew_time"
|
|
'textField_qdfzzs0':data_details["saas_edition_fmt"][i],#"saas_edition_fmt"
|
|
'textField_t8435fj':data_details["version_amount_total"][i],#"version_amount_total"
|
|
'textField_7t38y19':data_details["version_amount_saas"][i],#"version_amount_saas"
|
|
'textField_byiv4dl':data_details["version_amount_other"][i],#"version_amount_other"
|
|
'textField_h4kdrs7':data_details["unversion_amount_total"][i],#"unversion_amount_total"
|
|
'textField_exj8c95':data_details["unversion_amount_mngv"][i],#"unversion_amount_mngv"
|
|
'textField_mw4xx1u':data_details["unversion_amount_message"][i],#"unversion_amount_message"
|
|
# 'employeeField_li011zja':ALL_DATA_staff[data_details["service_impl_principal"][i]] if data_details["service_impl_principal"][i] !='' else '',#"service_impl_principal"
|
|
# 'employeeField_li011zjb':ALL_DATA_staff[data_details["area_manager"][i]] if data_details["area_manager"][i] !='' else '',#"area_manager"
|
|
'textField_dws7nm0':data_details["region_name"][i],#"region_name"
|
|
'textField_cko12i7':data_details["branch_name"][i],#"branch_name"
|
|
'dateField_emp5rqh':int(time.mktime(time.strptime(data_details["etl_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["etl_time"][i] !='' else '',#"etl_time"
|
|
'textField_rvee4bk':data_details["group_grade"][i],#"group_grade"
|
|
'textField_wicoruo':data_details["province_name"][i],#"province_name"
|
|
'textField_lodaran':data_details["city_name"][i],#"city_name"
|
|
# 'employeeField_li011zjc':ALL_DATA_staff[data_details["technician"][i]] if data_details["technician"][i] !='' else '',#"technician"
|
|
'textField_ss0efql':data_details["from_flag_fmt"][i],#"from_flag_fmt"
|
|
'textField_0rq00hg':data_details["franchise_group_name"][i],#"franchise_group_name"
|
|
'textField_pmy2x21':data_details["pt"][i],#"pt"
|
|
}
|
|
try:
|
|
formData['employeeField_li011zjc'] = ALL_DATA_staff[data_details["technician"][i]]
|
|
except:
|
|
formData['employeeField_li011zjc'] = ""
|
|
try:
|
|
formData['employeeField_li011zjb'] = ALL_DATA_staff[data_details["area_manager"][i]]
|
|
except:
|
|
formData['employeeField_li011zjb'] = ""
|
|
try:
|
|
formData['employeeField_li011zja'] = ALL_DATA_staff[data_details["service_impl_principal"][i]]
|
|
except:
|
|
formData['employeeField_li011zja'] = ""
|
|
|
|
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
|
except:
|
|
pass
|
|
FORMID = "FORM-XN966G71FGWAWFJU6P9MA5MPMC6829BG110ILU"
|
|
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
|
print("新建第",i,"条数据!",res_new)
|
|
time.sleep(10)
|
|
|
|
'''校验是否新建正常'''
|
|
time.sleep(120)
|
|
FORMID = "FORM-XN966G71FGWAWFJU6P9MA5MPMC6829BG110ILU"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
if int(form_data.get('totalCount')) ==len(data_details):
|
|
print("数据新建成功!")
|
|
else:
|
|
|
|
def start_instance_process(token: str, name):
|
|
|
|
"""发送宜搭表单 -- 发起流程表单
|
|
|
|
Args:
|
|
token
|
|
data:需要发送的数据字典
|
|
"""
|
|
|
|
yida_api = "https://api.dingtalk.com/v1.0/yida/processes/instances/start"
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
send_data = {
|
|
"textField_l9fe0uiw": name,
|
|
"textField_l9fe0uiv": name
|
|
}
|
|
|
|
payload = {
|
|
"appType": "APP_TNVBVZ3K8G56HG03Z45Q",
|
|
"systemToken": "CH7669818R0WN18TYTYJ42PE6GY22WZN0BYWKD1",
|
|
"userId": "yida_pub_account",# 超级管理员账号
|
|
"language": "zh_CN",
|
|
"formUuid": "FORM-UX866Q61GNLAZBCIEDF77BGVIIR83K82WYPHLH2",
|
|
"formDataJson": json.dumps(send_data),
|
|
"processCode":"TPROC--UX866Q61GNLAZBCIEDF77BGVIIR83M92WYPHLI2"
|
|
}
|
|
|
|
res = requests.post(yida_api, headers=headers, json=payload)
|
|
return res
|
|
try:
|
|
name = "1、月度续费率分母:holo_ads_report_sales_saas_to_renew_org_regular_income_d 新建条数不正确!"
|
|
res_yujing = start_instance_process(TOKEN,name)
|
|
except:
|
|
pass
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
import psycopg2
|
|
import pandas as pd
|
|
import calendar
|
|
import datetime
|
|
# 获得连接
|
|
conn = psycopg2.connect(database="f6_bi", user="BASIC$ro_caowei", password="!ro_caowei123", host="hgprecn-cn-nif1vnv0y002-cn-shanghai.hologres.aliyuncs.com", port="80")
|
|
# 获得游标对象,一个游标对象可以对数据库进行执行操作
|
|
cursor = conn.cursor()
|
|
|
|
# 获取当前日期
|
|
today = datetime.date.today()
|
|
# 获取当月
|
|
first_day_of_month = int(today.replace().strftime('%Y%m'))
|
|
|
|
|
|
# sql语句 建表
|
|
sql =f"""SELECT * FROM "public"."holo_ads_report_sales_saas_to_renew_org_summary_income_d" WHERE "pt" = '{first_day_of_month}';"""
|
|
# 执行语句
|
|
cursor.execute(sql)
|
|
# 获取结果集的每一行
|
|
rows = cursor.fetchall()
|
|
# 获取所有字段名
|
|
all_fields = cursor.description
|
|
#执行结果转化为dataframe
|
|
col = []
|
|
for i in all_fields:
|
|
col.append(i[0])
|
|
data_details = pd.DataFrame(list(rows),columns=col)
|
|
# data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
|
# 关闭数据库连接
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
from decimal import Decimal
|
|
data_details['version_amount_total'] = data_details['version_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_saas'] = data_details['version_amount_saas'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_other'] = data_details['version_amount_other'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_total'] = data_details['unversion_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_mngv'] = data_details['unversion_amount_mngv'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_message'] = data_details['unversion_amount_message'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_total'] = data_details['irregular_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_activity'] = data_details['irregular_amount_activity'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_service'] = data_details['irregular_amount_service'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
|
|
# 基础函数配置
|
|
import pandas as pd
|
|
import pandas as pd
|
|
import requests
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
import json
|
|
import numpy as np
|
|
import time
|
|
|
|
ROOT = Path('.').absolute() # 当前工作目录
|
|
|
|
def generateToken() -> str:
|
|
""" 生成 token """
|
|
|
|
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
|
|
|
|
# 该信息在钉钉开放应用中
|
|
data = {
|
|
"appKey": "ding5kqocon5s9oph5uq",
|
|
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
|
|
}
|
|
|
|
res = requests.post(token_api, json=data)
|
|
token = res.json()['accessToken']
|
|
|
|
return token
|
|
|
|
def read_instances(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_delete(token, formInstanceId):
|
|
""" 函数功能:调用本接口删除表单数据。 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formInstanceId" : formInstanceId
|
|
}
|
|
|
|
res = requests.delete(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_new(FORMID,formData):
|
|
""" 通过实例id 获取表单内容 """
|
|
api = f'https://api.dingtalk.com/v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
print(res.json())
|
|
|
|
return res.json()
|
|
|
|
|
|
def component(FORMID,TOKEN):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/formFields'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
# "language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.get(api, headers=headers, json=payload)
|
|
|
|
return res.json()
|
|
def Batch_creation(FORMID,TOKEN,ALL_formData):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchSave'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"noExecuteExpression" : "true",
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"keepRunningAfterException" : "true",
|
|
"userId" : "yida_pub_account",
|
|
"formDataJsonList" : json.dumps(ALL_formData, cls=NpEncoder),
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in_batches(FORMID,TOKEN,ALL_DATA_instance):
|
|
""" 批量删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchRemove'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"formInstanceIdList" : json.dumps(ALL_DATA_instance, cls=NpEncoder),
|
|
"userId" : "yida_pub_account",
|
|
"executeExpression" : "false" # 不触发
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in(TOKEN,formInstanceIdList):
|
|
""" 逐条删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances?appType=APP_UYZ0KG6L0CCNV80GZ66O&systemToken=XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2&userId=yida_pub_account&language=zh_CN&formInstanceId={formInstanceIdList}'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
res = requests.delete(api, headers=headers)
|
|
return res.json()
|
|
|
|
class NpEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, np.integer):
|
|
return int(obj)
|
|
elif isinstance(obj, np.floating):
|
|
return float(obj)
|
|
elif isinstance(obj, np.ndarray):
|
|
return obj.tolist()
|
|
else:
|
|
return super(NpEncoder, self).default(obj)
|
|
TOKEN = generateToken()
|
|
'''读取员工对应关系:宜搭员工-ID对应表 '''
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6" # 宜搭员工-ID对应表 FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6
|
|
# 读取流程表单数据
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 + 1
|
|
ALL_DATA_staff = {}
|
|
""" 获取全量数据 """
|
|
for i in range(1, PAGES+1):
|
|
# form_data = read_processes_instances(token=TOKEN, formUuid=FORMID, createFromTimeGMT=CREATE_FROM, createToTimeGMT=CREATE_TO, page=i, n=100, searchField={'textField_l7if5ff9': '否'})
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=i, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_staff[data['formData']['textField_lfrw3u58']]=data['formData']['textField_lfrw3u59']
|
|
|
|
'''获取表内控件信息 '''
|
|
FORMID = "FORM-Q4A664A1BT0BVKD1AZJZD5YHUYJ62OFI220IL0"
|
|
component_list = component(FORMID,TOKEN)
|
|
for i in range(len(component_list['result'])):
|
|
componentName = component_list['result'][i]['componentName']
|
|
name_value = component_list['result'][i]['label']['value']
|
|
fieldId = component_list['result'][i]['fieldId']
|
|
print("'",fieldId,"':","data_details[",name_value,"][i], # ",name_value)
|
|
|
|
|
|
# '''批量删除NGV数据'''
|
|
for i in range(0,10):
|
|
default = True
|
|
while default:
|
|
FORMID = "FORM-Q4A664A1BT0BVKD1AZJZD5YHUYJ62OFI220IL0"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 +1
|
|
print(form_data.get('totalCount'))
|
|
if form_data.get('totalCount')<=0:
|
|
default = False
|
|
for i in range(PAGES, 1,-1):
|
|
ALL_DATA_instance = []
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_instance.append(data['formInstanceId'])
|
|
print(f'读取到表单中 {len(ALL_DATA_instance)} 条数据!')
|
|
red_delete = delete_in_batches(FORMID,TOKEN,ALL_DATA_instance)
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
formInstanceId = data['formInstanceId']
|
|
delete_in(TOKEN,formInstanceId)
|
|
print(f'单条删除 {formInstanceId} 数据!')
|
|
|
|
'''遍历数据进行新建'''
|
|
data_details = data_details.astype('string')
|
|
data_details = data_details.fillna('',inplace=False)
|
|
for a in range(0,len(data_details["date_fmt"]),100):
|
|
ALL_formData = []
|
|
for i in range(a,a+100): # for i in range(len(data_NGV["date_fmt"])):
|
|
try:
|
|
formData = {
|
|
'textField_xhuz5sf':data_details["date_fmt"][i],#"date_fmt"
|
|
'textField_uxtuvoq':data_details["org_crm_id"][i],#"org_crm_id"
|
|
'textField_az562wk':data_details["org_id"][i],#"org_id"
|
|
'textField_fztifyw':data_details["org_code"][i],#"org_code"
|
|
'textField_gfd6yje':data_details["org_name"][i],#"org_name"
|
|
'textField_parg16c':data_details["is_main_org"][i],#"is_main_org"
|
|
'textField_ny44eb5':data_details["group_id"][i],#"group_id"
|
|
'textField_0ba9ono':data_details["group_name"][i],#"group_name"
|
|
'dateField_amip622':int(time.mktime(time.strptime(data_details["saas_create_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["saas_create_time"][i] !='' else '',#"saas_create_time"
|
|
'dateField_d498rsx':int(time.mktime(time.strptime(data_details["to_renew_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["to_renew_time"][i] !='' else '',#"to_renew_time"
|
|
'textField_ujg6a7f':data_details["saas_edition_fmt"][i],#"saas_edition_fmt"
|
|
'textField_fyl38xa':data_details["version_amount_total"][i],#"version_amount_total"
|
|
'textField_g6rkx8k':data_details["version_amount_saas"][i],#"version_amount_saas"
|
|
'textField_bt75ped':data_details["version_amount_other"][i],#"version_amount_other"
|
|
'textField_4q542vb':data_details["unversion_amount_total"][i],#"unversion_amount_total"
|
|
'textField_q7pf942':data_details["unversion_amount_mngv"][i],#"unversion_amount_mngv"
|
|
'textField_vh6yz7p':data_details["unversion_amount_message"][i],#"unversion_amount_message"
|
|
'textField_zrgo8w4':data_details["irregular_amount_total"][i],#"irregular_amount_total"
|
|
'textField_70wr156':data_details["irregular_amount_activity"][i],#"irregular_amount_activity"
|
|
'textField_17kj4dv':data_details["irregular_amount_service"][i],#"irregular_amount_service"
|
|
# 'employeeField_li022upx':ALL_DATA_staff[data_details["service_impl_principal"][i]] if data_details["service_impl_principal"][i] !='' else '',#"service_impl_principal"
|
|
# 'employeeField_li022upy':ALL_DATA_staff[data_details["area_manager"][i]] if data_details["area_manager"][i] !='' else '',#"area_manager"
|
|
'textField_eholn8e':data_details["region_name"][i],#"region_name"
|
|
'textField_33lfgw4':data_details["branch_name"][i],#"branch_name"
|
|
'dateField_crxj192':int(time.mktime(time.strptime(data_details["etl_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["etl_time"][i] !='' else '',#"etl_time"
|
|
'textField_18sgd8t':data_details["group_grade"][i],#"group_grade"
|
|
'textField_0et812k':data_details["province_name"][i],#"province_name"
|
|
'textField_hzxff94':data_details["city_name"][i],#"city_name"
|
|
# 'employeeField_li022upz':ALL_DATA_staff[data_details["technician"][i]] if data_details["technician"][i] !='' else '',#"technician"
|
|
'textField_kdsrwdm':data_details["coupon_amount"][i],#"coupon_amount"
|
|
'textField_sqrwp65':data_details["from_flag_fmt"][i],#"from_flag_fmt"
|
|
'textField_6u4j5j2':data_details["franchise_group_name"][i],#"franchise_group_name"
|
|
'textField_rzv2xy1':data_details["pt"][i],#"pt"
|
|
}
|
|
try:
|
|
formData['employeeField_li022upz'] = ALL_DATA_staff[data_details["technician"][i]]
|
|
except:
|
|
formData['employeeField_li022upz'] = ""
|
|
try:
|
|
formData['employeeField_li022upy'] = ALL_DATA_staff[data_details["area_manager"][i]]
|
|
except:
|
|
formData['employeeField_li022upy'] = ""
|
|
try:
|
|
formData['employeeField_li022upx'] = ALL_DATA_staff[data_details["service_impl_principal"][i]]
|
|
except:
|
|
formData['employeeField_li022upx'] = ""
|
|
|
|
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
|
except:
|
|
pass
|
|
FORMID = "FORM-Q4A664A1BT0BVKD1AZJZD5YHUYJ62OFI220IL0"
|
|
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
|
print("新建第",i,"条数据!")
|
|
time.sleep(10)
|
|
|
|
'''校验是否新建正常'''
|
|
time.sleep(120)
|
|
FORMID = "FORM-Q4A664A1BT0BVKD1AZJZD5YHUYJ62OFI220IL0"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
if int(form_data.get('totalCount')) ==len(data_details):
|
|
print("数据新建成功!")
|
|
else:
|
|
|
|
def start_instance_process(token: str, name):
|
|
|
|
"""发送宜搭表单 -- 发起流程表单
|
|
|
|
Args:
|
|
token
|
|
data:需要发送的数据字典
|
|
"""
|
|
|
|
yida_api = "https://api.dingtalk.com/v1.0/yida/processes/instances/start"
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
send_data = {
|
|
"textField_l9fe0uiw": name,
|
|
"textField_l9fe0uiv": name
|
|
}
|
|
|
|
payload = {
|
|
"appType": "APP_TNVBVZ3K8G56HG03Z45Q",
|
|
"systemToken": "CH7669818R0WN18TYTYJ42PE6GY22WZN0BYWKD1",
|
|
"userId": "yida_pub_account",# 超级管理员账号
|
|
"language": "zh_CN",
|
|
"formUuid": "FORM-UX866Q61GNLAZBCIEDF77BGVIIR83K82WYPHLH2",
|
|
"formDataJson": json.dumps(send_data),
|
|
"processCode":"TPROC--UX866Q61GNLAZBCIEDF77BGVIIR83M92WYPHLI2"
|
|
}
|
|
|
|
res = requests.post(yida_api, headers=headers, json=payload)
|
|
return res
|
|
try:
|
|
name = "2、月度续费率分子:holo_ads_report_sales_saas_to_renew_org_summary_income_d 新建条数不正确!"
|
|
res_yujing = start_instance_process(TOKEN,name)
|
|
except:
|
|
pass
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
import psycopg2
|
|
import pandas as pd
|
|
import calendar
|
|
import datetime
|
|
# 获得连接
|
|
conn = psycopg2.connect(database="f6_bi", user="BASIC$ro_caowei", password="!ro_caowei123", host="hgprecn-cn-nif1vnv0y002-cn-shanghai.hologres.aliyuncs.com", port="80")
|
|
# 获得游标对象,一个游标对象可以对数据库进行执行操作
|
|
cursor = conn.cursor()
|
|
|
|
# 获取当前日期
|
|
today = datetime.date.today()
|
|
# 获取当月
|
|
first_day_of_month = int(today.replace().strftime('%Y%m'))
|
|
|
|
|
|
# sql语句 建表
|
|
sql =f"""SELECT * FROM "public"."holo_ads_report_sales_saas_regular_income_snapshot_m" WHERE "pt" >= '202301' AND "pt" <= '{first_day_of_month}'"""
|
|
# 执行语句
|
|
cursor.execute(sql)
|
|
# 获取结果集的每一行
|
|
rows = cursor.fetchall()
|
|
# 获取所有字段名
|
|
all_fields = cursor.description
|
|
#执行结果转化为dataframe
|
|
col = []
|
|
for i in all_fields:
|
|
col.append(i[0])
|
|
data_details = pd.DataFrame(list(rows),columns=col)
|
|
# data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
|
# 关闭数据库连接
|
|
cursor.close()
|
|
conn.close()
|
|
print(len(data_details))
|
|
|
|
|
|
from decimal import Decimal
|
|
data_details['version_amount_total'] = data_details['version_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_saas'] = data_details['version_amount_saas'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_other'] = data_details['version_amount_other'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_total_renew'] = data_details['version_amount_total_renew'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_saas_renew'] = data_details['version_amount_saas_renew'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_other_renew'] = data_details['version_amount_other_renew'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['coupon_amount'] = data_details['coupon_amount'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
|
|
|
|
|
|
# 基础函数配置
|
|
import pandas as pd
|
|
import pandas as pd
|
|
import requests
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
import json
|
|
import numpy as np
|
|
import time
|
|
|
|
ROOT = Path('.').absolute() # 当前工作目录
|
|
|
|
def generateToken() -> str:
|
|
""" 生成 token """
|
|
|
|
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
|
|
|
|
# 该信息在钉钉开放应用中
|
|
data = {
|
|
"appKey": "ding5kqocon5s9oph5uq",
|
|
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
|
|
}
|
|
|
|
res = requests.post(token_api, json=data)
|
|
token = res.json()['accessToken']
|
|
|
|
return token
|
|
|
|
def read_instances(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_delete(token, formInstanceId):
|
|
""" 函数功能:调用本接口删除表单数据。 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formInstanceId" : formInstanceId
|
|
}
|
|
|
|
res = requests.delete(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_new(FORMID,formData):
|
|
""" 通过实例id 获取表单内容 """
|
|
api = f'https://api.dingtalk.com/v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
print(res.json())
|
|
|
|
return res.json()
|
|
|
|
|
|
def component(FORMID,TOKEN):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/formFields'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
# "language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.get(api, headers=headers, json=payload)
|
|
|
|
return res.json()
|
|
def Batch_creation(FORMID,TOKEN,ALL_formData):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchSave'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"noExecuteExpression" : "true",
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"keepRunningAfterException" : "true",
|
|
"userId" : "yida_pub_account",
|
|
"formDataJsonList" : json.dumps(ALL_formData, cls=NpEncoder),
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in_batches(FORMID,TOKEN,ALL_DATA_instance):
|
|
""" 批量删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchRemove'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"formInstanceIdList" : json.dumps(ALL_DATA_instance, cls=NpEncoder),
|
|
"userId" : "yida_pub_account",
|
|
"executeExpression" : "false" # 不触发
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in(TOKEN,formInstanceIdList):
|
|
""" 逐条删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances?appType=APP_UYZ0KG6L0CCNV80GZ66O&systemToken=XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2&userId=yida_pub_account&language=zh_CN&formInstanceId={formInstanceIdList}'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
res = requests.delete(api, headers=headers)
|
|
return res.json()
|
|
|
|
class NpEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, np.integer):
|
|
return int(obj)
|
|
elif isinstance(obj, np.floating):
|
|
return float(obj)
|
|
elif isinstance(obj, np.ndarray):
|
|
return obj.tolist()
|
|
else:
|
|
return super(NpEncoder, self).default(obj)
|
|
TOKEN = generateToken()
|
|
'''读取员工对应关系:宜搭员工-ID对应表 '''
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6" # 宜搭员工-ID对应表 FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6
|
|
# 读取流程表单数据
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 + 1
|
|
ALL_DATA_staff = {}
|
|
""" 获取全量数据 """
|
|
for i in range(1, PAGES+1):
|
|
# form_data = read_processes_instances(token=TOKEN, formUuid=FORMID, createFromTimeGMT=CREATE_FROM, createToTimeGMT=CREATE_TO, page=i, n=100, searchField={'textField_l7if5ff9': '否'})
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=i, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_staff[data['formData']['textField_lfrw3u58']]=data['formData']['textField_lfrw3u59']
|
|
|
|
'''获取表内控件信息 '''
|
|
FORMID = "FORM-EA866E71ZOWAG1M67SFF37AR7PRT2CCJQ20ILL"
|
|
component_list = component(FORMID,TOKEN)
|
|
for i in range(len(component_list['result'])):
|
|
componentName = component_list['result'][i]['componentName']
|
|
name_value = component_list['result'][i]['label']['value']
|
|
fieldId = component_list['result'][i]['fieldId']
|
|
print("'",fieldId,"':","data_details[",name_value,"][i], # ",name_value)
|
|
|
|
# '''批量删除NGV数据'''
|
|
for i in range(0,10):
|
|
default = True
|
|
while default:
|
|
FORMID = "FORM-EA866E71ZOWAG1M67SFF37AR7PRT2CCJQ20ILL"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 +1
|
|
print(form_data.get('totalCount'))
|
|
if form_data.get('totalCount')<=0:
|
|
default = False
|
|
for i in range(PAGES, 1,-1):
|
|
ALL_DATA_instance = []
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_instance.append(data['formInstanceId'])
|
|
print(f'读取到表单中 {len(ALL_DATA_instance)} 条数据!')
|
|
red_delete = delete_in_batches(FORMID,TOKEN,ALL_DATA_instance)
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
formInstanceId = data['formInstanceId']
|
|
delete_in(TOKEN,formInstanceId)
|
|
print(f'单条删除 {formInstanceId} 数据!')
|
|
|
|
'''遍历数据进行新建'''
|
|
data_details = data_details.astype('string')
|
|
data_details = data_details.fillna('',inplace=False)
|
|
for a in range(0,len(data_details["month_id"]),100):
|
|
ALL_formData = []
|
|
for i in range(a,a+100): # for i in range(len(data_NGV["date_fmt"])):
|
|
try:
|
|
try:
|
|
if data_details["franchise_group_name"][i] =="":
|
|
data_details["franchise_group_name"][i] = "普通SAAS"
|
|
except:
|
|
pass
|
|
formData = {
|
|
'textField_bsp60hb':data_details["month_id"][i],#"month_id"
|
|
'textField_ilso6hg':data_details["org_crm_id"][i],#"org_crm_id"
|
|
'textField_58qfhc3':data_details["id_deleted_his"][i],#"id_deleted_his"
|
|
'textField_wttytti':data_details["org_id"][i],#"org_id"
|
|
'textField_mhncv5b':data_details["org_code"][i],#"org_code"
|
|
'textField_9p4y2on':data_details["org_name"][i],#"org_name"
|
|
'textField_33dscdq':data_details["is_main_org"][i],#"is_main_org"
|
|
'textField_tmgv5wb':data_details["group_id"][i],#"group_id"
|
|
'textField_ucui6ff':data_details["group_name"][i],#"group_name"
|
|
'textField_qi5gpdj':data_details["org_status"][i],#"org_status"
|
|
'dateField_zoh7nqn':int(time.mktime(time.strptime(data_details["saas_create_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["saas_create_time"][i] !='' else '',#"saas_create_time"
|
|
'textField_qc0hq6c':data_details["saas_use_year"][i],#"saas_use_year"
|
|
'dateField_xttx6bc':int(time.mktime(time.strptime(data_details["to_renew_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["to_renew_time"][i] !='' else '',#"to_renew_time"
|
|
'textField_8k9qa00':data_details["saas_edition_fmt"][i],#"saas_edition_fmt"
|
|
'textField_jzdnckg':data_details["version_amount_total"][i],#"version_amount_total"
|
|
'textField_kc4vc5b':data_details["version_amount_saas"][i],#"version_amount_saas"
|
|
'textField_3c1vkle':data_details["version_amount_other"][i],#"version_amount_other"
|
|
# 'employeeField_li02quti':ALL_DATA_staff[data_details["service_impl_principal"][i]] if data_details["service_impl_principal"][i] !='' else '',#"service_impl_principal"
|
|
# 'employeeField_li02qutj':ALL_DATA_staff[data_details["area_manager"][i]] if data_details["area_manager"][i] !='' else '',#"area_manager"
|
|
'textField_ju5lwc8':data_details["region_name"][i],#"region_name"
|
|
'textField_alfkqae':data_details["branch_name"][i],#"branch_name"
|
|
'textField_nt0ymex':data_details["group_grade"][i],#"group_grade"
|
|
'textField_gbupbr1':data_details["province_name"][i],#"province_name"
|
|
'textField_llw4vl7':data_details["city_name"][i],#"city_name"
|
|
'textField_ro83a0k':data_details["version_amount_total_renew"][i],#"version_amount_total_renew"
|
|
'textField_bdqwv17':data_details["version_amount_saas_renew"][i],#"version_amount_saas_renew"
|
|
'textField_xnhbxsi':data_details["version_amount_other_renew"][i],#"version_amount_other_renew"
|
|
'dateField_is6u6ub':int(time.mktime(time.strptime(data_details["renew_date"][i], "%Y-%m-%d"))*1000) if data_details["renew_date"][i] !='' else '',#"renew_date"
|
|
'textField_pqubfpr':data_details["is_renew"][i],#"is_renew"
|
|
'dateField_d97awb6':int(time.mktime(time.strptime(data_details["etl_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["etl_time"][i] !='' else '',#"etl_time"
|
|
'textField_r3qicl9':data_details["renew_saas_edition_fmt"][i],#"renew_saas_edition_fmt"
|
|
# 'employeeField_li02qutk':ALL_DATA_staff[data_details["technician"][i]] if data_details["technician"][i] !='' else '',#"technician"
|
|
'textField_rcv7viu':data_details["coupon_amount"][i],#"coupon_amount"
|
|
'textField_44zj8ho':data_details["franchise_group_name"][i],#"franchise_group_name"
|
|
'textField_0yyb08y':data_details["pt"][i],#"pt"
|
|
}
|
|
try:
|
|
formData['employeeField_li02qutk'] = ALL_DATA_staff[data_details["technician"][i]]
|
|
except:
|
|
formData['employeeField_li02qutk'] = ""
|
|
try:
|
|
formData['employeeField_li02qutj'] = ALL_DATA_staff[data_details["area_manager"][i]]
|
|
except:
|
|
formData['employeeField_li02qutj'] = ""
|
|
try:
|
|
formData['employeeField_li02quti'] = ALL_DATA_staff[data_details["service_impl_principal"][i]]
|
|
except:
|
|
formData['employeeField_li02quti'] = ""
|
|
|
|
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
|
except:
|
|
pass
|
|
FORMID = "FORM-EA866E71ZOWAG1M67SFF37AR7PRT2CCJQ20ILL"
|
|
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
|
print("新建第",i,"条数据!")
|
|
time.sleep(10)
|
|
|
|
'''校验是否新建正常'''
|
|
time.sleep(120)
|
|
FORMID = "FORM-EA866E71ZOWAG1M67SFF37AR7PRT2CCJQ20ILL"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
if int(form_data.get('totalCount')) ==len(data_details):
|
|
print("数据新建成功!")
|
|
else:
|
|
|
|
def start_instance_process(token: str, name):
|
|
|
|
"""发送宜搭表单 -- 发起流程表单
|
|
|
|
Args:
|
|
token
|
|
data:需要发送的数据字典
|
|
"""
|
|
|
|
yida_api = "https://api.dingtalk.com/v1.0/yida/processes/instances/start"
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
send_data = {
|
|
"textField_l9fe0uiw": name,
|
|
"textField_l9fe0uiv": name
|
|
}
|
|
|
|
payload = {
|
|
"appType": "APP_TNVBVZ3K8G56HG03Z45Q",
|
|
"systemToken": "CH7669818R0WN18TYTYJ42PE6GY22WZN0BYWKD1",
|
|
"userId": "yida_pub_account",# 超级管理员账号
|
|
"language": "zh_CN",
|
|
"formUuid": "FORM-UX866Q61GNLAZBCIEDF77BGVIIR83K82WYPHLH2",
|
|
"formDataJson": json.dumps(send_data),
|
|
"processCode":"TPROC--UX866Q61GNLAZBCIEDF77BGVIIR83M92WYPHLI2"
|
|
}
|
|
|
|
res = requests.post(yida_api, headers=headers, json=payload)
|
|
return res
|
|
try:
|
|
name = "3、年度滚动续费率分母:holo_ads_report_sales_saas_regular_income_snapshot_m 新建条数不正确!"
|
|
res_yujing = start_instance_process(TOKEN,name)
|
|
except:
|
|
pass
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
import psycopg2
|
|
import pandas as pd
|
|
import calendar
|
|
import datetime
|
|
# 获得连接
|
|
conn = psycopg2.connect(database="f6_bi", user="BASIC$ro_caowei", password="!ro_caowei123", host="hgprecn-cn-nif1vnv0y002-cn-shanghai.hologres.aliyuncs.com", port="80")
|
|
# 获得游标对象,一个游标对象可以对数据库进行执行操作
|
|
cursor = conn.cursor()
|
|
|
|
# 获取当前日期
|
|
today = datetime.date.today()
|
|
# 获取当月
|
|
first_day_of_month = int(today.replace().strftime('%Y%m'))-2
|
|
|
|
# sql语句 建表
|
|
sql =f"""SELECT * FROM "public"."holo_ads_report_sales_saas_org_summary_income_m" WHERE "pt" >= '{first_day_of_month}'"""
|
|
# 执行语句
|
|
cursor.execute(sql)
|
|
# 获取结果集的每一行
|
|
rows = cursor.fetchall()
|
|
# 获取所有字段名
|
|
all_fields = cursor.description
|
|
#执行结果转化为dataframe
|
|
col = []
|
|
for i in all_fields:
|
|
col.append(i[0])
|
|
data_details = pd.DataFrame(list(rows),columns=col)
|
|
# data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
|
# 关闭数据库连接
|
|
cursor.close()
|
|
conn.close()
|
|
print(len(data_details))
|
|
|
|
|
|
from decimal import Decimal
|
|
data_details['version_amount_total'] = data_details['version_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_saas'] = data_details['version_amount_saas'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['version_amount_other'] = data_details['version_amount_other'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_total'] = data_details['unversion_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_mngv'] = data_details['unversion_amount_mngv'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['unversion_amount_message'] = data_details['unversion_amount_message'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_total'] = data_details['irregular_amount_total'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_activity'] = data_details['irregular_amount_activity'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['irregular_amount_service'] = data_details['irregular_amount_service'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
data_details['coupon_amount'] = data_details['coupon_amount'].apply(lambda x: float(x) if isinstance(x, Decimal) else x)
|
|
|
|
|
|
|
|
|
|
|
|
# 基础函数配置
|
|
import pandas as pd
|
|
import pandas as pd
|
|
import requests
|
|
from pathlib import Path
|
|
from urllib.parse import quote
|
|
import json
|
|
import numpy as np
|
|
import time
|
|
|
|
ROOT = Path('.').absolute() # 当前工作目录
|
|
|
|
def generateToken() -> str:
|
|
""" 生成 token """
|
|
|
|
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
|
|
|
|
# 该信息在钉钉开放应用中
|
|
data = {
|
|
"appKey": "ding5kqocon5s9oph5uq",
|
|
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
|
|
}
|
|
|
|
res = requests.post(token_api, json=data)
|
|
token = res.json()['accessToken']
|
|
|
|
return token
|
|
|
|
def read_instances(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_delete(token, formInstanceId):
|
|
""" 函数功能:调用本接口删除表单数据。 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formInstanceId" : formInstanceId
|
|
}
|
|
|
|
res = requests.delete(api, headers=headers, json=formData)
|
|
return res.json()
|
|
|
|
def read_new(FORMID,formData):
|
|
""" 通过实例id 获取表单内容 """
|
|
api = f'https://api.dingtalk.com/v1.0/yida/forms/instances'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
print(res.json())
|
|
|
|
return res.json()
|
|
|
|
|
|
def component(FORMID,TOKEN):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/formFields'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
# "language" : "zh_CN",
|
|
"userId" : "yida_pub_account"
|
|
}
|
|
|
|
res = requests.get(api, headers=headers, json=payload)
|
|
|
|
return res.json()
|
|
def Batch_creation(FORMID,TOKEN,ALL_formData):
|
|
""" 获取组件信息 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchSave'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
payload = {
|
|
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
|
"noExecuteExpression" : "true",
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"keepRunningAfterException" : "true",
|
|
"userId" : "yida_pub_account",
|
|
"formDataJsonList" : json.dumps(ALL_formData, cls=NpEncoder),
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
|
|
def delete_in_batches(FORMID,TOKEN,ALL_DATA_instance):
|
|
""" 批量删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/batchRemove'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
|
|
payload = {
|
|
"formUuid" : FORMID,
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"asynchronousExecution" : "true",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"formInstanceIdList" : json.dumps(ALL_DATA_instance, cls=NpEncoder),
|
|
"userId" : "yida_pub_account",
|
|
"executeExpression" : "false" # 不触发
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=payload)
|
|
return res.json()
|
|
def read_instances_pt(token, formUuid, page, n):
|
|
""" 函数功能:读取普通表单的所有数据 """
|
|
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances/search'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
formData = {
|
|
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
|
|
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
|
|
"userId" : "yida_pub_account",
|
|
"language" : "zh_CN",
|
|
"formUuid" : formUuid,
|
|
"currentPage" : page,
|
|
"pageSize" : n
|
|
}
|
|
|
|
res = requests.post(api, headers=headers, json=formData)
|
|
return res.json()
|
|
def delete_in(TOKEN,formInstanceIdList):
|
|
""" 逐条删除表单实例 """
|
|
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances?appType=APP_UYZ0KG6L0CCNV80GZ66O&systemToken=XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2&userId=yida_pub_account&language=zh_CN&formInstanceId={formInstanceIdList}'
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": TOKEN
|
|
}
|
|
res = requests.delete(api, headers=headers)
|
|
return res.json()
|
|
|
|
class NpEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, np.integer):
|
|
return int(obj)
|
|
elif isinstance(obj, np.floating):
|
|
return float(obj)
|
|
elif isinstance(obj, np.ndarray):
|
|
return obj.tolist()
|
|
else:
|
|
return super(NpEncoder, self).default(obj)
|
|
TOKEN = generateToken()
|
|
'''读取员工对应关系:宜搭员工-ID对应表 '''
|
|
FORMID = "FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6" # 宜搭员工-ID对应表 FORM-EA866E715PF9YA7ECCAGSABX91Q72PVA3WRFL6
|
|
# 读取流程表单数据
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 + 1
|
|
ALL_DATA_staff = {}
|
|
""" 获取全量数据 """
|
|
for i in range(1, PAGES+1):
|
|
# form_data = read_processes_instances(token=TOKEN, formUuid=FORMID, createFromTimeGMT=CREATE_FROM, createToTimeGMT=CREATE_TO, page=i, n=100, searchField={'textField_l7if5ff9': '否'})
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=i, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_staff[data['formData']['textField_lfrw3u58']]=data['formData']['textField_lfrw3u59']
|
|
|
|
'''获取表内控件信息 '''
|
|
FORMID = "FORM-GI666T81ZBWAFJZVABUMA80PJSDC2YCOZ20ILZ"
|
|
component_list = component(FORMID,TOKEN)
|
|
for i in range(len(component_list['result'])):
|
|
componentName = component_list['result'][i]['componentName']
|
|
name_value = component_list['result'][i]['label']['value']
|
|
fieldId = component_list['result'][i]['fieldId']
|
|
print("'",fieldId,"':","data_details[",name_value,"][i], # ",name_value)
|
|
'''获取订单明细'''
|
|
form_data = read_instances_pt(token=TOKEN, formUuid="FORM-E6766M8103CDA5M3CJ06JDRCLSKV2FJ6Z3CLLA" , page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 + 1
|
|
ALL_DATA = []
|
|
""" 获取全量数据 """
|
|
for i in range(1, PAGES+1):
|
|
form_data = read_instances_pt(token=TOKEN, formUuid="FORM-E6766M8103CDA5M3CJ06JDRCLSKV2FJ6Z3CLLA" , page=i, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA.append([data['formData']["textField_dv032lp"],data['formData']["textField_cwqfrcq"],data['formData']["textField_qplm0p9"]+data['formData']["textField_wy749n8"]])
|
|
print(f'读取到流程表中 {len(ALL_DATA)} 条数据')
|
|
df_DataFrame = pd.DataFrame(ALL_DATA, columns=[ '优惠券金额', '提前续约优惠', '公司名称门店名称'])
|
|
# '''批量删除NGV数据'''
|
|
for i in range(0,10):
|
|
default = True
|
|
while default:
|
|
FORMID = "FORM-GI666T81ZBWAFJZVABUMA80PJSDC2YCOZ20ILZ"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
PAGES = form_data.get('totalCount')//100 +1
|
|
print(form_data.get('totalCount'))
|
|
if form_data.get('totalCount')<=0:
|
|
default = False
|
|
for i in range(PAGES, 1,-1):
|
|
ALL_DATA_instance = []
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
ALL_DATA_instance.append(data['formInstanceId'])
|
|
print(f'读取到表单中 {len(ALL_DATA_instance)} 条数据!')
|
|
red_delete = delete_in_batches(FORMID,TOKEN,ALL_DATA_instance)
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
for data in form_data.get('data'):
|
|
formInstanceId = data['formInstanceId']
|
|
delete_in(TOKEN,formInstanceId)
|
|
print(f'单条删除 {formInstanceId} 数据!')
|
|
|
|
'''遍历数据进行新建'''
|
|
data_details = data_details.astype('string')
|
|
data_details = data_details.fillna('',inplace=False)
|
|
for a in range(0,len(data_details["year_id"]),100):
|
|
ALL_formData = []
|
|
for i in range(a,a+100): # for i in range(len(data_NGV["date_fmt"])):
|
|
try:
|
|
try:
|
|
if data_details["franchise_group_name"][i] =="":
|
|
data_details["franchise_group_name"][i] = "普通SAAS"
|
|
except:
|
|
pass
|
|
formData = {
|
|
'textField_l556qrj':data_details["year_id"][i],#"year_id"
|
|
'textField_ioghzon':data_details["org_crm_id"][i],#"org_crm_id"
|
|
'textField_kr3pc6c':data_details["org_id"][i],#"org_id"
|
|
'textField_sv3xmix':data_details["org_code"][i],#"org_code"
|
|
'textField_nk98j1i':data_details["org_name"][i],#"org_name"
|
|
'textField_gvdimtj':data_details["group_id"][i],#"group_id"
|
|
'textField_ntc1mqo':data_details["group_name"][i],#"group_name"
|
|
'textField_dssu351':data_details["group_grade"][i],#"group_grade"
|
|
'dateField_hgp5gh8':int(time.mktime(time.strptime(data_details["saas_create_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["saas_create_time"][i] !='' else '',#"saas_create_time"
|
|
'textField_m6wvkhp':data_details["from_flag_fmt"][i],#"from_flag_fmt"
|
|
'textField_rzrq44v':data_details["franchise_group_name"][i],#"franchise_group_name"
|
|
'textField_s8u1211':data_details["region_name"][i],#"region_name"
|
|
'textField_bifjpqz':data_details["branch_name"][i],#"branch_name"
|
|
'textField_zz547i2':data_details["province_name"][i],#"province_name"
|
|
'textField_j86bqku':data_details["city_name"][i],#"city_name"
|
|
# 'employeeField_li0304o0':ALL_DATA_staff[data_details["area_manager"][i]] if data_details["area_manager"][i] !='' else '',#"area_manager"
|
|
# 'employeeField_li0304o1':ALL_DATA_staff[data_details["service_impl_principal"][i]] if data_details["service_impl_principal"][i] !='' else '',#"service_impl_principal"
|
|
# 'employeeField_li0304o2':ALL_DATA_staff[data_details["technician"][i]] if data_details["technician"][i] !='' else '',#"technician"
|
|
'dateField_f7cjejs':int(time.mktime(time.strptime(data_details["to_renew_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["to_renew_time"][i] !='' else '',#"to_renew_time"
|
|
'textField_4a171yy':data_details["saas_edition_fmt"][i],#"saas_edition_fmt"
|
|
'textField_9cja7t3':data_details["version_amount_total"][i],#"version_amount_total"
|
|
'textField_tdd8vic':data_details["version_amount_saas"][i],#"version_amount_saas"
|
|
'textField_yc1liag':data_details["version_amount_other"][i],#"version_amount_other"
|
|
'textField_sazuktm':data_details["unversion_amount_total"][i],#"unversion_amount_total"
|
|
'textField_dzyddir':data_details["unversion_amount_mngv"][i],#"unversion_amount_mngv"
|
|
'textField_rimc2k8':data_details["unversion_amount_message"][i],#"unversion_amount_message"
|
|
'textField_whlq8rb':data_details["irregular_amount_total"][i],#"irregular_amount_total"
|
|
'textField_jm29spy':data_details["irregular_amount_activity"][i],#"irregular_amount_activity"
|
|
'textField_hf46l34':data_details["irregular_amount_service"][i],#"irregular_amount_service"
|
|
'textField_pkyoug6':data_details["coupon_amount"][i],#"coupon_amount"
|
|
'dateField_uwztkat':int(time.mktime(time.strptime(data_details["etl_time"][i], "%Y-%m-%d %H:%M:%S"))*1000) if data_details["etl_time"][i] !='' else '',#"etl_time"
|
|
'textField_s6mf0at':data_details["pt"][i],#"pt"
|
|
'textField_llm3mclm':"0" #"提前续约优惠"
|
|
}
|
|
try:
|
|
formData['employeeField_li0304o2'] = ALL_DATA_staff[data_details["technician"][i]]
|
|
except:
|
|
formData['employeeField_li0304o2'] = ""
|
|
try:
|
|
formData['employeeField_li0304o1'] = ALL_DATA_staff[data_details["service_impl_principal"][i]]
|
|
except:
|
|
formData['employeeField_li0304o1'] = ""
|
|
try:
|
|
formData['employeeField_li0304o0'] = ALL_DATA_staff[data_details["area_manager"][i]]
|
|
except:
|
|
formData['employeeField_li0304o0'] = ""
|
|
try:
|
|
group_name_org_name = formData['textField_ntc1mqo'] + formData['textField_nk98j1i']
|
|
row_numbers = df_DataFrame.index[df_DataFrame['公司名称门店名称'] == group_name_org_name].tolist()
|
|
if row_numbers[0]>0:
|
|
pandan_new = int(float(df_DataFrame.loc[row_numbers,'优惠券金额'].values[0])) - int(float(formData['textField_pkyoug6']))
|
|
if pandan_new == 0 or df_DataFrame.loc[row_numbers,'优惠券金额'].values[0] == "0":
|
|
formData['textField_llm3mclm'] = df_DataFrame.loc[row_numbers[0],'提前续约优惠'] # 提前续约优惠
|
|
except:
|
|
formData['textField_llm3mclm'] = "0"
|
|
|
|
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
|
except:
|
|
pass
|
|
FORMID = "FORM-GI666T81ZBWAFJZVABUMA80PJSDC2YCOZ20ILZ"
|
|
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
|
print("新建第",i,"条数据!")
|
|
time.sleep(10)
|
|
|
|
'''校验是否新建正常'''
|
|
time.sleep(120)
|
|
FORMID = "FORM-GI666T81ZBWAFJZVABUMA80PJSDC2YCOZ20ILZ"
|
|
form_data = read_instances(token=TOKEN, formUuid=FORMID, page=1, n=100)
|
|
if int(form_data.get('totalCount')) ==len(data_details):
|
|
print("数据新建成功!")
|
|
else:
|
|
|
|
def start_instance_process(token: str, name):
|
|
|
|
"""发送宜搭表单 -- 发起流程表单
|
|
|
|
Args:
|
|
token
|
|
data:需要发送的数据字典
|
|
"""
|
|
|
|
yida_api = "https://api.dingtalk.com/v1.0/yida/processes/instances/start"
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"x-acs-dingtalk-access-token": token
|
|
}
|
|
|
|
send_data = {
|
|
"textField_l9fe0uiw": name,
|
|
"textField_l9fe0uiv": name
|
|
}
|
|
|
|
payload = {
|
|
"appType": "APP_TNVBVZ3K8G56HG03Z45Q",
|
|
"systemToken": "CH7669818R0WN18TYTYJ42PE6GY22WZN0BYWKD1",
|
|
"userId": "yida_pub_account",# 超级管理员账号
|
|
"language": "zh_CN",
|
|
"formUuid": "FORM-UX866Q61GNLAZBCIEDF77BGVIIR83K82WYPHLH2",
|
|
"formDataJson": json.dumps(send_data),
|
|
"processCode":"TPROC--UX866Q61GNLAZBCIEDF77BGVIIR83M92WYPHLI2"
|
|
}
|
|
|
|
res = requests.post(yida_api, headers=headers, json=payload)
|
|
return res
|
|
try:
|
|
name = "4、年度滚动续费率分子:holo_ads_report_sales_saas_org_summary_income_m 新建条数不正确!"
|
|
res_yujing = start_instance_process(TOKEN,name)
|
|
except:
|
|
pass |