from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, ForeignKey, Integer, MetaData, String
from sqlalchemy.orm import relationship

app = Flask(__name__)
db = SQLAlchemy(app)

# 设置数据库连接
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:123456@127.0.0.1:3306/test1'

class Fangjianshiyongqingkuangbiao(db.Model):
    __tablename__ = 'fangjianshiyongqingkuangbiao'

    fangjianhao = Column(String(3), primary_key=True)
    fangjianweizhi = Column(ForeignKey('huanzherjibenxinxibiao.fangjianweizhi'), index=True)
    fangjianshifoumanyuan = Column(String(2))

    huanzherjibenxinxibiao = relationship('Huanzherjibenxinxibiao',
                                          primaryjoin='Fangjianshiyongqingkuangbiao.fangjianweizhi == Huanzherjibenxinxibiao.fangjianweizhi',
                                          backref='fangjianshiyongqingkuangbiaos')


class Huanzhebingqingcanzhaobiao(db.Model):
    __tablename__ = 'huanzhebingqingcanzhaobiao'

    huanzhebingqingdaihao = Column(Integer, primary_key=True)
    jutibingqing = Column(String(2), nullable=False)


class Huanzhebingqingzhuangkuangbiao(db.Model):
    __tablename__ = 'huanzhebingqingzhuangkuangbiao'

    huanzhehao = Column(String(10), primary_key=True)
    fangjianhao = Column(Integer)
    huanzhebingqingdaihao = Column(ForeignKey('huanzhebingqingcanzhaobiao.huanzhebingqingdaihao'), index=True)
    teshuqingkuangbeizhu = Column(String(255))
    jiashuyouwuganran = Column(String(2))

    huanzhebingqingcanzhaobiao = relationship('Huanzhebingqingcanzhaobiao',
                                              primaryjoin='Huanzhebingqingzhuangkuangbiao.huanzhebingqingdaihao == Huanzhebingqingcanzhaobiao.huanzhebingqingdaihao',
                                              backref='huanzhebingqingzhuangkuangbiaos')


class Huanzherjibenxinxibiao(db.Model):
    __tablename__ = 'huanzherjibenxinxibiao'

    shenfenzhenghao = Column(String(18), primary_key=True)
    xingming = Column(String(3))
    xingbie = Column(String(2))
    nianling = Column(Integer)
    jiguan = Column(String(4))
    huanzhehao = Column(ForeignKey('huanzhebingqingzhuangkuangbiao.huanzhehao'), index=True)
    fangjianweizhi = Column(Integer, index=True)
    ruyuanshijian = Column(String(10))
    jiashulianxitianhua = Column(String(11))

    huanzhebingqingzhuangkuangbiao = relationship('Huanzhebingqingzhuangkuangbiao',
                                                  primaryjoin='Huanzherjibenxinxibiao.huanzhehao == Huanzhebingqingzhuangkuangbiao.huanzhehao',
                                                  backref='huanzherjibenxinxibiaos')


class Siwanghuanzhebiao(Huanzherjibenxinxibiao):
    __tablename__ = 'siwanghuanzhebiao'

    xingming = Column(String(3))
    xingbie = Column(String(2))
    nianling = Column(Integer)
    shenfenzhenghao = Column(ForeignKey('huanzherjibenxinxibiao.shenfenzhenghao'), primary_key=True)
    siwangshijian = Column(String(10))


class Yihuanguanxibiao(Huanzherjibenxinxibiao):
    __tablename__ = 'yihuanguanxibiao'

    huanzhehao = Column(ForeignKey('huanzherjibenxinxibiao.huanzhehao'), primary_key=True)
    yishenggongzuozhenghao = Column(ForeignKey('yiyuanrenyuanxinxibiao.yishenggongzuozhenghao'), index=True)
    yiyurenyuandegongzuoleixingdaihao = Column(Integer)

    yiyuanrenyuanxinxibiao = relationship('Yiyuanrenyuanxinxibiao',
                                          primaryjoin='Yihuanguanxibiao.yishenggongzuozhenghao == Yiyuanrenyuanxinxibiao.yishenggongzuozhenghao',
                                          backref='yihuanguanxibiaos')


