#ifndef _PPS_REDIS__H_
#define _PPS_REDIS__H_
#include "ppsm.h"

// all Redis field of one KEY
#define OCS_REDIS_FIELD_NULL 0
#define OCS_REDIS_FIELD_BALANCE 1
#define OCS_REDIS_FIELD_PROMO_BALANCE 2
#define OCS_REDIS_FIELD_BUNDLE_DATA 3
#define OCS_REDIS_FIELD_BUNDLE_MOV 4
#define OCS_REDIS_FIELD_BUNDLE_MTV 5
#define OCS_REDIS_FIELD_BUNDLE_SMS 6
#define OCS_REDIS_FIELD_BUNDLE_VALIDITY 7
#define OCS_REDIS_FIELD_BUNDLE_WARN_SMS_FLAG 8
#define OCS_REDIS_FIELD_MAX_NUM 9

typedef struct OCS_usr_redis{
	char msisdn[24];
	double		usr_balance;
	double		pro_balance;

	long long	data;	/* left in, in B */
	int			mo_voice;	/* in seconds */
	int			mt_voice;
	u_short		sms;
	u_long		validity_date;
	int			warning_sms_flag;
}OCS_usr_redis;

extern int RedisInit(u32 localip, u32 peerip);
extern void RedisLinkMon();
extern int RedisFlushDb();
extern void RedisCreateDbFromOcs();
extern void RedisSaveDbToOcs();
//extern void RedisLoadFromRedis();

extern int RedisSetUserBalanceFromOcs(_prepaid_info *pUsr, double balance);
extern int RedisUpdateUserBalance(OCS_usr_redis *usr_redis);
extern int RedisUpdateUserInfo(OCS_usr_redis *usr_redis);
extern int RedisUpdateUserInfoFromOcs(_prepaid_info *pUsr);
extern int RedisUpdateBundleInfoFromOcs(_prepaid_info *pUsr);
extern int RedisUpdateUsrFieldFromOcs(_prepaid_info *pUsr, int field_id);
extern int RedisUpdateUserField(OCS_usr_redis *pUsr, int field_id);
extern int RedisGetUserInfo(_prepaid_info *pUsr, OCS_usr_redis *usr_redis);
extern int RedisGetBundleInfo(_prepaid_info *pUsr, OCS_usr_redis *usr_redis);
extern int RedisGetUserInfo2Ocs(_prepaid_info *pUsr);
extern int RedisGetUserField(_prepaid_info *pUsr, OCS_usr_redis *usr_redis, int field_id);
extern int RedisGetUserBalance(_prepaid_info *pUsr, OCS_usr_redis *usr_redis);

extern int RedisClearUserBundle(OCS_usr_redis *pUsr);
extern int RedisDelUserInfoByMsisdn(char *msisdn);
extern int RedisDelUserInfo(_prepaid_info *pUsr);
extern void RedisTest(int i);
extern int RedisCreateLogTask(pthread_t *idTask);
extern void RedisAddLogStr(char *time, char *level, char *, int line, char *text);


// Redis log writer
#define MAX_REDIS_LOG_LINES 81920
#define MAX_REDIS_LINE_LEN 512
typedef struct OCS_redis_log_t
{
    int header;
    int tailer;
    
    char log_str[MAX_REDIS_LOG_LINES][MAX_REDIS_LINE_LEN];
}OCS_redis_log_t;

// simple log
enum RedisLogLevel
{
    RS_ERROR = 1,
    RS_WARN  = 2,
    RS_INFO  = 3,
    RS_DEBUG = 4,
};

void mylog1(const char* filename, int line, enum RedisLogLevel level, const char* fmt, ...) __attribute__((format(printf,4,5)));

#define redislog(level, format, ...) mylog1(__FILE__, __LINE__, level, format, ## __VA_ARGS__)

#endif


