Commit 00e3e46d by 杨林龙

优化业务代码

1 parent 7a804129
...@@ -388,7 +388,7 @@ def calc_return_share(calcType): ...@@ -388,7 +388,7 @@ def calc_return_share(calcType):
sql = ( sql = (
f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end,"
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count," f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count,"
f"sum(IFNULL(hw_cost,0) + IFNULL(install_cost, 0) ) as amount, channel " f"sum(IFNULL(hw_cost,0) + IFNULL(install_cost, 0) ) as amount, channel ,id "
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 "
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel " f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel "
f"order by settle_merchant,settle_month" f"order by settle_merchant,settle_month"
...@@ -397,7 +397,7 @@ def calc_return_share(calcType): ...@@ -397,7 +397,7 @@ def calc_return_share(calcType):
sql = ( sql = (
f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end,"
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count," f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count,"
f"sum(IFNULL(pk_price, 0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as amount, channel " f"sum(IFNULL(pk_price, 0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as amount, channel ,id "
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 "
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel " f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel "
f"order by settle_merchant,settle_month" f"order by settle_merchant,settle_month"
...@@ -405,12 +405,13 @@ def calc_return_share(calcType): ...@@ -405,12 +405,13 @@ def calc_return_share(calcType):
if not bizConn.query(sql): if not bizConn.query(sql):
return return
result = bizConn.fetchAllRows() result = bizConn.fetchAllRows()
headName = ( headName = f"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入,"
f"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入," year_list = [2017, 2018, 2019, 2020, 2021, 2022]
f"2017年摊销金额,2017年末累计摊销余额,2018年摊销金额," for year in year_list:
f"2018年末累计摊销余额,2019年摊销金额,2019年末累计摊销余额,2020年摊销金额,2020年末累计摊销余额,2021年摊销金额," headName += f"{year}年摊销金额,{year}年末累计摊销余额,"
f"2021年末累计摊销余额,2022年摊销金额,2022年末累计摊销余额,产品分类 \n" headName += f"{year}-01月分摊,{year}-02月分摊,{year}-03月分摊,{year}-04月分摊,{year}-05月分摊,{year}-06月分摊,{year}-07月分摊,{year}-08月分摊,{year}-09月分摊,{year}-10月分摊,{year}-11月分摊,{year}-12月分摊,"
) headName += f"产品分类"
headName += "\n"
fileHandle.write(headName) fileHandle.write(headName)
for row in result: for row in result:
settleMer = row[0] settleMer = row[0]
...@@ -427,6 +428,7 @@ def calc_return_share(calcType): ...@@ -427,6 +428,7 @@ def calc_return_share(calcType):
total = row[4] total = row[4]
amount = row[5] amount = row[5]
channel = row[6] channel = row[6]
device_id = row[7]
start_share_month = datetime.datetime.strptime(settle_start_month, "%Y-%m") start_share_month = datetime.datetime.strptime(settle_start_month, "%Y-%m")
end_share_month = settle_end_month end_share_month = settle_end_month
...@@ -436,63 +438,114 @@ def calc_return_share(calcType): ...@@ -436,63 +438,114 @@ def calc_return_share(calcType):
remainTotal = amount remainTotal = amount
share_2017, remainShare2017, remainTotal = calc_remain_year( (
share_2017,
remainShare2017,
remainTotal,
my_fentan_detail_map_2017,
) = calc_remain_year(
2017, 2017,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
share_2018, remainShare2018, remainTotal = calc_remain_year( (
share_2018,
remainShare2018,
remainTotal,
my_fentan_detail_map_2018,
) = calc_remain_year(
2018, 2018,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
share_2019, remainShare2019, remainTotal = calc_remain_year( (
share_2019,
remainShare2019,
remainTotal,
my_fentan_detail_map_2019,
) = calc_remain_year(
2019, 2019,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
share_2020, remainShare2020, remainTotal = calc_remain_year( (
share_2020,
remainShare2020,
remainTotal,
my_fentan_detail_map_2020,
) = calc_remain_year(
2020, 2020,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
share_2021, remainShare2021, remainTotal = calc_remain_year( (
share_2021,
remainShare2021,
remainTotal,
my_fentan_detail_map_2021,
) = calc_remain_year(
2021, 2021,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
share_2022, remainShare2022, remainTotal = calc_remain_year( (
share_2022,
remainShare2022,
remainTotal,
my_fentan_detail_map_2022,
) = calc_remain_year(
2022, 2022,
start_share_month, start_share_month,
end_share_month, end_share_month,
share_per_month, share_per_month,
remainTotal, remainTotal,
amount, amount,
device_id,
) )
lineName = ( # lineName = (
f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month}," # f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},"
f"{settle_end_month},{price},{share_per_month},{share_2017},{remainShare2017},{share_2018}," # f"{settle_end_month},{price},{share_per_month},{share_2017},{remainShare2017},{share_2018},"
f"{remainShare2018},{share_2019},{remainShare2019},{share_2020},{remainShare2020}," # f"{remainShare2018},{share_2019},{remainShare2019},{share_2020},{remainShare2020},"
f"{share_2021},{remainShare2021},{share_2022},{remainShare2022},{devName}\n" # f"{share_2021},{remainShare2021},{share_2022},{remainShare2022},{devName}\n"
) # )
lineName = f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},{settle_end_month},{price},{share_per_month},"
lineName += f"{share_2017},{remainShare2017},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2017, 2017)
lineName += f"{share_2018},{remainShare2018},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2018, 2018)
lineName += f"{share_2019},{remainShare2019},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2019, 2019)
lineName += f"{share_2020},{remainShare2020},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2020, 2020)
lineName += f"{share_2021},{remainShare2021},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2021, 2021)
lineName += f"{share_2022},{remainShare2022},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2022, 2022)
lineName += f"{devName}"
lineName += "\n"
fileHandle.write(lineName) fileHandle.write(lineName)
fileHandle.close() fileHandle.close()
...@@ -503,7 +556,7 @@ def calc_return_share(calcType): ...@@ -503,7 +556,7 @@ def calc_return_share(calcType):
if __name__ == "__main__": if __name__ == "__main__":
# BaseCreateTable(base_table).create_table() # BaseCreateTable(base_table).create_table()
# calc_income_share(calcType="收入") # calc_income_share(calcType="收入")
calc_income_share(calcType="成本") # calc_income_share(calcType="成本")
# calc_return_share("退货收入") calc_return_share("退货收入")
# calc_return_share("退货成本") calc_return_share("退货成本")
print("end===") print("end===")
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!