Commit c8175839 authored by Kingdom曾庆达's avatar Kingdom曾庆达

Delete code_analysis

parent c49570d4
# 加载依赖
import pandas as pd
import psycopg2
import warnings
if __name__ == "__main__":
warnings.filterwarnings("ignore") #忽略警告信息
pd.set_option('expand_frame_repr',False) #打印不换行
def rest(data):
data.reset_index(drop=True, inplace=True)
return data
# 生成连接字符串
conn = psycopg2.connect(host="hgprecn-cn-zvp2v0vcp00a-cn-shenzhen.hologres.aliyuncs.com", port=80, dbname="gsdw", user="LTAI5t9YWCusBEG9FqHvzpLD",\
password="lJl6YVMzK3O5GfPhfDDCbR1dkwUlGG", application_name="Python Test")
def fetchall_as_dict(sql):
data = []
#cnx = dbpool.connection()
cursor = conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
# 将结果封装为字典
columns = [i[0] for i in cursor.description]
for row in result:
the_dirt = {}
for i, col in enumerate(columns):
the_dirt[col] = row[i]
data.append(the_dirt)
result_data= pd.DataFrame(data)
cursor.close()
# conn.close()
return result_data
# 积分入账的sql
sql_in_list = '''select case when a.is_frozen=0 then left(a.effective_time::text,7) end as 积分生效月份,
d.company,
case when a.is_frozen=0 then a.effective_time end as 生效时间,
b.id as "推荐人ID",
b.username as "推荐人姓名",
b.mobile as "推荐人号码",
bb.pom_order_sn as "推荐人合同号",
bb.pom_sign_time as "推荐人签单时间",
b2.id as "被推荐人ID",
b2.username as 被推荐人姓名,
bbb.pom_sign_time 被推荐人签约时间,
bbb.pom_order_sn 被推荐人合同号,
bbb.fpp_name 被推荐人签约项目,
bbb.fpp_type 被推荐人项目类型,
a.change as 积分发放金额,
a.point_event_name as 积分类型,
case when a.point_event_name = '推荐签约' and bbb.fpp_type in (1,2,3,14) then '身份类推荐'
when a.point_event_name = '活动赠送' then '活动赠送' end
as 积分发放类型,
a.change ,
case when a.change <0 and a.is_frozen=0 then '已支出'
when a.change >0 and a.is_frozen=1 then '锁定中'
when a.change >0 and a.is_frozen=0 then '已发放' end as 积分状态,
a.remarks
from ods.ods_mbm_micros_point_event_log a
left join ods.ods_pre_micros_customer b on a.mobile = b.mobile and b.deleted_at is null and b.mobile <>''
left join ods.ods_pre_micros_customer b2 on a.crm_source_mobile = b2.mobile and b.deleted_at is null and b2.mobile <>''
left join
(
select * from
(select pom_customer_id ,pom_order_sn ,pom_sign_time ,row_number()over(partition by pom_customer_id order by pom_sign_time ) as row_num from dws.dws_pre_sign_order_main_v)aa where row_num = 1
)bb on b.id = bb.pom_customer_id
left join
(
select * from
(select pom_customer_id ,pom_order_sn ,pom_sign_time,fpp_name,fpp_type,row_number()over(partition by pom_customer_id order by pom_sign_time ) as row_num from dws.dws_pre_sign_order_main_v)aa where row_num = 1
)bbb on b2.id = bbb.pom_customer_id
left join
(select phone ,min(nick_name)nick_name from ods.ods_gal_galaxy_wechat_client where is_deleted='0' and phone is not null group by phone)c on a.mobile = c.phone
left join
(select point_event_log_id ,array_agg(company)company from ods.ods_mbm_micros_point_company where deleted_at is null
group by point_event_log_id )d on a."id" = d.point_event_log_id
where a.is_frozen in (0) and a.deleted_at is null
-- and left(a.created_at,10) between '${start_date}' and '${end_date}'
and a.point_event_name in ('积分同步','系统调整-增加','推荐签约','线下推荐签约','活动赠送')
order by b.mobile,a.created_at'''
# 积分兑换的sql
sql_out_list = '''select mobile ,amount::int * 10 as amount,updated_at from ods.ods_mbm_micros_cash_out_log where deleted_at is null and "status" in (1,3)
union all
select mobile , change*(-1) as change ,effective_time from ods.ods_mbm_micros_point_event_log where point_event_name = '系统调整-减少' and effective_time is not null
order by mobile ,updated_at;'''
in_list = fetchall_as_dict(sql_in_list)
out_list = fetchall_as_dict(sql_out_list)
# 关闭数据库连接
conn.close()
# 先标记为积分未平
out_list['code_type'] = '未平'
in_list['code_type'] = '未平'
# 增加三个字段
in_list['剩余积分'] = in_list[['change']]
in_list['已兑换积分'] = 0
in_list['兑付时间'] =''
# 推荐人号码生成list
aa_list = in_list['推荐人号码'].tolist()
aa_list = list(set(aa_list))
r_data=pd.DataFrame()
for i_mobile in aa_list:
#i_mobile = in_list.at[i,'推荐人号码']
#i_mobile= '18818009671'
i_out_list = out_list[out_list['mobile']==i_mobile]
if i_out_list.empty: # 若b表为空,则直接不进入循环
table_a = in_list[in_list['推荐人号码']==i_mobile]
#r_data = r_data.append(table_a,ignore_index=True)
# print(r_data)
#r_data.to_csv('r_data.csv')
else : # 若b表不为空,则进入循环
table_a = in_list[in_list['推荐人号码']==i_mobile]
table_b = i_out_list
rest(table_a)
rest(table_b)
# u=len(table_b)
# print(table_a)
for m in range(0,len(table_a)):
for n in range(0,len(table_b)):
print(m,n)
# print(table_a)
# print(table_b)
if m == 0:
if n == 0: # 只有1条入账,1条提现的情况
print(134)
if table_a.at[m, 'change'] - table_b.at[0, 'amount'] ==0:
table_a.at[m, '剩余积分'] = table_a.at[m, 'change'] - table_b.at[0, 'amount']
table_a.at[m, '已兑换积分'] = table_b.at[0, 'amount']
table_a.at[m,'code_type'] = '已平'
table_b.at[n,'code_type'] = '已平'
table_a.at[m,'兑付时间'] = table_b.at[0,'updated_at']
# print(table_a)
# print(table_b)
break
elif table_a.at[m, 'change'] - table_b.at[0, 'amount'] < 0:
table_a.at[m,'剩余积分'] = table_a.at[m, 'change']
table_a.at[m,'已兑换积分'] = 0
table_a.at[m,'code_type'] = '未平'
table_b.at[n,'code_type'] = '未平'
# print(table_a)
# print(table_b)
elif table_a.at[m, 'change'] - table_b.at[0, 'amount'] > 0:
table_a.at[m, '剩余积分'] = table_a.at[m, 'change'] - table_b.at[0, 'amount']
table_a.at[m, '已兑换积分'] = table_b.at[0, 'amount']
table_a.at[m, 'code_type'] = '未平'
table_b.at[n, 'code_type'] = '已平'
# print(table_a)
# print(table_b)
elif n>0:
print(154)
if table_a.at[m,'code_type'] == '未平':
if table_a.at[m,'剩余积分']-table_b.at[n, 'amount'] == 0 :
table_a.at[m, '剩余积分'] = table_a.at[m, '剩余积分'] - table_b.at[n, 'amount']
table_a.at[m, '已兑换积分'] =table_a.at[m, '已兑换积分']+ table_b.at[n, 'amount']
table_a.at[m, 'code_type'] = '已平'
table_b.at[n, 'code_type'] = '已平'
table_a.at[m, '兑付时间'] = table_b.at[n, 'updated_at']
# print(table_a)
# print(table_b)
break
elif table_a.at[m,'剩余积分']-table_b.at[n, 'amount'] >0 :
table_a.at[m, '剩余积分'] = table_a.at[m, '剩余积分'] - table_b.at[n, 'amount']
table_a.at[m, '已兑换积分'] =table_a.at[m, '已兑换积分']+ table_b.at[n, 'amount']
table_a.at[m, 'code_type'] = '未平'
table_b.at[n, 'code_type'] = '已平'
# print(table_a)
# print(table_b)
else :
print(i_mobile,m,n)
# print(table_a)
# print(table_b)
elif m>0:
print(174)
if table_a.at[m-1,'code_type'] == '未平' :
if table_a.at[m,'code_type'] == '已平':
break
else :
if table_b.at[n,'code_type'] == '已平':
print(i_mobile,m,n)
# print(table_a)
# print(table_b)
else:
if table_a.at[m-1,'剩余积分']+table_a.at[m,'change'] - table_b.at[n,'amount'] == 0:
table_a.at[m,'剩余积分'] = 0
table_a.at[m,'已兑换积分'] = table_b.at[n,'amount']
table_a.at[m,'code_type'] = '已平'
table_b.at[n,'code_type'] = '已平'
table_a.at[m, '兑付时间'] = table_b.at[n, 'updated_at']
# print(table_a)
# print(table_b)
break
elif table_a.at[m-1,'剩余积分']+table_a.at[m,'change'] - table_b.at[n,'amount'] <0:
table_a.at[m,'剩余积分'] = table_a.at[m-1,'剩余积分']+table_a.at[m,'change']
table_a.at[m, '已兑换积分'] = 0
table_a.at[m,'code_type'] = '未平'
table_b.at[n,'code_type'] = '未平'
# print(table_a)
# print(table_b)
elif table_a.at[m-1,'code_type'] == '已平' :
if table_b.at[n,'code_type'] == '已平':
print(i_mobile,m,n)
# print(table_a)
# print(table_b)
elif table_b.at[n,'code_type'] == '未平' :
print(196)
# print(table_a[m,'change'])
# print(table_b[n,'amount'])
if table_a.at[m,'change'] - table_b.at[n,'amount'] ==0:
print(198)
table_a.at[m,'剩余积分'] = table_a.at[m,'change'] - table_b.at[n,'amount']
table_a.at[m,'已兑换积分'] = table_a.at[m,'change']
table_a.at[m,'code_type'] = '已平'
table_b.at[n,'code_type'] = '已平'
table_a.at[m, '兑付时间'] = table_b.at[n, 'updated_at']
# print(table_a)
# print(table_b)
break
elif table_a.at[m,'change'] - table_b.at[n,'amount'] >0:
table_a.at[m, '剩余积分'] = table_a.at[m, '剩余积分'] - table_b.at[n, 'amount']
table_a.at[m, '已兑换积分'] = table_a.at[m, '已兑换积分'] + table_b.at[n, 'amount']
table_a.at[m, 'code_type'] = '未平'
table_b.at[n, 'code_type'] = '已平'
# print(table_a)
# print(table_b)
elif table_a.at[m,'change'] - table_b.at[n,'amount'] <0:
print(213)
table_a.at[m, '剩余积分'] =table_a.at[m-1, '剩余积分']+ table_a.at[m, 'change']
table_a.at[m, '已兑换积分'] = 0
table_a.at[m, 'code_type'] = '未平'
table_b.at[n, 'code_type'] = '未平'
# print(table_a)
# print(table_b)
# print(table_a)
# print(table_b)
r_data = r_data.append(table_a, ignore_index=True)
r_data['生效时间'] = r_data['生效时间'].dt.tz_localize(None)
r_data['兑付时间'] = r_data['生效时间'].dt.tz_localize(None)
print(r_data)
r_data.to_excel('r_data.xlsx')
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