DaoDbManager.java 13.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
package com.glsx.dao;

import android.content.Context;
import android.database.SQLException;

import com.blankj.utilcode.util.LogUtils;
import com.glsx.dao.bean.SysMsgType;
import com.glsx.dao.bean.CarKeyBean;
import com.glsx.dao.bean.TrafficSearchResultBean;
import com.glsx.dao.bean.SysMsgBean;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class DaoDbManager {
    private final String TAG = DaoDbManager.class.getSimpleName();

    DatabaseHelper mDbHelper;
    Context mContext;

    private static class Holder {
        private static final DaoDbManager INSTANCE = new DaoDbManager();
    }

    public static DaoDbManager getInstance() {
        return DaoDbManager.Holder.INSTANCE;
    }

    public void init(Context context) {
        mContext = context;
        mDbHelper = new DatabaseHelper(context);
    }

    private DatabaseHelper getDbHelper() {
        if (null != mContext) {
            return new DatabaseHelper(mContext);
        }
        return null;
    }

    // 向SysMsgBean表中添加一条数据
    public void insertSysMsg(SysMsgBean bean) {
        if (null == bean) {
            return;
        }
        LogUtils.dTag(TAG, "insertSysMsg,bean=" + bean.toString());
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            dao.create(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 向SysMsgBean表中添加批量数据
    public void insertSysMsgList(List<SysMsgBean> list) {
        if (null == list || list.size() < 1) {
            return;
        }
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            for (SysMsgBean bean : list) {
                dao.create(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除SysMsgBean表中的一条数据
    public void deleteSysMsg(SysMsgBean bean) {
        if (null == bean) {
            return;
        }
        LogUtils.dTag(TAG, "deleteSysMsg,bean=" + bean.toString());
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            dao.delete(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有系统保存消息,除了抓拍消息
     *
     * @return 消息列表
     */
    public List<SysMsgBean> queryAllSysMsgList() {
        LogUtils.dTag(TAG, "queryAllSysMsgList");
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        List<SysMsgBean> beans = null;
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            beans = dao.queryBuilder()
                    .orderBy(SysMsgBean.COLUMN_NAME_ID, false)
                    .limit(300L)
                    .where()
                    .ne(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.PICTURE_SNAP)
                    .and()
                    .ne(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.VEDIO_SNAP)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LogUtils.dTag(TAG, "queryAllSysMsgList,size=" + (beans == null ? 0 : beans.size()));
        return beans;
    }

    /**
     * 查询抓拍的系统消息列表
     *
     * @return 消息列表
     */
    public List<SysMsgBean> queryAllCaptureMsgList() {
        LogUtils.dTag(TAG, "queryAllCaptureMsgList");
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        List<SysMsgBean> beans = null;
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            beans = dao.queryBuilder()
                    .orderBy(SysMsgBean.COLUMN_NAME_ID, false)
                    .limit(300L)
                    .where()
                    .eq(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.PICTURE_SNAP)
                    .or()
                    .eq(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.VEDIO_SNAP)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LogUtils.dTag(TAG, "queryAllCaptureMsgList,size=" + (beans == null ? 0 : beans.size()));
        return beans;
    }


    /**
     * 查询最后一条抓拍消息
     *
     * @return 消息列表
     */
    public SysMsgBean queryLastCaptureMsg() {
        LogUtils.dTag(TAG, "queryLastCaptureMsg");
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        SysMsgBean bean = null;
        try {
            Dao<SysMsgBean, Integer> dao = mDbHelper.getDao(SysMsgBean.class);
            bean = dao.queryBuilder()
                    .orderBy(SysMsgBean.COLUMN_NAME_ID, false)
                    .where()
                    .eq(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.PICTURE_SNAP)
                    .or()
                    .eq(SysMsgBean.COLUMN_NAME_MSG_TYPE, SysMsgType.VEDIO_SNAP)
                    .queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LogUtils.dTag(TAG, "queryLastCaptureMsg,bean=" + (null == bean ? "null" : bean.toString()));
        return bean;
    }

    /**
     * 新增路况查询记录
     *
     * @param bean 查询记录
     */
    public void insertTrafficSearchRecord(TrafficSearchResultBean bean) {
        if (null == bean) {
            return;
        }
        LogUtils.dTag(TAG, "insertTrafficSearchRecord,bean=" + bean.toString());
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<TrafficSearchResultBean, Integer> dao = mDbHelper.getDao(TrafficSearchResultBean.class);
            dao.create(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询指定页的记录
     *
     * @param page 页码
     * @param size 个数
     * @return 列表数据
     */
    public List<TrafficSearchResultBean> queryTrafficSearchRecordList(int page, int size) {
        LogUtils.dTag(TAG, "queryTrafficSearchRecordList,page=" + page + ",size=" + size);
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        List<TrafficSearchResultBean> beans = null;
        try {
            Dao<TrafficSearchResultBean, Integer> dao = mDbHelper.getDao(TrafficSearchResultBean.class);
            beans = dao.queryBuilder()
                    .orderBy(TrafficSearchResultBean.COLUMN_ID, false)
                    .limit((long) size)
                    .offset((long) ((page - 1) * size))
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beans;
    }

    /**
     * 清楚所有路况搜索记录
     *
     * @return 操作是否成功
     */
    public boolean clearTrafficSearchRecord() {
        LogUtils.dTag(TAG, "clearTrafficSearchRecord");
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return false;
        }
        try {
            Dao<TrafficSearchResultBean, Integer> dao = mDbHelper.getDao(TrafficSearchResultBean.class);
            dao.delete(dao.queryForAll());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 查询获取所有钥匙的列表
     *
     * @return 钥匙列表
     */
    public List<CarKeyBean> queryAllCarKey() {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        List<CarKeyBean> beans = null;
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            beans = dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beans;
    }

    /**
     * 保存更新钥匙
     *
     * @param bean 钥匙对象
     */
    public void insertCarKey(CarKeyBean bean) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            dao.createOrUpdate(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据SN判断钥匙是否存在
     *
     * @param sn 钥匙SN
     * @return 是|否
     */
    public boolean isCarKeyExist(String sn) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return false;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            Map<String, Object> map = new HashMap<>();
            map.put(CarKeyBean.COLUMN_SN, sn);
            if (dao.queryForFieldValues(map).size() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据SN获取钥匙信息
     *
     * @param sn 钥匙SN
     * @return 钥匙数据
     */
    public CarKeyBean queryCarKeyBySn(String sn) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return null;
        }
        CarKeyBean bean = null;
        try {

            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            bean = dao.queryBuilder()
                    .where()
                    .eq(CarKeyBean.COLUMN_SN, sn)
                    .queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bean;
    }

    /**
     * 更新钥匙别名
     *
     * @param sn       钥匙SN
     * @param nickname 别名
     */
    public void updateCarKeyNickname(String sn, String nickname) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            UpdateBuilder<CarKeyBean, Integer> updateBuilder = dao.updateBuilder();
            updateBuilder.where().eq(CarKeyBean.COLUMN_SN, sn);
            updateBuilder.updateColumnValue(CarKeyBean.COLUMN_KEY_NAME, nickname);
            updateBuilder.update();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新钥匙自动感应状态值
     *
     * @param sn              钥匙SN
     * @param lockShareStatus 自动感应状态值
     */
    public void updateCarKeyLockShareStatus(String sn, String lockShareStatus) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            UpdateBuilder<CarKeyBean, Integer> updateBuilder = dao.updateBuilder();
            updateBuilder.where().eq(CarKeyBean.COLUMN_SN, sn);
            updateBuilder.updateColumnValue(CarKeyBean.COLUMN_LOCK_SHARE_STATUS, lockShareStatus);
            updateBuilder.update();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定钥匙
     *
     * @param sn 钥匙SN
     */
    public void deleteCarKeyBySn(String sn) {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            DeleteBuilder<CarKeyBean, Integer> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq(CarKeyBean.COLUMN_SN, sn);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除所有钥匙信息
     */
    public void clearAllCarKey() {
        if (null == mDbHelper) {
            mDbHelper = getDbHelper();
        }
        if (null == mDbHelper) {
            return;
        }
        try {
            Dao<CarKeyBean, Integer> dao = mDbHelper.getDao(CarKeyBean.class);
            dao.delete(dao.queryForAll());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setLogcatPrint(boolean isPrint){
        LogUtils.getConfig().setLogSwitch(isPrint); //日志开关
        LogUtils.getConfig().setConsoleSwitch(isPrint); //日志打印开关,是否输出到logcat
    }


}