pg_app_db_func.py 6.5 KB
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2020/7/12 16:27
# @Author:bxh
# @file: pg_db_func.py
import psycopg2
from datetime import date, timedelta
import datetime

def get_today():
    now_today = datetime.datetime.now()
    str_time = now_today.strftime("%Y-%m-%d")
    return str_time

def get_tomorrow():
    tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
    return tomorrow

# 获得连接
def GetConnect():
    conn = False
    try:
        conn = psycopg2.connect(database="partner_test",
                                    user="bianxinhua_user",
                                    password="Bianxinhua_user#123",
                                    host="101.37.68.23",
                                    port="15432")
    except Exception as err:
        print("连接数据库失败,%s" % err)
    return conn
#获取超级管理员id
def get_partner_id_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  id  from \"{}\" where account = '13459147023' '''.format(tbl)
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return result[0]
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()
#获取超级管理员账号
def get_partner_account_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  account  from \"{}\" where account = '13459147023' '''.format(tbl)
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return result[0]
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()
#获取超级管理员密码
def get_partner_password_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select "password" from \"{}\" where account = '13459147023' '''.format(tbl)
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return result[0]
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()
#累计实发订单
def get_cumulativeQuantity_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  COUNT(id) from \"{}\" where partner_id = {} and order_status in (2,3)  '''.format(tbl,get_partner_id_from_database('partner_info'))
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return result[0]
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

#累计实发订单金额
def get_cumulativeMoney_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  sum(order_actual_amount) from \"{}\" where partner_id = {} and order_status in (2,3)  '''.format(tbl,get_partner_id_from_database('partner_info'))
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

#今日新增实发订单金额
def get_todayRealMoney_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  sum(order_actual_amount) from \"{}\" where create_at between \'{}\' and \'{}\'  and order_status in (2,3)  '''.format(tbl,get_today(),get_tomorrow())
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

#今日新增实发订单
def get_todayRealQuantity_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select COUNT(id) from \"{}\" where create_at between \'{}\' and \'{}\'  and order_status in (2,3)  '''.format(tbl,get_today(),get_tomorrow())
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

#今日新增意向订单
def get_todayIntentionQuantity_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select COUNT(id) from \"{}\" where create_at between \'{}\' and \'{}\'  and order_status in (1)  '''.format(tbl,get_today(),get_tomorrow())
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

#今日新增意向订单金额
def get_todayIntentionMoney_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select  sum(order_actual_amount) from \"{}\" where create_at between \'{}\' and \'{}\'  and order_status in (1)  '''.format(tbl,get_today(),get_tomorrow())
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()
#订单数量
def get_orderQuantity_from_database(tbl):
    db = GetConnect()
    cursor = db.cursor()
    # sql= "select id from " + '''"''' +  tbl + '''"'''+ ' ' + 'order by id desc '
    sql = '''select COUNT(id) from \"{}\" where create_at between \'{}\' and \'{}\'  and order_status in (1)  '''.format(tbl,get_today(),get_tomorrow())
    # print(sql)
    try:
        cursor.execute(sql)
        result = cursor.fetchone()
        return int(result[0])
    except Exception as err:
        # 发生错误时回滚
        print(err)
        db.rollback()
    db.close()

print(get_partner_account_from_database('partner_info'))