1/** 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import dataShare from '@ohos.data.dataShare'; 17import dataSharePredicates from '@ohos.data.dataSharePredicates'; 18 19import BaseModel from './BaseModel'; 20import common from '../data/commonData'; 21import mmsTable from '../data/tableData'; 22import HiLog from '../utils/HiLog'; 23import LooseObject from '../data/LooseObject'; 24import StringUtil from '../utils/StringUtil'; 25 26const TAG = 'ConversationListModel'; 27 28export default class ConversationListModel extends BaseModel { 29 public async insertSession(valueBucket, callback, context): Promise<void> { 30 let dataHelper = await dataShare.createDataShareHelper(context, common.string.URI_MESSAGE_LOG); 31 let managerUri: string = common.string.URI_MESSAGE_LOG + common.string.URI_MESSAGE_SESSION_TABLE; 32 dataHelper.insert(managerUri, valueBucket).then(data => { 33 if (callback) { 34 callback(this.encapsulateReturnResult(common.int.SUCCESS, data)); 35 } 36 }).catch(error => { 37 HiLog.e(TAG, 'insertSession fail, error: ' + JSON.stringify(error)); 38 if (callback) { 39 callback(this.encapsulateReturnCode(common.int.FAILURE)); 40 } 41 }); 42 } 43 44 public async deleteSessionByCondition(actionData, callback, context): Promise<void> { 45 let dataHelper = await dataShare.createDataShareHelper(context, common.string.URI_MESSAGE_LOG); 46 let condition: dataSharePredicates.DataSharePredicates = this.buildQuerySessionCondition(actionData); 47 let managerUri: string = common.string.URI_MESSAGE_LOG + common.string.URI_MESSAGE_SESSION_TABLE; 48 dataHelper.delete(managerUri, condition).then(data => { 49 if (callback) { 50 callback(this.encapsulateReturnResult(common.int.SUCCESS, data)); 51 } 52 }).catch(error => { 53 HiLog.e(TAG, 'deleteSessionByCondition, error: ' + JSON.stringify(error)); 54 if (callback) { 55 callback(this.encapsulateReturnCode(common.int.FAILURE)); 56 } 57 }); 58 } 59 60 public async updateSessionByCondition(actionData, valueBucket, callback, context): Promise<void> { 61 let dataHelper = await dataShare.createDataShareHelper(context, common.string.URI_MESSAGE_LOG); 62 let condition: dataSharePredicates.DataSharePredicates = 63 this.buildQuerySessionCondition(actionData); 64 if (valueBucket == null) { 65 HiLog.e(TAG, 'updateSessionByCondition fail, valueBucket is null!'); 66 if (callback) { 67 callback(this.encapsulateReturnCode(common.int.FAILURE)); 68 } 69 } 70 let managerUri: string = common.string.URI_MESSAGE_LOG + common.string.URI_MESSAGE_SESSION_TABLE; 71 dataHelper.update(managerUri, condition, valueBucket).then(data => { 72 if (callback) { 73 callback(this.encapsulateReturnResult(common.int.SUCCESS, data)); 74 } 75 }).catch(error => { 76 HiLog.e(TAG, 'updateSessionByCondition fail, error: ' + JSON.stringify(error)); 77 if (callback) { 78 callback(this.encapsulateReturnCode(common.int.FAILURE)); 79 } 80 }); 81 } 82 83 public async querySessionByCondition(actionData, callback, context): Promise<void> { 84 let dataHelper = await dataShare.createDataShareHelper(context, common.string.URI_MESSAGE_LOG); 85 let condition: dataSharePredicates.DataSharePredicates = 86 this.buildQuerySessionCondition(actionData); 87 let managerUri: string = common.string.URI_MESSAGE_LOG + common.string.URI_MESSAGE_SESSION_TABLE; 88 dataHelper.query(managerUri, condition, this.buildSessionTableColumns()).then(resultSet => { 89 let resultList: Array<LooseObject> = []; 90 if (resultSet.rowCount > 0) { 91 while (resultSet.goToNextRow()) { 92 resultList.push(this.buildSessionResult(resultSet)); 93 } 94 } else { 95 resultSet.goToFirstRow(); 96 resultList.push(this.buildSessionResult(resultSet)); 97 } 98 callback(this.encapsulateReturnResult(common.int.SUCCESS, resultList)); 99 resultSet.close(); 100 }).catch(error => { 101 HiLog.e(TAG, 'querySessionByCondition fail, error: ' + JSON.stringify(error)); 102 callback(this.encapsulateReturnCode(common.int.FAILURE)); 103 }); 104 } 105 106 public async querySessionSizeByCondition(actionData, callback, context): Promise<void> { 107 let dataHelper = await dataShare.createDataShareHelper( 108 context, common.string.URI_MESSAGE_LOG); 109 let condition: dataSharePredicates.DataSharePredicates = 110 this.buildQuerySessionCondition(actionData); 111 let managerUri: string = common.string.URI_MESSAGE_LOG + 112 common.string.URI_MESSAGE_SESSION_TABLE; 113 dataHelper.query(managerUri, condition, this.buildSessionTableColumns()).then(resultSet => { 114 callback(this.encapsulateReturnResult(common.int.SUCCESS, resultSet.rowCount)); 115 }).catch(error => { 116 HiLog.e(TAG, 'querySessionSizeByCondition, fail, error: ' + JSON.stringify(error)); 117 callback(this.encapsulateReturnCode(common.int.FAILURE)); 118 }); 119 } 120 121 private buildQuerySessionCondition(actionData): dataSharePredicates.DataSharePredicates { 122 let condition: dataSharePredicates.DataSharePredicates = 123 new dataSharePredicates.DataSharePredicates(); 124 condition.isNotNull(mmsTable.sessionField.id); 125 if (actionData.smsType != null) { 126 condition.and().equalTo(mmsTable.sessionField.smsType, actionData.smsType); 127 } 128 if (actionData.telephone != null) { 129 condition.and().equalTo(mmsTable.sessionField.telephone, actionData.telephone); 130 } 131 if (actionData.telephone_like != null) { 132 condition.and().like(mmsTable.sessionField.telephone, '%' + actionData.telephone_like + '%'); 133 } 134 if (actionData.threadId != null) { 135 condition.and().equalTo(mmsTable.sessionField.id, actionData.threadId); 136 } 137 if (actionData.threadIds != null && actionData.threadIds.length > 0) { 138 condition.and().in(mmsTable.sessionField.id, actionData.threadIds); 139 } 140 if (actionData.unreadCount_greaterThan != null) { 141 condition.and().greaterThan(mmsTable.sessionField.unreadCount, actionData.unreadCount_greaterThan); 142 } 143 if (actionData.orderByTimeDesc) { 144 condition.orderByDesc(mmsTable.sessionField.time); 145 } 146 if (actionData.page != null && actionData.limit != null) { 147 condition.limit(actionData.limit, StringUtil.getOffsetForSession(actionData.page)); 148 } 149 return condition; 150 } 151 152 private buildSessionResult(resultSet): LooseObject { 153 let result: LooseObject = {}; 154 result.id = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.id))); 155 result.time = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.time))); 156 result.telephone = resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.telephone)); 157 result.content = resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.content)); 158 result.contactsNum = Number(resultSet.getString( 159 resultSet.getColumnIndex(mmsTable.sessionField.contactsNum))); 160 result.smsType = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.smsType))); 161 result.unreadCount = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.unreadCount))); 162 result.sendStatus = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.sendStatus))); 163 result.hasDraft = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.hasDraft))); 164 result.hasLock = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.hasLock))); 165 result.messageCount = Number(resultSet.getString( 166 resultSet.getColumnIndex(mmsTable.sessionField.messageCount))); 167 result.hasMms = Number(resultSet.getString(resultSet.getColumnIndex(mmsTable.sessionField.hasMms))); 168 result.hasAttachment = Number(resultSet.getString( 169 resultSet.getColumnIndex(mmsTable.sessionField.hasAttachment))); 170 result.telephoneFormat = result.telephone; 171 return result; 172 } 173 174 private buildSessionTableColumns(): Array<string> { 175 let sessionTableColumns: Array<string> = [ 176 mmsTable.sessionField.id, 177 mmsTable.sessionField.time, 178 mmsTable.sessionField.telephone, 179 mmsTable.sessionField.content, 180 mmsTable.sessionField.contactsNum, 181 mmsTable.sessionField.smsType, 182 mmsTable.sessionField.unreadCount, 183 mmsTable.sessionField.sendStatus, 184 mmsTable.sessionField.hasDraft, 185 mmsTable.sessionField.hasLock, 186 mmsTable.sessionField.messageCount, 187 mmsTable.sessionField.hasMms, 188 mmsTable.sessionField.hasAttachment 189 ]; 190 return sessionTableColumns; 191 } 192}