|
|
@@ -1,12 +1,28 @@
|
|
|
package com.xzjmyk.pm.activity.db.dao;
|
|
|
|
|
|
+import android.content.ContentValues;
|
|
|
+import android.database.Cursor;
|
|
|
+import android.database.sqlite.SQLiteDatabase;
|
|
|
+
|
|
|
+import com.xzjmyk.pm.activity.MyApplication;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.db.DBOpenHelper;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.model.MessageModel;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.ListUtils;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* Created by Bitliker on 2017/3/2.
|
|
|
*/
|
|
|
|
|
|
public class MessageDao {
|
|
|
- private final String TABLE_NAME="em_erpnews";
|
|
|
+ private DBOpenHelper helper = null;
|
|
|
+ private final String TABLE_NAME = "em_erpnews";
|
|
|
private static MessageDao instance;
|
|
|
+
|
|
|
public static MessageDao getInstance() {
|
|
|
if (instance == null) {
|
|
|
synchronized (MessageDao.class) {
|
|
|
@@ -17,14 +33,285 @@ public class MessageDao {
|
|
|
}
|
|
|
|
|
|
private MessageDao() {
|
|
|
+ helper = new DBOpenHelper(MyApplication.getInstance());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 事务创建或是更新,注意 如果该id已经存在,就更新,更新时候不更新状态,要到upStatus更新状态
|
|
|
+ *
|
|
|
+ * @param models
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean createOrinstart(List<MessageModel> models) {
|
|
|
+ if (ListUtils.isEmpty(models)) return false;
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+ db.beginTransaction();
|
|
|
+ ContentValues values = null;
|
|
|
+ //对象为空或是拜访时间没有情况下不插入数据库
|
|
|
+ for (MessageModel m : models) {
|
|
|
+ values = getValues(m, master, emcode);
|
|
|
+ i = db.insert(TABLE_NAME, null, values);
|
|
|
+ if (i == -1) {//如果插入不成功,就是数据库中有该条数据
|
|
|
+ String where = "master=? and emcode=? and id=?";
|
|
|
+ String[] whereArgs = {master, emcode, String.valueOf(m.getId())};
|
|
|
+ values.remove("isReaded");//不更新状态
|
|
|
+ i = db.update(TABLE_NAME, values, where, whereArgs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ db.setTransactionSuccessful();
|
|
|
+ db.endTransaction();
|
|
|
+ } catch (android.database.SQLException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 注意 如果该id已经存在,就更新,更新时候不更新状态,要到upStatus更新状态
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean createOrinstart(MessageModel model) {
|
|
|
+ if (model == null) return false;
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+
|
|
|
+ ContentValues values = getValues(model, master, emcode);
|
|
|
+ //对象为空或是拜访时间没有情况下不插入数据库
|
|
|
+ i = db.insert(TABLE_NAME, null, values);
|
|
|
+ if (i == -1) {//如果插入不成功,就是数据库中有该条数据
|
|
|
+ String where = "master=? and emcode=? and id=? ";
|
|
|
+ String[] whereArgs = {master, emcode, String.valueOf(model.getId())};
|
|
|
+ values.remove("isReaded");//不更新状态
|
|
|
+ i = db.update(TABLE_NAME, values, where, whereArgs);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- private void createOrinstart(){
|
|
|
+ /**
|
|
|
+ * 修改状态
|
|
|
+ *
|
|
|
+ * @param models
|
|
|
+ * @param isReaded
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upStatus(List<MessageModel> models, boolean isReaded) {
|
|
|
+ if (ListUtils.isEmpty(models)) return false;
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+ ContentValues values;
|
|
|
+ String where = "master=? and emcode=? and id=?";
|
|
|
+ for (MessageModel m : models) {
|
|
|
+ values = new ContentValues();
|
|
|
+ values.put("isReaded", isReaded ? 1 : 0);
|
|
|
+ String[] whereArgs = {master, emcode, String.valueOf(m.getId())};
|
|
|
+ i = db.update(TABLE_NAME, values, where, whereArgs);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 修改单个
|
|
|
+ *
|
|
|
+ * @param id 消息id
|
|
|
+ * @param isReaded
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upStatus(int id, boolean isReaded) {
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+ ContentValues values = new ContentValues();
|
|
|
+ values.put("isReaded", isReaded ? 1 : 0);
|
|
|
+ String where = "master=? and emcode=? and id=? ";
|
|
|
+ String[] whereArgs = {master, emcode, String.valueOf(id)};
|
|
|
+ i = db.update(TABLE_NAME, values, where, whereArgs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 通过类型修改状态为已读未读
|
|
|
+ *
|
|
|
+ * @param type 类型
|
|
|
+ * @param isReaded 是否阅读
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upStatusByType(String type, boolean isReaded) {
|
|
|
+ if (StringUtils.isEmpty(type)) return false;
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+ ContentValues values = new ContentValues();
|
|
|
+ values.put("isReaded", isReaded ? 1 : 0);
|
|
|
+ String where = "master=? and emcode=? and type=?";
|
|
|
+ String[] whereArgs = {master, emcode, type};
|
|
|
+ i = db.update(TABLE_NAME, values, where, whereArgs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取第一层数据
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MessageModel> queryFirstFloor() {
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ List<MessageModel> messageModels = null;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return messageModels;
|
|
|
+ db = helper.getReadableDatabase();
|
|
|
+ String[] columns = {"id", "count", "title", "subTitle", "time", "type", "isReaded"};
|
|
|
+ String selection = "hierarchy=?";
|
|
|
+ String[] selectArgs = {String.valueOf(0)};
|
|
|
+ Cursor c = db.query(TABLE_NAME, columns, selection, selectArgs, null, null, null);
|
|
|
+ messageModels = getMessage(c, 0);
|
|
|
+ c.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return messageModels;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取第二层数据 ,通过类型
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MessageModel> queryByType(String type) {
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ int hierarchy = 1;
|
|
|
+ List<MessageModel> messageModels = null;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return messageModels;
|
|
|
+ db = helper.getReadableDatabase();
|
|
|
+ String[] columns = {"id", "count", "title", "subTitle", "time", "type", "isReaded"};
|
|
|
+ String selection = "type=? and hierarchy=?";
|
|
|
+ String[] selectArgs = {type, String.valueOf(hierarchy)};
|
|
|
+ Cursor c = db.query(TABLE_NAME, columns, selection, selectArgs, null, null, null);
|
|
|
+ messageModels = getMessage(c, hierarchy);
|
|
|
+ c.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return messageModels;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除整个类型的数据,包括第一第二层
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean deleteBytype(String type) {
|
|
|
+ if (StringUtils.isEmpty(type)) return false;
|
|
|
+ SQLiteDatabase db = null;
|
|
|
+ long i = 0;
|
|
|
+ try {
|
|
|
+ String master = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master");
|
|
|
+ String emcode = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
|
|
|
+ if (StringUtils.isEmpty(master) || StringUtils.isEmpty(emcode)) return false;
|
|
|
+ db = helper.getWritableDatabase();
|
|
|
+ String where = "master=? and emcode=? and type=?";
|
|
|
+ String[] whereArgs = {master, emcode, type};
|
|
|
+ i = db.delete(TABLE_NAME, where, whereArgs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ if (db != null)
|
|
|
+ db.close();
|
|
|
+ return i > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<MessageModel> getMessage(Cursor c, int hierarchy) {
|
|
|
+ List<MessageModel> messageModels = new ArrayList<>();
|
|
|
+ MessageModel model;
|
|
|
+ while (c.moveToNext()) {
|
|
|
+ int id = c.getInt(c.getColumnIndex("id"));
|
|
|
+ int count = c.getInt(c.getColumnIndex("count"));
|
|
|
+ String title = c.getString(c.getColumnIndex("title"));
|
|
|
+ String subTitle = c.getString(c.getColumnIndex("subTitle"));
|
|
|
+ String time = c.getString(c.getColumnIndex("time"));
|
|
|
+ String type = c.getString(c.getColumnIndex("type"));
|
|
|
+ int isReaded = c.getInt(c.getColumnIndex("isReaded"));
|
|
|
+ model = new MessageModel(id, hierarchy, count, title, subTitle, time, type, isReaded == 1);
|
|
|
+ messageModels.add(model);
|
|
|
+ }
|
|
|
+ return messageModels;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ContentValues getValues(MessageModel m, String master, String emcode) {
|
|
|
+ ContentValues values = new ContentValues();
|
|
|
+ values.put("id", m.getId());
|
|
|
+ values.put("master", master);
|
|
|
+ values.put("emcode", emcode);
|
|
|
+ values.put("count", m.getCount());
|
|
|
+ values.put("isReaded", m.isReaded() ? 1 : 0);
|
|
|
+ values.put("type", m.getType());
|
|
|
+ values.put("time", m.getTime());
|
|
|
+ values.put("title", m.getTitle());
|
|
|
+ values.put("subTitle", m.getSubTitle());
|
|
|
+ values.put("hierarchy", m.getHierarchy());
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|