package com.tofly.ghrq_route.module.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.mgq.base.utils.string.StringUtil;
import com.tofly.data.sql.dao.BuildExamineBeanDao;
import com.tofly.data.sql.dao.BuildFileBeanDao;
import com.tofly.data.sql.dao.BuildHandleDataBeanDao;
import com.tofly.data.sql.dao.BuildLogBeanDao;
import com.tofly.data.sql.dao.BuildManageBeanDao;
import com.tofly.data.sql.dao.BuildManageFileBeanDao;
import com.tofly.data.sql.dao.BuildProcessManageBeanDao;
import com.tofly.data.sql.dao.BuildProjectTypeBeanDao;
import com.tofly.data.sql.dao.BuildReportBeanDao;
import com.tofly.data.sql.dao.BuildSignDataBeanDao;
import com.tofly.data.sql.dao.BuildTypeBeanDao;
import com.tofly.data.sql.dao.CheckGPSDataBeanDao;
import com.tofly.data.sql.dao.DaoMaster;
import com.tofly.data.sql.dao.DaoSession;
import com.tofly.data.sql.dao.DeviceInfoBeanDao;
import com.tofly.data.sql.dao.DownloadDataDao;
import com.tofly.data.sql.dao.FieldsBeanDao;
import com.tofly.data.sql.dao.GPSDataBeanDao;
import com.tofly.data.sql.dao.LocalPlanRecordBeanDao;
import com.tofly.data.sql.dao.LocationGPSDataBeanDao;
import com.tofly.data.sql.dao.MaintainAdjustParamDao;
import com.tofly.data.sql.dao.MaintainCommonParamDao;
import com.tofly.data.sql.dao.MapUrlConfigDataBeanDao;
import com.tofly.data.sql.dao.OfflineJobNeedDataDao;
import com.tofly.data.sql.dao.OfflinePathBeanDao;
import com.tofly.data.sql.dao.OfflineWorkJobBeanDao;
import com.tofly.data.sql.dao.PlanBeanDao;
import com.tofly.data.sql.dao.PlanPointRepairDao;
import com.tofly.data.sql.dao.RushRepairBeanDao;
import com.tofly.data.sql.dao.RushRepairExamineBeanDao;
import com.tofly.data.sql.dao.RushRepairFileBeanDao;
import com.tofly.data.sql.dao.RushRepairProcessBeanDao;
import com.tofly.data.sql.dao.ServerSysConfigBeanDao;
import com.tofly.data.sql.dao.SignDataBeanDao;
import com.tofly.data.sql.dao.SignHistoryDataBeanDao;
import com.tofly.data.sql.dao.SystemConfigBeanDao;
import com.tofly.data.sql.dao.TroubleAudioBeanDao;
import com.tofly.data.sql.dao.TroublePictureBeanDao;
import com.tofly.data.sql.dao.UploadPhotoBeanDao;
import com.tofly.data.sql.dao.UserBeanDao;
import com.tofly.data.sql.dao.UserMapDataBeanDao;
import com.tofly.data.sql.dao.UserMenuBeanDao;
import com.tofly.data.sql.dao.UserOnlineMapDataBeanDao;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineJobNeedData;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean;
import com.tofly.ghrq_route.module.buildManage.BuildExamineBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildFileBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildHandleDataBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildLogBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageFileBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildProcessManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildProjectTypeBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildReportBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildSignDataBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildTypeBean;
import com.tofly.ghrq_route.module.buildManage.domain.DepartmentBean;
import com.tofly.ghrq_route.module.buildManage.domain.UserListBean;
import com.tofly.ghrq_route.module.deviceInfo.domain.DeviceInfoBean;
import com.tofly.ghrq_route.module.download.domain.DownloadData;
import com.tofly.ghrq_route.module.download.domain.UpDownloadData;
import com.tofly.ghrq_route.module.gps.domain.CheckGPSDataBean;
import com.tofly.ghrq_route.module.gps.domain.GPSDataBean;
import com.tofly.ghrq_route.module.gps.domain.LocationGPSDataBean;
import com.tofly.ghrq_route.module.job.domain.JobBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.FieldsBean;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.OfflinePointLineBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainAdjustParam;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainCommonParam;
import com.tofly.ghrq_route.module.planPointRepair.domain.PlanPointRepair;
import com.tofly.ghrq_route.module.planPointRepair.domain.RepairSysDeviceSetting;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairExamineBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairFileBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairProcessBean;
import com.tofly.ghrq_route.module.sign.domain.SignDataBean;
import com.tofly.ghrq_route.module.sign.domain.SignHistoryDataBean;
import com.tofly.ghrq_route.module.sketch.domain.SketchBean;
import com.tofly.ghrq_route.module.systemConfig.domain.ServerSysConfigBean;
import com.tofly.ghrq_route.module.systemConfig.domain.SystemConfigBean;
import com.tofly.ghrq_route.module.trouble.domain.LocalPlanRecordBean;
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 com.tofly.ghrq_route.module.troubleType.domain.SysDeviceTypeBean;
import com.tofly.ghrq_route.module.troubleType.domain.TroubleRiskLevelBean;
import com.tofly.ghrq_route.module.troubleType.domain.TroubleTypeBean;
import com.tofly.ghrq_route.module.uploadPhoto.domain.UploadPhotoBean;
import com.tofly.ghrq_route.module.user.domain.DepartListBean;
import com.tofly.ghrq_route.module.user.domain.DepartUserListBean;
import com.tofly.ghrq_route.module.user.domain.MapUrlConfigDataBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.module.user.domain.UserMapDataBean;
import com.tofly.ghrq_route.module.user.domain.UserMenuBean;
import com.tofly.ghrq_route.module.user.domain.UserOnlineMapDataBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;
import com.tofly.ghrq_route.common.util.file.FileUtil;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Amu on 2017/2/10.
 */

public class DBDaoHelper {
    private static DBDaoHelper ourInstance = new DBDaoHelper();

    public static DBDaoHelper getInstance() {
        return ourInstance;
    }

    private static final String DB_NAME = "zmrq_route_dao";
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private static SQLiteDatabase database = null;

    private DBDaoHelper() {
    }

    public void setupDatabase(Context context) {
        UserOpenHelper openHelper = new UserOpenHelper(context, DB_NAME);
        database = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(database);
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public void clearAll() {
        daoSession.deleteAll(BuildHandleDataBean.class);
        daoSession.deleteAll(BuildLogBean.class);
        daoSession.deleteAll(BuildManageBean.class);
        daoSession.deleteAll(BuildProjectTypeBean.class);
        daoSession.deleteAll(BuildReportBean.class);
        daoSession.deleteAll(BuildSignDataBean.class);
        daoSession.deleteAll(CheckGPSDataBean.class);
        daoSession.deleteAll(DeviceInfoBean.class);
        daoSession.deleteAll(DownloadData.class);
        daoSession.deleteAll(GPSDataBean.class);
        daoSession.deleteAll(LocalPlanRecordBean.class);
        daoSession.deleteAll(LocationGPSDataBean.class);
        daoSession.deleteAll(MapUrlConfigDataBean.class);
        daoSession.deleteAll(OfflineJobNeedData.class);
        daoSession.deleteAll(OfflinePathBean.class);
        daoSession.deleteAll(OfflinePointLineBean.class);
        daoSession.deleteAll(OfflineWorkJobBean.class);
        daoSession.deleteAll(PlanBean.class);
        daoSession.deleteAll(PlanPointRepair.class);
        daoSession.deleteAll(RepairSysDeviceSetting.class);
        daoSession.deleteAll(ServerSysConfigBean.class);
        daoSession.deleteAll(SignDataBean.class);
        daoSession.deleteAll(SignHistoryDataBean.class);
        daoSession.deleteAll(JobBean.class);
        daoSession.deleteAll(SketchBean.class);
        daoSession.deleteAll(SysDeviceTypeBean.class);
        daoSession.deleteAll(SystemConfigBean.class);
        daoSession.deleteAll(TroubleAudioBean.class);
        daoSession.deleteAll(TroubleBean.class);
        daoSession.deleteAll(TroublePictureBean.class);
        daoSession.deleteAll(TroubleTypeBean.class);
        daoSession.deleteAll(TroubleRiskLevelBean.class);
        daoSession.deleteAll(UploadPhotoBean.class);
        daoSession.deleteAll(UserMapDataBean.class);
        daoSession.deleteAll(UserBean.class);
        daoSession.deleteAll(FieldsBean.class);
    }

    public void clearAllButUser() {
        daoSession.deleteAll(BuildHandleDataBean.class);
        daoSession.deleteAll(BuildLogBean.class);
        daoSession.deleteAll(BuildManageBean.class);
        //daoSession.deleteAll(BuildProjectTypeBean.class);
        daoSession.deleteAll(BuildReportBean.class);
        daoSession.deleteAll(BuildSignDataBean.class);
        daoSession.deleteAll(CheckGPSDataBean.class);
        daoSession.deleteAll(DeviceInfoBean.class);
        daoSession.deleteAll(DownloadData.class);
        daoSession.deleteAll(GPSDataBean.class);
        daoSession.deleteAll(LocalPlanRecordBean.class);
        daoSession.deleteAll(LocationGPSDataBean.class);
        daoSession.deleteAll(OfflineJobNeedData.class);
        daoSession.deleteAll(OfflinePathBean.class);
        daoSession.deleteAll(OfflinePointLineBean.class);
        daoSession.deleteAll(OfflineWorkJobBean.class);
        daoSession.deleteAll(PlanBean.class);
        daoSession.deleteAll(PlanPointRepair.class);
        daoSession.deleteAll(RepairSysDeviceSetting.class);
        //daoSession.deleteAll(ServerSysConfigBean.class);
        daoSession.deleteAll(SignDataBean.class);
        daoSession.deleteAll(SignHistoryDataBean.class);
        daoSession.deleteAll(JobBean.class);
        daoSession.deleteAll(SketchBean.class);
        //daoSession.deleteAll(SystemConfigBean.class);
        daoSession.deleteAll(TroubleAudioBean.class);
        daoSession.deleteAll(TroubleBean.class);
        daoSession.deleteAll(TroublePictureBean.class);
        //daoSession.deleteAll(TroubleTypeBean.class);
        //daoSession.deleteAll(TroubleTypeEquipmentBean.class);
        //daoSession.deleteAll(SysDeviceTypeBean.class);
        daoSession.deleteAll(UploadPhotoBean.class);
        //daoSession.deleteAll(UserMapDataBean.class);
        //daoSession.deleteAll(MapUrlConfigDataBean.class);
        daoSession.deleteAll(FieldsBean.class);
        daoSession.deleteAll(UserMenuBean.class);
        daoSession.deleteAll(JobBean.class);
        daoSession.deleteAll(MaintainAdjustParam.class);
        daoSession.deleteAll(MaintainCommonParam.class);
        daoSession.deleteAll(UserOnlineMapDataBean.class);
        daoSession.deleteAll(ServerSysConfigBean.class);
    }

    public Property getPropertyByName(AbstractDao dao, String columnName) {
        Property propertyReturn = null;
        Property[] properties = dao.getProperties();
        for (Property property : properties) {
            if (property.name.equals(columnName)) {
                propertyReturn = property;
                break;
            }
        }
        return propertyReturn;
    }

    public <T> AbstractDao getTableDao(Class<T> entityClass) {
        AbstractDao dao = daoSession.getDao(entityClass);
        return dao;
    }

    public <T> String[] getTableTitles(Class<T> entityClass) {
        AbstractDao dao = daoSession.getDao(entityClass);
        String[] title = null;
        if (dao instanceof GPSDataBeanDao || dao instanceof CheckGPSDataBeanDao
                || dao instanceof DeviceInfoBeanDao || dao instanceof OfflinePathBeanDao) {
            title = dao.getAllColumns();
        }
        return title;
    }

    /**
     * UserBeanDao
     */
    public List<UserBean> getUserList() {
        UserBeanDao userDao = daoSession.getUserBeanDao();
        List<UserBean> users = userDao.queryBuilder().list();
        return users;
    }

    /**
     * 根据用户userid查询用户
     *
     * @param userid 用户的userid
     * @return 查询到的User
     */
    public UserBean queryUserByUserId(String userid) {
        UserBeanDao userDao = daoSession.getUserBeanDao();
        QueryBuilder<UserBean> queryBuilder = userDao.queryBuilder();
        queryBuilder.where(UserBeanDao.Properties.Userid.eq(userid));
        UserBean user = queryBuilder.unique();
        return user;
    }

    /**
     * 根据用户名查询用户
     *
     * @param username 用户的loginid，用户的用户名为中文
     * @return 查询到的User
     */
    public UserBean queryUserByLoginid(String username) {
        UserBeanDao userDao = daoSession.getUserBeanDao();
        QueryBuilder<UserBean> queryBuilder = userDao.queryBuilder();
        queryBuilder.where(UserBeanDao.Properties.Username.eq(username));
        UserBean user = queryBuilder.unique();
        return user;
    }

    /**
     * 插入一个新的用户或者更新已存在的用户信息
     *
     * @param user 新用户或者需要更新的用户信息
     * @return 数据表中的rowid
     */
    public long insertOrUpdateUser(UserBean user) {
        return daoSession.insertOrReplace(user);
    }

    /**
     * UserMenuBeanDao
     */

    public void deleteUserMenuList() {
        daoSession.deleteAll(UserMenuBean.class);
    }

    public void deleteUserMenuList(String userid) {
        UserMenuBeanDao userMenuBeanDao = daoSession.getUserMenuBeanDao();
        List<UserMenuBean> userMenuList = userMenuBeanDao.queryBuilder()
                .where(UserMenuBeanDao.Properties.Userid.eq(userid)).list();
        if (userMenuList != null && userMenuList.size() > 0) {
            userMenuBeanDao.deleteInTx(userMenuList);
        }
    }

    public void saveUserMenuList(List<UserMenuBean> userMenus, String userid) {
        if (userMenus != null && userid != null && !userid.isEmpty()) {
            for (UserMenuBean menuBean : userMenus) {
                menuBean.setUserid(userid);
                daoSession.insert(menuBean);
            }
        }
    }

    /*获取当前用户的功能列表，以顺序排序*/
    public List<UserMenuBean> getUserMenuList(String userid) {
        UserMenuBeanDao userMenuBeanDao = daoSession.getUserMenuBeanDao();
        return userMenuBeanDao.queryBuilder()
                .where(UserMenuBeanDao.Properties.Userid.eq(userid))
                .orderAsc(UserMenuBeanDao.Properties.Sort).list();
    }

    /*获取当前用户的子功能列表*/
    public List<UserMenuBean> getSubModuleMenuList(String userid, String main_code) {
        List<UserMenuBean> subModules = null;
        UserMenuBeanDao userMenuBeanDao = daoSession.getUserMenuBeanDao();
        UserMenuBean userMenuBean = userMenuBeanDao.queryBuilder()
                .where(UserMenuBeanDao.Properties.Userid.eq(userid),
                        UserMenuBeanDao.Properties.Code.eq(main_code)).unique();
        if (userMenuBean != null && userMenuBean.getId() != null) {
            String main_id = userMenuBean.getId();
            subModules = userMenuBeanDao.queryBuilder()
                    .where(UserMenuBeanDao.Properties.ParentId.eq(main_id)).list();
        }
        return subModules;
    }

    /**
     * 根据id获取图片
     */

    public TroublePictureBean getTroublePicture(String id) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        TroublePictureBean troubleBeens = troublePictureBeanDao.queryBuilder().
                where(TroublePictureBeanDao.Properties.Id.eq(id)).unique();
        return troubleBeens;
    }