class Zhiyuhuanzhebiao(Huanzherjibenxinxibiao):
    __tablename__ = 'zhiyuhuanzhebiao'

    xingming = Column(String(3))
    xingbie = Column(String(2))
    nianling = Column(Integer)
    shenfenzhenghao = Column(ForeignKey('huanzherjibenxinxibiao.shenfenzhenghao'), primary_key=True)
    zhiyushijian = Column(String(10))
    lianxidianhua = Column(String(11))


class Shebeishiyongqingkuangbiao(db.Model):
    __tablename__ = 'shebeishiyongqingkuangbiao'

    shebeihao = Column(String(5), primary_key=True)
    shebeileixing = Column(String(10), nullable=False)
    shifoushiyong = Column(String(2), nullable=False)
    yongyunagehuanzhe_huanzhehao_ = Column('yongyunagehuanzhe(huanzhehao)', String(10), index=True)


class Yiyuanrenyuangongzuoqufenbiao(db.Model):
    __tablename__ = 'yiyuanrenyuangongzuoqufenbiao'

    yiyuanrenyuandegongzuoleixingdaihao = Column(String(4), primary_key=True)
    jutigongzuoleixing = Column(String(4), nullable=False)


class Yiyuanrenyuanxinxibiao(db.Model):
    __tablename__ = 'yiyuanrenyuanxinxibiao'

    yishenggongzuozhenghao = Column(String(10), primary_key=True)
    xingming = Column(String(3), nullable=False)
    xingbie = Column(String(2), nullable=False)
    nianling = Column(Integer, nullable=False)
    dianhua = Column(String(11), nullable=False)
    shifoudushengzinv = Column(String(2), nullable=False)
    shifoudangyuan = Column(String(2), nullable=False)
    gongzuonianling = Column(Integer, nullable=False)
    yiyuanrenyuandegongzuoleixingdaihao = Column(
        ForeignKey('yiyuanrenyuangongzuoqufenbiao.yiyuanrenyuandegongzuoleixingdaihao'), nullable=False, index=True)

    yiyuanrenyuangongzuoqufenbiao = relationship('Yiyuanrenyuangongzuoqufenbiao',
                                                 primaryjoin='Yiyuanrenyuanxinxibiao.yiyuanrenyuandegongzuoleixingdaihao == Yiyuanrenyuangongzuoqufenbiao.yiyuanrenyuandegongzuoleixingdaihao',
                                                 backref='yiyuanrenyuanxinxibiaos')


@app.route('/')
def check_in():
    return render_template("check_in.html")

@app.route('/suggestion')
def suggestion():
    return render_template("suggestion.html")


@app.route('/call')
def call():
    return render_template("call.html")


@app.route('/repair')
def repair():
    return render_template("repair.html")


@app.route('/doctor')
def doctor():
    doctor_list = Yiyuanrenyuanxinxibiao.query.all()
    return render_template("doctor.html", doctor_list=doctor_list)


@app.route('/start')
def start():
    return render_template("start.html")


@app.route('/device')
def device():
    device_list = Shebeishiyongqingkuangbiao.query.all()
    return render_template("device.html", device_list=device_list)


@app.route('/bingfang')
def bingfang():
    bingfang_list = Fangjianshiyongqingkuangbiao.query.all()
    return render_template("bingfang.html", bingfang_list=bingfang_list)


@app.route('/huanzhe')
def huanzhe():
    huanzhe_list = Huanzherjibenxinxibiao.query.all()
    return render_template("huanzhe.html", huanzhe_list=huanzhe_list)

