# coding=utf-8
import dbConn
from SendEmail import Email
from wxSend import WX


# 重置通知列表的上下限状态
def resetNotify():
    db = dbConn.MY_SQL()
    db.resetNotifyList()
    print('=== reset Notify success ===')


# 检查监控点是否需要发通知
def checkNotify():
    db = dbConn.MY_SQL()
    list = db.getNotifyList()
    allMsg = ''
    for i in list:
        msg = ''
        price = db.getLastPrice(i[1])
        if float(price[1]) >= float(i[3]) and float(i[5]) == 0:
            # 更新上限状态
            db.updateNotifyType(i[0], 1, i[6], i[7])
            msg += '上限 》 %s[<font color="#ff0000">%s</font>]， 当前价： %s， %s%% <br>' % \
                   (i[2], i[1], price[1], price[2])

        if float(price[1]) <= float(i[4]) and float(i[6]) == 0:
            # 更新下限状态
            db.updateNotifyType(i[0], i[5], 1, i[7])
            msg += '下限 》 %s[<font color="#00ff00">%s</font>]， 当前价： %s， %s%% <br>' % \
                   (i[2], i[1], price[1], price[2])

        # 判断今天的涨跌幅度, (2, 5, 7, 9.9)
        if msg == '':
            p1 = abs(float(price[2]))    # 当前幅度
            p2 = abs(float(i[7]))        # 记录幅度
            if p1 >= 9.9 and p2 != 9.9:
                db.updateNotifyType(i[0], i[5], i[6], 9.9)
            elif 9.9 > p1 >= 7 and p2 != 7:
                db.updateNotifyType(i[0], i[5], i[6], 7)
            elif 7 > p1 >= 5 and p2 != 5:
                db.updateNotifyType(i[0], i[5], i[6], 5)
            elif 5 > p1 >= 2 and p2 != 2:
                db.updateNotifyType(i[0], i[5], i[6], 2)
            else:
                continue

            msg = '波动 ~ %s[<font color="#0000ff">%s</font>]， 当前价： %s， %s%% <br>' % \
                  (i[2], i[1], price[1], price[2])

        allMsg += msg

    if allMsg != '':
        Email.sendNofity(allMsg)
        WX.send('STK', allMsg)


# checkNotify()
resetNotify()