    /**
     * 根据id获取语音
     */
    public TroubleAudioBean getTroubleAudio(String id) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        TroubleAudioBean troubleAudioBean = troubleAudioBeanDao.queryBuilder().
                where(TroubleAudioBeanDao.Properties.Id.eq(id)).unique();
        return troubleAudioBean;
    }

    /**
     * 根据id获附件
     * type  1:工地新建  2：工地过程管理  3：抢维修新建  4：抢维修过程管理
     */
    public BuildFileBean getBuildFile(String id, int type) {
        BuildFileBeanDao buildFileBeanDao = daoSession.getBuildFileBeanDao();
        BuildFileBean troubleBeens = null;
        if (type == 1) {
            troubleBeens = buildFileBeanDao.queryBuilder().
                    where(BuildFileBeanDao.Properties.Id.eq(id)).unique();
        } else if (type == 2) {
            troubleBeens = buildFileBeanDao.queryBuilder().
                    where(BuildFileBeanDao.Properties.Id.eq(id)).unique();
        } else if (type == 3) {
            troubleBeens = buildFileBeanDao.queryBuilder().
                    where(BuildFileBeanDao.Properties.Id.eq(id)).unique();
        } else if (type == 4) {
            troubleBeens = buildFileBeanDao.queryBuilder().
                    where(BuildFileBeanDao.Properties.Id.eq(id)).unique();
        }
        return troubleBeens;
    }

    /**
     * UploadPhotoBean
     */

    /**
     * 插入一个新的照片上报信息或者更新已存在的照片上报信息
     *
     * @param photoBean 新照片上报或者需要更新的照片上报信息
     */
    public void insertOrUpdateUploadPhoto(UploadPhotoBean photoBean) {
        daoSession.insertOrReplace(photoBean);
    }

    public UploadPhotoBean getUploadPhoto(String photo_id) {
        UploadPhotoBeanDao photoBeanDao = daoSession.getUploadPhotoBeanDao();
        return photoBeanDao.load(photo_id);
    }

    /**
     * 插入多个隐患或者更新已存在的隐患信息
     *
     * @param photoBeans 新隐患或者需要更新的隐患信息
     */
    public void insertOrUpdateUploadPhotos(List<UploadPhotoBean> photoBeans) {
        UploadPhotoBeanDao photoBeanDao = daoSession.getUploadPhotoBeanDao();
        photoBeanDao.insertOrReplaceInTx(photoBeans);
    }

    public List<UploadPhotoBean> getUploadPhotoList(String userid, int state) {
        UploadPhotoBeanDao photoBeanDao = daoSession.getUploadPhotoBeanDao();
        List<UploadPhotoBean> photoBeans = photoBeanDao.queryBuilder().
                where(UploadPhotoBeanDao.Properties.State.eq(state),
                        UploadPhotoBeanDao.Properties.Userid.eq(userid)).
                orderDesc(UploadPhotoBeanDao.Properties.FindTime).list();
        return photoBeans;
    }


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

    /**
     * TroublePictureBeanDao
     */

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

    public TroublePictureBean queryTroublePictureByUrl(String fileUrl) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        QueryBuilder<TroublePictureBean> queryBuilder = troublePictureBeanDao.queryBuilder();
        queryBuilder.where(TroublePictureBeanDao.Properties.Fileurl.eq(fileUrl));
        TroublePictureBean troublePictureBean = queryBuilder.unique();
        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 long insertOrUpdateBuildFiles(BuildFileBean fileBean) {
        return daoSession.insertOrReplace(fileBean);
    }

    //更新部门信息
    public long insertOrUpdateDepartList(DepartListBean departListBean) {
        return daoSession.insertOrReplace(departListBean);
    }

    //更新部门信息
    public long insertOrUpdateDepartUserList(DepartUserListBean departUserListBean) {
        return daoSession.insertOrReplace(departUserListBean);
    }

    public void insertTroublePicture(TroublePictureBean pictureBean) {
        TroublePictureBean localPicture = queryTroublePicture(pictureBean.getId());
        if (localPicture == null)
            daoSession.insert(pictureBean);
    }

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

    public void deleteSavedTroublePicture(String picture_id) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        troublePictureBeanDao.deleteByKey(picture_id);
    }

    public void deleteSavedTroublePictures(List<TroublePictureBean> troublePictureList) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        troublePictureBeanDao.deleteInTx(troublePictureList);
    }

    /**
     * TroubleAudioBean
     */

    /**
     * 根据录音id查询隐患录音
     *
     * @param audioId 隐患录音的id
     * @return 查询到的隐患录音
     */
    public TroubleAudioBean queryTroubleAudioById(String audioId) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        return troubleAudioBeanDao.load(audioId);
    }

    /**
     * 根据隐患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();
    }

    public List<TroubleAudioBean> queryTroubleAudios(String troubleId, int state) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        queryBuilder.where(TroubleAudioBeanDao.Properties.Troubleid.eq(troubleId),
                TroubleAudioBeanDao.Properties.Status.eq(state));
        return queryBuilder.list();
    }

    public void deleteTroubleAudios(String troubleId, int state) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        List<TroubleAudioBean> audioBeanList = queryBuilder.where(TroubleAudioBeanDao.Properties.Troubleid.eq(troubleId),
                TroubleAudioBeanDao.Properties.Status.eq(state)).list();
        if (audioBeanList != null)
            troubleAudioBeanDao.deleteInTx(audioBeanList);
    }

    /**
     * 根据隐患录音的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;
    }

    public TroubleAudioBean queryTroubleAudio1(String path) {
        TroubleAudioBean troubleAudioBean = null;
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        QueryBuilder<TroubleAudioBean> queryBuilder = troubleAudioBeanDao.queryBuilder();
        queryBuilder.where(TroubleAudioBeanDao.Properties.Path.eq(path));
        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 insertTroubleAudio(TroubleAudioBean audioBean) {
        TroubleAudioBean localAudio = queryTroubleAudioById(audioBean.getId());
        if (localAudio == null)
            daoSession.insert(audioBean);
    }

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

    public void deleteSavedTroubleAudio(String audio_id) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        troubleAudioBeanDao.deleteByKey(audio_id);
    }

    public void deleteSavedTroubleAudios(List<TroubleAudioBean> troubleAudioList) {
        TroubleAudioBeanDao troubleAudioBeanDao = daoSession.getTroubleAudioBeanDao();
        troubleAudioBeanDao.deleteInTx(troubleAudioList);
    }

    public void deleteBuildFiles(List<BuildFileBean> fileBeanList) {
        BuildFileBeanDao buildFileBeanDao = daoSession.getBuildFileBeanDao();
        buildFileBeanDao.deleteInTx(fileBeanList);
    }

    /**
     * GPSDataBeanDao
     */
    /*获取保存Gps数据列表，指定个数*/
    public List<GPSDataBean> getSavedGpsList(int count) {
        GPSDataBeanDao gpsDataBeanDao = daoSession.getGPSDataBeanDao();
        List<GPSDataBean> gpsDataBeanList = gpsDataBeanDao.queryBuilder().
                orderDesc(GPSDataBeanDao.Properties.Gpstime).limit(count).list();
        return gpsDataBeanList;
    }

    public long getSavedGpsCount() {
        GPSDataBeanDao gpsDataBeanDao = daoSession.getGPSDataBeanDao();
        return gpsDataBeanDao.queryBuilder().
                orderDesc(GPSDataBeanDao.Properties.Gpstime).count();
    }

    /*获取保存Gps数据列表，指定个数*/
    public GPSDataBean getSavedGpsById(String gpsId) {
        GPSDataBeanDao gpsDataBeanDao = daoSession.getGPSDataBeanDao();
        GPSDataBean gpsDataBean = gpsDataBeanDao.queryBuilder().
                where(GPSDataBeanDao.Properties.Gps_id.eq(gpsId)).unique();
        return gpsDataBean;
    }

    /*获取保存Gps数据列表,全部数据*/
    public List<GPSDataBean> getSavedGpsList() {
        GPSDataBeanDao gpsDataBeanDao = daoSession.getGPSDataBeanDao();
        List<GPSDataBean> gpsDataBeanList = gpsDataBeanDao.queryBuilder().
                orderDesc(GPSDataBeanDao.Properties.Gpstime).list();
        return gpsDataBeanList;
    }

    /**
     * 插入一个新的GPS数据
     *
     * @param gpsDataBean 新GPS数据
     * @return 数据表中的rowid
     */
    public long insertOrUpdateGPSData(GPSDataBean gpsDataBean) {
        return daoSession.insertOrReplace(gpsDataBean);
    }

    public void deleteAllSavedGps() {
        daoSession.deleteAll(GPSDataBean.class);
    }

    public void deleteSavedGps(List<GPSDataBean> gpsDataList) {
        GPSDataBeanDao gpsDataBeanDao = daoSession.getGPSDataBeanDao();
        gpsDataBeanDao.deleteInTx(gpsDataList);
    }

    /**
     * CheckGPSDataBeanDao
     */
    public CheckGPSDataBean getLastCheckGps() {
        CheckGPSDataBean lastCheckGps = null;
        String gps_id = DateUtils.getYMD();
        CheckGPSDataBeanDao checkGpsDataBeanDao = daoSession.getCheckGPSDataBeanDao();
        lastCheckGps = checkGpsDataBeanDao.queryBuilder().
                where(CheckGPSDataBeanDao.Properties.Gps_id.eq(gps_id)).unique();
        return lastCheckGps;
    }

    public CheckGPSDataBean getLastValidCheckGps() {
        CheckGPSDataBean lastValidCheckGps = null;
        String gps_id = String.format("%s_VALID", DateUtils.getYMD());
        CheckGPSDataBeanDao checkGpsDataBeanDao = daoSession.getCheckGPSDataBeanDao();
        lastValidCheckGps = checkGpsDataBeanDao.queryBuilder().
                where(CheckGPSDataBeanDao.Properties.Gps_id.eq(gps_id)).unique();
        return lastValidCheckGps;
    }

    public long saveLastCheckGps(CheckGPSDataBean checkGPSData) {
        String gps_id = DateUtils.getYMD();
        checkGPSData.setGps_id(gps_id);
        return daoSession.insertOrReplace(checkGPSData);
    }

    public long saveLastValidCheckGps(CheckGPSDataBean checkGPSData) {
        String gps_id = String.format("%s_VALID", DateUtils.getYMD());
        checkGPSData.setGps_id(gps_id);
        return daoSession.insertOrReplace(checkGPSData);
    }

    /**
     * DeviceInfoBeanDao
     */

    /*获取保存设备信息数据列表，指定个数*/
    public List<DeviceInfoBean> getSavedDeviceInfoList(int count) {
        DeviceInfoBeanDao deviceInfoBeanDao = daoSession.getDeviceInfoBeanDao();
        List<DeviceInfoBean> deviceInfoBeanList = deviceInfoBeanDao.queryBuilder().
                orderDesc(DeviceInfoBeanDao.Properties.Check_time).limit(count).list();
        return deviceInfoBeanList;
    }

    /*获取保存设备信息数据列表,全部数据*/
    public List<DeviceInfoBean> getSavedDeviceInfoList() {
        DeviceInfoBeanDao deviceInfoBeanDao = daoSession.getDeviceInfoBeanDao();
        List<DeviceInfoBean> deviceInfoBeanList = deviceInfoBeanDao.queryBuilder().
                orderDesc(DeviceInfoBeanDao.Properties.Check_time).list();
        return deviceInfoBeanList;
    }

    /**
     * 插入一个新的设备信息数据
     *
     * @param deviceInfoBean 新设备信息数据
     * @return 数据表中的rowid
     */
    public long insertOrUpdateDeviceInfo(DeviceInfoBean deviceInfoBean) {
        return daoSession.insertOrReplace(deviceInfoBean);
    }

    /**
     * 获取设备信息数据
     *
     * @param device_id 设备信息id
     * @return 设备信息数据
     */
    public DeviceInfoBean getDeviceInfo(String device_id) {
        DeviceInfoBeanDao deviceInfoBeanDao = daoSession.getDeviceInfoBeanDao();
        return deviceInfoBeanDao.queryBuilder().where(DeviceInfoBeanDao.Properties.Id.eq(device_id)).unique();
    }

    /**
     * 删除保存的设备信息数据
     */
    public void deleteSavedDeviceInfo(DeviceInfoBean deviceInfoBean) {
        daoSession.delete(deviceInfoBean);
    }


    /**
     * 删除所有保存的设备信息数据
     */
    public void deleteAllSavedDeviceInfo() {
        daoSession.deleteAll(DeviceInfoBean.class);
    }


    /**
     * 删除数据库中包含在此list中的所有设备信息数据
     *
     * @param deviceInfoBeanList 要删除的设备信息列表
     */
    public void deleteSavedDeviceInfo(List<DeviceInfoBean> deviceInfoBeanList) {
        DeviceInfoBeanDao deviceInfoBeanDao = daoSession.getDeviceInfoBeanDao();
        deviceInfoBeanDao.deleteInTx(deviceInfoBeanList);
    }

    /**
     * DownloadDataDao
     */
    public void insertPipeList(List<DownloadData> downloadDatas) {
        for (DownloadData d :
                downloadDatas) {
            daoSession.insertOrReplace(d);
        }
    }

    /*
     *   修改 下载状态
     * */
    public void updatePipeDbState(String id, int download_state) {

        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        DownloadData downloadData = ddd.queryBuilder().where(DownloadDataDao.Properties.Id.eq(id)).unique();
        downloadData.setDownload_state(download_state);
        ddd.update(downloadData);
    }

    /*根据 id 获取 下载状态
     *  4 正在下载
     *  1 已下载
     * 2 下载失败
     * 3 未下载
     * 0 未知状态
     * */
    public int isDownloadByMapId(String id) {
        int isDownloading;
        DownloadData unique = daoSession.getDownloadDataDao().queryBuilder().where(DownloadDataDao.Properties.Id.eq(id)).unique();
        if (unique == null) {
            isDownloading = 4;
        } else {
            isDownloading = unique.getDownload_state();
        }
        return isDownloading;

    }

    /*根据 id 获取 下载状态
     *  4 正在下载
     *  1 已下载
     * 2 下载失败
     * 3 未下载
     * 0 未知状态
     * */
    public boolean isDownloadMap() {
        boolean isDownloading = false;
        List<DownloadData> list = daoSession.getDownloadDataDao().queryBuilder().list();
        for (DownloadData dd :
                list) {
            int download_state = dd.getDownload_state();
            if (download_state == 4) {
                isDownloading = true;
                break;
            }
        }

        return isDownloading;

    }

    /*
     *   修改 下载时间
     * */
    public void updatePipeDb(String id, String time) {

        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        DownloadData downloadData = ddd.queryBuilder().where(DownloadDataDao.Properties.Id.eq(id)).unique();
        downloadData.setDownloadTime(time);
        downloadData.setDownload_state(1);
        ddd.update(downloadData);
    }

    /**
     * 更新工地类型
     */
    public void upBuildType(BuildTypeBean buildTypeBean) {
        daoSession.getBuildTypeBeanDao().insertOrReplace(buildTypeBean);
    }

    /**
     * 同步服务器地图列表数据
     *
     * @param list
     */
