saas_dj.py 7.6 KB
import logging
import pandas as pdas

from djcardrenewal.common.dbase.MysqlHelper import MysqlHelper
from djcardrenewal.common.dbase import DbConect
from djcardrenewal.common.sqlcontent import DjSql
from djcardrenewal.common.sqlcontent import CurMonthYzl
from djcardrenewal.common.string.StringBuilder import StringBuilder

print("sys config " + __name__)
log_name = './log/djcardrenewal/handlemain.log'
log_format = '[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]'
log_level = logging.DEBUG
log_mode = 'a'
logging.basicConfig(filename=log_name, format=log_format, level=log_level, filemode=log_mode)

class HandelMain:

    handleDescription = ''
    handleMonth = ''
    hmonth = ''

    djdb = None
    plusDb = None
    yearArray = ['2020','2021','2022']

    def __init__(self, handle_month):
        logging.info("init construct bean")
        self.handleDescription = "dj renewal match iccid and device location"

        self.handleMonth = handle_month
        self.hmonth = handle_month.replace('-', '')[2:6]

        self.djdb = MysqlHelper(DbConect.DB_shen_release_dj)
        self.plusDb = MysqlHelper(DbConect.DB_shen_release_plus)



    #------1 线索 ------
    def dealDyXiansuoMerchant(self):
        for yearline in self.yearArray:
            selectSql = f'''SELECT count(distinct dsm.merchant_id)
                        FROM dj_device_details d LEFT JOIN dj_system_merchant dsm ON d.active_merchant =dsm.merchant_name 
                        WHERE active_merchant <> '' AND
                        ((active_time < '{yearline}-01-01 00:00:00' AND DATE_ADD(active_time,INTERVAL pk_period MONTH) > '{yearline}-01-01 00:00:00') OR
                        (active_time >= '{yearline}-01-01 00:00:00' AND active_time < '{yearline}-12-31 23:59:59'))
                ;'''
            merData = self.djdb.execute_select(selectSql)
            print("---订阅_线索 %s 商户数:%s"  % (yearline, merData[0][0]))
        return True

    #------1 权益 ------
    def dealDyEquitMerchant(self):
        for yearline in self.yearArray:
            selectSql = f'''SELECT count(DISTINCT merchant_id )
                        FROM equity_order
                        WHERE DATE_ADD(success_date,INTERVAL 12 MONTH)>'{yearline}-01-01 00:00:00' and success_date<'{yearline}-12-31 23:59:59'
            ;'''
            merData = self.djdb.execute_select(selectSql)
            print("---订阅_权益 %s 商户数:%s"  % (yearline, merData[0][0]))
        return True

    #------1 合并 ------
    def dealDyXsAndEqMerchant(self):
        for yearline in self.yearArray:
            selectSql = f'''SELECT COUNT(DISTINCT aat.merchant_id) FROM (
                        SELECT DISTINCT dsm.merchant_id
                        FROM dj_device_details d LEFT JOIN dj_system_merchant dsm ON d.active_merchant =dsm.merchant_name 
                        WHERE active_merchant <> '' AND
                        ((active_time < '{yearline}-01-01 00:00:00' AND DATE_ADD(active_time,INTERVAL pk_period MONTH) > '{yearline}-01-01 00:00:00') OR
                        (active_time >= '{yearline}-01-01 00:00:00' AND active_time < '{yearline}-12-31 23:59:59'))
                        UNION ALL
                        SELECT DISTINCT merchant_id
                        FROM equity_order
                        WHERE DATE_ADD(success_date,INTERVAL 12 MONTH)>'{yearline}-01-01 00:00:00' AND success_date<'{yearline}-12-31 23:59:59'
                    ) aat
                ;'''
            merData = self.djdb.execute_select(selectSql)
            print("---订阅_合并 %s 商户数:%s"  % (yearline, merData[0][0]))
        return True



    #------2 saasPlus在订阅中开展业务的店面数 ------
    def dealPlusInnerDyMerchant(self):
        for yearline in self.yearArray:
            selectSql = f'''SELECT COUNT(*) FROM `temp_saasplus_bus_merchant_22` aat WHERE aat.`merchant_id` IN (
                    SELECT DISTINCT merchant_id AS merId
                    FROM (
                        SELECT DISTINCT dsm.merchant_id
                        FROM glsx_data.dj_device_details d LEFT JOIN dj_system_merchant dsm ON d.active_merchant =dsm.merchant_name 
                        WHERE active_merchant <> '' AND
                        ((active_time < '{yearline}-01-01 00:00:00' AND DATE_ADD(active_time,INTERVAL pk_period MONTH) > '{yearline}-01-01 00:00:00') OR
                        (active_time >= '{yearline}-01-01 00:00:00' AND active_time < '{yearline}-12-31 23:59:59'))
                        UNION ALL
                        SELECT DISTINCT merchant_id 
                        FROM equity_order
                        WHERE YEAR(success_date) ='{yearline}'
                    ) a
                ) AND aat.`bus_year`={yearline}
                ;'''
            merData = self.djdb.execute_select(selectSql)
            print("---SAASPLUS_Inner %s 商户数:%s"  % (yearline, merData[0][0]))
        return True

    #------2 saasPlus在订阅中开展业务的店面数 ------
    def dealPlusMergeDyMerchant(self):
        for yearline in self.yearArray:
            selectSql = f'''SELECT count(DISTINCT aat.merchant_id)
                    FROM (
                        SELECT (CASE WHEN dsm.merchant_id THEN dsm.merchant_id ELSE 1 END) merchant_id
                        FROM glsx_data.dj_device_details d LEFT JOIN dj_system_merchant dsm ON d.active_merchant =dsm.merchant_name 
                        WHERE active_merchant <> '' AND dsm.`merchant_id` IS NOT NULL AND
                        ((active_time < '{yearline}-01-01 00:00:00' AND DATE_ADD(active_time,INTERVAL pk_period MONTH) > '{yearline}-01-01 00:00:00') OR
                        (active_time >= '{yearline}-01-01 00:00:00' AND active_time < '{yearline}-12-31 23:59:59'))
                        UNION ALL
                        SELECT DISTINCT merchant_id 
                        FROM equity_order
                        WHERE DATE_ADD(success_date,INTERVAL 12 MONTH)>'{yearline}-01-01 00:00:00' AND success_date<'{yearline}-12-31 23:59:59'
                        UNION ALL
                        SELECT merchant_id FROM `temp_saasplus_bus_merchant_22` WHERE `bus_year`={yearline}
                    ) aat
                ;'''
            merData = self.djdb.execute_select(selectSql)
            print("---SAASPLUS_合并订阅 %s 商户数:%s"  % (yearline, merData[0][0]))
        return True



    def proxySaasDYMerchantCount(self):
        # 处理 订阅数据
        xsRes = False
        xsRes = handleMain.dealDyXiansuoMerchant()
        logging.info(f'===订阅线索:{xsRes}')

        # 处理 权益数据
        qyRes = False
        qyRes = handleMain.dealDyEquitMerchant()
        logging.info(f'===订阅权益:{qyRes}')

        # 合并数据
        hbRes = False
        hbRes = handleMain.dealDyXsAndEqMerchant()
        logging.info(f'===订阅合并处理OK:{hbRes}')

    def proxyDyAndSaasPlusMerchantCount(self):
        # 处理 SAAS_PLUS在订阅中有业务的店面数
        xsRes = False
        xsRes = handleMain.dealPlusInnerDyMerchant()
        logging.info(f'===PLUS_inner:{xsRes}')
        
        # 合并数据
        hbRes = False
        hbRes = handleMain.dealPlusMergeDyMerchant()
        logging.info(f'===PLUS订阅合并处理OK:{hbRes}')
        


if __name__ == "__main__":
    handle_month = '2022-12'
    dedRes = False
    diRes = False
    urRes = False

    #启动初始化
    handleMain = HandelMain(handle_month)
    logging.info("start handle" + handleMain.handleDescription + ' the month:' + handleMain.handleMonth)

    # 输出店面数据
    handleMain.proxySaasDYMerchantCount()

    # 输出SAAS+与DJ重叠业务的店面数据
    handleMain.proxyDyAndSaasPlusMerchantCount()