from flask import Flask,Response, json
from flask_sqlalchemy import SQLAlchemy
from flask import jsonify
from MyEncoder import MyEncoder
from DateEncoder import DateEncoder
import csv
app = Flask(__name__)
# app.config.from_object('multiSetting')
#测试
#默认数据库
# SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:[email protected]:0000/users?charset=utf8'
# #绑定的数据库
# SQLALCHEMY_BINDS = {
# 'health': 'mysql+pymysql://user:[email protected]:0000/health?charset=utf8',
# 'algorithm': 'mysql+pymysql://user:[email protected]:0000/algorithm?charset=utf8',
# }
#dev
#默认数据库#mysql+pymysql://user(用户名):password(密码)@主机:端口/库名称?编码格式
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:[email protected]:0000/users?charset=utf8'
#绑定的数据库
SQLALCHEMY_BINDS = {
'users':'mysql+pymysql://user:[email protected]:0000/users?charset=utf8',
'health': 'mysql+pymysql://user:[email protected]:0000/health?charset=utf8',
'algorithm': 'mysql+pymysql://user:[email protected]:0000/algorithm?charset=utf8',
}
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI # 默认数据库引擎
app.config['SQLALCHEMY_BINDS'] = SQLALCHEMY_BINDS
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Blood(db.Model):
__tablename__ = 'blood_datum_value'
__bind_key__ = 'health' # 已设置__bind_key__ 数据库名
id = db.Column(db.String(80),primary_key=True)
blood_type = db.Column(db.String(80),unique=True)
datum_value = db.Column(db.String(80),unique=True)
standard_interval = db.Column(db.String(80),unique=True)
create_time = db.Column(db.String(80),unique=True)
remark = db.Column(db.String(80),unique=True)
user_id = db.Column(db.String(80),unique=True)
def keys(self):
return ('id','blood_type','datum_value','standard_interval','create_time','remark','user_id')
def __getitem__(self, item):
return getattr(self, item)
def __repr__(self):
return self.id
class User(db.Model):
__tablename__ = 'user'
__bind_key__ = 'users'
id = db.Column(db.INTEGER,primary_key=True)
login_name = db.Column(db.String(80),unique=True)
create_time = db.Column(db.String(80),nullable=False)
sex = db.Column(db.String(80),nullable=False)
cellphone = db.Column(db.String(80),nullable=False)
cname = db.Column(db.String(80),nullable=False)
password = db.Column(db.String(80),nullable=False)
head_portrait = db.Column(db.String(80),nullable=False)
birthday = db.Column(db.String(80),nullable=False)
stature = db.Column(db.String(80),nullable=False)
weight = db.Column(db.String(80),nullable=False)
medical_history = db.Column(db.String(80),nullable=False)
update_time = db.Column(db.String(80),nullable=False)
location = db.Column(db.String(80),nullable=False)
blood_glucose = db.Column(db.String(80),nullable=False)
lastLoginTime = db.Column(db.String(80),nullable=False)
newUploadTime = db.Column(db.String(80),nullable=False)
identity = db.Column(db.String(80),nullable=False)
deviceid = db.Column(db.String(80),nullable=False)
head_portrait_time = db.Column(db.String(80),nullable=False)
accid = db.Column(db.String(80),nullable=False)
token = db.Column(db.String(80),nullable=False)
wallet = db.Column(db.String(80),nullable=False)
idcardNum = db.Column(db.String(80),nullable=False)
faith = db.Column(db.String(80),nullable=False)
nation = db.Column(db.String(80),nullable=False)
status = db.Column(db.String(80),nullable=False)
country_code = db.Column(db.String(80),nullable=False)
time_diff = db.Column(db.String(80),nullable=False)
final_phone = db.Column(db.String(80),nullable=False)
enterprise_id = db.Column(db.String(80),nullable=False)
thirdUserid = db.Column(db.String(80),nullable=False)
isDoctor = db.Column(db.String(80),nullable=False)
isNurse = db.Column(db.String(80),nullable=False)
imAccid = db.Column(db.String(80),nullable=False)
payPassWord = db.Column(db.String(80),nullable=False)
isCancel = db.Column(db.String(80),nullable=False)
recommendUser = db.Column(db.String(80),nullable=False)
def keys(self):
return ('id','login_name','create_time','sex','cellphone','cname','password','head_portrait','birthday','stature','weight','medical_history','update_time','location','blood_glucose','lastLoginTime','newUploadTime','identity','deviceid','head_portrait_time','accid','token','wallet','idcardNum','faith','nation','status','country_code','time_diff','final_phone','enterprise_id','thirdUserid','isDoctor','isNurse','imAccid','payPassWord','isCancel','recommendUser')
def __getitem__(self, item):
return getattr(self, item)
def __repr__(self):
# # return 'User:%s' % self.id,self.login_name,self.create_time,self.sex,self.cellphone,self.cname,self.password,self.head_portrait,self.birthday,self.stature,self.weight,self.medical_history,self.update_time,self.location,self.blood_glucose,self.lastLoginTime,self.newUploadTime,self.identity,self.deviceid,self.head_portrait_time,self.accid,self.token,self.wallet,self.idcardNum,self.faith,self.nation,self.status,self.country_code,self.time_diff,self.final_phone,self.enterprise_id,self.thirdUserid,self.isDoctor,self.isNurse,self.imAccid,self.payPassWord,self.isCancel,self.recommendUser
return self.id
#特征值表1
class ScaleOne(db.Model):
__tablename__ = 'disease_scale_value_one'
__bind_key__ = 'algorithm' # 已设置__bind_key__ 数据库名
id = db.Column(db.INTEGER,primary_key=True)
create_time = db.Column(db.String(80),unique=False)
update_time = db.Column(db.String(80),unique=False)
user_id = db.Column(db.String(80),unique=False)
start_value_scale = db.Column(db.String(80),unique=False)
start_index_scale = db.Column(db.String(80),unique=False)
end_value_scale = db.Column(db.String(80),unique=False)
end_index_scale = db.Column(db.String(80),unique=False)
center_value_scale = db.Column(db.String(80),unique=False)
center_index_scale = db.Column(db.String(80),unique=False)
downcenter_value_scale = db.Column(db.String(80),unique=False)
downcenter_index_scale = db.Column(db.String(80),unique=False)
are_scale = db.Column(db.String(80),unique=False)
width_scale = db.Column(db.String(80),unique=False)
single_length_scale = db.Column(db.String(80),unique=False)
speed_scale = db.Column(db.String(80),unique=False)
aone_value_scale = db.Column(db.String(80),unique=False)
aone_index_scale = db.Column(db.String(80),unique=False)
atwo_value_scale = db.Column(db.String(80),unique=False)
atwo_index_scale = db.Column(db.String(80),unique=False)
athr_value_scale = db.Column(db.String(80),unique=False)
athr_index_scale = db.Column(db.String(80),unique=False)
bone_value_scale = db.Column(db.String(80),unique=False)
bone_index_scale = db.Column(db.String(80),unique=False)
average_press_scale = db.Column(db.String(80),unique=False)
sugarValue = db.Column(db.String(80),unique=False)
bloodStick_scale = db.Column(db.String(80),unique=False)
def __getitem__(self, item):
return getattr(self, item)
def __repr__(self):
# # return 'User:%s' % self.id,self.login_name,self.create_time,self.sex,self.cellphone,self.cname,self.password,self.head_portrait,self.birthday,self.stature,self.weight,self.medical_history,self.update_time,self.location,self.blood_glucose,self.lastLoginTime,self.newUploadTime,self.identity,self.deviceid,self.head_portrait_time,self.accid,self.token,self.wallet,self.idcardNum,self.faith,self.nation,self.status,self.country_code,self.time_diff,self.final_phone,self.enterprise_id,self.thirdUserid,self.isDoctor,self.isNurse,self.imAccid,self.payPassWord,self.isCancel,self.recommendUser
return self.id
#特征值表2
class ScaleTwo(db.Model):
__tablename__ = 'disease_scale_value_two'
__bind_key__ = 'algorithm' # 已设置__bind_key__ 数据库名
id = db.Column(db.INTEGER,primary_key=True)
disease_scale_one_id = db.Column(db.String(80),unique=False)
btwo_value_scale = db.Column(db.String(80),unique=False)
btwo_index_scale = db.Column(db.String(80),unique=False)
bthr_value_scale = db.Column(db.String(80),unique=False)
bthr_index_scale = db.Column(db.String(80),unique=False)
cone_value_scale = db.Column(db.String(80),unique=False)
cone_index_scale = db.Column(db.String(80),unique=False)
ctwo_value_scale = db.Column(db.String(80),unique=False)
ctwo_index_scale = db.Column(db.String(80),unique=False)
hertrate_scale = db.Column(db.String(80),unique=False)
up_time_scale = db.Column(db.String(80),unique=False)
down_time_scale = db.Column(db.String(80),unique=False)
single_wave_length_scale = db.Column(db.String(80),unique=False)
aone_shallow_up_scale = db.Column(db.String(80),unique=False)
aone_shallow_dwon_scale = db.Column(db.String(80),unique=False)
aone_center_up_scale = db.Column(db.String(80),unique=False)
aone_center_dwon_scale = db.Column(db.String(80),unique=False)
aone_deep_up_scale = db.Column(db.String(80),unique=False)
aone_deep_dwon_scale = db.Column(db.String(80),unique=False)
atwo_shallow_up_scale = db.Column(db.String(80),unique=False)
atwo_shallow_dwon_scale = db.Column(db.String(80),unique=False)
atwo_center_up_scale = db.Column(db.String(80),unique=False)
atwo_center_dwon_scale = db.Column(db.String(80),unique=False)
atwo_deep_up_scale = db.Column(db.String(80),unique=False)
atwo_deep_dwon_scale = db.Column(db.String(80),unique=False)
athr_shallow_up_scale = db.Column(db.String(80),unique=False)
athr_shallow_dwon_scale = db.Column(db.String(80),unique=False)
athr_center_up_scale = db.Column(db.String(80),unique=False)
athr_center_dwon_scale = db.Column(db.String(80),unique=False)
athr_deep_up_scale = db.Column(db.String(80),unique=False)
athr_deep_dwon_scale = db.Column(db.String(80),unique=False)
updateFlag = db.Column(db.String(80),unique=False)
singleWaveLengthScaleStr = db.Column(db.String(80),unique=False)
def __getitem__(self, item):
return getattr(self, item)
def __repr__(self):
# # return 'User:%s' % self.id,self.login_name,self.create_time,self.sex,self.cellphone,self.cname,self.password,self.head_portrait,self.birthday,self.stature,self.weight,self.medical_history,self.update_time,self.location,self.blood_glucose,self.lastLoginTime,self.newUploadTime,self.identity,self.deviceid,self.head_portrait_time,self.accid,self.token,self.wallet,self.idcardNum,self.faith,self.nation,self.status,self.country_code,self.time_diff,self.final_phone,self.enterprise_id,self.thirdUserid,self.isDoctor,self.isNurse,self.imAccid,self.payPassWord,self.isCancel,self.recommendUser
return self.id
@app.route('/')
def hello_world():
return 'Response'
@app.route('/select_user')
def select_user():
# 查询所有用户
users_list = User.query.all()
list = [dict(i) for i in users_list]
return json.dumps(list,cls=DateEncoder,indent=4)
@app.route('/queryT')
def queryT():
users = User.query.with_entities(User.id,User.sex,User.weight,User.stature,User.birthday).distinct().all()
list = [dict(i) for i in users]
return json.dumps(list, cls=DateEncoder, indent=4)
@app.route('/blood')
def blood():
blood_value = Blood.query.with_entities(Blood.datum_value).filter(Blood.blood_type==3).all()
list = [dict(i) for i in blood_value]
return json.dumps(list, cls=DateEncoder, indent=4)
#关联查询
@app.route('/bagas')
def bagas():
#先产讯出user表里面的信息,然取出userId再去查找血糖基准值,然后再去查找oldlength
users = User.query.with_entities(User.id,User.sex,User.weight,User.stature,User.birthday).distinct().all()
# blood_value=Blood.query.with_entities(Blood.datum_value,Blood.user_id,Blood.blood_type).filter(Blood.blood_type==3).distinct().all()
N_users=[]
for i in users:
blood_value = Blood.query.with_entities(Blood.datum_value).filter(i[0] == Blood.user_id , Blood.blood_type == 3).all()
#先查找出来基准值集合[[]],然后取出来[],再把[]放到N_users
if len(blood_value) != 0:
n1=[s for s in i]
n1.append(blood_value[0][0])
N_users.append(n1)
#传入数据进行数据保存为cvs
writeCVS(N_users)
return json.dumps(N_users, cls=DateEncoder, indent=4)
#2022/2/15添加
#查询old_length
@app.route('/old_value')
def feature():
#查询user表数据
users = User.query.with_entities(User.id).distinct().all()
single_wave_length = []
for i in users:
single = db.session.query(ScaleOne.id, ScaleTwo.single_wave_length_scale).join(ScaleTwo, ScaleOne.id == ScaleTwo.disease_scale_one_id).filter(ScaleOne.user_id == i[0]).all()
if len(single) != 0:
#元组转换为列表
single_wave_length.append(list(single[0]))
print(single_wave_length)
return json.dumps(single_wave_length, cls=DateEncoder, indent=4)
def writeCVS(user):
# 1. 创建文件对象
f = open('blood.csv', 'w', encoding='utf-8')
# 2. 基于文件对象构建 csv写入对象
csv_writer = csv.writer(f)
# 3. 构建列表头
csv_writer.writerow(["id号", "性别", "体重","身高","年龄","血糖值"])
# 4. 写入csv文件内容
for i in user:
csv_writer.writerow(i)
# 5. 关闭文件
f.close()
#备注,明天开始数据训练工作(嘻嘻)
#备注训练工作完成,明天开始java调用python的大数据模型,pkl文件
if __name__ == '__main__':
app.run()
关于一段代码的记录