<?php
/**
 * PtbController.php UTF-8
 * 平台币
 *
 * @date    : 2018/6/2 15:46
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : luowei <lw@huosdk.com>
 * @version : HUOSDK 8.0
 */

namespace api\cfloat\controller;

use api\common\controller\CFloatBaseController;
use huo\controller\common\CommonFunc;
use huo\controller\common\HuoSession;
use huo\controller\member\Member;
use huo\controller\member\MemCache;
use huo\controller\wallet\Ptb;
use huolib\constant\DeviceTypeConst;
use huolib\constant\FormatConst;
use huolib\constant\OrderConst;
use huolib\constant\WalletConst;
use huolib\pay\Pay;
use huolib\status\CommonStatus;
use huolib\status\OrderStatus;
use huolib\tool\StrUtils;
use huolib\utils\OrderUtils;
use think\Exception;

class PtbController extends CFloatBaseController {
    private $mem_data;

    public function _initialize() {
        parent::_initialize();
        $this->checkLogin();
        $this->mem_data = (new Member())->getMemInfo($this->mem_id);
        if (FormatConst::FORMAT_HTML == $this->response_type) {
            $this->assign('userinfo', $this->mem_data);
        }
    }

    /**
     * 平台消费记录列表
     * http://doc.1tsdk.com/138?page_id=3403
     * 【域名】/cfloat/ptb/consume_record
     */
    public function consumeRecord() {
        $_mem_id = $this->mem_id;
        $_page = get_val($this->rq_data, 'page', 0);  /* 页码 默认为1 代表第一页	1 */
        $_offset = get_val($this->rq_data, 'offset', 10);  /* 每页显示数量 默认为10 */

        $_map = [];
        $_page = $_page.','.$_offset;
        $_rdata = (new Ptb())->getMemConsumeList($_mem_id, $_map, $_page);

        return $this->returnData($_rdata);
    }

    /**
     * 平台充值记录列表
     * http://doc.1tsdk.com/138?page_id=3405
     * 【域名】/cfloat/ptb/recharge_record
     */
    public function rechargeRecord() {
        $_mem_id = $this->mem_id;
        $_page = get_val($this->rq_data, 'page', 0);  /* 页码 默认为1 代表第一页	1 */
        $_offset = get_val($this->rq_data, 'offset', 10);  /* 每页显示数量 默认为10 */
        $_status = get_val($this->rq_data, 'status', 0);

        $_map = [];
        $_order_status = array_keys(OrderConst::getPayStatusMsg(0,true));
        if (in_array($_status, $_order_status)) {
            $_map['status'] = $_status;
        }
        $_page = $_page.','.$_offset;
        $_where['type'] = WalletConst::WALLET_TYPE_CHARGE;
        $_rdata = (new Ptb())->getMemChargeList($_mem_id, $_map, $_page);

        return $this->returnData($_rdata);
    }

    /**
     * 平台币充值
     * http://doc.1tsdk.com/138?page_id=3406
     * 【域名】/cfloat/ptb/recharge
     */
    public function recharge() {
        $_mc_class = MemCache::ins();
        $_me_data = $_mc_class->getMeInfoById($this->mem_id);
        $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
        $_pay_type = (new Ptb())->getPayWays(0);
        $_rdata = [
            'code' => CommonStatus::NO_ERROR,
            'msg' => CommonStatus::getMsg(CommonStatus::NO_ERROR),
            'data' => [
                'ptb_cnt' => isset($_me_data['ptb_cnt']) ? StrUtils::formatNumber($_me_data['ptb_cnt']) : 0,
                'ptb_rate' => $_ptb_rmb_rate,
                'pay_type' => $_pay_type
            ],
        ];

        return $this->returnData($_rdata);
    }

    /**
     * 选择支付进行支付
     * http://doc.1tsdk.com/138?page_id=3407
     * 【域名】/cfloat/ptb/pay/post
     */
    public function payPost() {
        $_payway = $this->request->param('payway/s', '');
        $_amount = $this->request->param('amount/f', 0);
        $_pw_check_rs = OrderUtils::checkPayway($_payway);
        if (OrderStatus::NO_ERROR != $_pw_check_rs) {
            $this->error(OrderStatus::getMsg($_pw_check_rs), [], $_pw_check_rs);
        }
        $_amount_chk_rs = OrderUtils::checkAmount($_amount);
        if (OrderStatus::NO_ERROR != $_amount_chk_rs) {
            $this->error(OrderStatus::getMsg($_amount_chk_rs), [], $_amount_chk_rs);
        }
        $_type = WalletConst::WALLET_FROM_CHARGE;
        $_rdata = (new Ptb())->preorder($this->mem_id, $_amount, $_type, $_payway);
        if (CommonStatus::NO_ERROR != $_rdata['code']) {
            $this->returnData($_rdata);
        }
        $_order_id = $_rdata['data']['order_id'];
        try {
            $_pay_class = Pay::ins()->get($_payway);
            $_func = $this->getFunc($this->device_type);
            $_show_url = url('cfloat/' . $_payway.'/show', ['order_id' => $_order_id], false, APISITE);
            $_return_url = url(
                'cfloat/' . $_payway.'/return', ['order_id' => $_order_id], false, APISITE
            );
            $_pay_class->setReturnUrl($_return_url);
            $_pay_class->setShowUrl($_show_url);
            $_pay_class->setOrderId($_order_id);
            $_product_name = config('CURRENCY_NAME').'充值'.$_amount;
            $_pay_class->setProductName($_product_name);
            $_pay_class->setProductDesc($_product_name);
            $_pay_class->setRealAmount($_amount);
            $_pay_class->setProductId(WalletConst::WALLET_PRODUCT_MEM_PTB);
            $_pay_class->setIp($this->request->ip());
            $_pay_class->setOpenId((new HuoSession($this->mem_id))->getOpenId());
            $_pay_rs = $_pay_class->$_func();
            if (empty($_pay_rs)) {
                $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
                $this->error(OrderStatus::getMsg($_code), [], $_code);
            }
            $_code = OrderStatus::NO_ERROR;
            $this->success(OrderStatus::getMsg($_code), $_pay_rs, $_code);
        } catch (Exception $e) {
            $_code = OrderStatus::PAYWAY_NOT_EXISTS;
            $this->error(OrderStatus::getMsg($_code), [], $_code);
        }
    }

    /**
     * 查询支付结果
     * http://doc.1tsdk.com/138?page_id=3408
     * 【域名】/cfloat/ptb/order/query
     */
    public function orderQuery() {
        $_order_id = $this->request->param('order-order_id/s', '');
        $_order = new Ptb();
        $_rdata = $_order->getStatus($_order_id);
        if (false == $_rdata) {
            $this->error(lang('ERROR'));
        }
        $_rdata['order_id'] = $_order_id;
        $this->success(lang('SUCCESS'), $_rdata);
    }

    /**
     * @param $device_type
     *
     * @return string clientPay mobilePay pcPay
     */
    private function getFunc($device_type) {
        switch ($device_type) {
            case DeviceTypeConst::DEVICE_TYPE_IOSAPPLESDK:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_IOSSDK:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_IOSAPP:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_IOSH5APP:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_SAFARI:
                $_type = 'mobilePay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_ANDSDK:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_ANDAPP:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_ANDH5APP:
                $_type = 'clientPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_ANDBROWSER:
                $_type = 'mobilePay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_WEIXIN:
                $_type = 'jsPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_PC:
                $_type = 'pcPay';
                break;
            case DeviceTypeConst::DEVICE_TYPE_WAP:
                $_type = 'mobilePay';
                break;
            default:
                $_type = 'mobilePay';
        }

        return $_type;
    }
}