Commit 1b0fb537 by yanglei

Merge branch 'master' into yanglei

parents 3db5cabd 1332af34
......@@ -11,3 +11,5 @@ SECRET_KEY = os.getenv('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI")
MONGO_DATABASE_URI = os.getenv("MONGO_DATABASE_URI")
TENCENT_REDIS_URL = os.getenv("TENCENT_REDIS_URL")
NFC_PAY_LOAD_SECRET = os.getenv("NFC_PAY_LOAD_SECRET")
......@@ -37,6 +37,8 @@ class Hatch(Base):
production_id = Column(INTEGER(10), nullable=False, comment='商品id')
name = Column(String(100, 'utf8mb4_unicode_ci'), nullable=False, comment='商品名称')
title = Column(String(200, 'utf8mb4_unicode_ci'), nullable=False, comment='商品标题')
left_count = Column(TINYINT(3), nullable=False, comment='剩余数量')
total_count = Column(TINYINT(3), nullable=False, comment='总数')
brand_id = Column(INTEGER(10), nullable=False, comment='品牌ID')
left_count = Column(INTEGER(10), nullable=False, comment='余额')
total_count = Column(INTEGER(10), nullable=False, comment='总数')
......@@ -73,6 +75,7 @@ class Machine(Base):
status = Column(TINYINT(1), server_default=text("'1'"), comment='状态: 1正常-1删除')
created_at = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP"))
updated_at = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
command_time = Column(INTEGER(1), nullable=False)
class MachineProduction(Base):
......
......@@ -88,6 +88,7 @@ def run_get_machine_no():
machine = Machine.query.filter_by(qrcode_no=qrcode_no).first()
if machine:
return BaseResponse(data={"machine_no": machine.machine_no, "status": machine.status})
return BaseResponse(data={"machine_no": machine.machine_no, "status": machine.status, "mac_no": machine.mac,
"command_time":machine.command_time})
else:
return jsonify(MACHINE_NOT_EXIST_ERROR)
......@@ -12,7 +12,9 @@ import time
from flask import Blueprint, request, jsonify, g
from sqlalchemy import extract
from sqlalchemy.exc import SQLAlchemyError
from config.base_config import NFC_PAY_LOAD_SECRET
from config.commen_config import USER_RENT_PREPAY_ID
from config.wechat_config import platform_appid_config_list, pay_config_list, NFC_PAY_CALLBCK_URL
from models.base_model import db
......@@ -20,7 +22,7 @@ from models.models import NfcCard, NfcCardPayRecord, NfcCardPayRefund
from service.rent_service import RentService
from service.wechat_service import WeChatPayService
from utils.error_code import NFC_CARD_NOT_EXIST, NFC_CARD_ACTIVATED_ERROR, WE_MINIAPP_PAY_FAIL, NO_RENT_RECORD, \
NO_NFC_CARD_ERROR
NO_NFC_CARD_ERROR, NFC_PAY_LOAD_SECRET_ERROR
from utils.my_redis_cache import redis_client
from utils.my_response import BaseResponse
......@@ -327,43 +329,104 @@ def run_nfc_card_pay_refund():
return BaseResponse()
#查询充值记录
# 查询充值记录
@nfc_card_route.route('/user_pay_record', methods=['POST'])
def run_nfc_card_user_pay_record():
json_data = request.get_json()
secret = json_data["secret"]
card_no = json_data["card_no"]
page = json_data.get("page", 1)
page_size = json_data.get("page_size", 5)
if secret != NFC_PAY_LOAD_SECRET:
return jsonify(NFC_PAY_LOAD_SECRET_ERROR)
card_result = NfcCard.query.filter_by(card_no=card_no, status=1).first()
if not card_result:
return jsonify(NFC_CARD_NOT_EXIST)
card_result = NfcCardPayRecord.query.filter_by(card_no=card_no,is_pay=1).all()
result_data=[]
for card in card_result:
record={
"card_no":card.card_no,
"id":card.id,
"pay_money":card.pay_money,
"created_at":card.created_at,
"updated_at":card.updated_at
total_count = NfcCardPayRecord.query.filter_by(card_no=card_no, status=1).count()
if not total_count:
return BaseResponse(data=[], total_count=0, page=page, page_size=page_size)
pay_records = NfcCardPayRecord.query.filter_by(card_no=card_no, status=1).offset((page - 1) * page_size).limit(
page_size).all()
result_data = []
for record in pay_records:
tmp_data = {
"card_no": record.card_no,
"record_no": record.rent_no,
"pay_money": record.pay_money,
"pay_time": record.created_at.strftime("%Y-%m-%d %H:%M:%S"),
}
result_data.append(record)
return BaseResponse(data=result_data)
#充值成功后改变数据库
@nfc_card_route.route('/user_pay_succeed', methods=['POST'])
def run_nfc_card_pay_succeed():
json_data=request.get_json()
for key,val in json_data.items():
id=json_data[key]["id"]
card_no=json_data[key]["card_no"]
pay_money = json_data[key]["pay_money"]
card_result = NfcCard.query.filter_by(card_no=card_no).first() # 查询到卡号
card_record= NfcCardPayRecord.query.filter_by(id=id).first() #查询到充值记录
card_result.money += pay_money
card_record.is_pay=2
db.session.add(card_record,card_record)
result_data.append(tmp_data)
return BaseResponse(data=result_data, total=total_count, page=page, page_size=page_size)
# 充值成功后改变数据库
@nfc_card_route.route('/load_succeed', methods=['POST'])
def run_nfc_card_load_succeed():
json_data = request.get_json()
secret = json_data["secret"]
record_nos = json_data["record_nos"]
card_no = json_data["card_no"]
if secret != NFC_PAY_LOAD_SECRET:
return jsonify(NFC_PAY_LOAD_SECRET_ERROR)
card_result = NfcCard.query.filter_by(card_no=card_no).first() # 查询到卡号
if not card_result:
return jsonify(NFC_CARD_NOT_EXIST)
for record_no in record_nos:
card_record = NfcCardPayRecord.query.filter_by(rent_no=record_no, status=1).first() # 查询到充值记录
if card_record:
card_result.money += card_record.pay_money
card_record.status = 2
db.session.add(card_record)
try:
db.session.add(card_result)
db.session.commit()
except SQLAlchemyError as e:
db.session.rollback()
return_data = {
"card_no": card_result.card_no,
"money": card_result.money
}
return BaseResponse(data=return_data)
return BaseResponse()
# 查询充值记录
@nfc_card_route.route('/user_load_record', methods=['POST'])
def run_nfc_card_user_load_record():
json_data = request.get_json()
secret = json_data["secret"]
card_no = json_data["card_no"]
page = json_data.get("page", 1)
page_size = json_data.get("page_size", 5)
if secret != NFC_PAY_LOAD_SECRET:
return jsonify(NFC_PAY_LOAD_SECRET_ERROR)
card_result = NfcCard.query.filter_by(card_no=card_no, status=1).first()
if not card_result:
return jsonify(NFC_CARD_NOT_EXIST)
total_count = NfcCardPayRecord.query.filter_by(card_no=card_no, status=2).count()
if not total_count:
return BaseResponse(data=[], total_count=0, page=page, page_size=page_size)
pay_records = NfcCardPayRecord.query.filter_by(card_no=card_no, status=2).offset((page - 1) * page_size).limit(
page_size).all()
result_data = []
for record in pay_records:
tmp_data = {
"card_no": record.card_no,
"record_no": record.rent_no,
"load_money": record.pay_money,
"load_time": record.updated_at.strftime("%Y-%m-%d %H:%M:%S"),
}
result_data.append(tmp_data)
return BaseResponse(data=result_data, total=total_count, page=page, page_size=page_size)
......@@ -44,41 +44,38 @@ def create_rent():
return jsonify(MACHINE_NOT_EXIST_ERROR)
total_fee = 0
open_hatchs = {}
for id, count in productions.items():
count = int(count)
id = int(id)
# 根据给的仓号去获取商品信息
hatch_list = Hatch.query.filter(Hatch.machine_no == machine_no, Hatch.production_id == id,
Hatch.status == 1).order_by(func.random()).all()
Hatch.left_count >= 1).order_by(func.random()).all()
if not hatch_list:
return jsonify(HATCH_NOT_EXIST_ERROR)
if len(hatch_list) < int(count):
left_count = count
for i in hatch_list:
if i.left_count <= left_count:
open_hatchs[str(i.hatch_no)] = i.left_count
left_count = left_count - i.left_count
else:
open_hatchs[str(i.hatch_no)] = left_count
left_count = 0
if left_count <= 0:
break
if left_count > 0:
return jsonify(HATCH_COUNT_ERROR)
total_fee += hatch_list[0].price * int(count)
# 生成订单编号
rent_no = RentService.create_order_no()
open_hatchs = []
for id, count in productions.items():
count = int(count)
id = int(id)
# 根据给的仓号去获取商品信息
hatch_list = Hatch.query.filter(Hatch.machine_no == machine_no, Hatch.production_id == id,
Hatch.status == 1).order_by(func.random()).all()
if not hatch_list:
return jsonify(HATCH_NOT_EXIST_ERROR)
if len(hatch_list) < count:
return jsonify(HATCH_NOT_ALL_EXIST_ERROR)
hatch_no_list = [x.hatch_no for x in hatch_list[:count]]
open_hatchs += hatch_no_list
# 配置微信订单数据
wechat_service = WeChatPayService(app_id=platform_appid_config_list[user.platform],
config_name=pay_config_list[machine.mch_platform])
# 生成订单编号
rent_no = RentService.create_order_no()
pay_data = {
'body': '灰兔智能租借押金', # 商品描述
'out_trade_no': rent_no, # 商户订单号
......@@ -153,17 +150,6 @@ def wx_pay_callback():
machine = Machine.query.filter_by(machine_no=machine_no).first()
try:
lock_time = 0
while lock_time < 3:
# redis 加锁
lock_res = redis_client.set(RENT_SALE_LOCK + str(machine.id),
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), ex=60,
nx=True)
if lock_res:
break
lock_time += 1
time.sleep(1)
rent = Rent()
rent.rent_no = rent_no
rent.machine_no = machine.machine_no
......@@ -181,33 +167,30 @@ def wx_pay_callback():
total_fee = 0
hatchs = Hatch.query.filter(Hatch.machine_no == machine_no, Hatch.hatch_no.in_(open_hatchs),
Hatch.status == 1).order_by(func.random()).all()
hatchs = Hatch.query.filter(Hatch.machine_no == machine_no, Hatch.hatch_no.in_(open_hatchs.keys())).order_by(
func.random()).all()
for i in hatchs:
if i.status == 1:
rent_detail = RentDetail()
rent_detail.rent_no = rent_no
rent_detail.user_id = rent.user_id
rent_detail.machine_no = rent.machine_no
rent_detail.hatch_no = i.hatch_no
rent_detail.production_id = i.production_id
rent_detail.name = i.name
rent_detail.title = i.title
rent_detail.brand_id = i.brand_id
rent_detail.brand_name = i.brand_name
rent_detail.cate_id = i.cate_id
rent_detail.cate_name = i.cate_name
rent_detail.price = i.price
rent_detail.img = i.img
rent_detail.tags = i.tags
rent_detail.content = i.content
rent_detail.summary = i.summary
i.status = 2
db.session.add(rent_detail)
db.session.add(i)
total_fee += i.price
rent_detail = RentDetail()
rent_detail.rent_no = rent_no
rent_detail.user_id = rent.user_id
rent_detail.machine_no = rent.machine_no
rent_detail.hatch_no = i.hatch_no
rent_detail.production_id = i.production_id
rent_detail.name = i.name
rent_detail.title = i.title
rent_detail.brand_id = i.brand_id
rent_detail.brand_name = i.brand_name
rent_detail.cate_id = i.cate_id
rent_detail.cate_name = i.cate_name
rent_detail.price = i.price * open_hatchs[str(i.hatch_no)]
rent_detail.img = i.img
rent_detail.tags = i.tags
rent_detail.content = i.content
rent_detail.summary = i.summary
db.session.add(rent_detail)
db.session.add(i)
total_fee += rent_detail.price
if total_fee != int(callback_data['total_fee']):
return xmltodict.unparse({'xml': error_data}, pretty=True), header
......@@ -216,10 +199,8 @@ def wx_pay_callback():
db.session.commit()
except Exception as e:
redis_client.delete(RENT_SALE_LOCK + str(machine.id))
return xmltodict.unparse({'xml': error_data}, pretty=True), header
redis_client.delete(RENT_SALE_LOCK + str(machine.machine_id))
return xmltodict.unparse({'xml': response_data}, pretty=True), header
......
......@@ -347,15 +347,16 @@ def get_tally_report():
def run_machine_activate():
json_data = request.get_json()
qrcode_no = json_data["qrcode_no"]
machine_no = json_data["machine_no"]
mac = json_data["mac"]
# machine_no = json_data["machine_no"]
mac = json_data.get("mac", None)
machine = Machine.query.filter_by(qrcode_no=qrcode_no).first()
if machine and machine.machine_no == qrcode_no:
if machine:
if machine.status == 1:
return jsonify(MACHINE_ACTIVATED_ERROR)
machine.status = 1
machine.mac = mac
if mac:
machine.mac = mac
db.session.add(machine)
db.session.commit()
return BaseResponse()
......
......@@ -13,4 +13,4 @@ app = create_app(os.getenv('RUN_ENV', 'prod'))
logger.info("run server")
if __name__ == '__main__':
app.run('127.0.0.1', 8893, debug=True)
app.run('127.0.0.1', 8893, debug=False)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment