<?php

namespace loyalsoft;

/**
 * Description of ShopProc
 * 商城
 * @author gwang
 * @version 1.0.0  Created.  2020.8.19 -gwang
 */
class ShopProc {

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法 
     */
    public static function procMain() {
        switch (req()->cmd) {
            case CmdCode::shop_limit_maininfo:
                return self::GetLimitMainInfo();
            case CmdCode::shop_limit_buy:
                return self::BuyLimitPackage();
            case CmdCode::shop_monthlyVIP_Info:
                return self::GetMonthlyVipInfo();
            case CmdCode::shop_monthlyVIP_Buy:
                return self::BuyMonthlyVip();

            default:
                Err(ErrCode::cmd_err);
        }
    }

    public static function GetMonthlyVipInfo() {
        return Resp::ok(array(
                    "shopdata" => req()->userInfo->game->shopdata
        ));
    }

    public static function BuyMonthlyVip() {
        $packageId = req()->paras[0];                                            # 参数; 礼包id
        $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
        $packageCfg = GameConfig::shop_monthVIP_getItem($packageId);
        my_Assert(null != $packageCfg, ErrCode::err_const_no);
        if ($packageCfg->subType == 4) {
            my_Assert($shopdata->monthlyVIP1_ts + $packageCfg->ExpireTs * 3600 * 24 < now(), ErrCode::shop_monthlyvip_buyed);
        } elseif ($packageCfg->subType == 5) {
            my_Assert($shopdata->monthlyVIP2_ts + $packageCfg->ExpireTs * 3600 * 24 < now(), ErrCode::shop_monthlyvip_buyed);
        }
        $err = StoreProc::AddMultiItemInStore($packageCfg->pri_reward);         # 发放一次性奖励
        my_Assert(ErrCode::ok == $err, $err);
        if ($packageCfg->subType == 4) {
            $shopdata->monthlyVIP1_ts = 3600 * 24 * (tsDay() + 1) - 1;
        } elseif ($packageCfg->subType == 5) {
            $shopdata->monthlyVIP2_ts = 3600 * 24 * (tsDay() + 1) - 1;
        }
        req()->userInfo->game->shopdata = $shopdata;                             # 回写数据
        UserProc::updateUserInfo();
        $user = req()->userInfo->game;
        return Resp::ok(array(
                    "reward" => $packageCfg->pri_reward,
                    "shopdata" => $shopdata,
                    'gold' => $user->baseInfo->gold,
                    'cash' => $user->baseInfo->cash,
                    'tili' => $user->baseInfo->tili,
                    'store' => $user->store,
                    'hero' => $user->heros
        ));
    }

    /**
     * 【7101】限购礼包主界面 
     */
    public static function GetLimitMainInfo() {
        return Resp::ok(array(
                    "shopdata" => req()->userInfo->game->shopdata
        ));
    }

    /**
     * 【7102】 购买限购礼包 
     */
    public static function BuyLimitPackage() {
        $packageId = req()->paras[0];                                            # 参数; 礼包id
        $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
        $packageCfg = GameConfig::shop_limit_getItem($packageId);
        my_Assert(null != $packageCfg, ErrCode::err_const_no);

        $arr = array();
        switch ((int) $packageCfg->subType) {
            case 1:
                $shopdata->purchasedDailyLimitPackages[] = $packageId;
                $arr = $shopdata->purchasedDailyLimitPackages;
                break;
            case 2:
                $shopdata->purchasedWeeklyLimitPackages[] = $packageId;
                $arr = $shopdata->purchasedWeeklyLimitPackages;
                break;
            case 3:
                $shopdata->purchasedMonthlyLimitPackages[] = $packageId;
                $arr = $shopdata->purchasedMonthlyLimitPackages;
                break;
            default:
                Err(ErrCode::err_innerfault);
        }
        $hasN = count(array_filter($arr, function ($val)use ($packageId) {
                    return $val == $packageId;
                }));
        my_Assert($hasN <= $packageCfg->limit_num, ErrCode::shop_limit_max);
        $itemid = explode(',', $packageCfg->reward)[0];                         # 奖励礼包Id
        $itemMO = GameConfig::item_package_getItem($itemid);
        my_Assert(null != $itemMO, ErrCode::err_const_no);
        $err = StoreProc::AddMultiItemInStore($itemMO->contents);               # 发放奖励
        my_Assert(ErrCode::ok == $err, $err);
        req()->userInfo->game->shopdata = $shopdata;                             # 回写数据
        UserProc::updateUserInfo();
        $user = req()->userInfo->game;
        return Resp::ok(array(
                    "reward" => $itemMO->contents,
                    "shopdata" => $shopdata,
                    'gold' => $user->baseInfo->gold,
                    'cash' => $user->baseInfo->cash,
                    'tili' => $user->baseInfo->tili,
                    'store' => $user->store,
                    'hero' => $user->heros
        ));
    }

    /**
     * 每日检查 
     */
    static function DailyCheck() {
        CLog::info("shop-每日检查-清除每日/周限量礼包购买记录");
        $zoneid = req()->zoneid;
        $uid = req()->uid;
        $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
        $shopdata->purchasedDailyLimitPackages = array();                       # 每天重置
        if (date("N") == 1) {                                                   # 周一重置
            $shopdata->purchasedWeeklyLimitPackages = array();
        }
        if (date("d") == 1) {                                                   # 1号重置
            $shopdata->purchasedMonthlyLimitPackages = array();
        }

        if ($shopdata->monthlyVIP1_ts + 30 * 24 * 3600 > now()) {
            $id = 901001;
            $card1 = GameConfig::shop_monthVIP_getItem($id);
            my_Assert(null != $card1, ErrCode::err_const_no);
            EmailProc::SendMonthlyVIPDailyReward($zoneid, $uid, $card1->name, $card1->daily_reward);
        }
        if ($shopdata->monthlyVIP2_ts + 30 * 24 * 3600 > now()) {
            $id = 901002;
            $card2 = GameConfig::shop_monthVIP_getItem($id);
            my_Assert(null != $card2, ErrCode::err_const_no);
            EmailProc::SendMonthlyVIPDailyReward($zoneid, $uid, $card2->name, $card2->daily_reward);
        }
    }

}
