Blame view

app/src/main/java/com/sunvote/xpadapp/audition/dao/EvaluationRuleDaoImpl.java 3.43 KB
27983dbe   孙向锦   project init
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
  package com.sunvote.xpadapp.audition.dao;
  
  import android.content.ContentValues;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  
  import com.sunvote.xpadapp.ConstantValues;
  import com.sunvote.xpadapp.MyApp;
  import com.sunvote.xpadapp.audition.EvaluationRuleInfo;
  import com.sunvote.xpadapp.db.DBLocalHelper;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  
  /**
   * 评议规则信息
   * Created by wutaian on 2017/11/13.
   */
  public class EvaluationRuleDaoImpl implements EvaluationRuleDao {
      private DBLocalHelper helper;
      private SQLiteDatabase db;
  
      public EvaluationRuleDaoImpl(){
          helper = MyApp.getInstance().getHelper();
          db = MyApp.getInstance().getDb();
      }
  
      @Override
      public boolean isOpen() {
          if (db == null) {
              return false;
          }
          return true;
      }
  
      @Override
      public void close() {
  //        if (db != null && db.isOpen()) {
  //            db.close();
  //            db = null;
  //        }
      }
  
      @Override
      public Boolean addEvaluationRuleInfo(EvaluationRuleInfo evaluationRuleInfo) {
          if (evaluationRuleInfo == null) {
              return false;
          }
          boolean flag = true;
          ContentValues values = new ContentValues();
          values.put("ItemNum", evaluationRuleInfo.getItemNum());
          values.put("DefaultValue", evaluationRuleInfo.getDefaultValue());
          values.put("ItemName", evaluationRuleInfo.getItemName());
  
          if (db == null) {
              db = helper.getWritableDatabase();
          }
          long resultValue = 0;
          if (db.isOpen()) {
              db.beginTransaction(); // 开始事务
              resultValue = db.replace(ConstantValues.TBL_EVALUATION_RULE, null, values);
              flag = (resultValue > 0 ? true : false && flag);
              if (flag) {
                  db.setTransactionSuccessful();
              }
              db.endTransaction(); // 结束事务
              close();
          }
          return resultValue > 0 ? true : false;
      }
  
      @Override
      public void delEvaluationRuleInfo() {
          String sql = "DELETE FROM " + ConstantValues.TBL_EVALUATION_RULE;
          if (db == null) {
              db = helper.getWritableDatabase();
          }
          if (db.isOpen()) {
              db.execSQL(sql);
              close();
          }
      }
  
      @Override
      public List<EvaluationRuleInfo> findEvaluationRuleInfoAll() {
          List<EvaluationRuleInfo> evaluationRuleInfoLs= new ArrayList<>();
          String sql = "SELECT ItemNum,DefaultValue,ItemName FROM  " + ConstantValues.TBL_EVALUATION_RULE;
          if (db == null) {
              db = helper.getWritableDatabase();
          }
          if (db.isOpen()) {
              Cursor cursor = db.rawQuery(sql, null);
              while (cursor.moveToNext()) {
                  EvaluationRuleInfo evaluationRuleInfo = getCursor(cursor);
                  evaluationRuleInfoLs.add(evaluationRuleInfo);
              }
              cursor.close();
              close();
          }
          return evaluationRuleInfoLs;
      }
  
      private EvaluationRuleInfo getCursor(Cursor cursor){
          EvaluationRuleInfo evaluationRuleInfo = new EvaluationRuleInfo();
          evaluationRuleInfo.setItemNum(cursor.getInt(cursor.getColumnIndex("ItemNum")));
          evaluationRuleInfo.setItemName(cursor.getString(cursor.getColumnIndex("ItemName")));
          evaluationRuleInfo.setDefaultValue(cursor.getInt(cursor.getColumnIndex("DefaultValue")));
          return evaluationRuleInfo;
      }
  }