//    public void asyDownloadList(List<DownloadData> list, String userid) {
//        DownloadDataDao ddd = daoSession.getDownloadDataDao();
//        for (DownloadData dd : list) {
//            DownloadData unique = ddd.queryBuilder().where(DownloadDataDao.Properties.Id.eq(dd.getId())).unique();
//            if (unique == null) {
//                dd.setUserId(userid);
//                ddd.insertOrReplace(dd);
//            } else {
//                unique.setStatus(dd.getStatus());
//                unique.setUpdatetime(dd.getUpdatetime());
//                unique.setDatasize(dd.getDatasize());
//                unique.setUserId(userid);
//                ddd.update(unique);
//            }
//        }
//    }
    public void asyDownloadList1(List<DownloadData> list) {
        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        for (DownloadData dd : list) {
            dd.setDownload_state(2);
            ddd.insertOrReplace(dd);
        }
    }

    public void asyDownloadList(List<UpDownloadData> list) {
        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        for (UpDownloadData dd : list) {
            DownloadData unique = ddd.queryBuilder().where(DownloadDataDao.Properties.Cval.eq(dd.getFileName())).unique();
            if (unique != null) {
                unique.setCval(dd.getFileName());
                unique.setUpdatetime(dd.getCreateTime());
                if (TextUtils.isEmpty(unique.getDownloadTime())) {
                    unique.setDownload_state(2);
                } else {
                    boolean isdiffert = FileUtil.getTimeDifferent(unique.getUpdatetime(), unique.getDownloadTime());
                    if (isdiffert) {
                        unique.setDownload_state(2);
                    }
                }
                unique.setDatasize(dd.getSize());
                ddd.update(unique);
            }
        }
    }

    //刷新误删本地文件后文件状态
    public void asyDownloadData(DownloadData downloadData) {
        downloadData.setDownload_state(2);
        daoSession.getDownloadDataDao().update(downloadData);
    }

    /**
     * 查询所有地图下载列表数据
     *
     * @return
     */
