<?php
/*
 * description：
 * author：wh
 * email：
 * createTime：{2024/5/22} {22:56} 
 */

namespace app\api\controller;


use app\common\model\TabConf;
use think\Db;
use wanghua\general_utility_tools_php\Mmodel;
use wanghua\general_utility_tools_php\tool\Tools;

class Gameemail extends BaseHttpApi
{
    /**
     * desc：游戏邮件列表
     * api/gameemail/getlists
     *
     * is_read 阅读状态
     * is_deleted 删除标记
     */
    function getlists(){
        return Mmodel::catchJson(function (){
            $lists = Db::table(TabConf::$fa_gameemail)->order('id desc')->select();
            foreach ($lists as &$list){
                $is_read = Db::table(TabConf::$fa_user_email_read_record)
                    ->where('openid',api_user_openid())
                    ->where('gameemail_id',$list['id'])
                    ->find();
                //阅读状态
                $list['is_read'] = $is_read?1:0;
                //删除标记
                $list['is_deleted'] = $is_read?$is_read['is_deleted']:0;
            }
            return Tools::set_ok('ok',$lists);
        });
    }

    /**
     * 设置邮件为已读
     * api/gameemail/read
     * 参数：
     * email_id 邮件id
     */
    function read(){
        return Mmodel::catchJson(function (){
            $email_id = input('email_id');
            if(empty($email_id)){
                return Tools::set_fail('邮件id不能为空');
            }
            Db::table(TabConf::$fa_user_email_read_record)
                ->data([
                    'openid'=>api_user_openid(),
                    'gameemail_id'=>$email_id,
                ])
                ->insert();
            return Tools::set_ok('ok');
        });
    }

    /**
     * 删除所有已读邮件
     * api/gameemail/delete
     * 参数：
     */
    function delete(){
        return Mmodel::catchJson(function (){
            //记录存在表示已读
            Db::table(TabConf::$fa_user_email_read_record)
                ->where('openid',api_user_openid())
                ->data([
                    'is_deleted'=>'1',
                ])
                ->update();
            return Tools::set_ok('ok');
        });
    }
}