import relationalStore from '@ohos.data.relationalStore'; import CommonConstants from '../../common/constants/CommonConstants'; import {UserAuthInfo } from '../../viewmodel/wms/UserInfo'; class UserModel { private rdbStore: relationalStore.RdbStore |undefined=undefined private tableName: string = 'USER' /** * 初始化表 */ async initTaskDB(context:Context){ // 1.rdb配置 const config = { name: CommonConstants.RDB_NAME, securityLevel: relationalStore.SecurityLevel.S1 }as relationalStore.StoreConfig try { // 获取数据库存储对象 this.rdbStore = await relationalStore.getRdbStore(context,config); } catch (err) { console.info(`getRdbStore err ${JSON.stringify(err)}`); } // 2.初始化SQL语句 const sql = `CREATE TABLE IF NOT EXISTS USER ( ID INTEGER PRIMARY KEY AUTOINCREMENT, USER_ID INTEGER NOT NULL, USER_NAME VARCHAR NOT NULL, PASSWORD VARCHAR NOT NULL, ORG_ID INTEGER NOT NULL, STATION_ID INTEGER NOT NULL, STATION_NAME VARCHAR NULL, STATION_DICE_VALUE VARCHAR NULL, AVATAR VARCHAR NULL, MAINTAIN_LOGIN_STATUS INTEGER NOT NULL, UPDATE_TIME INTEGER NOT NULL )` // 3.获取rdb relationalStore.getRdbStore(context, config, (err, rdbStore) => { if(err){ console.log('testTag', '获取rdbStore失败!') return } // 执行Sql rdbStore.executeSql(sql).then(()=>{ console.log('testTag', '创建用户设置表成功!') }).catch((err: object)=>{ console.log('testTag', '创建失败') }) // 保存rdbStore this.rdbStore = rdbStore }) } // 查询最后一次用户登录的数据 async getLast(): Promise { // 1.构建查询条件 let predicates = new relationalStore.RdbPredicates(this.tableName) predicates.orderByDesc('UPDATE_TIME') predicates.limitAs(1) // 2.查询 let result = await this.rdbStore?.query(predicates, ['ID', 'USER_ID', 'USER_NAME', 'PASSWORD', 'ORG_ID', 'STATION_ID', 'STATION_NAME', 'STATION_DICE_VALUE', 'AVATAR', 'MAINTAIN_LOGIN_STATUS', 'UPDATE_TIME']) // 3.2.遍历封装 只取第一条 while(!result?.isAtLastRow){ // 3.3.指针移动到下一行 result?.goToNextRow() // 3.4.获取数据 let id = result?.getLong(result?.getColumnIndex('ID')) let userId = result?.getLong(result?.getColumnIndex('USER_ID')) let userName = result?.getString(result?.getColumnIndex('USER_NAME')) let password = result?.getString(result?.getColumnIndex('PASSWORD')) let orgId = result?.getLong(result?.getColumnIndex('ORG_ID')) let stationId = result?.getLong(result?.getColumnIndex('STATION_ID')) let stationName = result?.getString(result?.getColumnIndex('STATION_NAME')) let stationDictValue = result?.getString(result?.getColumnIndex('STATION_DICE_VALUE')) let avatar = result?.getString(result?.getColumnIndex('AVATAR')) let maintainLoginStatus = result?.getLong(result?.getColumnIndex('MAINTAIN_LOGIN_STATUS')) let updateTime = result?.getLong(result?.getColumnIndex('UPDATE_TIME')) return {id: id, userId: userId, userName: userName, password: password, orgId: orgId, stationId: stationId, stationName: stationName, stationDictValue: stationDictValue, avatar: avatar, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime} } return } // 查询最后一次用户登录的数据 async getByUserId(userId: number): Promise { if (!userId) { return } // 1.构建查询条件 let predicates = new relationalStore.RdbPredicates(this.tableName) predicates.equalTo('USER_ID', userId) predicates.orderByDesc('UPDATE_TIME') predicates.limitAs(1) // 2.查询 let result: relationalStore.ResultSet | undefined = await this.rdbStore?.query(predicates, ['ID', 'USER_ID', 'USER_NAME', 'PASSWORD', 'ORG_ID', 'STATION_ID', 'STATION_NAME', 'STATION_DICE_VALUE', 'AVATAR', 'MAINTAIN_LOGIN_STATUS', 'UPDATE_TIME']) // 3.2.遍历封装 只取第一条 while(result && !result?.isAtLastRow){ // 3.3.指针移动到下一行 result.goToNextRow() // 3.4.获取数据 let id = result.getLong(result.getColumnIndex('ID')) let userId = result.getLong(result.getColumnIndex('USER_ID')) let userName = result.getString(result.getColumnIndex('USER_NAME')) let password = result.getString(result.getColumnIndex('PASSWORD')) let orgId = result.getLong(result.getColumnIndex('ORG_ID')) let stationId = result.getLong(result.getColumnIndex('STATION_ID')) let stationName = result?.getString(result?.getColumnIndex('STATION_NAME')) let stationDictValue = result?.getString(result?.getColumnIndex('STATION_DICE_VALUE')) let avatar = result.getString(result.getColumnIndex('AVATAR')) let maintainLoginStatus = result.getLong(result.getColumnIndex('MAINTAIN_LOGIN_STATUS')) let updateTime = result.getLong(result.getColumnIndex('UPDATE_TIME')) return {id: id, userId: userId, userName: userName, password: password, orgId: orgId, stationId: stationId, stationName: stationName, stationDictValue: stationDictValue, avatar: avatar, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime} } return } /** * 添加用户设置 * @param user 用户信息 * @returns 用户信息id */ addUser(user: UserAuthInfo): Promise | undefined { return this.rdbStore?.insert(this.tableName, {USER_ID: user.userId, USER_NAME: user.userName, PASSWORD: user.password, ORG_ID: user.orgId, STATION_ID: user.stationId, STATION_NAME: user.stationName, STATION_DICE_VALUE: user.stationDictValue, AVATAR: user.avatar, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus, UPDATE_TIME: user.updateTime} as relationalStore.ValuesBucket) } /** * 根据id更新用户设置状态 * @param id 任务id * @param finished 任务是否完成 */ updateUser(user: UserAuthInfo) { // 1.要更新的数据 let data: relationalStore.ValuesBucket = {USER_ID: user.userId as number, USER_NAME: user.userName as string, PASSWORD: user.password as string, ORG_ID: user.orgId as number, STATION_ID: user.stationId as number, STATION_NAME: user.stationName as string, STATION_DICE_VALUE: user.stationDictValue as string, AVATAR: user.avatar as string, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus as number, UPDATE_TIME: user.updateTime as number} // 2.更新的条件 let predicates = new relationalStore.RdbPredicates(this.tableName) predicates.equalTo('ID', user.id) // 3.更新操作 return this.rdbStore?.update(data, predicates) } /** * 根据id删除任务 * @param id 任务id */ deleteUserById(id: number){ // 1.删除的条件 let predicates = new relationalStore.RdbPredicates(this.tableName) predicates.equalTo('ID', id) // 2.删除操作 return this.rdbStore?.delete(predicates) } } let userModel = new UserModel(); export default userModel as UserModel;