//    public List<DownloadData> getDownloadList(String userid) {
//        DownloadDataDao ddd = daoSession.getDownloadDataDao();
//        return ddd.queryBuilder().where(DownloadDataDao.Properties.UserId.eq(userid)).list();
//    }
    public List<DownloadData> getDownloadList() {
        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        return ddd.queryBuilder().list();
    }

    public DownloadData getDownloadData(String dataId) {
        DownloadDataDao ddd = daoSession.getDownloadDataDao();
        return ddd.queryBuilder().where(DownloadDataDao.Properties.Id.eq(dataId)).unique();
    }

    /**
     * SystemConfigBeanDao
     */

    /**
     * 插入一个新的系统配置数据
     *
     * @param systemConfigBean 新配置数据
     * @return 数据表中的rowid
     */
    public long insertOrUpdateSysConfig(SystemConfigBean systemConfigBean) {
        return daoSession.insertOrReplace(systemConfigBean);
    }

    public SystemConfigBean getSysConfigValue(String code) {
        SystemConfigBeanDao systemConfigBeanDao = daoSession.getSystemConfigBeanDao();
        QueryBuilder<SystemConfigBean> queryBuilder = systemConfigBeanDao.queryBuilder();
        queryBuilder.where(SystemConfigBeanDao.Properties.Code.eq(code));
        SystemConfigBean systemConfigBean = queryBuilder.unique();
        return systemConfigBean;
    }

    /**
     * LocalPlanRecordBeanDao
     */

    /**
     * 插入一个新的隐患或者更新已存在的隐患信息
     *
     * @return 数据表中的rowid
     */
    public long insertOrUpdateLocalPlanRecord(LocalPlanRecordBean planRecord) {
        return daoSession.insertOrReplace(planRecord);
    }

    public LocalPlanRecordBean getLocalPlanRecord(String record_id) {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        return recordBeanDao.load(record_id);
    }

    public List<LocalPlanRecordBean> getLocalPlanRecordList() {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        List<LocalPlanRecordBean> recordBeens = recordBeanDao.queryBuilder().
                where(LocalPlanRecordBeanDao.Properties.Status.eq("-1")).
                orderDesc(LocalPlanRecordBeanDao.Properties.Create_time).list();
        return recordBeens;
    }

    public List<LocalPlanRecordBean> getLocalPlanRecordList(String planPointId, int state) {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        return recordBeanDao.queryBuilder().
                where(LocalPlanRecordBeanDao.Properties.Status.eq(state),
                        LocalPlanRecordBeanDao.Properties.Plan_Point_id.eq(planPointId)).
                orderDesc(LocalPlanRecordBeanDao.Properties.Create_time).list();
    }

    public List<LocalPlanRecordBean> getLocalPlanClearRecordList(String troubleid, String planid, int state) {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        return recordBeanDao.queryBuilder().
                where(LocalPlanRecordBeanDao.Properties.Status.eq(state),
                        LocalPlanRecordBeanDao.Properties.Trouble_id.eq(troubleid),
                        LocalPlanRecordBeanDao.Properties.Plan_id.eq(planid)).
                orderDesc(LocalPlanRecordBeanDao.Properties.Create_time).list();
    }

    public LocalPlanRecordBean getLastLocalPlanClearRecord(String troubleid, String planid) {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        return recordBeanDao.queryBuilder().
                where(LocalPlanRecordBeanDao.Properties.Status.eq("-2"),
                        LocalPlanRecordBeanDao.Properties.Trouble_id.eq(troubleid),
                        LocalPlanRecordBeanDao.Properties.Plan_id.eq(planid)).
                orderDesc(LocalPlanRecordBeanDao.Properties.Create_time).limit(1).unique();
    }

    public void deleteSavedLocalPlanRecord(String recordId) {
        LocalPlanRecordBeanDao recordBeanDao = daoSession.getLocalPlanRecordBeanDao();
        QueryBuilder<LocalPlanRecordBean> queryBuilder = recordBeanDao.queryBuilder();
        queryBuilder.where(LocalPlanRecordBeanDao.Properties.Id.eq(recordId));
        LocalPlanRecordBean recordBean = queryBuilder.unique();
        if (recordBean != null)
            daoSession.delete(recordBean);
    }

    /**
     * PlanPointRepairDao
     */

    /**
     * 插入一个新的或者更新已存在的设备检修数据
     *
     * @return 数据表中的rowid
     */
    public long insertOrUpdateRepairRecord(PlanPointRepair pointRepair) {
        return daoSession.insertOrReplace(pointRepair);
    }

    public void insertOrUpdateRepairRecords(List<PlanPointRepair> pointRepairs) {
        PlanPointRepairDao repairDao = daoSession.getPlanPointRepairDao();
        repairDao.insertInTx(pointRepairs);
    }

    public List<PlanPointRepair> getLocalRepairRecordList() {
        PlanPointRepairDao repairDao = daoSession.getPlanPointRepairDao();
        List<PlanPointRepair> recordBeens = repairDao.queryBuilder().
                where(PlanPointRepairDao.Properties.State.eq("-1")).
                orderDesc(PlanPointRepairDao.Properties.Create_time).list();
        return recordBeens;
    }

    public List<PlanPointRepair> getLocalRepairRecordList(String plan_id, String point_id) {
        PlanPointRepairDao repairDao = daoSession.getPlanPointRepairDao();
        List<PlanPointRepair> recordBeens = repairDao.queryBuilder().
                where(PlanPointRepairDao.Properties.Plan_id.eq(plan_id),
                        PlanPointRepairDao.Properties.Point_id.eq(point_id)).
                orderDesc(PlanPointRepairDao.Properties.Create_time).list();
        return recordBeens;
    }

    public PlanPointRepair getLocalRepairInfo(String temp_id) {
        PlanPointRepairDao repairDao = daoSession.getPlanPointRepairDao();
        PlanPointRepair recordBeen = repairDao.queryBuilder().
                where(PlanPointRepairDao.Properties.Temp_id.eq(temp_id)).unique();
        return recordBeen;
    }

    public PlanPointRepair getLocalRepairInfo(String plan_id, String point_id) {
        PlanPointRepairDao repairDao = daoSession.getPlanPointRepairDao();
        PlanPointRepair recordBeen = repairDao.queryBuilder().
                where(PlanPointRepairDao.Properties.Plan_id.eq(plan_id),
                        PlanPointRepairDao.Properties.Point_id.eq(point_id)).unique();
        return recordBeen;
    }

    public void deleteLocalRepairRecord(String recordId) {
        PlanPointRepairDao recordBeanDao = daoSession.getPlanPointRepairDao();
        QueryBuilder<PlanPointRepair> queryBuilder = recordBeanDao.queryBuilder();
        queryBuilder.where(PlanPointRepairDao.Properties.Id.eq(recordId));
        PlanPointRepair recordBean = queryBuilder.unique();
        if (recordBean != null)
            daoSession.delete(recordBean);
    }

    public void deleteLocalRepairRecord(PlanPointRepair pointRepair) {
        PlanPointRepairDao recordBeanDao = daoSession.getPlanPointRepairDao();
        QueryBuilder<PlanPointRepair> queryBuilder = recordBeanDao.queryBuilder();
        queryBuilder.where(PlanPointRepairDao.Properties.Temp_id.eq(pointRepair.getTemp_id()));
        PlanPointRepair recordBean = queryBuilder.unique();
        if (recordBean != null)
            daoSession.delete(recordBean);
    }

    /**
     * SignDataBeanDao
     */
    public void insertOrUpdateSignOn(SignDataBean dataBean) {
        daoSession.insertOrReplace(dataBean);
    }

    public SignDataBean getTodaySignOn() {
        SignDataBean signOnData = null;
        SignDataBeanDao signDao = daoSession.getSignDataBeanDao();
        String todayDate = DateUtils.getYMD();
        signOnData = signDao.queryBuilder().where(SignDataBeanDao.Properties.PlanDate.eq(todayDate)).unique();
        return signOnData;
    }

    public void deleteSignOn() {
        daoSession.deleteAll(SignDataBean.class);
    }

    /**
     * SignHistoryDataBeanDao
     */

    public void saveSignHistoryData(SignHistoryDataBean dataBean) {
        daoSession.insertOrReplace(dataBean);
    }

    public void deleteSignHistoryData(SignHistoryDataBean dataBean) {
        daoSession.delete(dataBean);
    }

    public SignHistoryDataBean getSignHistoryData(String sign_id) {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        SignHistoryDataBean signHistory = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Id.eq(sign_id)).unique();
        return signHistory;
    }


    public List<SignHistoryDataBean> getSignHistorys(String userid) {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        List<SignHistoryDataBean> signHistorys = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Userid.eq(userid)).list();
        return signHistorys;
    }

    public List<SignHistoryDataBean> getUnuploadSignHistorys(String userid) {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        List<SignHistoryDataBean> signHistorys = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Userid.eq(userid),
                        SignHistoryDataBeanDao.Properties.IsUpload.eq(0)).
                orderDesc(SignHistoryDataBeanDao.Properties.SignTime).list();
        return signHistorys;
    }

    public SignHistoryDataBean getLastSignHistory(String userid) {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        SignHistoryDataBean signHistory = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Userid.eq(userid)).
                orderDesc(SignHistoryDataBeanDao.Properties.SignTime).limit(1).unique();
        return signHistory;
    }

    public SignHistoryDataBean getSignHistoryToday(String userid, String sign_time, int sign_type) {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        SignHistoryDataBean signHistory = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Userid.eq(userid),
                        SignHistoryDataBeanDao.Properties.SignTime.eq(sign_time),
                        SignHistoryDataBeanDao.Properties.SignType.eq(sign_type))
                .orderAsc(SignHistoryDataBeanDao.Properties.SignTime).unique();
        return signHistory;
    }

    public List<SignHistoryDataBean> getSignHistorysToday(String userid) {
        String minToday = DateUtils.getTodayMinTime();
        String maxToday = DateUtils.getTodayMaxTime();
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        List<SignHistoryDataBean> signHistorys = signDao.queryBuilder().
                where(SignHistoryDataBeanDao.Properties.Userid.eq(userid),
                        SignHistoryDataBeanDao.Properties.SignTime.between(minToday, maxToday))
                .orderAsc(SignHistoryDataBeanDao.Properties.SignTime).list();
        return signHistorys;
    }

    // 清除历史保存的签到记录(最多保存45天)
    public void clearSignHistoryData() {
        SignHistoryDataBeanDao signDao = daoSession.getSignHistoryDataBeanDao();
        String sevenDaysAgoYMD = DateUtils.getDaysAgoYMD(BaseApp.GPS_SAVED_DAY_MAX);
        List<SignHistoryDataBean> signList = signDao.queryBuilder().where(SignHistoryDataBeanDao
                .Properties.SignTime.lt(sevenDaysAgoYMD)).list();
        if (signList != null && !signList.isEmpty()) {
            signDao.deleteInTx(signList);
        }
    }

    /**
     * OfflinePathBeanDao
     */
    public void insertOfflinePathList(String userid, List<OfflinePathBean> pathList) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        if (pathList == null || pathList.size() == 0) {
            /*没有新计划，旧计划全部设为过期*/
            List<OfflinePathBean> pastPaths = pathBeanDao.queryBuilder().
                    where(OfflinePathBeanDao.Properties.Userid.eq(userid),
                            OfflinePathBeanDao.Properties.Ispast.eq(0)).list();
            for (OfflinePathBean path : pastPaths) {
                path.setIspast(1);
            }
            pathBeanDao.updateInTx(pastPaths);
        } else {
            /*有新计划，不在新计划中的旧计划全部设为过期*/
            /*List<String> validPathIds = new ArrayList<>();
            for (OfflinePathBean pathBean : pathList){
                validPathIds.add(pathBean.getTemp_id());
            }
            List<OfflinePathBean> pastPaths = pathBeanDao.queryBuilder().
                    where(OfflinePathBeanDao.Properties.Temp_id.notIn(validPathIds),
                            OfflinePathBeanDao.Properties.Ispast.eq(0)).list();
            for (OfflinePathBean path : pastPaths){
                path.setIspast(1);
            }
            pathBeanDao.updateInTx(pastPaths);*/
            List<OfflinePathBean> pastPaths = pathBeanDao.queryBuilder().
                    where(OfflinePathBeanDao.Properties.Userid.eq(userid),
                            OfflinePathBeanDao.Properties.Ispast.eq(0)).list();
            for (OfflinePathBean path : pastPaths) {
                path.setIspast(1);
            }
            pathBeanDao.updateInTx(pastPaths);
            /*插入新的计划*/
            for (OfflinePathBean offlinePath : pathList) {
                if (offlinePath.getTemp_id() != null && !offlinePath.getTemp_id().isEmpty()) {
                    OfflinePathBean localPath = pathBeanDao.queryBuilder().where(
                            OfflinePathBeanDao.Properties.Userid.eq(userid),
                            OfflinePathBeanDao.Properties.Temp_id.eq(offlinePath.getTemp_id())).unique();
                    if (localPath == null) {
                        if (offlinePath.getIsover() == 1) {
                            offlinePath.setIsupload(1);
                        }
                        offlinePath.setIspast(0);
                        daoSession.insert(offlinePath);
                    } else {
                        if (offlinePath.getIsover() == 1) {
                            localPath.setIsover(1);
                            localPath.setIsupload(1);
                        } else {
                            localPath.setIsupload(0);
                        }
                        localPath.setIspast(0);
                        daoSession.insertOrReplace(localPath);
                    }
                }
            }
        }
    }

    public void insertOfflinePathListWithAll(String userid, List<OfflinePathBean> pathList) {
        if (pathList != null && pathList.size() > 0) {
            for (OfflinePathBean pathBean : pathList) {
                pathBean.setUserid(userid);
                if (pathBean.getIsover() == OfflinePathBean.POINTLINE_IS_OVER) {
                    pathBean.setIsupload(1);
                }
            }
            OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
            pathBeanDao.insertOrReplaceInTx(pathList);
        }
    }

    public void insertOfflinePathListWithAll(String userid, List<OfflinePathBean> pathList, int pageindex) {
        if (pathList != null && pathList.size() > 0) {
            for (OfflinePathBean pathBean : pathList) {
                pathBean.setUserid(userid);
                pathBean.setPageindex(pageindex);
                if (pathBean.getIsover() == OfflinePathBean.POINTLINE_IS_OVER) {
                    pathBean.setIsupload(1);
                }
            }
            OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
            pathBeanDao.insertOrReplaceInTx(pathList);
        }
    }

    public long getLocalPathSize(String userid, int planMode) {
        long pathSize = 0;
        List<OfflinePathBean> pathBeanList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathSize = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Plan_mode.eq(planMode)).count();
        return pathSize;
    }

    public void updateLocalPointsFromServerOver(List<String> pointIds) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        if (pointIds != null && pointIds.size() > 0) {
            for (String ids : pointIds) {
                OfflinePathBean pathBean = pathBeanDao.queryBuilder().where(OfflinePathBeanDao.Properties.Temp_id.eq(ids)).unique();
                if (pathBean != null) {
                    pathBean.setIsover(1);
                    pathBean.setIsupload(1);
                    pathBean.setCheck_time("-1");
                    pathBeanDao.update(pathBean);
                }
            }
        }
    }

    public List<OfflinePathBean> getNotOverPathList(String userid, int planMode) {
        List<OfflinePathBean> pathBeanList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(0),
                OfflinePathBeanDao.Properties.Plan_mode.eq(planMode)).list();
        return pathBeanList;
    }

    public List<OfflinePathBean> getCurrentOverPathList(String userid, double xMin, double xMax, double yMin, double yMax) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"ISPAST\"=? AND T.\"ISOVER\"=? AND T.\"USERID\"=? AND T.\"LNG\">=? AND T.\"LNG\"<=? AND T.\"LAT\">=? AND T.\"LAT\"<=? ",
                new String[]{"0", "0", String.valueOf(userid), String.valueOf(xMin), String.valueOf(xMax), String.valueOf(yMin), String.valueOf(yMax)});
        return lineList;
    }

    public List<OfflinePathBean> getCurrentOverPathListByPlan(String userid,String planId, double xMin, double xMax, double yMin, double yMax) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"ISPAST\"=? AND T.\"ISOVER\"=? AND T.\"PLAN_ID\"=? AND T.\"USERID\"=? AND T.\"LNG\">=? AND T.\"LNG\"<=? AND T.\"LAT\">=? AND T.\"LAT\"<=? ",
                new String[]{"0", "0", String.valueOf(planId), String.valueOf(userid), String.valueOf(xMin), String.valueOf(xMax), String.valueOf(yMin), String.valueOf(yMax)});
        return lineList;
    }

    public List<OfflinePathBean> getCurrentOverPathList(String userid, int planMode, double xMin, double xMax, double yMin, double yMax) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"ISPAST\"=? AND T.\"ISOVER\"=? AND T.\"PLAN_MODE\"=? AND T.\"USERID\"=? AND T.\"LNG\">=? AND T.\"LNG\"<=? AND T.\"LAT\">=? AND T.\"LAT\"<=? ",
                new String[]{"0", "0", String.valueOf(planMode), String.valueOf(userid), String.valueOf(xMin), String.valueOf(xMax), String.valueOf(yMin), String.valueOf(yMax)});
        return lineList;
    }

    public List<OfflinePathBean> getPathListByPathId(String userid, String pathId) {
        List<OfflinePathBean> pathBeanList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Path_id.eq(pathId))
                .orderAsc(OfflinePathBeanDao.Properties.Order).list();
        return pathBeanList;
    }

    /*获取巡检线已到位且点离开时间为空的点*/
    public List<OfflinePathBean> getOverPathLineListForNullOffTime(String userid, int planmode) {
        List<OfflinePathBean> pathBeanList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Type.eq(OfflinePathBean.PATH_TYPE_LINE),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER),
                OfflinePathBeanDao.Properties.Plan_mode.eq(planmode),
                OfflinePathBeanDao.Properties.Check_time.isNotNull(),
                OfflinePathBeanDao.Properties.First_left_time.isNull()).list();
        return pathBeanList;
    }

    public List<OfflinePathBean> getUnUploadPathPoint(String userid, int planMode, int total_limit) {
        List<OfflinePathBean> unUPloadPathList;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        QueryBuilder<OfflinePathBean> builder = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER),
                OfflinePathBeanDao.Properties.Isupload.eq(0));
        if (total_limit > 0) {
            builder = builder.limit(total_limit);
        }
        unUPloadPathList = builder.list();
        return unUPloadPathList;
    }

    public List<OfflinePathBean> getUnUploadPathLine(String userid, int planMode) {
        List<OfflinePathBean> unUPloadPathList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        unUPloadPathList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER),
                OfflinePathBeanDao.Properties.Isupload.eq(0),
                OfflinePathBeanDao.Properties.Type.eq(1),
                OfflinePathBeanDao.Properties.Plan_mode.eq(planMode)).list();
        return unUPloadPathList;
    }

    public long getUnUploadPointsCount(String userid, int planMode) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        return pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER),
                OfflinePathBeanDao.Properties.Isupload.eq(0)/*,
                OfflinePathBeanDao.Properties.Plan_mode.eq(planMode)*/).count();
    }

    public boolean hasCarModePlan(String userid) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        long count = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                PlanBeanDao.Properties.Plan_mode.eq(1)).count();
        return count > 0;
    }

    public List<OfflinePathBean> getUploadPathLine(String userid, int planMode) {
        List<OfflinePathBean> unUPloadPathList = null;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        unUPloadPathList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Userid.eq(userid),
                OfflinePathBeanDao.Properties.Ispast.eq(0),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER),
                OfflinePathBeanDao.Properties.Isupload.eq(1),
                OfflinePathBeanDao.Properties.Type.eq(1),
                OfflinePathBeanDao.Properties.Plan_mode.eq(planMode)).list();
        return unUPloadPathList;
    }

    public OfflinePathBean getPlanOfflinePath(String temp_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        OfflinePathBean pathBean = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Temp_id.eq(temp_id)).unique();
        return pathBean;
    }

    public OfflinePathBean getPlanOfflinePath(String point_id, String plan_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        OfflinePathBean pathBean = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Point_id.eq(point_id),
                OfflinePathBeanDao.Properties.Plan_id.eq(plan_id)).unique();
        return pathBean;
    }

    public List<OfflinePathBean> getPlanOfflinePathList(String plan_id, String Path_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                OfflinePathBeanDao.Properties.Path_id.eq(Path_id))
                .orderAsc(OfflinePathBeanDao.Properties.Order).list();
        return lineList;
    }

    public List<OfflinePathBean> getPlanOfflinePathList(String plan_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Plan_id.eq(plan_id)).list();
        return lineList;
    }

    public List<OfflinePathBean> getPlanOfflinePathListOrderByPathId(String plan_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Plan_id.eq(plan_id))
                .orderAsc(OfflinePathBeanDao.Properties.Path_id).list();
        return lineList;
    }

    public List<OfflinePathBean> getPlanOfflinePathOverListOrderByPathId(String plan_id) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryBuilder().where(
                OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER))
                .orderAsc(OfflinePathBeanDao.Properties.Path_id).list();
        return lineList;
    }

    public List<OfflinePathBean> getPlanOfflinePathListInRegion(String plan_id, double xMin, double xMax, double yMin, double yMax) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where  T.\"PLAN_ID\"=? AND T.\"LNG\">=? AND T.\"LNG\"<=? AND T.\"LAT\">=? AND T.\"LAT\"<=? ORDER BY T.\"PATH_ID\" ASC",
                new String[]{String.valueOf(plan_id), String.valueOf(xMin), String.valueOf(xMax), String.valueOf(yMin), String.valueOf(yMax)});
        return lineList;
    }

    /*获取单个巡检线计划的所有的线的id-在范围内*/
    public List<String> getPlanOfflinePathIdInRegion(String plan_id, double xMin, double xMax, double yMin, double yMax) {
        List<String> pathIds = new ArrayList<>();
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"PLAN_ID\"=? AND T.\"LNG\">=? AND T.\"LNG\"<=? AND T.\"LAT\">=? AND T.\"LAT\"<=? GROUP BY T.\"PATH_ID\"",
                new String[]{String.valueOf(plan_id), String.valueOf(xMin), String.valueOf(xMax), String.valueOf(yMin), String.valueOf(yMax)});
        if (lineList != null) {
            for (OfflinePathBean pathBean : lineList) {
                String path_id = pathBean.getPath_id();
                pathIds.add(path_id);
            }
        }
        return pathIds;
    }

    /*获取单个巡检线计划的所有的线的id*/
    public List<String> getPlanOfflinePathId(String plan_id) {
        List<String> pathIds = new ArrayList<>();
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"PLAN_ID\"=? GROUP BY T.\"PATH_ID\"", new String[]{String.valueOf(plan_id)});
        if (lineList != null) {
            for (OfflinePathBean pathBean : lineList) {
                String path_id = pathBean.getPath_id();
                pathIds.add(path_id);
            }
        }
        return pathIds;
    }

    public List<OfflinePathBean> getPlanPathListGroupByPathId(String userid, String childplanid, int type) {
        Map<String, List<OfflinePathBean>> pathMap = new HashMap<>();
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> lineList = pathBeanDao.queryRaw(" where T.\"ISPAST\"=? AND T.\"TYPE\"=? AND T.\"CHILD_PLAN_ID\"=? GROUP BY T.\"PATH_ID\"", new String[]{"0", String.valueOf(type), String.valueOf(childplanid)});
        /*if (lineList != null){
            for (OfflinePathBean pathBean : lineList){
                String path_id = pathBean.getPath_id();
                List<OfflinePathBean> pathBeanList = getPathListByPathId(userid, path_id);
                pathMap.put(path_id, pathBeanList);
            }
        }
        return pathMap;*/
        return lineList;
    }

    public void updateOfflinePath(OfflinePathBean path) {
        daoSession.update(path);
    }

    public void updateOfflinePaths(List<OfflinePathBean> paths) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanDao.updateInTx(paths);
    }

    public void deletPlanPathBean(OfflinePathBean pathBean) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanDao.delete(pathBean);
    }

    public void deletPlanPathBean(List<OfflinePathBean> pathBeans) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        pathBeanDao.deleteInTx(pathBeans);
    }

    public void deletPlanPathBeanById(List<String> pathBeanIds) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        String pathIds = StringUtil.listToString(pathBeanIds, ',');
        if (pathIds != null && !pathIds.isEmpty()) {
            List<OfflinePathBean> pastPaths = pathBeanDao.queryBuilder().
                    where(OfflinePathBeanDao.Properties.Temp_id.in(pathIds)).list();
            if (pastPaths != null && pastPaths.size() > 0) {
                pathBeanDao.deleteInTx(pastPaths);
            }
        }
    }

    // 清除已过期的计划的离线点
    public void clearOfflinePathPastDeadline(String userid) {
        String now = DateUtils.getDate(new Date().getTime());
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> pastList = pathBeanDao.queryBuilder().
                where(OfflinePathBeanDao.Properties.Userid.eq(userid),
                        OfflinePathBeanDao.Properties.Child_end_date.lt(now)).list();
        if (pastList != null && !pastList.isEmpty()) {
            pathBeanDao.deleteInTx(pastList);
        }
    }

    // 测试用：删除计划的离线点
    public void clearOfflinePathTest(String userid) {
        String now = DateUtils.getDate(new Date().getTime());
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> pastList = pathBeanDao.queryBuilder().
                where(OfflinePathBeanDao.Properties.Userid.eq(userid),
                        OfflinePathBeanDao.Properties.Ispast.eq(0)).limit(100).list();
        if (pastList != null && !pastList.isEmpty()) {
            pathBeanDao.deleteInTx(pastList);
        }
    }

    /*获取本地所有离线点的计划id*/
    public List<String> getOfflinePathPlanIds(String userid, int plan_mode) {
        List<String> planIds = new ArrayList<>();
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
/*        List<OfflinePathBean> list = pathBeanDao.queryBuilder()
                .where(OfflinePathBeanDao.Properties.Plan_mode.eq(plan_mode),
                        OfflinePathBeanDao.Properties.Userid.eq(userid),
                        OfflinePathBeanDao.Properties.Ispast.eq(0)).distinct().list();*/

        List<OfflinePathBean> list = pathBeanDao.queryRaw(" where T.\"ISPAST\"=? AND T.\"USERID\"=? GROUP BY T.\"PLAN_ID\" ",
                new String[]{"0", /*String.valueOf(plan_mode),*/ String.valueOf(userid)});
        for (OfflinePathBean pathBean : list) {
            String planid = pathBean.getPlan_id();
            if (!planIds.contains(planid)) {
                planIds.add(planid);
            }
        }
        return planIds;
    }

    /*获取本地某个巡检计划已下载的离线点的个数*/
    public long getOfflinePathPlanCounts(String planId) {
        long count = 0;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        count = pathBeanDao.queryBuilder()
                .where(OfflinePathBeanDao.Properties.Plan_id.eq(planId)).count();
        return count;
    }

    /*获取本地某个巡检计划已下载的离线点的个数*/
    public long getOfflinePathPlanCounts(String planId, int pageindex) {
        long count = 0;
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        count = pathBeanDao.queryBuilder()
                .where(OfflinePathBeanDao.Properties.Plan_id.eq(planId),
                        OfflinePathBeanDao.Properties.Pageindex.eq(pageindex)).count();
        return count;
    }

    /**
     * 根据计划Id和设备的rfid标识查找某个离线点
     *
     * @param planId
     * @param rfid
     * @return
     */
    public OfflinePathBean getOfflinePathByRfid(String planId, String rfid) {
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        OfflinePathBean pathBean = pathBeanDao.queryBuilder().where(OfflinePathBeanDao.Properties.Plan_id.eq(planId),
                OfflinePathBeanDao.Properties.Rfid.eq(rfid)).unique();
        return pathBean;
    }

    // 清除本地计划所有的离线点
    public void clearAllLocalOfflinePath(String userid, int plan_mode) {
        String now = DateUtils.getDate(new Date().getTime());
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        List<OfflinePathBean> pastList = pathBeanDao.queryBuilder().
                where(/*OfflinePathBeanDao.Properties.Plan_mode.eq(plan_mode),*/
                        OfflinePathBeanDao.Properties.Userid.eq(userid)).list();
        if (pastList != null && !pastList.isEmpty()) {
            pathBeanDao.deleteInTx(pastList);
        }
    }

    /*{
        OfflinePathBeanDao pathBeanDao = daoSession.getOfflinePathBeanDao();
        final String SQL_CHILD_PLAN = "SELECT DISTINCT "+ OfflinePathBeanDao.Properties.Child_plan_id.columnName +
                " FROM "+ OfflinePathBeanDao.TABLENAME + " WHERE " + OfflinePathBeanDao.Properties.Plan_mode.eq(plan_mode);
        pathBeanDao.getDatabase().rawQuery()

    }*/

    /**
     * MapUrlConfigDataBeanDao
     */
    public void insertOrUpdateMapUrlConfig(MapUrlConfigDataBean urlConfigDataBean) {
        daoSession.insertOrReplace(urlConfigDataBean);
    }

    public MapUrlConfigDataBean getMapUrlConfig(String userid) {
        MapUrlConfigDataBeanDao mapUrlConfigDataBeanDao = daoSession.getMapUrlConfigDataBeanDao();
        MapUrlConfigDataBean mapUrlConfigDataBean = mapUrlConfigDataBeanDao.queryBuilder().where(MapUrlConfigDataBeanDao.Properties.UserId.eq(userid)).unique();
        return mapUrlConfigDataBean;
    }

    /**
     * UserMapDataBeanDao
     */
    public void deleteUserMapDataList(String userid) {
        UserMapDataBeanDao userMapDataBeanDao = daoSession.getUserMapDataBeanDao();
        List<UserMapDataBean> userMapDataBeanList = userMapDataBeanDao.queryBuilder().where(UserMapDataBeanDao.Properties.UserId.eq(userid)).list();
        if (userMapDataBeanList != null && userMapDataBeanList.size() > 0) {
            userMapDataBeanDao.deleteInTx(userMapDataBeanList);
        }
    }

    public void saveUserMapDataList(List<UserMapDataBean> userMapDataBeanList, String userid) {
        if (userMapDataBeanList != null && userid != null && !userid.isEmpty()) {
            for (UserMapDataBean userMapDataBean : userMapDataBeanList) {
                String id = UUID.randomUUID().toString();
                userMapDataBean.setId(id);
                userMapDataBean.setUserId(userid);
                daoSession.insert(userMapDataBean);
            }
        }
    }

    public List<UserMapDataBean> getUserMapDataList(String userId) {
        QueryBuilder<UserMapDataBean> where = daoSession.getUserMapDataBeanDao()
                .queryBuilder().where(UserMapDataBeanDao.Properties
                        .UserId.eq(userId));
        return where.list();
    }

    /**
     * FieldsBeanDao
     */

    public void deleteMapFieldConfigList() {
        daoSession.deleteAll(FieldsBean.class);
    }

    public void saveMapFieldConfigList(List<FieldsBean> fieldConfigList) {
        if (fieldConfigList != null) {
            deleteMapFieldConfigList();
            FieldsBeanDao fieldsBeanDao = daoSession.getFieldsBeanDao();
            fieldsBeanDao.insertOrReplaceInTx(fieldConfigList);
        }
    }

    public List<FieldsBean> getMapFieldConfigList(String key) {
        FieldsBeanDao fieldsBeanDao = daoSession.getFieldsBeanDao();
        return fieldsBeanDao.queryBuilder()
                .where(FieldsBeanDao.Properties.Key.eq(key))
                .orderAsc(FieldsBeanDao.Properties.Sort).list();
    }


    /**
     * UserOnlineMapDataBeanDao
     */
    public void deleteUserOnlineMapDataList(String userid) {
        UserOnlineMapDataBeanDao userMapDataBeanDao = daoSession.getUserOnlineMapDataBeanDao();
        List<UserOnlineMapDataBean> userMapDataBeanList =
                userMapDataBeanDao.queryBuilder().where(UserOnlineMapDataBeanDao.Properties.Userid.eq(userid)).list();
        if (userMapDataBeanList != null && userMapDataBeanList.size() > 0) {
            userMapDataBeanDao.deleteInTx(userMapDataBeanList);
        }
    }

    public void saveUserOnlineMapDataList(List<UserOnlineMapDataBean> userMapDataBeanList, String userid) {
        if (userMapDataBeanList != null && userid != null && !userid.isEmpty()) {
            for (UserOnlineMapDataBean userMapDataBean : userMapDataBeanList) {
                userMapDataBean.setUserid(userid);
                daoSession.insert(userMapDataBean);
            }
        }
    }

    public List<UserOnlineMapDataBean> getUserOnlineMapDataList(String userId) {
        QueryBuilder<UserOnlineMapDataBean> where = daoSession.getUserOnlineMapDataBeanDao()
                .queryBuilder().where(UserOnlineMapDataBeanDao.Properties.Userid.eq(userId));
        return where.list();
    }

    public UserOnlineMapDataBean getUserOnlineMapData(String userId, String code) {
        QueryBuilder<UserOnlineMapDataBean> where = daoSession.getUserOnlineMapDataBeanDao()
                .queryBuilder().where(
                        UserOnlineMapDataBeanDao.Properties.Userid.eq(userId),
                        UserOnlineMapDataBeanDao.Properties.Code.eq(code));
        return where.unique();
    }


    /**
     * LocationGPSDataBeanDao
     */