#插入
@app.route('/insert_gen', methods=['GET', 'POST'])
def insert_gen():
    if request.method == 'POST':
        type = request.args.get("type")
        if type == 'bingfang':
            province = request.form['province']
            cityname = request.form['city']
            number = request.form['number']
            print(province, cityname, number)
            bingfang = Fangjianshiyongqingkuangbiao(fangjianhao=province, fangjianweizhi=cityname,
                                                    fangjianshifoumanyuan=number)

            try:
                db.session.add(bingfang)
                db.session.commit()
            except:
                db.session.rollback()
                detail = Fangjianshiyongqingkuangbiao.query.get(province)
                detail.fangjianhao=province
                detail.fangjianweizhi=cityname
                detail.fangjianshifoumanyuan = number

                db.session.commit()

            # 添加完成重定向至主页
            return redirect('/bingfang')
        elif type == 'device':
            province = request.form['province']
            cityname = request.form['city']
            number = request.form['number']
            belong=request.form['belong']
            print(province, cityname, number,belong)
            bingfang = Shebeishiyongqingkuangbiao(shebeihao=province, shebeileixing=cityname,
                                                  shifoushiyong=number,yongyunagehuanzhe_huanzhehao_=belong)
            try:
                db.session.add(bingfang)
                db.session.commit()
            except:
                db.session.rollback()
                detail = Shebeishiyongqingkuangbiao.query.get(province)
                detail.shebeihao=province
                detail.shebeileixing = cityname
                detail.shifoushiyong=number
                detail.yongyunagehuanzhe_huanzhehao_=belong
                db.session.commit()

            # 添加完成重定向至主页
            return redirect('/device')
        elif type == 'doctor':
            ysh = request.form['ysh']
            name = request.form['name']
            sex = request.form['sex']
            olf = request.form['old']
            child = request.form['child']
            dy = request.form['dy']
            job = request.form['job']
            phone = request.form['phone']
            number = request.form['number']
            try:
                ys = Yiyuanrenyuanxinxibiao(yishenggongzuozhenghao=ysh, xingbie=sex, nianling=olf, xingming=name,
                                            shifoudushengzinv=child, shifoudangyuan=dy, dianhua=phone,
                                            gongzuonianling=job,
                                            yiyuanrenyuandegongzuoleixingdaihao=number)
                db.session.add(ys)
                db.session.commit()
            except:
                db.session.rollback()
                detail = Yiyuanrenyuanxinxibiao.query.get(ysh)
                detail.yishenggongzuozhenghao=ysh
                detail.xingbie=sex
                detail.nianling=olf
                detail.xingming = name
                detail.shifoudushengzinv=child
                detail.shifoudangyuan=dy
                detail.dianhua=phone
                detail.gongzuonianling=job
                detail.yiyuanrenyuandegongzuoleixingdaihao=number
                db.session.commit()

            return redirect('/doctor')
        elif type == 'huanzhe':
            ysh = request.form['a']
            name = request.form['b']
            sex = request.form['c']
            olf = request.form['d']
            child = request.form['e']
            dy = request.form['f']
            job = request.form['g']
            phone = request.form['h']
            number = request.form['phone']
            try:
                ys = Huanzherjibenxinxibiao(shenfenzhenghao=ysh, xingming=name, xingbie=sex, nianling=olf, jiguan=child,
                                            huanzhehao=dy, fangjianweizhi=job, ruyuanshijian=phone,
                                            jiashulianxitianhua=number)
                db.session.add(ys)
                db.session.commit()
            except:
                db.session.rollback()
                detail = Huanzherjibenxinxibiao.query.get(ysh)
                detail.shenfenzhenghao=ysh
                detail.xingming = name
                detail.xingbie=sex
                detail.nianling=olf
                detail.jiguan=child
                detail.huanzhehao=dy
                detail.fangjianweizhi=job
                detail.ruyuanshijian=phone
                detail.jiashulianxitianhua=number
                db.session.commit()

            return redirect('/huanzhe')

