package com.tofly.ghrq_route.module.trouble;

import com.tofly.data.sql.dao.DaoSession;
import com.tofly.data.sql.dao.TroubleAudioBeanDao;
import com.tofly.data.sql.dao.TroubleBeanDao;
import com.tofly.data.sql.dao.TroublePictureBeanDao;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class TroubleDaoHelper {

    private static TroubleDaoHelper ourInstance = new TroubleDaoHelper();

    public static TroubleDaoHelper getInstance() {
        return ourInstance;
    }

    private DBDaoHelper dbDaoHelper;
    private DaoSession daoSession;

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

    /**
     * TroubleBeanDao
     */

    /**
     * 根据隐患id查询隐患
     * @param troubleId 隐患的id
     * @return 查询到的隐患
     */
    public TroubleBean queryTrouble(String troubleId) {
        TroubleBeanDao troubleBeanDao = daoSession.getTroubleBeanDao();
        return troubleBeanDao.load(troubleId);
    }

    /**
     * 插入一个新的隐患或者更新已存在的隐患信息
     * @param trouble 新隐患或者需要更新的隐患信息
     * @return 数据表中的rowid
     */
    public void insertOrUpdateTrouble(TroubleBean trouble) {
        daoSession.insertOrReplace(trouble);
    }

    /**
     * 插入多个隐患或者更新已存在的隐患信息
     * @param troubleBeans 新隐患或者需要更新的隐患信息
     * @return 数据表中的rowid
     */
    public void insertOrUpdateTroubles(List<TroubleBean> troubleBeans) {
        TroubleBeanDao troubleBeanDao = daoSession.getTroubleBeanDao();
        troubleBeanDao.insertOrReplaceInTx(troubleBeans);
    }

    /**
     * 删除已存在的隐患信息
     * @param trouble 要删除的隐患信息
     * @return 数据表中的rowid
     */
    public void deleteSavedTrouble(TroubleBean trouble) {
        daoSession.delete(trouble);
    }

    public long getTroubleSize() {
        TroubleBeanDao troubleBeanDao = daoSession.getTroubleBeanDao();
        return troubleBeanDao.queryBuilder().count();
    }

    public List<TroubleBean> getTroubleList(String userid, int state) {
        TroubleBeanDao troubleBeanDao = daoSession.getTroubleBeanDao();
        List<TroubleBean> troubleBeens = troubleBeanDao.queryBuilder().
                where(TroubleBeanDao.Properties.State.eq(state),
                        TroubleBeanDao.Properties.Userid.eq(userid)).
                orderDesc(TroubleBeanDao.Properties.FindTime).list();
        return troubleBeens;
    }

    public List<TroubleBean> getTroubleList(String userid, int state, int trouble_type) {
        TroubleBeanDao troubleBeanDao = daoSession.getTroubleBeanDao();
        List<TroubleBean> troubleBeens = troubleBeanDao.queryBuilder().
                where(TroubleBeanDao.Properties.State.eq(state),
                        TroubleBeanDao.Properties.Userid.eq(userid)).
                orderDesc(TroubleBeanDao.Properties.FindTime).list();
        return troubleBeens;
    }


    /**
     * TroublePictureBeanDao
     */

    /**
     * 根据隐患图片id查询隐患图片
     * @param pictureId 隐患图片的id
     * @return 查询到的隐患图片
     */
    public TroublePictureBean queryTroublePicture(String pictureId) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        return troublePictureBeanDao.load(pictureId);
    }

    public TroublePictureBean queryTroublePictureByUrl(String fileUrl) {
        TroublePictureBean troublePictureBean = null;
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        QueryBuilder<TroublePictureBean> queryBuilder = troublePictureBeanDao.queryBuilder();
        queryBuilder.where(TroublePictureBeanDao.Properties.Fileurl.eq(fileUrl));
        List<TroublePictureBean> pictureBeans = queryBuilder.list();
        if (pictureBeans != null && pictureBeans.size() > 0) {
            troublePictureBean = pictureBeans.get(0);
        }
        return troublePictureBean;
    }

    /**
     * 根据隐患id查询隐患图片
     *
     * @param troubleId 隐患的id
     * @return 查询到的隐患图片数组
     */
    public List<TroublePictureBean> queryTroublePictures(String troubleId) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        QueryBuilder<TroublePictureBean> queryBuilder = troublePictureBeanDao.queryBuilder();
        queryBuilder.where(TroublePictureBeanDao.Properties.Troubleid.eq(troubleId));
        List<TroublePictureBean> pictureBeanList = queryBuilder.list();
        return pictureBeanList;
    }

    public List<TroublePictureBean> queryTroublePictures(String troubleId, int state) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        QueryBuilder<TroublePictureBean> queryBuilder = troublePictureBeanDao.queryBuilder();
        queryBuilder.where(TroublePictureBeanDao.Properties.Troubleid.eq(troubleId),
                TroublePictureBeanDao.Properties.Status.eq(state));
        List<TroublePictureBean> pictureBeanList = queryBuilder.list();
        return pictureBeanList;
    }

    /**
     * 插入一个新的隐患图片或者更新已存在的隐患图片
     *
     * @param pictureBean 新隐患图片或者需要更新的隐患图片
     * @return 数据表中的rowid
     */
    public long insertOrUpdateTroublePicture(TroublePictureBean pictureBean) {
        return daoSession.insertOrReplace(pictureBean);
    }

    public void deleteSavedTroublePicture(TroublePictureBean pictureBean) {
        daoSession.delete(pictureBean);
    }

    /**
     * 根据隐患id查询隐患音频
     * @param troubleId 隐患的id
     * @return 查询到的隐患音频数组
     */
    public List<TroubleAudioBean> queryTroubleAudios(String troubleId) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        queryBuilder.where(TroubleAudioBeanDao.Properties.Troubleid.eq(troubleId));
        return queryBuilder.list();
    }

    /**
     * 根据隐患录音的url查询隐患音频
     *
     * @param url 隐患图片的地址
     * @return 查询到的隐患图片
     */
    public TroubleAudioBean queryTroubleAudio(String url) {
        TroubleAudioBean troubleAudioBean = null;
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        queryBuilder.where(TroubleAudioBeanDao.Properties.Url.eq(url));
        List<TroubleAudioBean> audioBeanList = queryBuilder.limit(1).list();
        if (audioBeanList != null && audioBeanList.size() > 0) {
            troubleAudioBean = audioBeanList.get(0);
        }
        return troubleAudioBean;
    }

    /**
     * 根据隐患录音的url查询隐患音频
     *
     * @param fileNote 文件的类型描述
     * @return 查询到的隐患图片
     */
    public TroubleAudioBean queryTroubleAudioByFileNote(String fileNote) {
        TroubleAudioBean troubleAudioBean = null;
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        queryBuilder.where(TroubleAudioBeanDao.Properties.Filenote.eq(fileNote));
        List<TroubleAudioBean> audioBeanList = queryBuilder.limit(1).list();
        if (audioBeanList != null && audioBeanList.size() > 0) {
            troubleAudioBean = audioBeanList.get(0);
        }
        return troubleAudioBean;
    }

    /**
     * 插入一个新的隐患音频或者更新已存在的隐患音频
     *
     * @param audioBean 新隐患音频或者需要更新的隐患音频
     * @return 数据表中的rowid
     */
    public long insertOrUpdateTroubleAudio(TroubleAudioBean audioBean) {
        return daoSession.insertOrReplace(audioBean);
    }

    public void deleteSavedTroubleAudio(TroubleAudioBean audioBean) {
        daoSession.delete(audioBean);
    }

}
