# -*- coding: utf-8 -*- import os import sys import json import time import random import logging import pymysql import configparser from time import sleep import pandas as pd from datetime import datetime from DBUtils.PooledDB import PooledDB #配置输出日志格式 LOG_FORMAT = "%(asctime)s %(filename)s[line:%(lineno)d] %(name)s %(levelname)s %(pathname)s %(message)s " #配置输出时间的格式,注意月份和天数不要搞乱了 DATE_FORMAT = '%Y-%m-%d %H:%M:%S %a ' logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt = DATE_FORMAT , filename=r"./logs/zhibin_profit.log" #有了filename参数就不会直接输出显示到控制台,而是直接写入文件 ) cf = configparser.RawConfigParser() #cf = configparser.ConfigParser() cf.read(r"./zhibin_profit.ini") # 读取配置文件,如果写文件的绝对路径,就可以不用os模块 ZHIBIN_PROFIT = json.loads(cf.get("profit","zhibin_profit")) DB_CONFIG = { 'host' : 'localhost', 'port' : 3306, 'user' : 'bizuser', 'password' : 'PuVqOWuLLDFJ3fjU', 'db' : 'fmp', 'charset' : 'utf8', 'autocommit' : 1 } def createPool(db_config): spool = PooledDB(pymysql, 5, **db_config) return spool def get_zhibin_profit_status(): today = datetime.today() today_start = today.strftime("%Y-%m-%d") + " 00:00:00" today_end = today.strftime("%Y-%m-%d") + " 23:59:59" #print(today_start) #print(today_end) sql = """ SELECT SUM(price - flow_amount * 0.003 - operator_balance_price) profit FROM flow_order_info WHERE apply_date BETWEEN '{}' AND '{}' AND enterprise_id = 96 AND status != 4 """.format(today_start,today_end) print(sql) #订单池关闭 response_int = 2 response_channel = 0 profit = -1000000 try: df = pd.read_sql(sql,conn) df['profit'] = df['profit'].astype(float) if df.empty is False: profit = df['profit'].iloc[0] #print(type(ZHIBIN_PROFIT)) if profit <= ZHIBIN_PROFIT: response_int = 1 #订单池打开 response_channel = 1 except Exception as ex: print(ex) response_int = 10 logging.info("get_zhibin_profit exception is : %s,",ex) #print(profit) #print(ZHIBIN_PROFIT) #logging.info("fail_rate is : %s, pool_status is : %s, pool_detail_status is : %s ",fail_rate,response_int,response_channel) logging.info("zhibin_profit is : %s,expect profit is : %s, pool_status is : %s, pool_detail_status is : %s ",profit,ZHIBIN_PROFIT,response_int,response_channel) return response_int,response_channel def change_order_pool_status(): pool_status,pool_detail_status = get_zhibin_profit_status() if pool_status == 10: return ''' pool_status = 2 #订单池关闭 pool_detail_status = 0 #订单池关闭 rand = random.randint(1,100) if rand >= 50 : print(rand) pool_status = 1 pool_detail_status = 1 ''' logging.info("change_order_pool3_status pool_status is : %s,pool_detail_status is : %s ",pool_status,pool_detail_status) sql = "UPDATE channel_supplier SET is_valid = {} WHERE supplier_code = 'OrderPool3' ".format(pool_status) sql1 = "UPDATE access_channel_info SET is_valid = {} WHERE channel_seq_id IN(132,133,134) ".format(pool_detail_status) #print(sql1) try: cursor = conn.cursor() cursor.execute(sql) cursor.execute(sql1) cursor.close() except Exception as ex: #print(ex) logging.info("change_order_pool3_status exception is : %s",ex) if __name__ == '__main__': conn = createPool(DB_CONFIG).connection() change_order_pool_status() #get_zhibin_profit() conn.close()