脚本
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import psycopg2
|
||||
import pandas as pd
|
||||
# 获得连接
|
||||
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()
|
||||
|
||||
import datetime
|
||||
now_time = datetime.datetime.now()
|
||||
yes_time = now_time + datetime.timedelta(days=-2)
|
||||
yes_time_nyr = int(yes_time.strftime('%Y%m%d'))# 获取前一天日期
|
||||
# sql语句 建表
|
||||
sql =f"""SELECT * FROM "public"."holo_ads_report_saas_profile_ngv_detail_d" WHERE "date_id" = '{yes_time_nyr}';"""
|
||||
# 执行语句
|
||||
cursor.execute(sql)
|
||||
# 获取结果集的每一行
|
||||
rows = cursor.fetchall()
|
||||
# 获取所有字段名
|
||||
all_fields = cursor.description
|
||||
#执行结果转化为dataframe
|
||||
col = []
|
||||
for i in all_fields:
|
||||
col.append(i[0])
|
||||
data_NGV = pd.DataFrame(list(rows),columns=col)
|
||||
# data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
||||
# 关闭数据库连接
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
import psycopg2
|
||||
import pandas as pd
|
||||
# 获得连接
|
||||
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()
|
||||
|
||||
import datetime
|
||||
now_time = datetime.datetime.now()
|
||||
yes_time = now_time + datetime.timedelta(days=-1)
|
||||
yes_time_nyr = int(yes_time.strftime('%Y%m%d'))# 获取前一天日期
|
||||
# sql语句 建表
|
||||
sql =f"""SELECT * FROM "public"."holo_ads_report_saas_profile_ngv_detail_d" WHERE "date_id" = '{yes_time_nyr}';"""
|
||||
# 执行语句
|
||||
cursor.execute(sql)
|
||||
# 获取结果集的每一行
|
||||
rows = cursor.fetchall()
|
||||
# 获取所有字段名
|
||||
all_fields = cursor.description
|
||||
#执行结果转化为dataframe
|
||||
col = []
|
||||
for i in all_fields:
|
||||
col.append(i[0])
|
||||
data_NGV_new = pd.DataFrame(list(rows),columns=col)
|
||||
# data_NGV.to_excel(r'C:\Users\admin\Desktop\NGV明细.xlsx')
|
||||
# 关闭数据库连接
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
# 使用isin方法筛选出A表中id_own_org字段不在B表中的行
|
||||
c_data = data_NGV_new[~data_NGV_new['id_own_org'].isin(data_NGV['id_own_org'])]
|
||||
|
||||
# 输出C表内容
|
||||
c_data = c_data.reset_index()
|
||||
|
||||
# 基础函数配置
|
||||
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_FE5IWP670JPRC5ZA6HK0",
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
"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_FE5IWP670JPRC5ZA6HK0",
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
"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_FE5IWP670JPRC5ZA6HK0",
|
||||
"formDataJson" : json.dumps(formData, cls=NpEncoder),
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
"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_FE5IWP670JPRC5ZA6HK0",
|
||||
# "formDataJson" : json.dumps(formData, cls=NpEncoder),
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
# "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_FE5IWP670JPRC5ZA6HK0",
|
||||
"asynchronousExecution" : "true",
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
"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_FE5IWP670JPRC5ZA6HK0",
|
||||
"asynchronousExecution" : "true",
|
||||
"systemToken" : "HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92",
|
||||
"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_FE5IWP670JPRC5ZA6HK0&systemToken=HP666C71ZLASJ0MPWC5ZOUA4AGDP17QU7TPRK92&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()
|
||||
'''遍历数据进行新建'''
|
||||
c_data = c_data.astype('string')
|
||||
c_data = c_data.fillna('',inplace=False)
|
||||
for a in range(0,len(c_data["date_fmt"]),100):
|
||||
ALL_formData = []
|
||||
for i in range(a,a+100): # for i in range(len(c_data["date_fmt"])):
|
||||
try:
|
||||
formData = {
|
||||
'textField_kwcwsccn': c_data["group_name"][i], # 公司名称
|
||||
'textField_kwcwsccj': c_data["org_name"][i], # 门店名称
|
||||
'textField_kwcwscci': c_data["org_code"][i], # 门店编码
|
||||
'textField_kwcwsccm': c_data["group_grade"][i], # 公司等级
|
||||
'textField_kwcwy3tw': c_data["saas_use_year"][i], # 使用时间
|
||||
'textField_kwcwscck': c_data["org_size"][i], # 门店规模
|
||||
'textField_kwcwy3tx': c_data["org_stage"][i], # 门店阶段
|
||||
'textField_kwcwsccl': c_data["saas_customer_type"][i] # 门店分层
|
||||
}
|
||||
ALL_formData.append(json.dumps(formData, cls=NpEncoder))
|
||||
except:
|
||||
pass
|
||||
FORMID = "FORM-NC966W8174MVWULSY88ZN2WHTF6J3JTWRWCWK2"
|
||||
res_new = Batch_creation(FORMID,TOKEN,ALL_formData)
|
||||
print("新建第",i,"条数据!",res_new)
|
||||
Reference in New Issue
Block a user