<?php

namespace loyalsoft;

require_once __DIR__ . '/PayProc/MLogType.php';                                 // 日志类型定义
if (PLAT == 'web') {
    include_once __DIR__ . '/PayProc/WebPayProc.php';
}

/**
 * 付费、游戏内消费相关处理
 * 全部动态交易数据需要保存到db作为[存根]
 * @version <br/>
 *          4.0.0 第四版: 言灵世界 商城模块
 *          2.2.0 第三版: 独立的游戏币<元宝>, 面向多渠道的充值模式, 拉起充值成功后, 给玩家发放元宝. 游戏内部消费元宝,钻石和金币.可以用元宝购买金币和钻石.这一版在上面的PayProc中 * 2015年9月23日 <br/>
 *          2.1.0 mn_pay mn_AddUserCash ... 以 mn_ 打头的函数是第二版, 修改为伪托管模式,除了充值的时候跟腾讯通讯,其余时间自己管理游戏币:钻石. * 2015年7月23日 <br/>
 *          2.0.1 m_pay m_AddUserCash ... 以 m_ 打头的函数是第一版, 完全按照应用宝官方要求托管游戏币的模式开发的.  * 2015年4月23日 <br/>
 *          1.0.0 Created at 2016-4-20. by --gwang <br/>
 * @author gwang (mail@wanggangzero.cn)
 * @copyright © 2016-4-20, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
class PayProc {

    const orderTab = 'tpl_order_tab';

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法 
     */
    public static function procMain() {
        switch (req()->cmd) {
            // 支付相关的活动
            case CmdCode::cmd_pay_getfirstpaygift:                              # 8802 领取首充礼包
                return PayProc::m_GetFirstPayGift();
            // <editor-fold defaultstate="collapsed" desc="  手机多渠道版 mpay  ">
            case CmdCode::cmd_mpay_pay:                                         # 8807 消费..
                return self::m_pay();                                           # ::==> 购买普通商城物品
            case CmdCode::cmd_mpay_get_balance:                                 # 8808 刷新账号余额
                return self::m_refreshChargeOrders();                           # ::==> 刷新订单-领取充值金额
            case CmdCode::cmd_mpay_buyDynamic:                                  # 8809 购买动态商城物品
                return self::m_pay_buyDynamic();
            case CmdCode::cmd_mpay_getDynamic:                                  # 8810 刷新神秘商城物品
                return self::m_pay_getDynamic();
            case CmdCode::cmd_mpay_selfCheckOrders:                             # 8811 自助检查异常订单
                return self::selfhelpCheckOrders();
            case CmdCode::cmd_mpay_getPayRecords:                               # 8812 查询订单记录
                return self::GetPayRecoreds();
// </editor-fold>

            default:
                return Resp::err(ErrCode::cmd_err);
        }
    }

    /**
     * 8812 查询充值记录 
     */
    static function GetPayRecoreds() {
        $uid = req()->uid;
        $table = "tpl_order_tab";
        $arr = daoInst()->select(" cpOrderId as '订单' ,product_name as '道具',amount as '金额' ,from_unixtime(order_ts) as '时间' ")->from($table)
                ->where("uid")->eq($uid)
                ->andWhere("status")->eq(1)
                ->fetchall();
        return Resp::ok($arr);
    }

