"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defRedisClient = void 0;
var redis_1 = require("redis");
var ErrorCode_1 = require("./ErrorCode");
/**
 *
 * @date 2023-02-16 15:30:42
 * @author 鲜伟坚
 */
var Redis = /** @class */ (function () {
    function Redis() {
    }
    Redis.prototype.connect = function (cfg) {
        var _this = this;
        return new Promise(function (resolve) {
            var options = {
                host: cfg.host,
                port: cfg.port,
                db: cfg.database,
                no_ready_check: true,
                socket_keepalive: true,
                retry_strategy: function (options) { return 1000; }
            };
            var client = redis_1.createClient(options);
            _this._client = client;
            client.on('ready', function () {
                _this._isEnabled = true;
                resolve({ code: ErrorCode_1.ErrCode.OK });
            });
            client.on("error", function (err) {
                _this._isEnabled = false;
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
            });
            client.on('reconnecting', function () {
                _this._isEnabled = false;
                console.info("[redis] reconnecting...");
            });
            client.on('end', function () {
                _this._isEnabled = false;
                console.info("[redis] end...");
            });
            client.on("connect", function () {
                _this._isEnabled = false;
                console.info("[redis] connect...");
            });
        });
    };
    Redis.prototype.SELECT = function (db) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.select(db, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.GET = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.GET(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.SET = function (key, value) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.SET(key, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.SETEX = function (key, value, expireSeconds) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.SETEX(key, expireSeconds, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.SETNX = function (key, value) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.SETNX(key, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.DEL = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.DEL(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.INCR = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.INCR(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.HINCRBY = function (key, field, increment) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.HINCRBY(key, field, increment, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.INCRBy = function (key, increment) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.INCRBY(key, increment, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.DECR = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.DECR(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.DECRBY = function (key, decrement) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.DECRBY(key, decrement, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.RENAME = function (key, newkey) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.RENAME(key, newkey, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.RENAMENX = function (key, newkey) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.RENAME(key, newkey, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.STRLEN = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.STRLEN(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.HSET = function (key, field, value) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.HSET(key, field, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.HGET = function (key, field) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.HGET(key, field, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.HMSET = function (key) {
        var _this = this;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return new Promise(function (resolve) {
            var _a;
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            (_a = _this._client).HMSET.apply(_a, __spreadArrays([key], args, [function (err, reply) {
                    if (err) {
                        resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                        return;
                    }
                    resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
                }]));
        });
    };
    Redis.prototype.HGETALL = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.hgetall(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.MULTI = function () {
        var _this = this;
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.MULTI(__spreadArrays(args)).EXEC(function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.KEYS = function (pattern) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.KEYS(pattern, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.LPUSH = function (key, value) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.LPUSH(key, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.RPUSH = function (key, value) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.RPUSH(key, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.RPOP = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.RPOP(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.LLEN = function (key) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.LLEN(key, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.LRANGE = function (key, start, stop) {
        var _this = this;
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.LRANGE(key, start, stop, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    Redis.prototype.HDEL = function (key) {
        var _this = this;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return new Promise(function (resolve) {
            var _a;
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            (_a = _this._client).HDEL.apply(_a, __spreadArrays([key], args, [function (err, reply) {
                    if (err) {
                        resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                        return;
                    }
                    resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
                }]));
        });
    };
    Redis.prototype.LREM = function (key, value, count) {
        var _this = this;
        if (count === void 0) { count = 0; }
        return new Promise(function (resolve) {
            if (!_this._isEnabled) {
                resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: "redis not enabled" });
                return;
            }
            _this._client.LREM(key, count, value, function (err, reply) {
                if (err) {
                    resolve({ code: ErrorCode_1.ErrCode.RedisErr, err: err });
                    return;
                }
                resolve({ code: ErrorCode_1.ErrCode.OK, msg: reply });
            });
        });
    };
    return Redis;
}());
var defRedisClient = new Redis();
exports.defRedisClient = defRedisClient;
//# sourceMappingURL=Redis.js.map