// 保存签到后的轨迹点
    public long saveLocationGPSData(LocationGPSDataBean locationGPSDataBean) {
        return daoSession.insert(locationGPSDataBean);
    }

    public LocationGPSDataBean getLastLocationGPSDataBean(String day) {
        return daoSession.queryBuilder(LocationGPSDataBean.class).where(LocationGPSDataBeanDao
                .Properties.Day.eq(day)).unique();
    }

    // 获取当日所有的轨迹点
    public List<LocationGPSDataBean> getTodayLocationGPSData(String day) {
        return daoSession.queryBuilder(LocationGPSDataBean.class).where(LocationGPSDataBeanDao
                .Properties.Day.eq(day)).list();
    }

    // 根据分段获取当日用户的轨迹点
    public List<LocationGPSDataBean> getTodayLocationGPSData(long section) {
        return daoSession.queryBuilder(LocationGPSDataBean.class)
                .where(LocationGPSDataBeanDao.Properties.Day.eq(DateUtils.getYMD()),
                        LocationGPSDataBeanDao.Properties.Section.eq(section),
                        LocationGPSDataBeanDao.Properties.IsValid.notEq(0))
                .orderAsc(LocationGPSDataBeanDao.Properties.Date,
                        LocationGPSDataBeanDao.Properties.Gps_time)
                .list();
    }

    // 根据分段获取当前日期用户的轨迹点
    public List<LocationGPSDataBean> getLocationGPSData(String date, long section) {
        return daoSession.queryBuilder(LocationGPSDataBean.class)
                .where(LocationGPSDataBeanDao.Properties.Day.eq(date),
                        LocationGPSDataBeanDao.Properties.Section.eq(section),
                        LocationGPSDataBeanDao.Properties.IsValid.notEq(0))
                .orderAsc(LocationGPSDataBeanDao.Properties.Date,
                        LocationGPSDataBeanDao.Properties.Gps_time)
                .list();
    }

    // 清除历史保存的轨迹(最多保存45天)
    public void clearLocationGPSData() {
        LocationGPSDataBeanDao locationGPSDataBeanDao = daoSession.getLocationGPSDataBeanDao();
        String sevenDaysAgoYMD = DateUtils.getDaysAgoYMD(BaseApp.GPS_SAVED_DAY_MAX);
        List<LocationGPSDataBean> list = locationGPSDataBeanDao.queryBuilder().where(LocationGPSDataBeanDao
                .Properties.Day.lt(sevenDaysAgoYMD)).list();
        if (list != null && !list.isEmpty()) {
            locationGPSDataBeanDao.deleteInTx(list);
        }
    }

    /**
     * BuildProjectTypeBeanDao  工地发现，工地监护模块
     */

    /*保存服务器的工地类型列表数据*/
    public void saveBuildProjectTypes(List<BuildProjectTypeBean> buildTypes) {
        BuildProjectTypeBeanDao buildProjectTypeBeanDao = daoSession.getBuildProjectTypeBeanDao();
        buildProjectTypeBeanDao.insertOrReplaceInTx(buildTypes);
    }

    /*读取本地保存的工地类型列表*/
    public List<BuildProjectTypeBean> getBuildProjectTypeList() {
        BuildProjectTypeBeanDao buildProjectTypeBeanDao = daoSession.getBuildProjectTypeBeanDao();
        return buildProjectTypeBeanDao.queryBuilder().list();
    }

    /*读取本地保存的工地类型列表*/
    public List<BuildTypeBean> getBuildTypeList() {
        BuildTypeBeanDao buildProjectTypeBeanDao = daoSession.getBuildTypeBeanDao();
        return buildProjectTypeBeanDao.queryBuilder().list();
    }

    /*删除所有本地保存的工地类型列表*/
    public void deleteAllBuildProjectTypes() {
        daoSession.deleteAll(BuildProjectTypeBean.class);
    }

    /**
     * BuildSignDataBeanDao
     */
    public void insertOrUpdateBuildSignData(BuildSignDataBean dataBean) {
        daoSession.insertOrReplace(dataBean);
    }

    /*设置是否重新获取工地监护列表*/
    public void updateBuildSignState(int planState) {
        BuildSignDataBean buildSignData = getTodayBuildSignData();
        if (buildSignData != null) {
            buildSignData.setPlanState(planState);
            daoSession.update(buildSignData);
        }
    }

    public void updateBuildSignHasPlan(boolean hasPlan) {
        BuildSignDataBean buildSignData = getTodayBuildSignData();
        if (buildSignData != null) {
            buildSignData.setPlanState(1);
            buildSignData.setHasPlan(hasPlan ? 1 : 0);
            daoSession.update(buildSignData);
        }
    }

    public BuildSignDataBean getTodayBuildSignData() {
        BuildSignDataBean buildSignData = null;
        BuildSignDataBeanDao buildSignDataBeanDao = daoSession.getBuildSignDataBeanDao();
        String todayDate = DateUtils.getYMD();
        buildSignData = buildSignDataBeanDao.queryBuilder().where(BuildSignDataBeanDao.Properties.PlanDate.eq(todayDate)).unique();
        return buildSignData;
    }

    /**
     * BuildManageBeanDao
     */

    /*插入工地信息*/
    public void insertNewBuildManage(BuildManageBean buildManage) {
        daoSession.insert(buildManage);
    }

    /*插入或者更新工地信息*/
    public void insertOrUpdateBuildManage(BuildManageBean buildManage) {
        daoSession.insertOrReplace(buildManage);
    }

    /*插入或者更新抢维修信息*/
    public void insertOrUpdateRushRepair(RushRepairBean buildManage) {
        daoSession.insertOrReplace(buildManage);
    }

    /*插入或者更新部门信息*/
    public void insertOrUpdateDepartmentBean(DepartmentBean departmentBean) {
        daoSession.insertOrReplace(departmentBean);
    }

    /*插入或者更新用户信息*/
    public void insertOrUpdateUserListBean(UserListBean userListBean) {
        daoSession.insertOrReplace(userListBean);
    }

    /*插入或者更新工地附件信息*/
    public void insertOrUpdateBuildManageFile(BuildManageFileBean buildManageFileBean) {
        daoSession.insertOrReplace(buildManageFileBean);
    }

    /*插入或者更新抢维修附件信息*/
    public void insertOrUpdateRushRepairFile(RushRepairFileBean rushRepairFileBean) {
        daoSession.insertOrReplace(rushRepairFileBean);
    }

    /*插入或者更新工地审核人信息*/
    public void insertOrUpdateExamine(BuildExamineBean buildExamineBean) {
        daoSession.insertOrReplace(buildExamineBean);
    }

    /*插入或者更新抢维修审核人信息*/
    public void insertOrUpdateRushExamine(RushRepairExamineBean examineBean) {
        daoSession.insertOrReplace(examineBean);
    }

    /*插入或者更新工地过程管理数据信息*/
    public void insertOrUpdateProcessManage(BuildProcessManageBean buildProcessManageBean) {
        daoSession.insertOrReplace(buildProcessManageBean);
    }

    /*插入或者更新抢维修过程管理数据信息*/
    public void insertOrUpdateRushRepairProcess(RushRepairProcessBean rushRepairProcessBean) {
        daoSession.insertOrReplace(rushRepairProcessBean);
    }

    /*读取本地保存的工地*/
    public BuildManageBean getBuildManage(String id) {
        BuildManageBeanDao buildManageBeanDao = daoSession.getBuildManageBeanDao();
        return buildManageBeanDao.queryBuilder().where(
                BuildManageBeanDao.Properties.Id.eq(id))
                .unique();
    }

    /*读取本地保存的工地*/
    public BuildProcessManageBean getBuildProcess(String id) {
        BuildProcessManageBeanDao buildProcessManageBeanDao = daoSession.getBuildProcessManageBeanDao();
        return buildProcessManageBeanDao.queryBuilder().where(
                BuildProcessManageBeanDao.Properties.Id.eq(id))
                .unique();
    }

    /*读取本地保存的抢维修*/
    public RushRepairBean getRushRepairBean(String id) {
        RushRepairBeanDao rushRepairBeanDao = daoSession.getRushRepairBeanDao();
        return rushRepairBeanDao.queryBuilder().where(
                RushRepairBeanDao.Properties.Id.eq(id))
                .unique();
    }

    /*读取本地保存的抢维修过程管理*/
    public RushRepairProcessBean getRushRepairProcess(String id) {
        RushRepairProcessBeanDao repairProcessBeanDao = daoSession.getRushRepairProcessBeanDao();
        return repairProcessBeanDao.queryBuilder().where(
                RushRepairProcessBeanDao.Properties.Id.eq(id))
                .unique();
    }

    /*读取本地保存的附件*/
    public List<BuildManageFileBean> getBuildManageFile(String build_id) {
        BuildManageFileBeanDao buildManageFileBeanDao = daoSession.getBuildManageFileBeanDao();
        return buildManageFileBeanDao.queryBuilder().where(BuildManageFileBeanDao.Properties.BuildId.eq(build_id)).list();
    }

    /*读取抢维修保存的附件*/
    public List<RushRepairFileBean> getRushRepairFile(String build_id) {
        RushRepairFileBeanDao rushRepairFileBeanDao = daoSession.getRushRepairFileBeanDao();
        return rushRepairFileBeanDao.queryBuilder().where(RushRepairFileBeanDao.Properties.BuildId.eq(build_id)).list();
    }

    //过程管理附件
    public List<RushRepairFileBean> getRushRepairFile(String build_id, String id) {
        RushRepairFileBeanDao rushRepairFileBeanDao = daoSession.getRushRepairFileBeanDao();
        return rushRepairFileBeanDao.queryBuilder().where(RushRepairFileBeanDao.Properties.BuildId.eq(build_id), RushRepairFileBeanDao.Properties.ProcessId.eq(id)).list();
    }

    //审核附件
    public List<RushRepairFileBean> getRushRepairFiles(String build_id, String id) {
        RushRepairFileBeanDao rushRepairFileBeanDao = daoSession.getRushRepairFileBeanDao();
        return rushRepairFileBeanDao.queryBuilder().where(RushRepairFileBeanDao.Properties.BuildId.eq(build_id), RushRepairFileBeanDao.Properties.ExamineId.eq(id)).list();
    }

    //过程管理附件
    public List<BuildManageFileBean> getBuildManageFile(String build_id, String id) {
        BuildManageFileBeanDao buildManageFileBeanDao = daoSession.getBuildManageFileBeanDao();
        return buildManageFileBeanDao.queryBuilder().where(BuildManageFileBeanDao.Properties.BuildId.eq(build_id), BuildManageFileBeanDao.Properties.ProcessId.eq(id)).list();
    }

    //审核附件
    public List<BuildManageFileBean> getBuildManageFiles(String build_id, String id) {
        BuildManageFileBeanDao buildManageFileBeanDao = daoSession.getBuildManageFileBeanDao();
        return buildManageFileBeanDao.queryBuilder().where(BuildManageFileBeanDao.Properties.BuildId.eq(build_id), BuildManageFileBeanDao.Properties.ExamineId.eq(id)).list();
    }

    /*读取本地保存的审核信息*/
    public List<BuildExamineBean> getBuildExamine(String build_id) {
        BuildExamineBeanDao buildExamineBeanDao = daoSession.getBuildExamineBeanDao();
        return buildExamineBeanDao.queryBuilder().where(BuildExamineBeanDao.Properties.WorkOrderId.eq(build_id)).list();
    }

    /*读取部门信息*/
    public List<DepartmentBean> getDepartment() {
        return daoSession.getDepartmentBeanDao().queryBuilder().list();
    }

    /*读取部门信息*/
    public List<DepartListBean> getDepartList() {
        return daoSession.getDepartListBeanDao().queryBuilder().list();
    }

    /*读取部门人员信息*/
    public List<DepartUserListBean> getDepartUserList() {
        return daoSession.getDepartUserListBeanDao().queryBuilder().list();
    }

    /*读取用户信息*/
    public List<UserListBean> getUserListBean() {
        return daoSession.getUserListBeanDao().queryBuilder().list();
    }


    /*读取本地保存的审核信息*/
    public List<RushRepairExamineBean> getRushExamine(String build_id) {
        RushRepairExamineBeanDao rushRepairExamineBeanDao = daoSession.getRushRepairExamineBeanDao();
        return rushRepairExamineBeanDao.queryBuilder().where(RushRepairExamineBeanDao.Properties.WorkOrderId.eq(build_id)).list();
    }

    /*读取本地保存的过程管理*/
    public List<BuildProcessManageBean> getBuildProcessManage(String build_id) {
        BuildProcessManageBeanDao buildManageBeanDao = daoSession.getBuildProcessManageBeanDao();
        return buildManageBeanDao.queryBuilder().where(
                BuildProcessManageBeanDao.Properties.WorkOrderId.eq(build_id))
                .list();
    }

    /*读取抢维修保存的过程管理*/
    public List<RushRepairProcessBean> getRushProcessManage(String build_id) {
        RushRepairProcessBeanDao repairProcessBeanDao = daoSession.getRushRepairProcessBeanDao();
        return repairProcessBeanDao.queryBuilder().where(
                RushRepairProcessBeanDao.Properties.WorkOrderId.eq(build_id))
                .list();
    }

    public void deleteBuildManage(BuildManageBean buildManage) {
        daoSession.delete(buildManage);
    }

    public void deleteBuildProcess(BuildProcessManageBean buildManage) {
        daoSession.delete(buildManage);
    }

    public void deleteRushProcess(RushRepairProcessBean buildManage) {
        daoSession.delete(buildManage);
    }

    /*保存服务器的工地列表数据*/
    public void saveBuildManages(List<BuildManageBean> buildManages) {
        BuildManageBeanDao buildManageBeanDao = daoSession.getBuildManageBeanDao();
        buildManageBeanDao.insertOrReplaceInTx(buildManages);
    }

    /*保存服务器的工地列表数据*///0表示工地 1表示工地过程管理 2表示抢维修 3抢维修过程管理
    public void saveBuildFiles(List<BuildFileBean> buildFileBeans, String type, String ids) {
        if (type.equals("0")) {
            for (BuildFileBean buildFileBean : buildFileBeans) {
                String id = UUID.randomUUID().toString();
                buildFileBean.setId(id);
                buildFileBean.setStatus(BuildManageBean.BUILD_SUBMIT_ZERO);
                buildFileBean.setBuilId(ids);
            }
        } else if (type.equals("1")) {
            for (BuildFileBean buildFileBean : buildFileBeans) {
                String id = UUID.randomUUID().toString();
                buildFileBean.setId(id);
                buildFileBean.setStatus(BuildManageBean.BUILD_SUBMIT_ZERO);
                buildFileBean.setRepairId(ids);
            }
        } else if (type.equals("2")) {
            for (BuildFileBean buildFileBean : buildFileBeans) {
                String id = UUID.randomUUID().toString();
                buildFileBean.setId(id);
                buildFileBean.setStatus(BuildManageBean.BUILD_SUBMIT_ZERO);
                buildFileBean.setRepairId(ids);
            }
        } else if (type.equals("3")) {
            for (BuildFileBean buildFileBean : buildFileBeans) {
                String id = UUID.randomUUID().toString();
                buildFileBean.setId(id);
                buildFileBean.setStatus(BuildManageBean.BUILD_SUBMIT_ZERO);
                buildFileBean.setRepairProcessId(ids);
            }
        }
        BuildFileBeanDao buildFileBeanDao = daoSession.getBuildFileBeanDao();
        buildFileBeanDao.insertOrReplaceInTx(buildFileBeans);
    }

