dealHengxinEdr.py 6.05 KB
import logging
import pandas as pdas
import os
import chardet
import datetime
import re

import openpyxl
from openpyxl import Workbook

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/supplychain/dealHengxinEdr.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 DealHengXinEdrMain:

    handleDescription = ''
    handleMonth = ''
    hmonth = ''
    module_file_path = ''
    defaultExportReleasePath = ''

    supplychainDb = None
    djDb = None

    def __init__(self, handle_month):
        logging.info("init construct bean")
        self.handleDescription = "---业务导出恒信数据---脚本执行..."
        self.handleMonth = handle_month
        self.hmonth = handle_month.replace('-', '')[2:6]
        self.supplychainDb = MysqlHelper(DbConect.DB_supplychain_release_slave)
        self.djDb = MysqlHelper(DbConect.dj_release_slave)

    def initCurrentMonthTable(self):
        logging.info("初始化:暂无操作")
        
    # 判断文件编码
    def detect_file_encoding(self, filename):
        with open(filename, 'rb') as f:
            content = f.read()
            result = chardet.detect(content)
            return result['encoding']

    def dealDeviceEndDateList(self):
        return True

    # 定义修改函数
    def modify_data(self, x):
        # 进行批量修改的逻辑
        if x is None:
            return x
        return x.replace("M{", "").replace("}", "")

    # 定义修改函数
    def modify_float_data(self, x):
        # 进行批量修改的逻辑
        if x is None:
            return 0
        current = str(x).replace("`", "").replace(' ', '')
        if current == '':
            return 0
        return current

    # 定义修改函数
    def dealSendDate(self, sendDate):
        # 去除字符串中的汉字
        sendDate = re.sub(r'[\u4e00-\u9fa5]', '', sendDate)

        # 进行批量修改的逻辑
        if ',' in sendDate:
            return sendDate.split(',')[0]
        
        if len(sendDate)<19:
            sendDate = sendDate + ' 00:00:00'
        return sendDate
    
    # 定义修改函数
    def dealContentFile(self):
        selectSupplychainSql = f'''select df.ID,df.SN as '设备号',df.DEVICE_CODE as '设备编码',df.ORDER_CODE as '订单号',df.VERIF_CODE,df.BATCH_NO as '批次',df.PACKAGE_ID as '套餐ID',df.ANDROID_PACKAGE_ID,df.OPERATOR_MERCHANT_NO,df.SEND_MERCHANT_NO as '发往商户号',df.IN_STORAGE_TIME as '入库时间',df.OUT_STORAGE_TIME as '出库时间',df.OUT_STORAGE_TYPE as '出库类型',df.TERMINAL_DISCODE,df.EXTERNAL_FLAG,df.MANUFACTURER_CODE,df.FIRMWARE_ID,df.CARD_ID,df.CREATED_BY,df.CREATED_DATE,df.UPDATED_BY,df.UPDATED_DATE,df.DELETED_FLAG,df.REMARK,df.IMEI,df.ORDER_MERCHANT_NO,df.send_time from device_file df,device_code dc where df.SEND_MERCHANT_NO=44193436 and df.DEVICE_CODE =dc.DEVICE_CODE and dc.TYPE_ID =6 and dc.DEVICE_CODE =100138;'''
        # 供应链的数据
        print('查询供应链数据')
        supplychainDbData = self.supplychainDb.execute_select(selectSupplychainSql)
        if supplychainDbData is None :
            logging.info("db supplychain enddate is NONE")
            return
        elif supplychainDbData is False:
            logging.info("db supplychain enddate is FALSE")
            return
        
        print('组装SN列表')
        snListStr = ''
        for chainItem in supplychainDbData:
            currentSn = f''''{chainItem[1]}',''';
            snListStr += currentSn;

        snListStr = snListStr[0:-1]
        # print(snListStr)

        # 匹配设备是否在DJ入库
        print('查询DJ导入记录')
        selectDjSql = f'''select * from dj_customer_device dcd where dcd.imei in ({snListStr});'''
        djDbData = self.djDb.execute_select(selectDjSql)

        dataExport = pdas.DataFrame(supplychainDbData)
        dataExport.columns = ['ID','设备号','设备编码','订单号','VERIF_CODE','批次','套餐ID','ANDROID_PACKAGE_ID','OPERATOR_MERCHANT_NO','发往商户号','入库时间','出库时间','出库类型','TERMINAL_DISCODE','EXTERNAL_FLAG','MANUFACTURER_CODE','FIRMWARE_ID','CARD_ID','CREATED_BY','CREATED_DATE','UPDATED_BY','UPDATED_DATE','DELETED_FLAG','REMARK','IMEI','ORDER_MERCHANT_NO','send_time']
        
        print('匹配DJ导入状态')
        djSnMapper = {}
        if djDbData is None or djDbData is False :
            print('dont match dj sn data')
        else:
            for djItem in djDbData:
                djMerchantId = djItem[3]
                djMerchantName = djItem[4]
                djSn = djItem[5]
                dataExport.loc[dataExport['设备号'] == djSn, '是否录入嘀加系统'] = '是'
                dataExport.loc[dataExport['设备号'] == djSn, '录入嘀加商户号'] = djMerchantId
                dataExport.loc[dataExport['设备号'] == djSn, '录入嘀加商户名称'] = djMerchantName

        print('输出excel数据')
        # 输出数据到excel
        dataExport.to_excel(f'{self.defaultExportReleasePath}', index=False)



if __name__ == "__main__":
    handle_month = '2021-2023'
    dedRes = False
    diRes = False
    urRes = False


    print('==========================跑数据根据SN获取发货信息及状态==========================')

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

    dealMain.module_file_path = 'E:/work/000 - 运营数据导出/恒信-C580记录仪设备明细-template.xlsx'
    dealMain.defaultExportReleasePath = 'E:/work/000 - 运营数据导出/export/恒信-C580记录仪设备明细-20231226.xlsx'

    dealMain.dealContentFile()
    print('导出完成。')