//
// <editor-fold defaultstate="collapsed" desc="  移动支付 - 刷新订单  ">

    /**
     *  刷新充值订单(领取充值结果) 
     */
    public static function m_refreshChargeOrders() {
# 客户端参数解析
        $uid = req()->uid;                                                      # userID
        $zoneid = req()->zoneid;                                                # 分区Id
        if (count(req()->paras) <= 0) {                                         # 参数为空, 执行自检逻辑
            return self::selfhelpCheckOrders();
        }
        $cpOrderId = req()->paras[0];                                           # 后台订单编号
        if (!$uid || ($zoneid < 1) || strlen($cpOrderId) != 16) {               # 订单编号长16位
            return Resp::err(ErrCode::paras_err);
        }
        $order = daoInst()->select()->from(self::orderTab)                      # 取订单数据
                        ->where('cpOrderId')->eq($cpOrderId)
                        ->limit(1)->fetch();
        if (!$order) {
//            var_dump($order);
            CLog::pay(' 订单不存在! ' . $cpOrderId);
            return Resp::err(ErrCode::pay_order_no);
        }
        if (false === strpos($uid, $order->uid)) {                              # UID不符
            return Resp::err(ErrCode::pay_order_uid);
        }

        if ($order->status != 1) {
            CLog::pay(' 订单尚未完成支付!');
            return Resp::err(ErrCode::pay_order_paystatus);
        }
        if ($order->drawed_ts > 0) {                                             # 定单已经发过货了
            CLog::pay(' 订单重复请求发货!' . $cpOrderId);
            return Resp::err(ErrCode::pay_order_drawed);
        }
        // 准备发货
        $product_id = $order->product_id;
        $product_count = $order->product_count;
        $typid = substr("$product_id", 0, 3);                                   # 提取商品编号前缀
        switch ($typid) {
            case '802':                                                         # 神秘商城物品
                $item = GameConfig::secretshop_typeId_getItem($product_id);
                break;
            case '801':                                                         # 商城物品
                $item = GameConfig::shop_getItem($product_id);
                break;
            default :
                return Resp::err(ErrCode::pay_systembusy_err);
        }
        my_Assert(isset($item), ErrCode::pay_shopItem_cosnt_goods_err);

        if ($item->pricetype != 0) {
            CLog::pay(" 道具付费类型异常:" . $product_id);
            return Resp::err(ErrCode::pay_m_type_err);
        }
        for ($i = 0; $i < $product_count; $i++) {                                       # 默认为1,还没有设计为n的情况
            StoreProc::AddMultiItemInStore($item->goods, 5);                      # 发货
        }
        $n = daoInst()->update(self::orderTab)
                ->data(array('drawed_ts' => now()))
                ->where('cpOrderId')->eq($cpOrderId)
                ->exec();
        my_Assert($n > 0, ErrCode::err_db);                                             # 数据库操作失败

        req()->userInfo->game->baseInfo->charge_amt += $order->amount;                   # 历史充值记录(单位分)
        UserProc::updateUserInfo();                                                     # 更新玩家数据信息
        Event::trigger('pay', array('amt' => $order->amount));                          # 触发充值事件
        CLog::pay("订单发货成功: $cpOrderId");
        return Resp::ok(# 返回
                        array('cash' => req()->userInfo->game->baseInfo->cash, #         # 直接将游戏币的最新值返回给客户端
                            'charge_amt' => req()->userInfo->game->baseInfo->charge_amt, # 充值总记录
                            'store' => req()->userInfo->game->store, #                   # 背包
                            'goods' => $item->goods, #                                  # 发货内容
                            'count' => $product_count, #                                # 发货数量
                        )
        );
    }

    /**
     *  检查内侧订单 
     */
    static function checkDeltest() {
        $uid = req()->uid;
        if (!isset(req()->userInfo->game->privateState->deltest)) {
            $orders = daoInst()->select()->from('tpl_order_tab_deltest')
                    ->where('uid')->eq(substr($uid, strpos($uid, "-") + 1))
                    ->andwhere('status')->eq(1)
                    ->fetchall();
            if (count($orders) > 0) {                                               # 有充值记录
                foreach ($orders as $order) {
                    // 准备补发
                    $product_id = $order->product_id;
                    $product_count = $order->product_count;
                    $typid = substr("$product_id", 0, 3);                           # 提取商品编号前缀

                    switch ($typid) {
                        case '802':                                                 # 神秘商城物品
                            $item = GameConfig::secretshop_typeId_getItem($product_id);
                            break;
                        case '801':                                                 # 商城物品
                            $item = GameConfig::shop_getItem($product_id);
                            break;
                        default :
                            return Resp::err(ErrCode::pay_systembusy_err);
                    }
                    if (!$item) {
                        return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
                    }
                    if ($item->pricetype != 0) {                                    # 并非人民币定价
                        CLog::pay(" 道具付费类型异常:" . $product_id);
                        return Resp::err(ErrCode::pay_m_type_err);
                    }
                    for ($i = 0; $i < $product_count; $i++) {                       # 默认为1,还没有设计为n的情况 
                        EmailProc::SendDelTestMail(req()->zoneid, $uid, $item->name, $item->goods);
                    }
                    CLog::pay(req()->uid . '发送删档内侧补偿邮件' . $order->cpOrderId);
                }
            }
            req()->userInfo->game->privateState->deltest = 0;
        }
    }

    /**
     * 检查异常订单 
     */
    public static function selfhelpCheckOrders() {
        if ('ios' == PLAT) {                                                    # ios版的创建订单时带着前缀了。。。
            $uid = req()->uid;
        } else {
            $uid = substr(req()->uid, strpos(req()->uid, '-') + 1);
        }
        $orders = daoInst()->select()
                ->from('tpl_order_tab')
                ->where('uid')->eq($uid)
                ->andWhere('status')->eq(1)
                ->andWhere('drawed_ts')->le(0)
                ->fetchAll();
        if (count($orders) > 0) {
            foreach ($orders as $order) {
                // 准备补发
                $product_id = $order->product_id;
                $product_count = $order->product_count;
                $typid = substr("$product_id", 0, 3);                           # 提取商品编号前缀
                $goods = '';
                switch ($typid) {
                    case '802':                                                 # 神秘商城物品
                        $item = GameConfig::secretshop_typeId_getItem($product_id);
                        break;
                    case '801':                                                 # 商城物品
                        $item = GameConfig::shop_getItem($product_id);
                        break;
                    default :
                        return Resp::err(ErrCode::pay_systembusy_err);
                }
                if (!$item) {
                    return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
                }
                if ($item->pricetype != 0) {                                    # 并非人民币定价
                    CLog::pay(" 道具付费类型异常:" . $product_id);
                    return Resp::err(ErrCode::pay_m_type_err);
                }
                for ($i = 0; $i < $product_count; $i++) {                       # 默认为1,还没有设计为n的情况
                    StoreProc::AddMultiItemInStore($item->goods, 5);      # 发货
                }
                $goods .= $item->goods;
                $n = daoInst()->update(self::orderTab)                          # 更新订单状态
                        ->data(array('drawed_ts' => now()))
                        ->where('cpOrderId')->eq($order->cpOrderId)
                        ->exec();
                if (!$n) {                                                      # 影响条数应为1
                    CLog::pay(req()->uid . ' 登录补发订单失败 ' . $order->cpOrderId);
                    return Resp::err(ErrCode::err_db);              # 数据库操作失败
                }
                req()->userInfo->game->baseInfo->charge_amt += $order->amount;             # 历史充值记录(单位分)
                UserProc::updateUserInfo();                                 # 回写玩家数据
                CLog::pay(req()->uid . ' 登录补发订单 ' . $order->cpOrderId);
            }
            return Resp::ok(# 返回
                            array('cash' => req()->userInfo->game->baseInfo->cash, #       # 直接将游戏币的最新值返回给客户端
                                'store' => req()->userInfo->game->store, #               # 背包
                                'goods' => $goods, #                                    # 发货内容
                                'count' => count($orders), #                            # 发货数量
                                'msg' => "找到并处理" . count($orders) . "条未发货订单."
            ));
        }
        return Resp::ok('ok');                                      # everything is ok!
    }

