Commit bfd26a13 by 杨林龙

添加分摊脚本

1 parent 3f838a7a
...@@ -242,7 +242,7 @@ def export_csv_2(fileName): ...@@ -242,7 +242,7 @@ 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(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, 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,sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0))/count(1) as pk_price,
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) 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 (6,12501) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ; FROM dj_device_details where `device_type` IN (6,12501) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ;
""" """
......
...@@ -39,7 +39,7 @@ saas_shen_db_cfg = { ...@@ -39,7 +39,7 @@ saas_shen_db_cfg = {
def deal_equity_data(): def deal_equity_data():
bizConn = MySQL(saas_shen_db_cfg) bizConn = MySQL(saas_shen_db_cfg)
# SELECT `id`,`sn`,`fentan_time`,`pk_period`,SUM(case WHEN pk_price is NULL then 0 else pk_price end) as total_price FROM dj_device_details where `device_type` IN (8) GROUP BY active_merchant,LEFT(fentan_time ,7),pk_period ;
sql = """ sql = """
SELECT `id`,`sn`,`fentan_time`,`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 ; SELECT `id`,`sn`,`fentan_time`,`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 ;
""" """
...@@ -56,11 +56,8 @@ def deal_equity_data(): ...@@ -56,11 +56,8 @@ def deal_equity_data():
服务期限 = paddnum_price(row[3]) 服务期限 = paddnum_price(row[3])
服务总金额 = paddnum_price(row[4]) 服务总金额 = paddnum_price(row[4])
if 服务期限 == 0 or 服务期限 is None:
服务期限 = 12
分摊金额 = 服务总金额 / 服务期限 分摊金额 = 服务总金额 / 服务期限
分摊金额 = Decimal(分摊金额).quantize(Decimal("0.00")) 分摊金额 = Decimal(分摊金额).quantize(Decimal("00000.00"))
values = buildApportionment(device_id, 分摊时间, 服务期限, 分摊金额) values = buildApportionment(device_id, 分摊时间, 服务期限, 分摊金额)
sql01 = f""" sql01 = f"""
INSERT INTO temp_device_details_ft (device_id,apportionment_date,apportionment_price) values {values}; INSERT INTO temp_device_details_ft (device_id,apportionment_date,apportionment_price) values {values};
...@@ -79,8 +76,8 @@ def buildApportionment(device_id, 分摊时间, 服务期限, 分摊金额): ...@@ -79,8 +76,8 @@ def buildApportionment(device_id, 分摊时间, 服务期限, 分摊金额):
分摊最后日期 = add_one_month(分摊时间, 服务期限) 分摊最后日期 = add_one_month(分摊时间, 服务期限)
print("分摊最后日期==", 分摊最后日期) print("分摊最后日期==", 分摊最后日期)
for i in range(103): for i in range(150):
start_time = datetime.strptime("2020-01-01", "%Y-%m-%d") start_time = datetime.strptime("2017-01-01", "%Y-%m-%d")
# 添加一个月的日期 # 添加一个月的日期
calculate_time = add_one_month(start_time, i) calculate_time = add_one_month(start_time, i)
if 分摊时间 <= calculate_time < 分摊最后日期: if 分摊时间 <= calculate_time < 分摊最后日期:
...@@ -163,7 +160,7 @@ def deal_zengjia_equity_data(): ...@@ -163,7 +160,7 @@ def deal_zengjia_equity_data():
def deal_qichu_equity_data(): def deal_qichu_equity_data():
bizConn = MySQL(saas_shen_db_cfg) bizConn = MySQL(saas_shen_db_cfg)
year_arr = [2020, 2021, 2022, 2023, 2024, 2025] year_arr = [2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027]
for year_num in year_arr: for year_num in year_arr:
sql = f""" sql = f"""
SELECT id, device_id, order_year, qichu_num, zengjia_num, jianshao_num FROM temp_device_details_zj WHERE order_year= {year_num} ; SELECT id, device_id, order_year, qichu_num, zengjia_num, jianshao_num FROM temp_device_details_zj WHERE order_year= {year_num} ;
...@@ -234,7 +231,7 @@ def deal_null_equity_data(): ...@@ -234,7 +231,7 @@ def deal_null_equity_data():
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")
yearList = [2020, 2021, 2022, 2023, 2024, 2025] yearList = [2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027]
headName = "客户名称,门店,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入" headName = "客户名称,门店,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入"
for year in yearList: for year in yearList:
headName += f",{year}期初数,{year}期初单价,{year}期初金额,{year}增加数,{year}增加单价,{year}增加金额,{year}减少数,{year}减少单价,{year}减少金额" headName += f",{year}期初数,{year}期初单价,{year}期初金额,{year}增加数,{year}增加单价,{year}增加金额,{year}减少数,{year}减少单价,{year}减少金额"
...@@ -243,7 +240,7 @@ def export_csv_2(fileName): ...@@ -243,7 +240,7 @@ 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(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, 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,sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0))/count(1) as pk_price,
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) 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 ;
""" """
...@@ -251,8 +248,9 @@ def export_csv_2(fileName): ...@@ -251,8 +248,9 @@ def export_csv_2(fileName):
logger.error("deal_equity_data fail, sql=%s", sql) logger.error("deal_equity_data fail, sql=%s", sql)
return return
result = bizConn.fetchAllRows() result = bizConn.fetchAllRows()
for row in result: logger.info("size", len(result))
logger.info(row) for index, row in enumerate(result):
logger.info("index===", index, "row===", row)
active_merchant = paddnum_active_merchant(row[1]) 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]}" 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:
...@@ -295,13 +293,13 @@ if __name__ == "__main__": ...@@ -295,13 +293,13 @@ if __name__ == "__main__":
# 处理基础数据 # 处理基础数据
# deal_equity_data() # deal_equity_data()
# 处理减少数据 # 处理减少数据
deal_jianshao_equity_data() # deal_jianshao_equity_data()
# 处理增加数据 # 处理增加数据
deal_zengjia_equity_data() # deal_zengjia_equity_data()
# 处理期初数据 # 处理期初数据
deal_qichu_equity_data() # deal_qichu_equity_data()
# 处理为NULL的数据 # 处理为NULL的数据
deal_null_equity_data() # deal_null_equity_data()
# 导出 csv # 导出 csv
# export_csv("分摊出货汇总-20230519.csv") # export_csv("分摊出货汇总-20230519.csv")
export_csv_2("分摊出货汇总-20230519-3.csv") export_csv_2("分摊出货汇总-20230519-3.csv")
import logging
from my_utils.MysqlUtility import MySQL
logger = logging.getLogger('deal_equity_data')
logger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler('../20230404/import.log')
fileHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
cnslhandler = logging.StreamHandler()
cnslhandler.setLevel(logging.DEBUG)
cnslhandler.setFormatter(formatter)
logger.addHandler(cnslhandler)
# saas_shen_db_cfg = {'host': '192.168.5.77',
# 'port': 3306,
# 'user': 'biz_saas_plus',
# 'passwd': 'lOmEudkc',
# 'db': 'saas_plus',
# 'charset': 'utf8'}
saas_shen_db_cfg = {'host': '192.168.3.222',
'port': 3306,
'user': 'biz_audit',
'passwd': 'hqVIs6jF',
'db': 'saas_shen',
'charset': 'utf8'}
class BaseCreateTable:
def __init__(self, base_table='temp_device_details_ft', zj_table='temp_device_details_zj'):
self.bizConn = MySQL(saas_shen_db_cfg)
self.base_table = base_table
self.zj_table = zj_table
def create_table(self):
drop_table_sql = f'''
drop table if exists {self.base_table};
'''
self.bizConn.query(drop_table_sql)
create_table_sql = f'''
CREATE TABLE `{self.base_table}` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`device_id` int(11) DEFAULT NULL COMMENT '设备表ID',
`apportionment_date` datetime DEFAULT NULL COMMENT '分摊日期',
`apportionment_price` decimal(12,2) DEFAULT NULL COMMENT '分摊金额',
PRIMARY KEY (`id`),
KEY `idx_device_id` (`device_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='基础分摊表';
'''
self.bizConn.query(create_table_sql)
drop_table_sql = f'''
drop table if exists {self.zj_table};
'''
self.bizConn.query(drop_table_sql)
create_table_sql = f'''
CREATE TABLE `{self.zj_table}` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`device_id` int(11) DEFAULT NULL COMMENT '设备表ID',
`order_year` varchar(32) DEFAULT NULL COMMENT '订单年份',
`qichu_num` int(11) DEFAULT NULL COMMENT '期初数量',
`qichu_unit_price` decimal(12,2) DEFAULT NULL COMMENT '期初单价',
`qichu_total_price` decimal(12,2) DEFAULT NULL COMMENT '期初金额',
`zengjia_num` int(11) DEFAULT NULL COMMENT '增加数量',
`zengjia_unit_price` decimal(12,2) DEFAULT NULL COMMENT '增加单价',
`zengjia_total_price` decimal(12,2) DEFAULT NULL COMMENT '增加金额',
`jianshao_num` int(11) DEFAULT NULL COMMENT '减少数量',
`jianshao_unit_price` decimal(12,2) DEFAULT NULL COMMENT '减少单价',
`jianshao_total_price` decimal(12,2) DEFAULT NULL COMMENT '减少金额',
PRIMARY KEY (`id`),
KEY `idx_device_id` (`device_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='增减表';
'''
self.bizConn.query(create_table_sql)
if __name__ == '__main__':
my_base = BaseCreateTable()
my_base.create_table()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!