#修改
@app.route('/update_gen', methods=['GET', 'POST'])
def update_gen():
    if request.method == 'GET':
        id = request.args.get("id")
        type = request.args.get("type")
        if type == 'doctor':
            detail = Yiyuanrenyuanxinxibiao.query.filter_by(yishenggongzuozhenghao=id).first()
            return render_template("insert_doctor.html", detail=detail)
        elif type == 'huanzhe':
            detail = Huanzherjibenxinxibiao.query.filter_by(shenfenzhenghao=id).first()
            return render_template("insert_huanzhe.html", detail=detail)
        elif type == 'device':
            detail = Shebeishiyongqingkuangbiao.query.filter_by(shebeihao=id).first()
            return render_template("insert_device.html", detail=detail)
        elif type == 'bingfang':
            detail = Fangjianshiyongqingkuangbiao.query.filter_by(fangjianhao=id).first()
            return render_template("insert_bingfang.html", detail=detail)
    else:
        type = request.args.get("type")
        if type == 'bingfang':
            province = request.form['province']
            cityname = request.form['city']
            number = request.form['number']
            print(province, cityname, number)
            bingfang = Fangjianshiyongqingkuangbiao(fangjianhao=province, fangjianweizhi=cityname,
                                                    fangjianshifoumanyuan=number)
            db.session.add(bingfang)
            db.session.commit()

            # 添加完成重定向至主页
            return redirect('/bingfang')
        elif type == 'doctor':
            ysh = request.form['ysh']
            name = request.form['name']
            sex = request.form['sex']
            olf = request.form['old']
            child = request.form['child']
            dy = request.form['dy']
            job = request.form['job']
            phone = request.form['phone']
            number = request.form['number']
            print(request.form)
            ys = Yiyuanrenyuanxinxibiao(yishenggongzuozhenghao=ysh, xingbie=sex, nianling=olf, xingming=name,
                                        shifoudushengzinv=child, shifoudangyuan=dy, dianhua=phone, gongzuonianling=job,
                                        yiyuanrenyuandegongzuoleixingdaihao=number)
            db.session.add(ys)
            db.session.commit()
            return redirect('/doctor')
        elif type == 'huanzhe':
            ysh = request.form['ysh']
            name = request.form['name']
            sex = request.form['sex']
            olf = request.form['old']
            child = request.form['child']
            dy = request.form['dy']
            job = request.form['job']
            phone = request.form['phone']
            number = request.form['number']
            print(request.form)
            ys = Huanzherjibenxinxibiao(shenfenzhenghao=ysh, xingbie=sex, nianling=olf, xingming=name,
                                        jiguan=child, huanzhehao=dy, fangjianweizhi=phone, ruyuanshijian=job,
                                        jiashulianxitianhua=number)
            db.session.add(ys)
            db.session.commit()
            return redirect('/huanzhe')
        elif type == 'device':
            ysh = request.form['ysh']
            name = request.form['name']
            sex = request.form['sex']
            olf = request.form['old']
            print(request.form)
            ys = Shebeishiyongqingkuangbiao(shebeihao=ysh, shebeileixing=sex, shifoushiyong=olf,
                                        yongyunagehuanzhe_huanzhehao_=name
                                        )
            db.session.add(ys)
            db.session.commit()
            return redirect('/device')
#删除
@app.route('/delete_gen', methods=['GET', 'POST'])
def delete_gen():
    if request.method == 'GET':
        id = request.args.get("id")
        type = request.args.get("type")
        if type == 'doctor':
            detail = Yiyuanrenyuanxinxibiao.query.filter_by(yishenggongzuozhenghao=id).first()

            db.session.delete(detail)
            db.session.commit()
            return redirect('/doctor')
        elif type == 'huanzhe':
            detail = Huanzherjibenxinxibiao.query.filter_by(shenfenzhenghao=id).first()

            db.session.delete(detail)
            db.session.commit()
            return redirect('/huanzhe')
        elif type == 'device':
            detail = Shebeishiyongqingkuangbiao.query.filter_by(shebeihao=id).first()

            db.session.delete(detail)
            db.session.commit()
            return redirect('/device')
        elif type == 'bingfang':
            detail = Fangjianshiyongqingkuangbiao.query.filter_by(fangjianhao=id).first()

            db.session.delete(detail)
            db.session.commit()
            return redirect('/bingfang')


@app.route("/insert_device")
def insert_device():
    # 跳转至添加信息页面
    if request.method == 'GET':
        type = request.args.get("type")
        if type == 'device':
            qufen = Shebeishiyongqingkuangbiao.query.all()
            return render_template("insert_device.html", qufen=qufen,mark='0')
        elif type == 'doctor':
            qufen = Yiyuanrenyuanxinxibiao.query.all()
            return render_template("insert_doctor.html", qufen=qufen, mark='0')
        elif type == 'huanzhe':
            qufen = Huanzherjibenxinxibiao.query.all()
            return render_template("insert_huanzhe.html", qufen=qufen,mark='0')
        elif type == 'bingfang':
            qufen = Fangjianshiyongqingkuangbiao.query.all()
            return render_template("insert_bingfang.html", qufen=qufen, mark='0')
    if request.method == 'POST':
        pass

if __name__ == "__main__":
    app.run(debug=True, host='127.0.0.1', port=8080)
