package com.tofly.ghrq_route.module.job;

import com.tofly.data.sql.dao.DaoSession;
import com.tofly.data.sql.dao.NotifyManageBeanDao;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;

import java.util.List;

public class NotifyMsgDaoHelper {

    private static NotifyMsgDaoHelper ourInstance = new NotifyMsgDaoHelper();

    public static NotifyMsgDaoHelper getInstance() {
        return ourInstance;
    }

    private DBDaoHelper dbDaoHelper;
    private DaoSession daoSession;

    private NotifyMsgDaoHelper() {
        dbDaoHelper = DBDaoHelper.getInstance();
        daoSession = dbDaoHelper.getDaoSession();
    }

    /*插入通知消息*/
    public void insertNotifyMsgBean(NotifyManageBean manageBean) {
        daoSession.insert(manageBean);
    }

    /*插入通知消息*/
    public void upadteNotifyMsgBean(NotifyManageBean manageBean) {
        daoSession.insertOrReplace(manageBean);
    }

    /*插入通知消息*/
    public void upadteNotifyMsgBeans(List<NotifyManageBean> manageBeans) {
        NotifyManageBeanDao notifyManageBeanDao = daoSession.getNotifyManageBeanDao();
        notifyManageBeanDao.insertOrReplaceInTx(manageBeans);
    }

    public NotifyManageBean getMessageNotifyMsgBean(String userid, String server_id) {
        return getNotifyMsgBean(userid, server_id, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_MESSAGE);
    }

    public NotifyManageBean getEmergencyNotifyMsgBean(String userid, String server_id) {
        return getNotifyMsgBean(userid, server_id, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_EMERGENCY);
    }

    public NotifyManageBean getSurveyNotifyMsgBean(String userid, String server_id) {
        return getNotifyMsgBean(userid, server_id, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_SURVEY);
    }

    public NotifyManageBean getJobNotifyMsgBean(String userid, String server_id) {
        return getNotifyMsgBean(userid, server_id, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_JOB);
    }

    public NotifyManageBean getTroubleNotifyMsgBean(String userid, String server_id) {
        return getNotifyMsgBean(userid, server_id, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_TROUBLE);
    }

    public NotifyManageBean getNotifyMsgBean(String userid, String server_id, NotifyManageBean.NOTIFY_MSG_TYPE msg_type) {
        NotifyManageBeanDao notifyManageBeanDao = daoSession.getNotifyManageBeanDao();
        return notifyManageBeanDao.queryBuilder().where(
                NotifyManageBeanDao.Properties.Userid.eq(userid),
                NotifyManageBeanDao.Properties.ServerId.eq(server_id),
                NotifyManageBeanDao.Properties.Notify_type.eq(msg_type.getType()))
                .unique();
    }

    public List<NotifyManageBean> getMessageUnNotifyMsgBean(String userid) {
        return getUnNotifyMsgBean(userid, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_MESSAGE);
    }

    public List<NotifyManageBean> getEmergencyUnNotifyMsgBean(String userid) {
        return getUnNotifyMsgBean(userid, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_EMERGENCY);
    }

    public List<NotifyManageBean> getJobUnNotifyMsgBean(String userid) {
        return getUnNotifyMsgBean(userid, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_JOB);
    }

    public List<NotifyManageBean> getTroubleUnNotifyMsgBean(String userid) {
        return getUnNotifyMsgBean(userid, NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_TROUBLE);
    }

    public List<NotifyManageBean> getUnNotifyMsgBean(String userid, NotifyManageBean.NOTIFY_MSG_TYPE msg_type) {
        NotifyManageBeanDao notifyManageBeanDao = daoSession.getNotifyManageBeanDao();
        return notifyManageBeanDao.queryBuilder().where(
                NotifyManageBeanDao.Properties.Userid.eq(userid),
                NotifyManageBeanDao.Properties.IsNotify.eq(false),
                NotifyManageBeanDao.Properties.Notify_type.eq(msg_type.getType()))
                .list();
    }

    public void setMsgBeanNotified(List<NotifyManageBean> msgBeans) {
        if (msgBeans != null) {
            for (NotifyManageBean msgBean : msgBeans) {
                msgBean.setIsNotify(true);
            }
            upadteNotifyMsgBeans(msgBeans);
        }
    }

    public void setMsgBeanRead(NotifyManageBean msgBean) {
        if (msgBean != null) {
            msgBean.setIsRead(true);
            upadteNotifyMsgBean(msgBean);
        }
    }
}
