|
@@ -0,0 +1,166 @@
|
|
|
|
+import relationalStore from '@ohos.data.relationalStore';
|
|
|
|
+import CommonConstants from '../../common/constants/CommonConstants';
|
|
|
|
+import UserInfo from '../UserInfo';
|
|
|
|
+
|
|
|
|
+class UserModel {
|
|
|
|
+
|
|
|
|
+ private rdbStore: relationalStore.RdbStore
|
|
|
|
+ private tableName: string = 'USER'
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化表
|
|
|
|
+ */
|
|
|
|
+ initTaskDB(context){
|
|
|
|
+ // 1.rdb配置
|
|
|
|
+ const config = {
|
|
|
|
+ name: CommonConstants.RDB_NAME,
|
|
|
|
+ securityLevel: relationalStore.SecurityLevel.S1
|
|
|
|
+ }
|
|
|
|
+ // 2.初始化SQL语句
|
|
|
|
+ const sql = `CREATE TABLE IF NOT EXISTS USER (
|
|
|
|
+ ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
+ USER_ID INTEGER NOT NULL,
|
|
|
|
+ USER_NAME TEXT NOT NULL,
|
|
|
|
+ PASSWORD TEXT NOT NULL,
|
|
|
|
+ ORG_ID INTEGER NOT NULL,
|
|
|
|
+ STATION_ID INTEGER NOT 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=>{
|
|
|
|
+ console.log('testTag', '创建失败')
|
|
|
|
+ })
|
|
|
|
+ // 保存rdbStore
|
|
|
|
+ this.rdbStore = rdbStore
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询用户设置列表
|
|
|
|
+ */
|
|
|
|
+ // async getUserList(){
|
|
|
|
+ // // 1.构建查询条件
|
|
|
|
+ // let predicates = new relationalStore.RdbPredicates(this.tableName)
|
|
|
|
+ // // 2.查询
|
|
|
|
+ // let result = await this.rdbStore.query(predicates, ['ID', 'USER_ID', 'MAINTAIN_LOGIN_STATUS'])
|
|
|
|
+ // // 3.解析查询结果
|
|
|
|
+ // // 3.1.定义一个数组,组装最终的查询结果
|
|
|
|
+ // let Users: User[] = []
|
|
|
|
+ // // 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 maintainLoginStatus = result.getLong(result.getColumnIndex('MAINTAIN_LOGIN_STATUS'))
|
|
|
|
+ // let updateTime = result.getLong(result.getColumnIndex('MAINTAIN_LOGIN_STATUS'))
|
|
|
|
+ // // 3.5.封装到数组
|
|
|
|
+ // Users.push({id: id, userId: userId, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime})
|
|
|
|
+ // }
|
|
|
|
+ // return Users
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // 查询最后一次用户登录的数据
|
|
|
|
+ async getLast(): Promise<UserInfo> {
|
|
|
|
+ // 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', '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 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, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime}
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 查询最后一次用户登录的数据
|
|
|
|
+ async getByUserId(userId: number): Promise<UserInfo> {
|
|
|
|
+ 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 = await this.rdbStore.query(predicates, ['ID', 'USER_ID', 'USER_NAME', 'PASSWORD', 'ORG_ID', 'STATION_ID', '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 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, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime}
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加用户设置
|
|
|
|
+ * @param user 用户信息
|
|
|
|
+ * @returns 用户信息id
|
|
|
|
+ */
|
|
|
|
+ addUser(user: UserInfo): Promise<number>{
|
|
|
|
+ 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, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus, UPDATE_TIME: user.updateTime})
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据id更新用户设置状态
|
|
|
|
+ * @param id 任务id
|
|
|
|
+ * @param finished 任务是否完成
|
|
|
|
+ */
|
|
|
|
+ updateUser(user: UserInfo) {
|
|
|
|
+ // 1.要更新的数据
|
|
|
|
+ let data = {USER_ID: user.userId, USER_NAME: user.userName, PASSWORD: user.password, ORG_ID: user.orgId, STATION_ID: user.stationId, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus, UPDATE_TIME: user.updateTime}
|
|
|
|
+ // 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;
|