"use strict";
/**
 * 比较常用的工具模块
 * @date 2022-03
 * @author 何华东
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateReplaceSql = exports.generateInsertSql = exports.interceptMethodCall = exports.uuid = exports.replaceAll = exports.delay = void 0;
var uuid_1 = require("uuid");
/**
 * 延迟
 */
function delay(ms) {
    return new Promise(function (resolve) {
        setTimeout(resolve, ms);
    });
}
exports.delay = delay;
/**
 * 替换所有字符串
 *
 * 不支持替换\\n
 */
function replaceAll(value, searchValue, replaceValue) {
    if (!value || !searchValue || !replaceValue)
        return value;
    //replace(/'/g, "/'");
    return value.replace(new RegExp(searchValue, 'g'), replaceValue);
}
exports.replaceAll = replaceAll;
/**
 * uuidv4
 */
function uuid() {
    return uuid_1.v4();
}
exports.uuid = uuid;
/**
 * 拦截对象方法的调用
 * @param handler Reflect.apply(target, thisArg, argArray);
 */
function interceptMethodCall(obj, handler) {
    return new Proxy(obj, {
        get: function (target, p, receiver) {
            if (typeof target[p] !== 'function')
                return Reflect.get(target, p, receiver);
            return new Proxy(target[p], {
                apply: handler
            });
        }
    });
}
exports.interceptMethodCall = interceptMethodCall;
/**
  * 生成insert into sql语句
  */
function generateInsertSql(tableName, entity) {
    var keys = Reflect.ownKeys(entity).filter(function (s) { return entity[s] !== undefined && entity[s] !== null; });
    return "INSERT INTO " + tableName + "(" + keys.join(',') + ") VALUES (" + keys.map(function (s) {
        return typeof entity[s] === 'number' ? entity[s] : "'" + entity[s] + "'";
    }).join(',') + ")";
}
exports.generateInsertSql = generateInsertSql;
/**
 * 生成replace into sql语句
 *
 * 自动跳过syncTime
 */
function generateReplaceSql(tableName, entity, excludeFields) {
    var keys = Reflect.ownKeys(entity).filter(function (s) { return entity[s] !== undefined && entity[s] !== null && s !== 'syncTime' && !(excludeFields === null || excludeFields === void 0 ? void 0 : excludeFields.includes(s.toString())); });
    return "REPLACE INTO " + tableName + "(" + keys.join(',') + ") VALUES (" + keys.map(function (s) {
        return typeof entity[s] === 'number' ? entity[s] : "'" + entity[s] + "'";
    }).join(',') + ")";
}
exports.generateReplaceSql = generateReplaceSql;
//# sourceMappingURL=Utils.js.map