// ==========================================================================
// 数据表：log_opers - 系统菜单
// ==========================================================================

TABLE_NAME = 'log_opers';
COLUMN_KEY = 'oper_id';
TABLE_FIELDS = '*';

'use api/lib/sql.js';

function getPlaceholders(condition) {
	const columnsArr = Object.keys(condition);
	const placeholders = [];
	const tempParams = [];
	columnsArr.forEach((item) => {
		if (item == 'title') {
			placeholders.push(`title LIKE ?`);
			tempParams.push('%' + condition[item] + '%');
		} else if (item == 'operName') {
			placeholders.push(`oper_name LIKE ?`);
			tempParams.push('%' + condition[item] + '%');
		} else if (item == 'businessType') {
			placeholders.push(`business_type = ?`);
			tempParams.push(condition[item]);
		}
	});
	return {
		placeholders: placeholders.join(' AND '),
		tempParams: tempParams,
	};
}

function del() {
	let filter = payload.get().path.split('/').pop();
	let res;
	if (filter != 'list') {
		const query = `UPDATE ${TABLE_NAME} SET delete_time = NOW() WHERE ${COLUMN_KEY} = ?`;
		res = SQL.exec(DB_NAME, query, filter);
	} else {
		// 清空
		const query = `UPDATE ${TABLE_NAME}  SET delete_time = NOW() WHERE delete_time IS NULL`;
		res = SQL.exec(DB_NAME, query);
	}

	if (res) {
		return okMsg(res);
	}
	return errMsg(507, '删除失败');
}

function main() {
	// 验证用户权限
	'use api/user/acl/excerpt.js'

	switch (payload.get().method) {
		case 'GET':
			return sql_select();
		case 'DELETE':
			return del();
		case 'POST':
			return sql_insert();
		default:
			response.status.notFound();
			return `404 page not found`;
	}
}
