package com.cnd3b.service;

import com.cnd3b.common.BaseClass;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DBConnect;
import com.cnd3b.common.data.db.SQLiteJDBC;

import java.util.Calendar;

public class MessageSendService extends BaseClass implements Runnable {
    public static boolean message1_sendflag = true;
    public static boolean message2_sendflag = true;
    public static boolean message3_sendflag = true;
    public static boolean message4_sendflag = true;

    @Override
    public void run() {
        try {
            if (new SQLiteJDBC().checkServicesUsed("MessageSendService",true)) {
                Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                message1(hour, minute);
                message2(hour, minute);
                message3(hour, minute);
                message4(hour, minute);
                messageClean();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void message1(int hour, int minute) {
        if (hour >= 17 && minute >= 30) {
            if (message1_sendflag) {
                SQLFactory factory = new SQLFactory(this, "当天无录入记录经销商提醒");
                String status = new DBConnect().runSqlUpdate(factory.getSQL());
                if ("true".equals(status)) {
                    message1_sendflag = false;
                }
            }
        } else if (hour < 17) {
            message1_sendflag = true;
        }
    }

    public void message2(int hour, int minute) {
        if (hour >= 18 && minute >= 30) {
            if (message2_sendflag) {
                SQLFactory factory = new SQLFactory(this, "代理商当天无数据录入业务员提醒");
                String status = new DBConnect().runSqlUpdate(factory.getSQL());
                if ("true".equals(status)) {
                    message2_sendflag = false;
                }
            }
        } else if (hour < 18) {
            message2_sendflag = true;
        }
    }

    public void message3(int hour, int minute) {
        if (hour >= 20 && minute >= 30) {
            if (message3_sendflag) {
                SQLFactory factory = new SQLFactory(this, "代理商三天无数据录入业务员提醒");
                String status = new DBConnect().runSqlUpdate(factory.getSQL());
                if ("true".equals(status)) {
                    message3_sendflag = false;
                }
            }
        } else if (hour < 20) {
            message3_sendflag = true;
        }
    }

    public void message4(int hour, int minute) {
        if (hour >= 20 && minute >= 30) {
            if (message4_sendflag) {
                SQLFactory factory = new SQLFactory(this, "代理商七天无数据录入业务员提醒");
                String status = new DBConnect().runSqlUpdate(factory.getSQL());
                if ("true".equals(status)) {
                    message4_sendflag = false;
                }
            }
        } else if (hour < 20) {
            message4_sendflag = true;
        }
    }

    /**
     * 系统消息清理
     */
    public void messageClean() {
        //自动清理1月前的系统消息
        new DBConnect().runSqlUpdate("delete from tmessage where DATEADD(MM,1, createdate)<GETDATE() and ftype='系统'");
        //自动清理3月前的非系统消息
        new DBConnect().runSqlUpdate("delete from tmessage where DATEADD(MM,3, createdate)<GETDATE() and ftype!='系统'");
    }
}
