Commit 3f838a7a by 杨林龙

优化分摊脚本

1 parent 7dd955a3
...@@ -137,7 +137,7 @@ def deal_jianshao_equity_data(): ...@@ -137,7 +137,7 @@ def deal_jianshao_equity_data():
def deal_zengjia_equity_data(): def deal_zengjia_equity_data():
bizConn = MySQL(saas_shen_db_cfg) bizConn = MySQL(saas_shen_db_cfg)
sql = """ sql = """
SELECT `id`, left(`fentan_time`,4) AS 'year',`pk_period`,SUM(case WHEN pk_price is NULL then 0 else pk_price end) as total_price SELECT `id`, left(`fentan_time`,4) AS 'year',`pk_period`,sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as total_price
FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7), pk_period ; FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7), pk_period ;
""" """
if not bizConn.query(sql): if not bizConn.query(sql):
...@@ -204,6 +204,10 @@ def paddnum_price(num): ...@@ -204,6 +204,10 @@ def paddnum_price(num):
return num if num is not None else 0 return num if num is not None else 0
def paddnum_active_merchant(active_merchant):
return active_merchant if active_merchant is not None else "激活客户"
def paddstr(strx): def paddstr(strx):
return ( return (
"NULL" "NULL"
...@@ -227,56 +231,6 @@ def deal_null_equity_data(): ...@@ -227,56 +231,6 @@ def deal_null_equity_data():
bizConn.update(sql) bizConn.update(sql)
def export_csv(fileName):
bizConn = MySQL(saas_shen_db_cfg)
fileHandle = open(file=fileName, mode="w", encoding="utf-8")
yearList = [2020, 2021, 2022, 2023, 2024, 2025]
headName = (
"id,channel,sn,device_type,delivery_time,delivery_merchant,active_time,active_merchant,pk_name,pk_period,settle_merchant,"
"settle_order,settle_time,pk_price,hw_price,install_price,hw_cost,install_cost,fentan_time"
)
for year in yearList:
headName += f",{year}期初数,{year}期初单价,{year}期初金额,{year}增加数,{year}增加单价,{year}增加金额,{year}减少数,{year}减少单价,{year}减少金额"
headName += "\n"
fileHandle.write(headName)
# SELECT teo.id ,teo.order_code, teo.order_time, teo.service_period, teo.total_price FROM temp_eq_order; where teo.id = 251
sql = f"""
SELECT id,channel,sn,device_type,delivery_time,delivery_merchant,active_time,active_merchant,pk_name,pk_period,settle_merchant,settle_order,settle_time,sum(pk_price)
,hw_price,install_price,hw_cost,install_cost ,fentan_time FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ;
"""
if not bizConn.query(sql):
logger.error("deal_equity_data fail, sql=%s", sql)
return
result = bizConn.fetchAllRows()
for row in result:
logger.info(row)
lineValue = f"{row[0]},{row[1]},{row[2]},{row[3]},{row[4]},{row[5]},{row[6]},{row[7]},{row[8]},{row[9]},{row[10]},{row[11]},{row[12]},{row[13]},{row[14]},{row[15]},{row[16]},{row[17]},{row[18]}"
for year in yearList:
logger.info("device_id" + str(row[0]) + ";year" + str(year))
sql = f"""
SELECT id,device_id,order_year,qichu_num,qichu_unit_price,qichu_total_price,zengjia_num,zengjia_unit_price,zengjia_total_price,jianshao_num,jianshao_unit_price,jianshao_total_price
FROM temp_device_details_zj tddz WHERE tddz.device_id = {row[0]} AND tddz.order_year = '{year}';
"""
logger.info(sql)
if not bizConn.query(sql):
logger.error("deal_equity_data fail, sql=%s", sql)
return
result = bizConn.fetchAllRows()
if len(result) == 0:
logger.debug(row)
lineValue += f",0,0,0,0,0,0,0,0,0"
else:
for row01 in result:
logger.info(row01)
lineValue += f",{paddnum_price(row01[3])},{paddnum_price(row01[4])},{paddnum_price(row01[5])},{paddnum_price(row01[6])},{paddnum_price(row01[7])},{paddnum_price(row01[8])},{paddnum_price(row01[9])},{paddnum_price(row01[10])},{paddnum_price(row01[11])}"
# 写入一行数据
lineValue += "\n"
logger.debug(lineValue)
fileHandle.write(lineValue)
# 关闭流
fileHandle.close()
def export_csv_2(fileName): def export_csv_2(fileName):
bizConn = MySQL(saas_shen_db_cfg) bizConn = MySQL(saas_shen_db_cfg)
fileHandle = open(file=fileName, mode="w", encoding="utf-8") fileHandle = open(file=fileName, mode="w", encoding="utf-8")
...@@ -289,8 +243,8 @@ def export_csv_2(fileName): ...@@ -289,8 +243,8 @@ def export_csv_2(fileName):
fileHandle.write(headName) fileHandle.write(headName)
sql = f""" sql = f"""
SELECT id,active_merchant,'门店',channel,pk_period,count(1) as num, settle_time, sum(pk_price) as total_price,fentan_time,DATE_ADD(fentan_time,INTERVAL pk_period MONTH ) as fentan_end_time,pk_price, SELECT id,active_merchant,'门店',channel,pk_period,count(1) as num, settle_time, sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as total_price,fentan_time,DATE_ADD(fentan_time,INTERVAL pk_period MONTH ) as fentan_end_time,IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0) as pk_price,
sum(pk_price)/pk_period, device_type,min(active_time),max(active_time),fentan_time,left(fentan_time,4) sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0))/pk_period, device_type,min(active_time),max(active_time),fentan_time,left(fentan_time,4)
FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ; FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ;
""" """
if not bizConn.query(sql): if not bizConn.query(sql):
...@@ -299,7 +253,8 @@ def export_csv_2(fileName): ...@@ -299,7 +253,8 @@ def export_csv_2(fileName):
result = bizConn.fetchAllRows() result = bizConn.fetchAllRows()
for row in result: for row in result:
logger.info(row) logger.info(row)
lineValue = f"{row[1]},{row[2]},{row[3]},{row[4]},{row[5]},{row[6]},{row[7]},{row[8]},{row[9]},{row[10]},{row[11]}" active_merchant = paddnum_active_merchant(row[1])
lineValue = f"{active_merchant},{row[2]},{row[3]},{row[4]},{row[5]},{row[6]},{row[7]},{row[8]},{row[9]},{row[10]},{row[11]}"
for year in yearList: for year in yearList:
logger.info("device_id" + str(row[0]) + ";year" + str(year)) logger.info("device_id" + str(row[0]) + ";year" + str(year))
sql = f""" sql = f"""
...@@ -322,6 +277,9 @@ def export_csv_2(fileName): ...@@ -322,6 +277,9 @@ def export_csv_2(fileName):
lineValue += ",记录仪" lineValue += ",记录仪"
elif row[12] == 12501: elif row[12] == 12501:
lineValue += ",车充" lineValue += ",车充"
elif row[12] == 8:
lineValue += ",追踪器"
lineValue += f",{row[13]},{row[14]},{row[15]},{row[16]}" lineValue += f",{row[13]},{row[14]},{row[15]},{row[16]}"
# 写入一行数据 # 写入一行数据
lineValue += "\n" lineValue += "\n"
...@@ -333,9 +291,9 @@ def export_csv_2(fileName): ...@@ -333,9 +291,9 @@ def export_csv_2(fileName):
if __name__ == "__main__": if __name__ == "__main__":
# 创建数据表 # 创建数据表
BaseCreateTable().create_table() # BaseCreateTable().create_table()
# 处理基础数据 # 处理基础数据
deal_equity_data() # deal_equity_data()
# 处理减少数据 # 处理减少数据
deal_jianshao_equity_data() deal_jianshao_equity_data()
# 处理增加数据 # 处理增加数据
......
def print_hi(name): def print_hi(name):
# Use a breakpoint in the code line below to debug your script. # Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. print(f"Hi, {name}") # Press Ctrl+F8 to toggle the breakpoint.
from datetime import datetime from datetime import datetime
...@@ -19,11 +19,11 @@ def add_one_month(date, i): ...@@ -19,11 +19,11 @@ def add_one_month(date, i):
def yang(): def yang():
print('nihao') print("nihao")
if __name__ == '__main__': if __name__ == "__main__":
print_hi('PyCharm') print_hi("PyCharm")
yang() yang()
# for i in range(60): # for i in range(60):
# print('i===', i) # print('i===', i)
...@@ -31,4 +31,6 @@ if __name__ == '__main__': ...@@ -31,4 +31,6 @@ if __name__ == '__main__':
# Example usage # Example usage
date = datetime(2021, 5, 1) date = datetime(2021, 5, 1)
new_date = add_one_month(date, 1) new_date = add_one_month(date, 1)
print('new_date', new_date) # Output: 2022-04-30 00:00:00 print("new_date", new_date) # Output: 2022-04-30 00:00:00
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!