'''
Author: your name
Date: 2021-06-29 19:24:32
LastEditTime: 2021-06-29 19:28:16
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\backend\controller\monitor.py
'''
from model.monitor import session, System, Lvgl, Evm, Image, Watch, Request

class SystemResource(object):
    def get(self):
        result = session.query(System).all()
        print(result)
        return result

    def post(self, params):
        result = System(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class LvglResource(object):
    def get(self):
        result = session.query(Lvgl).all()
        print(result)
        return result

    def post(self, params):
        result = Lvgl(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class EvmResource(object):
    def get(self):
        result = session.query(Evm).all()
        print(result)
        return result

    def post(self, params):
        result = Evm(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class ImageResource(object):
    def get(self):
        result = session.query(Image).all()
        print(result)
        return result

    def post(self, params):
        result = Image(**params)
        session.add(result)
        return session.commit()

    def post_array(self, array, watch):
        t = []
        for a in array:
            a.update({ "watch": watch })
            t.append(Image(**a))
        session.add_all(t)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

systemResource = SystemResource()
lvglResource = LvglResource()
evmResource = EvmResource()
imageResource = ImageResource()

def insert_data(msg):
    # 先判断手表imei是否存在，不存在则先注册手表IMEI
    watch_id = -1
    if msg.get("imei"):
        result = session.query(Watch).filter_by(imei=msg.get("imei")).first()
        if result:
            watch_id = result.id
        else:
            result = Watch(imei=msg.get("imei"))
            session.add(result)
            session.flush()
            session.commit()

            result = session.query(Watch).filter_by(imei=msg.get("imei")).first()
            if result:
                watch_id = result.id

    if msg.get("request"):
        msg.get("request").update({ "watch": watch_id })
        result = Request(**msg.get("request"))
        session.add(result)
        session.flush()
        session.commit()

    if msg.get("system"):
        msg.get("system").update({ "watch": watch_id })
        res = systemResource.post(msg.get("system"))
        print("!!!!!!", res)

    if msg.get("lvgl"):
        msg.get("lvgl").update({ "watch": watch_id })
        res = lvglResource.post(msg.get("lvgl"))
        print("@@@@@@", res)

    if msg.get("evm"):
        msg.get("evm").update({ "watch": watch_id })
        res = evmResource.post(msg.get("evm"))
        print("######", res)

    if msg.get("image"):
        res = imageResource.post_array(msg.get("image"), watch_id)
        print("$$$$$$", res)