import SqlliteDbUtil from '@/utils/sqllitedb'
import table from './sqllite/table.js'
import {
	fixNullVal
} from "@/utils/common";

// 用户
export default {

	async selectList() {
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		try {
			let rs = await sqllitedb.selectSQL(`select * from ${table.userName}`)
			return rs
		} catch (e) {
			console.log(e.message)
		} finally {
			await sqllitedb.closeDB();
		}

	},

	async login(data) {
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		try {
			let {
				account,
				pwd
			} = data
			const sql = `select * from ${table.userName} where user = '${account}' AND passWord ='${pwd}'`
			let res = await sqllitedb.selectSQL(sql);

			const user = res[0]
			return {
				data: user,
				msg: '登录成功'
			};
		} catch (e) {
			console.log(e.message)
		} finally {
			await sqllitedb.closeDB();
		}

	},
	async remove(id) {
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		try {
			if (!id) {
				return
			}
			let sql = `delete from ${table.userName} where userId = '${id}'`;
			await sqllitedb.executeSQL(sql);
		} catch (e) {
			console.log(e.message)
		} finally {
			// await sqllitedb.closeDB();
		}
	},
	async info(id) {
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		let sql = `select * from ${table.userName} where userId = '${id}'`;
		let res = await sqllitedb.selectSQL(sql);
		if (res && res.length > 0) {
			return res[0]
		}
	},

	async changePwd(data) {
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		try {
			let {
				userName,
				oldPwd,
				newPwd,
				userId
			} = data
			const current = await sqllitedb.selectSQL(
				`select * from ${table.userName} where userId = '${userId}' and user = '${userName}'`
			);
			console.log(current,`select * from ${table.userName} where userId = '${userId}' and user = '${userName}'`,'=-===========>')
			if (current[0].passWord !== oldPwd) {
				return {
					data: false,
					msg: '原密码错误'
				};
			}
			const update = await sqllitedb.executeSQL(
				`UPDATE ${table.userName} SET passWord = '${newPwd}' 
		   WHERE userId = '${userId}'`
			);
			return {
				data: true,
				msg: '恭喜您，密码更新成功。'
			};
		} catch (e) {
			console.log(e.message)
		} finally {
			await sqllitedb.closeDB();
		}

	},

	async saveBatch(list) {
		if (list.length === 0) {
			return;
		}
		console.log('开始保存用户信息....' + list.length)
		let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
		try {
			for (let data of list) {
				let column = ''
				let values = ''
				let idx = 0
				for (let attr in data) {
					let dataField = table['user'].find(v => {
						if (v.field === attr) {
							return v
						}
					})
					if (!dataField) {
						continue
					}
					column += dataField.field + ','
					values += "'" + fixNullVal(data[attr]) + "',"
					idx++
				}

				column = column.endsWith(',') ? column.substring(0, column.length - 1) : column
				values = values.endsWith(',') ? values.substring(0, values.length - 1) : values

				let sql = `insert into ${table.userName}(${column}) values(${values})`
				let has = await this.info(data.userId)
				if (has && has.userId) {
					await this.remove(data.userId)
				}
				await sqllitedb.executeSQL(sql)
			}
		} catch (e) {
			console.log(e.message)
		} finally {
			await sqllitedb.closeDB();
		}
		console.log('导入完成...')
	}
}