%%%------------------------------------------------
%%% File    : mail.erl
%%% Author  : zhenghehe
%%% Created : 2012-02-01
%%% Description: 信件record定义
%%%------------------------------------------------

%% ------------------ 进程字典 ------------------
-define(P_MAIL_KEY(RoleId), lists:concat(["P_Mail_Key", RoleId])). %% 邮件 value:[#mail{}|...]

%% ------------------ 邮件类型 ------------------
-define(MAIL_TYPE_SYS, 1).          % 系统邮件
-define(MAIL_TYPE_PRIV, 2).         % 私人邮件
-define(MAIL_TYPE_GUILD, 3).        % 公会邮件
-define(MAIL_TYPE_GUILD_MEMBER, 4). % 公会个人邮件
-define(MAIL_TYPE_AUCTION, 5).      % 拍卖邮件

%% ------------------ 邮件状态 ------------------
-define(MAIL_STATE_READ, 1).        % 已读
-define(MAIL_STATE_NO_READ, 2).     % 未读
-define(MAIL_STATE_HAS_RECEIVE, 3). % 已领取附件

%% ------------------ 锁定状态 ------------------
-define(MAIL_LOCKED_YES, 1).        % 邮件锁定
-define(MAIL_LOCKED_NO, 2).         % 邮件未锁定

%% ------------------ 附件类型 ------------------
-define(ATTACHMENT_TYPE_NO, 0).             % 没有附件
-define(ATTACHMENT_TYPE_OWN_SQ, 1).         % 拥有特殊物品的附件
-define(ATTACHMENT_TYPE_CM, 2).             % 只有普通附件

%% ------------------ 发送邮件日志的类型 ------------------
-define(SEND_LOG_TYPE_NO, 0).       % 不加入日志
-define(SEND_LOG_TYPE_YES, 1).      % 加入日志

%% ------------------ 是否立即发送邮件 ------------------
-define(SCHEDULE_SAVE_MAIL, 0).     % 否
-define(DIRECT_SAVE_MAIL, 1).       % 是

%% ------------------ 是否是重要邮件 ------------------
-define(NOT_IMPORTANT_MAIL, 0).     % 否
-define(IMPORTANT_MAIL, 1).         % 是

%% ------------------ 文本 ------------------
% -define(MAIL_TXT_SYS_NAME, 1).      % 邮件系统名字

%% ------------------ 其他定义 ------------------
-define(DEFAULT_LOGIN_DAY, 30). %% 默认最近30天登录

-define(DEFAULT_MAX_LV,   999). %% 默认最大等级


%% 邮件
-record(mail, {
        id = 0                  % 邮件id
        , type = 1              % 邮件类型
        , state = 2             % 邮件状态
        , locked = 2            % 锁定状态
        , sid = 0               % 发送者id
        , sname = undefined     % 发件人名字(第一次读信加载)
        , rid = 0               % 接受者id
        , title = <<>>          % 标题
        , content = <<>>        % 信件内容(第一次读信加载)  修改老版undefined|<<>>
        , cm_attachment = []    % 普通附件列表,[{Type,GoodsTypeId,Num},...]
        % , sq_attachment :: undefined | []    % 特殊附件列表(保留字段,后续处理)
        , timestamp = 0         % 时间戳(微秒)
        , time = 0              % 秒
        , effect_st = 0         % 有效开始时间
        , effect_et = 0         % 有效结束时间
        , module_id = 0         % 模块id
        , sub_module_id = 0     % 子模块id
        , important = 0         % 是否重要邮件 0否|1是
    }).

%% ------------------ 邮件 ------------------
%% 邮件属性
-define(sql_mail_insert, <<"
    INSERT INTO
        mail(id, type, state, locked, sid, rid, title, cm_attachment, module_id, sub_module_id, timestamp, effect_st, content, important)
    VALUES(~p, ~p, ~p, ~p, ~p, ~p, '~ts', '~ts', ~p, ~p, ~p, ~p, '~ts', ~p)">>).
-define(sql_mail_select, <<"SELECT id, type, state, locked, sid, rid, title, cm_attachment, timestamp, effect_st, module_id, sub_module_id, important FROM mail WHERE rid = ~p">>).
-define(sql_mail_delete, <<"DELETE FROM mail WHERE id = ~p">>).
-define(sql_mail_delete_more, <<"DELETE FROM mail WHERE id in (~ts)">>).

-define(sql_mail_update_cm_attachment, <<"UPDATE mail SET cm_attachment = '~ts' WHERE id = ~p">>).
-define(sql_mail_update_state_read, <<"UPDATE mail SET state = 1 WHERE id = ~p">>).
-define(sql_mail_update_cm_attachment_and_effect_st, <<"UPDATE mail SET cm_attachment = '~ts', effect_st = ~p WHERE id = ~p">>).
-define(sql_mail_update_state_and_effect_st, <<"UPDATE mail SET state = ~p, effect_st = ~p WHERE id = ~p">>).
-define(sql_mail_update_receive_state, <<"UPDATE mail SET state = ~p, cm_attachment = '~ts', effect_st = ~p WHERE id = ~p">>).

%% 邮件内容
%%-define(sql_mail_content_insert, <<"INSERT INTO mail_content(id, rid, content) VALUES(~p, ~p, '~ts')">>).
%%-define(sql_mail_content_select, <<"SELECT content FROM mail_content WHERE id = ~p">>).
%%-define(sql_mail_content_delete, <<"DELETE FROM mail_content WHERE id = ~p">>).
%%-define(sql_mail_content_delete_more, <<"DELETE FROM mail_content WHERE id in (~ts)">>).

%% 改造
-define(sql_mail_select_content, <<"SELECT content FROM mail WHERE id = ~p">>).

% %% 特殊附件列表(保留,后续看需求处理)
% -define(sql_mail_sq_attachment_replace, <<"REPLACE INTO mail_sq_attachment(id, rid, goods_id) WHERE id = ~p">>).
% -define(sql_mail_sq_attachment_select, <<"SELECT goods_id FROM mail_sq_attachment WHERE id = ~p">>).
% -define(sql_mail_sq_attachment_delete, <<"DELETE FROM mail_sq_attachment WHERE id = ~p">>).

%% ------------------ PHP后台 ------------------
%% 根据在线、来源、等级来获取玩家id
-define(sql_role_id_by_source_and_onlineflag_and_lv, <<"
    SELECT
        player_low.id FROM player_low, player_login
    WHERE player_login.source = '~ts' AND player_login.online_flag = ~p AND player_low.lv >= ~p AND player_login.id = player_low.id">>).
%% 根据在线、等级来获取玩家id
-define(sql_role_id_by_onlineflag_and_lv, <<"
    SELECT
        player_low.id FROM player_low, player_login
    WHERE player_login.online_flag = ~p AND player_low.lv >= ~p AND player_login.id = player_low.id">>).

%% 根据等级和平台来获取玩家id
-define(sql_role_id_by_source_and_lv, <<"
    SELECT
        player_low.id FROM player_low, player_login, role_vip
    WHERE player_login.source = '~ts' AND (player_login.last_logout_time >= ~p OR player_login.last_login_time >= ~p) AND player_low.lv>= ~p AND role_vip.real_lv >= ~p AND role_vip.real_lv <= ~p
     AND role_vip.role_id = player_login.id AND player_login.id = player_low.id">>).

%% 细分判断，以减少连表查询操作

%% ================ 有渠道 =================

%% 根据等级&平台&vip等级&最近登录时间来获取玩家id
-define(SQL_ROLE_ID_BY_ALL_CON,
        <<"SELECT log_res.logid FROM 
        (SELECT low.id AS logid, COALESCE(vip.real_lv, 0) AS viplv FROM player_low low LEFT JOIN player_login log ON low.id = log.id LEFT outer JOIN role_vip vip ON low.id = vip.role_id
            WHERE log.last_login_time >= ~p AND low.lv>= ~p AND low.lv <= ~p AND log.source = '~ts') log_res
        WHERE log_res.viplv >= ~p AND log_res.viplv <= ~p">>).

%% 无VIP无天数限制
-define(SQL_ROLE_ID_BY_LV,
        <<"SELECT low.id FROM player_low low LEFT JOIN player_login log ON low.id = log.id
     WHERE low.lv >= ~p AND low.lv <= ~p AND log.source = '~ts'">>).

%% 有VIP无天数限制
-define(SQL_ROLE_ID_BY_VIP_AND_LV,
        <<"SELECT log_res.logid FROM 
        (SELECT low.id AS logid, COALESCE(vip.real_lv,0) AS viplv FROM player_login log LEFT JOIN player_low low ON log.id = low.id LEFT outer JOIN role_vip vip ON low.id = vip.role_id
            WHERE low.lv >= ~p AND low.lv <= ~p AND log.source = '~ts') log_res 
        WHERE log_res.viplv >= ~p AND log_res.viplv <= ~p">>).

%% 无VIP有天数限制
-define(SQL_ROLE_ID_BY_LOGIN_DAY,
        <<"SELECT low.id FROM player_low low LEFT JOIN player_login log ON low.id = log.id
        WHERE log.last_login_time >= ~p AND low.lv >= ~p AND low.lv <= ~p AND log.source = '~ts'">>).


%% ================ 无渠道 ==================

%% 根据等级&vip&最近登录时间来获取玩家id
-define(sql_role_id_by_lv,
        <<"SELECT vip_res.vipid FROM (SELECT low.id as vipid, coalesce(vip.real_lv, 0) as viplv 
        FROM player_low low LEFT JOIN player_login log ON low.id = log.id LEFT outer JOIN role_vip vip ON low.id = vip.role_id WHERE low.lv >= ~p AND low.lv <= ~p AND log.last_login_time >= ~p) vip_res 
        WHERE vip_res.viplv >= ~p AND vip_res.viplv <= ~p">>).

%% 无VIP无天数限制
-define(SQL_ROLE_ID_BY_LV_WITHOUT_SOURCE,
        <<"SELECT id FROM player_low WHERE lv >= ~p AND lv <= ~p">>).

%% 无VIP有天数限制
-define(SQL_ROLE_ID_BY_DAY_WITHOUT_SOURCE,
        <<"SELECT low.id FROM player_low low LEFT JOIN player_login log ON low.id = log.id
     WHERE log.last_login_time >= ~p AND low.lv >= ~p AND low.lv <= ~p">>).

%% 有VIP无天数限制
-define(SQL_ROLE_ID_BY_VIP_WITHOUT_SOURCE,
        <<"SELECT log_res.vipid FROM 
        (SELECT low.id as vipid, coalesce(vip.real_lv, 0) as viplv FROM player_low low LEFT outer JOIN role_vip vip ON low.id = vip.role_id WHERE low.lv >= ~p AND low.lv <= ~p) log_res 
        WHERE log_res.viplv >= ~p AND log_res.viplv <= ~p">>).

%% 根据等级，最近下线时间来获取玩家id
-define(sql_role_id_by_lv_and_lastlogouttime, <<"
    SELECT
        low.id FROM player_low low, player_login login
    WHERE
    login.id = low.id AND low.lv >= ~p
    AND (login.online_flag = 1 OR (login.online_flag <> 1 AND login.last_logout_time >= ~p))">>).

%% ------------------ 反馈 ------------------
-define(sql_feedback_insert, <<"
    INSERT INTO feedback(
        player_id, player_name, content, timestamp, ip, server)
    VALUES(~p, '~ts', '~ts', ~p, '~ts', '~ts')">>).


%% ------------------ 限时邮件 -----------------
-define(SQL_LIMIT_MAIL_SELECT, 
    <<"SELECT id, title, content, gold, bgold, rewards, open_days, tar_time, last_day_login, lvs, vips, source FROM limited_mail">>).

-define(SQL_ONE_LIMIT_MAIL_SELECT, 
    <<"SELECT id, title, content, gold, bgold, rewards, open_days, tar_time, last_day_login, lvs, vips, source FROM limited_mail WHERE id = ~p">>).

-define(SQL_LIMIT_MAIL_DELETE, 
    <<"DELETE FROM limited_mail WHERE id = ~p">>).

-define (SQL_TARGET_LIMIT_MAIL_SELECT, 
    <<"SELECT id, title, content, gold, bgold, rewards, open_days, tar_time, last_day_login, lvs, vips, source FROM limited_mail WHERE id in ~s">>).

-define (SQL_TARGET_LIMIT_MAIL_DELETE, 
    <<"DELETE FROM limited_mail WHERE id in ~s">>).