// </editor-fold>
//
// <editor-fold defaultstate="collapsed" desc="  移动支付 - 商城购买  ">

    /**
     *  【移动支付】获取神秘商城物品
     *  刷新规则: 根据玩家拥有的英雄、装备、道具等数据进行刷新。（因英雄、道具、装备数量不足以支撑该刷新规则，目前先按照随机刷新做，几率平等）
     */
    public static function m_pay_getDynamic() {
        $user = req()->userInfo->game;
        $userSecretshop = new Info_UserSecretshop($user->userSecretshop);
        // 参数提取
        $refreshType = req()->paras[0];                                          # 刷新类型(参数)0,不刷,1,免费刷,2,钻石刷
        switch ($refreshType) {
            case 1:                                                             # 免费刷
                if (now() < $userSecretshop->lastRefreshTs + glc()->secretshop_refresh_interval) { // 检查是否达到免费刷新时间了, 执行自动更新
                    return Resp::err(ErrCode::pay_secretshopt_freeRefresh_Time);
                }
                break;
            case 2:                                                             # 钻石刷
                if (glc()->secretshop_refresh_maxtimes <= $userSecretshop->refreshedTimes) { // 检查刷新次数, 已达上限, 返回错误信息
                    return Resp::err(ErrCode::pay_refresh_times);
                }                                                               # 可以继续刷新,
                $cishu = $userSecretshop->refreshedTimes + 1;                   # 下次
                $amt = GameConfig::secretshop_refresh_getItem($cishu)->price;
                if (!$user->base()->Consume_Cash($amt)) {                       # 扣除本次所需费用, 余额不足, 返回错误信息
                    return Resp::err(ErrCode::notenough_cash_msg);
                }
                $userSecretshop->refreshedTimes++;                              # 增加当天付费刷新计数
                break;
            case 0:                                                             # 不刷
            default :                                                           # 默认不刷
                // do nothing.
                break;
        }
        if ($refreshType != 0) {                                                   # 是否刷新
            $err = self::refreshDynamicShopItems($userSecretshop);        # 更新物品表
            if ($err) {
                return Resp::err($err);
            }
            $user->userSecretshop = $userSecretshop;
            req()->userInfo->game = $user;
            UserProc::updateUserInfo();
        }
        // 返回最新物品表
        return Resp::ok(array(#                                                 # 成功后将最新的玩家数据返回给客户端
                    'gold' => $user->baseInfo->gold,
                    'tili' => $user->baseInfo->tili,
                    'cash' => $user->baseInfo->cash,
                    'uss' => $userSecretshop, #                                 # 当前神秘商城数据
        ));
    }

    /**
     * 更新神秘商城物品 
     * @param Info_UserSecretshop $userSecretshop Description
     */
    private static function refreshDynamicShopItems(&$userSecretshop) {
        $userSecretshop->lastRefreshTs = now();
        // todo: 这里补完更新物品的函数,     //        第一版: 随机
        $userSecretshop->currentItems = ObjectInit();
        for ($i = 1; $i <= 3; $i++) {                                             # 3种类型的商品
            $arr = GameConfig::secretshop_goodsType_getItem($i);
            if (count($arr) > 0) {
                $err = self::Dice(GameConfig::secretshop_goodsType_getItem($i), 1, $userSecretshop); # 一个种类一次1个物品
                if ($err) {
                    return $err;
                }
            }
        }
        return ErrCode::ok;
    }

    /**
     * 投骰子
     * @param assoc_array $arr 奖池物品
     * @param int $number 提取数量
     * @return string itemid,num;itemid,num;...
     */
    static function Dice($arr, $number, &$userSecretshop) {
        $max = 0;                                                               # 计算物品权重总和
        array_walk($arr, function ($value) use (&$max) {
            $max += $value->probability;
        });
        if (!$max) {                                                            # 配置数据有问题
            return ErrCode::err_const_no;
        }
        for ($i = 0; $i < $number; $i++) {
            $rnd = CommUtil::random(1, $max);                                   # 投骰子
            $start = 0;
            $rew = null;
            foreach ($arr as $item) {                                           # 循环判断落入那个物品上
                if (CommUtil::isPropertyExists($item, 'probability') //         #
                        && $start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
                    $rew = $item;                                               # 记录物品
                    break;
                }
                $start += $item->probability;                                   # 继续判断是否落入下一物品的区间
            } # foreach end
            if (!$rew) {
                return ErrCode::lottery_noselecteditem;
            }
            $id = $rew->typeId;
            $userSecretshop->currentItems->$id = 0;
        } # for end
        return ErrCode::ok;
    }

    /**
     * 清理每日各种上限 
     */
    private static function clearDailyLimits() {
        $userSecretshop = new Info_UserSecretshop(req()->userInfo->game->userSecretshop);
        $userSecretshop->refreshedTimes = 0;
        req()->userInfo->game->userSecretshop = $userSecretshop;
    }

    /**
     * [8809]【移动支付】购买神秘商城物品
     * 规则： 商城物品会刷新 
     */
    public static function m_pay_buyDynamic() {
        $user = new Data_UserGame(req()->userInfo->game);
        $userSecretshop = new Info_UserSecretshop($user->userSecretshop);
        $itemId = req()->paras[0];                                              # 商品id (点一次购买一个)
        $num = 1;                                                               # 暂时固定为一次购买一个
        #
        my_Assert(CommUtil::isPropertyExists($userSecretshop->currentItems, $itemId), ErrCode::pay_secretshop_noitem_err);
        $cishu = $userSecretshop->currentItems->$itemId;                        # 购买次数
        my_Assert($cishu < glc()->secretshop_itembuy_maxtimes, ErrCode::pay_secretshop_buytimes); # 判断购买次数

        $shopItem = GameConfig::secretshop_typeId_getItem($itemId);             # 常量数据
        $amt = $shopItem->price * pow(2, $cishu);                               # 计算价格
        switch ($shopItem->pricetype) {
            case 1:
                my_Assert($user->base()->Consume_Cash($amt), ErrCode::notenough_cash_msg); # 扣除钻石
                break;
            case 2:
                my_Assert($user->base()->Consume_Gold($amt), ErrCode::notenough_gold_msg); # 扣除金币
                break;
            default :
                return Resp::err(ErrCode::pay_m_type_err);
        }
        // 道具解包/发货
        $err = self::expendSecretShopItem($shopItem, $num);                     # 发放商品

        if ($err != ErrCode::ok) {                                              # 发货失败
            return Resp::err($err);
        }
        $userSecretshop->currentItems->$itemId += 1;                            # 购买次数+1
        $user->userSecretshop = $userSecretshop;
        req()->userInfo->game = $user;
        UserProc::updateUserInfo();                                             # 回写数据
//        StatProc::secretShopbuy($req->zoneid, $req->uid, $itemId, $num);        # 统计/监控数据
        return Resp::ok(array(#                                                 # 成功后将最新的玩家数据返回给客户端
                    'gold' => $user->baseInfo->gold,
                    'tili' => $user->baseInfo->tili,
                    'cash' => $user->baseInfo->cash,
                    'store' => $user->baseInfo->store, #                                  # 背包,(装备、碎片、。。)
                    'heros' => $user->baseInfo->heros #                                   # 角色
        ));
        //
    }

    /**
     *
     * @param sm_secretshop $shopItem
     * @param type $num 
     */
    private static function expendSecretShopItem($shopItem, $num) {
        for ($i = 0; $i < $num; $i++) {
            $err = StoreProc::AddMultiItemInStore($shopItem->goods, '神秘商城');
            if ($err) {
                return $err;
            }
        }
        return ErrCode::ok;
    }

    /**
     * [8807]【移动支付】 购买普通商城物品 
     */
    public static function m_pay() {
        $user = req()->userInfo->game;                                          # user引用
        my_Assert(count(req()->paras) >= 3, ErrCode::parasnotenough_msg);       # 参数不足

        list($paytype, $itemId, $num) = req()->paras;                           # 付费类型, 1.钻石, 2.金币 ,道具ID ,数量, 默认为1
        my_Assert($num >= 1, ErrCode::paras_err);                               # 参数非法,
        $shopItem = GameConfig::shop_getItem($itemId);                          # 取商品的常量数据
        my_Assert(isset($shopItem), ErrCode::err_const_no);                     # 检测是否存在道具的常量数据
        my_Assert($shopItem->pricetype == $paytype, ErrCode::pay_price_err);    # 商品定价类型检查
        $amt = $shopItem->price;                                                # 道具价格（钻石）
        my_Assert($amt > 0, ErrCode::pay_price_err);                            # 商品定价数值检查
        $bDeal = false;                                                         # 成交标志位
        switch ($paytype) {                                                     # 1. 钻石, 2. 金币, other:非法
            case 1:                                                             # 钻石
                $bDeal = req()->userInfo->game->base()->Consume_Cash($amt);     # 更新(扣除)玩家游戏币(钻石)余额
                my_Assert($bDeal, ErrCode::notenough_cash_msg);                 # 防范扣除失败
                break;
            case 2:                                                             # 金币
                $bDeal = $user->base()->Consume_Gold($amt * $num);        # 更新玩家金币余额
                my_Assert($bDeal, ErrCode::notenough_gold_msg);                 # 防范扣除失败
                break;
            default :                                                           # 未知的支付类型
                return Resp::err(ErrCode::pay_m_type_err);
        }

        my_Assert($bDeal, ErrCode::err_innerfault);                             # 交易失败
        $err = self::expendShopItem($shopItem, $num);                           # 发放商品(普通商城只有金币和体力)
        my_Assert(ErrCode::ok == $err, $err);                                   # 防范发货失败
        UserProc::updateUserInfo();                                             # 回写数据
//            StatProc::shopbuy($zoneid, $uid, $itemId, $num);                    # 统计/监控数据
        return Resp::ok(array(#                                                 # 成功后将最新的玩家数据返回给客户端
                    'gold' => $user->baseInfo->gold,
                    'tili' => $user->baseInfo->tili,
                    'cash' => $user->baseInfo->cash,
                    'store' => $user->store
        ));
    }

    /**
     * 解包商城物品
     * @param sm_Shop $shopItem
     * @param int $num 数量 
     */
    static function expendShopItem($shopItem, $num) {
        for ($i = 0; $i < $num; $i++) {
            $err = StoreProc::AddMultiItemInStore($shopItem->goods, '商城');
            my_Assert(ErrCode::ok == $err, $err);
        }
        return ErrCode::ok;
    }

