135 lines
5.7 KiB
Plaintext
135 lines
5.7 KiB
Plaintext
async def getBill(ns={}):
|
||
ak = ns.get('ak')
|
||
sk = ns.get('sk')
|
||
appId = ns.get('appId')
|
||
if not (ak and sk and appId):
|
||
return {
|
||
"code":"1",
|
||
"msg":"ak or sk or appId is None"
|
||
}
|
||
timestamp = str(int(time.time() * 1000))
|
||
token = str(uuid())
|
||
sign = sk + appId + token + timestamp
|
||
md5_object = hashlib.md5()
|
||
md5_object.update(sign.encode())
|
||
sign = md5_object.hexdigest()
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
# find uin from zj_user
|
||
orgid_li = await sor.R('users', {'id': await get_user()})
|
||
orgid = orgid_li[0].get('orgid') if orgid_li else ''
|
||
uin_li = await sor.R('zj_users', {'orgid': orgid})
|
||
uin = uin_li[0].get('thirdid') if uin_li else ''
|
||
if not (orgid and uin):
|
||
return {
|
||
'status': False,
|
||
'msg': 'get bill failed, orgid or uin is empty...'
|
||
}
|
||
url = 'http://101.36.139.188:8888/cdz-admin/zjapi/v1/bill/list'
|
||
header = {
|
||
'content-type': 'application/json',
|
||
'appId': appId,
|
||
'ak': ak,
|
||
'timestamp': timestamp,
|
||
'token': token,
|
||
'sign': sign
|
||
}
|
||
data = {
|
||
"supId": "99",
|
||
"uin": uin,
|
||
"page": ns.get('page'),
|
||
"limit": ns.get('limit') or '1000',
|
||
"billMonth": ns.get('billMonth'),
|
||
"payTime": ns.get('payTime')
|
||
}
|
||
res = requests.post(url=url, headers=header, json=data)
|
||
res_data = json.loads(res.text)
|
||
code = json.loads(res.text).get('code')
|
||
if code != 200:
|
||
return res_data
|
||
else:
|
||
data_list = res_data.get('data').get('list') if res_data.get('data') else []
|
||
# get all bill id
|
||
bills = await sor.R('zj_bill', {})
|
||
orderid_list = [bill.get('zj_orderid') for bill in bills]
|
||
if data_list:
|
||
for bill_local in data_list:
|
||
existid = bill_local.get('orderId') # TODO 字段唯一性 如何去重字段
|
||
if existid in orderid_list:
|
||
continue
|
||
bill_local['id'] = uuid()
|
||
bill_local['customerid'] = orgid
|
||
bill_local['zj_orderid'] = bill_local.get('orderId')
|
||
lowercase_dict = {key.lower(): value for key, value in bill_local.items()}
|
||
# add zj_bill
|
||
await sor.C('zj_bill', lowercase_dict) # TODO zj_orderId 字段修改 payTime不是null create_at字段失效
|
||
|
||
actionTypeName = bill_local['actionTypeName']
|
||
if '退款' in actionTypeName:
|
||
business_op = 'BUY_REVERSE'
|
||
else:
|
||
business_op = 'BUY'
|
||
|
||
# 计算折扣后的价格
|
||
kyy_discount_sql = """select * from cp_discount where customerid = ${customerid}$ and
|
||
productid = ${productid}$ and CURRENT_DATE <= end_date and CURRENT_DATE >= start_date"""
|
||
kyy_discount_li = await sor.sqlExe(kyy_discount_sql, {'customerid': orgid, 'productid': bill_local.get('productCode')})
|
||
kyy_discount = kyy_discount_li[0] if kyy_discount_li else {}
|
||
real_discount = float(kyy_discount.get('discount')) if kyy_discount.get('discount') else 1.00
|
||
realcost = float(bill_local.get('realCost')) if bill_local.get('realCost') else 0
|
||
if not realcost:
|
||
raise ValueError("bill realcost is empty")
|
||
amount = real_discount * realcost
|
||
|
||
# add bill
|
||
nss_bill = {
|
||
'id': uuid(),
|
||
'customerid': orgid,
|
||
'orderid': bill_local.get('orderId'),
|
||
'business_op': business_op,
|
||
'provider_amt': bill_local.get('realCost'),
|
||
'amount': amount,
|
||
'bill_date': bill_local.get('payTime'),
|
||
'bill_timestamp': bill_local.get('payTime'),
|
||
'bill_state': 0, # TODO 字段调整
|
||
'productid': bill_local.get('productCode'),
|
||
'providerid': '8',
|
||
'provider_billid': bill_local['id'],
|
||
'resourceid': bill_local.get('resourceId')
|
||
}
|
||
# add data to bill
|
||
await sor.C('bill', nss_bill)
|
||
|
||
# 比对账户余额和账单金额
|
||
balance_yuan = await getCustomerBalance(sor, orgid)
|
||
if not balance_yuan:
|
||
return {
|
||
'status': False,
|
||
'msg': 'can not get customer balance'
|
||
}
|
||
if balance_yuan < amount:
|
||
return {
|
||
'status': False,
|
||
'msg': 'user balance is not enough'
|
||
}
|
||
|
||
# 记账
|
||
ba = accounting.bill.BillAccounting(nss_bill)
|
||
r = await ba.accounting(sor)
|
||
|
||
# 修改本地bill状态 0:未支付,1:已支付;2:已取消
|
||
ns_bill_status = {
|
||
'id': nss_bill['id'],
|
||
'bill_state': 1
|
||
}
|
||
await sor.U('bill', ns_bill_status)
|
||
return res_data
|
||
else:
|
||
return {
|
||
'status': False,
|
||
'msg': 'get order failed'
|
||
}
|
||
|
||
|
||
ret = await getBill(params_kw)
|
||
return ret |