import sys
from pathlib import Path

from sqlalchemy import and_

sys.path.append(str(Path(__file__).absolute().parent.parent.parent))
from spec.ormtables.tables_test import Satellite
from spec.ormtables.tables_test import SatelliteWork
from feature.mysql.sqlkit import Dao


def main():
    dao = Dao('mysql+pymysql://root:lhr@192.168.0.210:3306/test')
    # 卫星添加
    sat = Satellite()
    sat.norad = '2000'  # 主键必填
    # dao.delete(Satellite, Satellite.norad == sat.norad)  # 插入前先执行下删除
    # print(sat.to_dict())
    dao.add(sat)
    print(sat.to_dict())
    # rows = dao.select(Satellite, Satellite.norad == sat.norad)  # Satellite.norad is not None
    # print('卫星添加', [row.to_dict() for row in rows])
    #
    # # 卫星修改
    # sat.en_name = 'abc'
    # dao.update(Satellite, sat, Satellite.norad == sat.norad)
    # rows = dao.select(Satellite, Satellite.norad == sat.norad)
    # print('卫星修改', [row.to_dict() for row in rows])
    #
    # # 卫星修改
    # list = [SatelliteWork(config_id=i, norad=sat.norad) for i in range(3)]
    # dao.delete(SatelliteWork, SatelliteWork.norad == sat.norad)  # 插入前先执行下删除
    # dao.add_all(list)
    # rows = dao.select(SatelliteWork, SatelliteWork.norad == sat.norad)
    # print('卫星修改', [row.to_dict() for row in rows])
    # print(list)
    #
    # # 工作参数删除单个
    # dao.delete(SatelliteWork, and_(SatelliteWork.norad == sat.norad, SatelliteWork.config_id == 1))
    # rows = dao.select(SatelliteWork, SatelliteWork.norad == sat.norad)
    # print('工作参数删除单个', [row.to_dict() for row in rows])
    #
    # # 卫星删除
    # dao.delete(Satellite, Satellite.norad == sat.norad)
    # rows = dao.select(Satellite, Satellite.norad == sat.norad)
    # print('卫星删除', [row.to_dict() for row in rows])
    #
    # # norad 外键关联，卫星删除，工作参数同样删除 级联删除后不能使用SatelliteWork.norad== sat_w.norad进行查询
    # # 工作参数级联删除
    # rows = dao.select(SatelliteWork, Satellite.norad is not None)
    # print('工作参数级联删除', [row.to_dict() for row in rows])

    # dao.close()


if __name__ == '__main__':
    main()