// </editor-fold>
// 
    //
// <editor-fold defaultstate="collapsed" desc="  支付活动 - 首付礼包">

    /**
     * [8802] 领取首付礼包 
     */
    public static function m_GetFirstPayGift() {
        $itemId = glc()->FirstPay_ItemId;
        $privateState = req()->userInfo->game->privateState;

        if (CommUtil::isPropertyExists($privateState, "firstPayGift") #
                && $privateState->firstPayGift) {                               # 如果已经领取首付礼包
            return Resp::err(ErrCode::pay_firstpaygetted);
        }
        my_Assert(req()->userInfo->game->baseInfo->charge_amt > 0, ErrCode::pay_firstpayno_err);                            # 如果尚未完成首付

        $itemModel = GameConfig::shop_getItem($itemId);                         # 取商品常量
        if ($itemModel == null) {                                               # 检测首付礼包是否存在
            return Resp::err(ErrCode::err_const_no);
        }
        req()->userInfo->game->privateState->firstPayGift = true;               # 设置首付标志
        StoreProc::AddMultiItemInStore($itemModel->goods);                      # 发放首付礼包到玩家仓库
        UserProc::updateUserInfo();                                             # 更新玩家数据
        return Resp::ok(array('itemid' => $itemId,
                    'rewardstr' => $itemModel->goods,
                    'store' => req()->userInfo->game->store));                   # 回送成功信息
    }

// </editor-fold>
//
//
//
}