//    /*读取本地保存的工地列表*/
//    public List<BuildManageBean> getBuildManages(String userid) {
//        BuildManageBeanDao buildManageBeanDao = daoSession.getBuildManageBeanDao();
//        return buildManageBeanDao.queryBuilder().where(BuildManageBeanDao.Properties.Build_state.ge(0)).list();
//    }

    /*读取本地保存的工地列表*/
    public List<BuildManageBean> getBuildManages(String types) {
        BuildManageBeanDao buildManageBeanDao = daoSession.getBuildManageBeanDao();
        WhereCondition cond3 = BuildManageBeanDao.Properties.ConstructionState.eq(types);
        WhereCondition cond4 = BuildManageBeanDao.Properties.ConstructionState.eq("4");
        if (types.equals("6")) {
            return buildManageBeanDao.queryBuilder().where(BuildManageBeanDao.Properties.ConstructionState.isNotNull()).list();
        } else {
            return types.equals("2") ? buildManageBeanDao.queryBuilder().whereOr(cond3, cond4).list() : buildManageBeanDao.queryBuilder().where(cond3).list();
        }
    }

    /*读取本地保存的工地列表*/
    public List<RushRepairBean> getRushRepair(String types) {
        RushRepairBeanDao rushRepairBeanDao = daoSession.getRushRepairBeanDao();
        WhereCondition cond3 = RushRepairBeanDao.Properties.WorkOrderState.eq(types);
        WhereCondition cond4 = RushRepairBeanDao.Properties.WorkOrderState.eq("4");
        if (types.equals("6")) {
            return rushRepairBeanDao.queryBuilder().where(RushRepairBeanDao.Properties.WorkOrderState.isNotNull()).list();
        } else {
            return types.equals("2") ? rushRepairBeanDao.queryBuilder().whereOr(cond3, cond4).list() : rushRepairBeanDao.queryBuilder().where(cond3).list();
        }
    }

    /*删除所有本地保存的工地列表*/
    public void deleteAllBuildManages() {
        List<BuildManageBean> data = daoSession.getBuildManageBeanDao().queryBuilder().list();
        for (BuildManageBean buildManageBean : data) {
            if (TextUtils.isEmpty(buildManageBean.getSubmit_state())) {
                daoSession.delete(buildManageBean);
            }
        }
    }

    /*删除所有本地保存的工地过程列表*/
    public void deleteAllBuildProcess() {
        List<BuildProcessManageBean> data = daoSession.getBuildProcessManageBeanDao().queryBuilder().list();
        for (BuildProcessManageBean buildProcessManageBean : data) {
            if (TextUtils.isEmpty(buildProcessManageBean.getSubmit_state())) {
                daoSession.delete(buildProcessManageBean);
            }
        }
    }

    /*删除所有本地保存的工地审核列表*/
    public void deleteAllBuildExamine() {
        List<BuildExamineBean> data = daoSession.getBuildExamineBeanDao().queryBuilder().list();
        for (BuildExamineBean buildExamineBean : data) {
            daoSession.delete(buildExamineBean);
        }
    }

    /*删除所有本地保存的工地列表*/
    public void deleteAllRushRepair() {
        List<RushRepairBean> data = daoSession.getRushRepairBeanDao().queryBuilder().list();
        for (RushRepairBean rushRepairBean : data) {
            if (TextUtils.isEmpty(rushRepairBean.getSubmit_state())) {
                daoSession.delete(rushRepairBean);
            }
        }
    }

    /*删除所有本地保存的抢维修过程列表*/
    public void deleteAllRushProcess() {
        List<RushRepairProcessBean> data = daoSession.getRushRepairProcessBeanDao().queryBuilder().list();
        for (RushRepairProcessBean rushRepairProcessBean : data) {
            if (TextUtils.isEmpty(rushRepairProcessBean.getSubmit_state())) {
                daoSession.delete(rushRepairProcessBean);
            }
        }
    }

    /*删除所有本地保存的抢维修审核列表*/
    public void deleteAllRushExamine() {
        List<RushRepairExamineBean> data = daoSession.getRushRepairExamineBeanDao().queryBuilder().list();
        for (RushRepairExamineBean rushRepairExamineBean : data) {
            daoSession.delete(rushRepairExamineBean);
        }
    }

    /*删除部门列表及用户*/
    public void deleteDepartmentAndUser() {
        daoSession.getDepartmentBeanDao().deleteAll();
        daoSession.getUserListBeanDao().deleteAll();
    }

    /*删除部门列表及用户*/
    public void deleteDepartAndUser() {
        daoSession.getDepartListBeanDao().deleteAll();
        daoSession.getDepartUserListBeanDao().deleteAll();
    }

    /*删除工地类型*/
    public void deleteBuildType() {
        daoSession.getBuildTypeBeanDao().deleteAll();
    }


    /*删除所有本地保存的附件列表*/
    public void deleteAllBuildFile() {
        List<BuildManageFileBean> data = daoSession.getBuildManageFileBeanDao().queryBuilder().list();
        for (BuildManageFileBean buildManageFileBean : data) {
            if (TextUtils.isEmpty(buildManageFileBean.getSubmit_state())) {
                daoSession.delete(buildManageFileBean);
            }
        }
    }

    /*删除所有本地保存的附件列表*/
    public void deleAllRushRepairFile() {
        List<RushRepairFileBean> data = daoSession.getRushRepairFileBeanDao().queryBuilder().list();
        for (RushRepairFileBean rushRepairFileBean : data) {
            if (TextUtils.isEmpty(rushRepairFileBean.getSubmit_state())) {
                daoSession.delete(rushRepairFileBean);
            }
        }
    }

    /**
     * BuildReportBeanDao  工地回报
     */

    /*插入或者更新工地回报信息*/
    public void insertOrUpdateBuildReport(BuildReportBean buildReport) {
        daoSession.insertOrReplace(buildReport);
    }

    public BuildReportBean getBuildReport(String report_id) {
        BuildReportBeanDao buildReportBeanDao = daoSession.getBuildReportBeanDao();
        return buildReportBeanDao.load(report_id);
    }

    public List<BuildReportBean> getBuildReportList(String build_id, int state) {
        BuildReportBeanDao buildReportBeanDao = daoSession.getBuildReportBeanDao();
        return buildReportBeanDao.queryBuilder().where(
                BuildReportBeanDao.Properties.Build_id.eq(build_id),
                BuildReportBeanDao.Properties.Save_state.eq(state)).list();
    }

    public void deleteBuildReport(BuildReportBean buildReport) {
        daoSession.delete(buildReport);
    }

    /**
     * BuildLogBeanDao  工地监护
     */

    /*插入或者更新工地监护信息*/
    public void insertOrUpdateBuildLog(BuildLogBean buildLog) {
        daoSession.insertOrReplace(buildLog);
    }

    public BuildLogBean getLocalBuildLog(String log_id) {
        BuildLogBeanDao buildLogBeanDao = daoSession.getBuildLogBeanDao();
        return buildLogBeanDao.load(log_id);
    }

    public List<BuildLogBean> getLocalBuildLogList() {
        BuildLogBeanDao buildLogBeanDao = daoSession.getBuildLogBeanDao();
        return buildLogBeanDao.queryBuilder().where(
                BuildLogBeanDao.Properties.State.eq(TroubleBean.SAVED_STATE))
                .list();
    }

    public void deleteBuildLog(BuildLogBean buildLog) {
        daoSession.delete(buildLog);
    }

    /**
     * BuildHandleDataBeanDao
     */
    /*保存工地监护列表*/
    public void insertBuildHandleList(List<BuildHandleDataBean> buildHandleList) {
        BuildHandleDataBeanDao buildHandleDataBeanDao = daoSession.getBuildHandleDataBeanDao();
        buildHandleDataBeanDao.deleteAll();
        if (buildHandleList != null && buildHandleList.size() > 0) {
            /*插入新的工地监护*/
            buildHandleDataBeanDao.insertOrReplaceInTx(buildHandleList);
        }
    }

    /*读取本地保存的工地监护列表*/
    public List<BuildHandleDataBean> getBuildHandleList() {
        BuildHandleDataBeanDao buildHandleDataBeanDao = daoSession.getBuildHandleDataBeanDao();
        List<BuildHandleDataBean> buildHandleList = buildHandleDataBeanDao.queryBuilder().list();
        return buildHandleList;
    }

    public void insertBuildHandle(BuildHandleDataBean buildHandle) {
        daoSession.insert(buildHandle);
    }

    /*读取本地保存的单个监护工地*/
    public BuildHandleDataBean getBuildHandle(String build_id) {
        BuildHandleDataBeanDao buildHandleDataBeanDao = daoSession.getBuildHandleDataBeanDao();
        BuildHandleDataBean buildHandle = buildHandleDataBeanDao.queryBuilder().where(BuildHandleDataBeanDao.Properties.Build_id.eq(build_id)).unique();
        return buildHandle;
    }

    /*读取本地保存的单个监护工地*/
    public void deleteBuildHandle(String build_id) {
        BuildHandleDataBeanDao buildHandleDataBeanDao = daoSession.getBuildHandleDataBeanDao();
        buildHandleDataBeanDao.deleteByKey(build_id);
    }

    /**
     * ServerSysConfigBeanDao  照片上传模块 UiphotoReport
     */

    /*保存服务器的参数配置数据*/
    public void saveServerConfigDatas(List<ServerSysConfigBean> sysConfigBeans) {
        ServerSysConfigBeanDao serverSysConfigBeanDao = daoSession.getServerSysConfigBeanDao();
        serverSysConfigBeanDao.insertOrReplaceInTx(sysConfigBeans);
    }

    /*读取本地保存的工地监护列表*/
    public List<ServerSysConfigBean> getServerConfigList(String config_type) {
        ServerSysConfigBeanDao serverSysConfigBeanDao = daoSession.getServerSysConfigBeanDao();
        List<ServerSysConfigBean> serverSysConfigBeans = serverSysConfigBeanDao.queryBuilder().where(
                ServerSysConfigBeanDao.Properties.Key.eq(config_type)).list();
        return serverSysConfigBeans;
    }

    /*删除本地所有保存的服务器的隐患设备类型数据*/
    public void deleteAllServerConfigDatas(String config_type) {
        List<ServerSysConfigBean> serverSysConfigBeans = getServerConfigList(config_type);
        if (serverSysConfigBeans != null) {
            ServerSysConfigBeanDao serverSysConfigBeanDao = daoSession.getServerSysConfigBeanDao();
            serverSysConfigBeanDao.deleteInTx(serverSysConfigBeans);
        }
    }

    /**
     * PlanBeanDao  巡检计划信息
     */

    public void insertNewPlans(List<PlanBean> planBeans) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        planBeanDao.insertInTx(planBeans);
    }

    public void insertNewPlan(PlanBean planBean) {
        daoSession.insert(planBean);
    }

    public void insertOrUpdatePlan(PlanBean planBean) {
        daoSession.insertOrReplace(planBean);
    }

    public PlanBean getPlan(String plan_id) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        PlanBean planBean = planBeanDao.queryBuilder().
                where(PlanBeanDao.Properties.Plan_id.eq(plan_id)).unique();
        return planBean;
    }

    public boolean isPlanOver(String plan_id) {
        boolean isOver = false;
        PlanBean planBean = getPlan(plan_id);
        if (planBean != null) {
            OfflinePathBeanDao offlinePathBeanDao = daoSession.getOfflinePathBeanDao();
            //巡检点，巡检线
            long total_count = offlinePathBeanDao.queryBuilder().where(
                    OfflinePathBeanDao.Properties.Plan_id.eq(plan_id)).count();
            if (planBean.isNotOperateDeviceType()) {
                long notOverCount = offlinePathBeanDao.queryBuilder().where(
                        OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                        OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_NOT_OVER)).count();
                isOver = ((notOverCount <= 0 && total_count != 0) ||
                        (planBean.getPercent1() == "1" && total_count == 0));
            } else {
                long notFinishCount = offlinePathBeanDao.queryBuilder().where(
                        OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                        OfflinePathBeanDao.Properties.State.eq(OfflinePathBean.POINT_STATE_UNFINISHED)).count();
                isOver = ((notFinishCount <= 0 && total_count != 0) ||
                        (planBean.getPercent1() == "1" && total_count == 0));
            }
        }
        return isOver;
    }

    public long getPlanOverCount(String plan_id) {
        long overCount = 0;
        PlanBean planBean = getPlan(plan_id);
        if (planBean != null) {
            //巡检点，巡检线
            if (planBean.isNotOperateDeviceType()) {
                OfflinePathBeanDao offlinePathBeanDao = daoSession.getOfflinePathBeanDao();
                overCount = offlinePathBeanDao.queryBuilder().where(
                        OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                        OfflinePathBeanDao.Properties.Isover.eq(OfflinePathBean.POINTLINE_IS_OVER)).count();
            } else {  //巡检设备，隐患
                OfflinePathBeanDao offlinePathBeanDao = daoSession.getOfflinePathBeanDao();
                overCount = offlinePathBeanDao.queryBuilder().where(
                        OfflinePathBeanDao.Properties.Plan_id.eq(plan_id),
                        OfflinePathBeanDao.Properties.State.eq(OfflinePathBean.POINT_STATE_FINISHED)).count();
            }
        }
        return overCount;
    }


    public void setPlanOfflinePointOver(String plan_id) {
        List<OfflinePathBean> offlinePathBeans = getPlanOfflinePathList(plan_id);
        if (offlinePathBeans != null && offlinePathBeans.size() > 0) {
            for (OfflinePathBean offlinePathBean : offlinePathBeans) {
                offlinePathBean.setIsover(1);
                offlinePathBean.setIsupload(1);
            }
            updateOfflinePaths(offlinePathBeans);
        }
    }

    public List<PlanBean> getPlanList(String userid, int plan_mode, int plan_state) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                //PlanBeanDao.Properties.Plan_mode.eq(plan_mode),
                PlanBeanDao.Properties.Plan_state.eq(plan_state))
                .orderDesc(PlanBeanDao.Properties.Plan_begindate)
                .list();
        return planBeans;
    }

    public List<PlanBean> getPlanList(String userid, int plan_mode, List<Integer> planStates) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                //PlanBeanDao.Properties.Plan_mode.eq(plan_mode),
                PlanBeanDao.Properties.Plan_state.in(planStates))
                .orderDesc(PlanBeanDao.Properties.Plan_begindate)
                .list();
        return planBeans;
    }

    public List<PlanBean> getPlanListByDeviceType(String userid, int plan_mode, int device_type_id) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                PlanBeanDao.Properties.Plan_mode.eq(plan_mode),
                PlanBeanDao.Properties.Device_type_id.eq(device_type_id))
                .orderDesc(PlanBeanDao.Properties.Plan_begindate)
                .list();
        return planBeans;
    }

    public List<PlanBean> getPlanListByDeviceType(String userid, int plan_mode, List<Integer> deviceTypes) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                PlanBeanDao.Properties.Plan_mode.eq(plan_mode),
                PlanBeanDao.Properties.Device_type_id.in(deviceTypes))
                .orderDesc(PlanBeanDao.Properties.Plan_begindate)
                .list();
        return planBeans;
    }

    public List<PlanBean> getPlanList(String userid, int plan_mode) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid))
                //PlanBeanDao.Properties.Plan_mode.eq(plan_mode))
                .orderDesc(PlanBeanDao.Properties.Create_time)
                .list();
        return planBeans;
    }

    public List<String> getLocalPlanIds(String userid, int plan_mode) {
        List<PlanBean> planBeans = getPlanList(userid, plan_mode);
        List<String> planIds = new ArrayList<>();
        if (planBeans != null) {
            for (PlanBean planBean : planBeans) {
                planIds.add(planBean.getPlan_id());
            }
        }
        return planIds;
    }

    public List<PlanBean> getPlanOverTime(String userid) {
        String today = DateUtils.getDate();
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        return planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid),
                PlanBeanDao.Properties.Plan_enddate.lt(today))
                .list();
    }

    public void deletePlans(List<PlanBean> planBeans) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        planBeanDao.deleteInTx(planBeans);
    }

    public void deleteAllPlans(String userid) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.User_id.eq(userid))
                .list();
        if (planBeans != null && !planBeans.isEmpty()) {
            planBeanDao.deleteInTx(planBeans);
        }
    }

    /*删除处过这些id以外的计划*/
    public void deletePlansNotIn(List<String> planids) {
        PlanBeanDao planBeanDao = daoSession.getPlanBeanDao();
        List<PlanBean> planBeans = planBeanDao.queryBuilder().where(
                PlanBeanDao.Properties.Plan_id.notIn(planids))
                .list();
        if (planBeans != null && !planBeans.isEmpty()) {
            planBeanDao.deleteInTx(planBeans);
        }
    }

    /**
     * MaintainAdjustParamDao
     */

    /*插入或者更新巡检设备录入输入参数*/
    public void insertAdjustDeviceSettings(List<MaintainAdjustParam> settings) {
        MaintainAdjustParamDao deviceSettingDao = daoSession.getMaintainAdjustParamDao();
        deviceSettingDao.insertOrReplaceInTx(settings);
    }

    public List<MaintainAdjustParam> getAdjustDeviceSettings() {
        return daoSession.loadAll(MaintainAdjustParam.class);
    }

    public Map<MaintainAdjustParam, List<MaintainAdjustParam>> getAdjustDeviceSettingsMapped() {
        MaintainAdjustParamDao deviceSettingDao = daoSession.getMaintainAdjustParamDao();

        List<MaintainAdjustParam> parentParams = deviceSettingDao.queryBuilder().
                where(MaintainAdjustParamDao.Properties.ParentId.isNull())
                .orderAsc(MaintainAdjustParamDao.Properties.ParentId).list();
        Map<MaintainAdjustParam, List<MaintainAdjustParam>> pairParams = new HashMap<>();
        if (parentParams != null) {
            for (MaintainAdjustParam parentParam : parentParams) {
                String parent_id = parentParam.getId();
                List<MaintainAdjustParam> childParams = deviceSettingDao.queryBuilder().
                        where(MaintainAdjustParamDao.Properties.ParentId.eq(parent_id)).list();
                pairParams.put(parentParam, childParams);
            }
        }
        return pairParams;
    }

    public void deleteAdjustDeviceSettings() {
        daoSession.deleteAll(MaintainAdjustParam.class);
    }

    /**
     * MaintainValveParamDao
     */

    /*插入或者更新巡检设备录入输入参数*/
    public void insertCommonDeviceSettings(List<MaintainCommonParam> settings) {
        MaintainCommonParamDao deviceSettingDao = daoSession.getMaintainCommonParamDao();
        deviceSettingDao.insertOrReplaceInTx(settings);
    }

    public List<MaintainCommonParam> getCommonDeviceSettings() {
        return daoSession.loadAll(MaintainCommonParam.class);
    }

    public void deleteValveDeviceSettings() {
        daoSession.deleteAll(MaintainCommonParam.class);
    }

    /**
     * OfflineJobNeedDataDao
     */
    public OfflineJobNeedData getTodayJobNeedData() {
        String today = DateUtils.getYMD();
        OfflineJobNeedDataDao jobNeedDataDao = daoSession.getOfflineJobNeedDataDao();
        OfflineJobNeedData jobNeedData = jobNeedDataDao.queryBuilder().where(OfflineJobNeedDataDao.Properties.PlanDate.eq(today)).unique();
        if (jobNeedData == null) {
            jobNeedData = new OfflineJobNeedData();
            jobNeedData.setPlanDate(today);
        }
        return jobNeedData;
    }

    /*插入或者更新工地信息*/
    public void updateTodayJobNeedData(OfflineJobNeedData jobNeedData) {
        daoSession.insertOrReplace(jobNeedData);
    }

    /**
     * OfflineWorkJobBeanDao
     */

    /*插入或者更新工地信息*/
    public void insertOfflineWorkJob(OfflineWorkJobBean jobBean) {
        daoSession.insertOrReplace(jobBean);
    }

    public OfflineWorkJobBean getOfflineWorkJob(String ref_id) {
        OfflineWorkJobBeanDao offlineWorkJobDao = daoSession.getOfflineWorkJobBeanDao();
        return offlineWorkJobDao.queryBuilder().where(
                OfflineWorkJobBeanDao.Properties.Ref_id.eq(ref_id))
                .unique();
    }

    public OfflineWorkJobBean getOfflineWorkJob(int job_type) {
        OfflineWorkJobBeanDao offlineWorkJobDao = daoSession.getOfflineWorkJobBeanDao();
        return offlineWorkJobDao.queryBuilder().where(
                OfflineWorkJobBeanDao.Properties.Job_type.eq(job_type))
                .unique();
    }

    public List<OfflineWorkJobBean> getOfflineWorkJobs(int job_type) {
        OfflineWorkJobBeanDao offlineWorkJobDao = daoSession.getOfflineWorkJobBeanDao();
        return offlineWorkJobDao.queryBuilder().where(
                OfflineWorkJobBeanDao.Properties.Job_type.eq(job_type))
                .list();
    }

    public List<OfflineWorkJobBean> getOfflineWorkJobs(String user_id) {
        OfflineWorkJobBeanDao offlineWorkJobDao = daoSession.getOfflineWorkJobBeanDao();
        return offlineWorkJobDao.queryBuilder().where(
                OfflineWorkJobBeanDao.Properties.User_id.eq(user_id))
                .list();
    }

    public long getOfflineWorkJobsCount(String user_id) {
        OfflineWorkJobBeanDao offlineWorkJobDao = daoSession.getOfflineWorkJobBeanDao();
        return offlineWorkJobDao.queryBuilder().where(
                OfflineWorkJobBeanDao.Properties.User_id.eq(user_id))
                .count();
    }

    public void deleteOfflineWorkJob(OfflineWorkJobBean jobBean) {
        daoSession.delete(jobBean);
    }

    /*插入或者更新抢维修工单信息*/
    public void insertOrUpdateBuildJobManage(RushRepairBean buildJobBean) {
        daoSession.insertOrReplace(buildJobBean);
    }

    /*删除本地工单信息*/
    public void deleteRushRepair(RushRepairBean rushRepairBean) {
        daoSession.delete(rushRepairBean);
    }
}
