<?php

namespace App\Facades\Wo;

use App\Jobs\TicketBack;
use App\Models\WoCoupon;

class Action extends Wo
{
    /**
     * [ticketGrant 沃支付发券接口]
     * @author 玄尘 2020-02-13
     * @param  [type]  $data    [post 过来的数据 array]
     * @return [type]           [description]
     */
    public function ticketGrant($data)
    {
        $mobile   = $data['mobile'];
        $activity = $data['activity'];
        $type     = $data['type'];
        $user     = $data['user'];

        // $activity .= '--';

        //如果是话费券
        if ($type == 'phoneTicket') {
            $info = WoCoupon::where('mobile', $mobile)->where('status', 1)->where('activityId', $activity)->first();
            if ($info) {
                return '已经领取过了';
            }
        }

        $logNo  = 'YSD' . $this->getMsecTime();
        $params = [
            'charset'         => $this->charset,
            'version'         => $this->version,
            'merchantSign'    => '',
            'merchantCert'    => '',
            'signType'        => $this->signType,
            'service'         => 'ticketGrant',
            'requestId'       => $logNo,
            'merchantId'      => $this->merchant_id,
            'activityId'      => $activity,
            'mobilepNo'       => $mobile,
            'ticketAmt'       => $data['ticketAmt'],
            'logNo'           => $logNo,
            'channelType'     => $this->channelType,
            'transactionDate' => now()->format('Ymd'),
            'effectDate'      => now()->format('Ymd'),
        ];

        $merchantCert = $this->merchantCert();
        if (!$merchantCert) {
            return '证书文件有错误';
        }

        $couponData = [
            'user_id'    => $user->id,
            'number'     => $data['number'] ?? '',
            'service'    => 'ticketGrant',
            'ticketAmt'  => $data['ticketAmt'] ?? '0.00',
            'mobile'     => $mobile,
            'logNo'      => $params['logNo'],
            'activityId' => $activity,
        ];

        //记录
        $coupon = WoCoupon::create($couponData);

        //签名
        $merchantSign = $this->rsaSign($params);
        if (is_array($merchantSign)) {
            return $merchantSign['msg'];
        }

        //验签
        // $checkdata = $params;
        // $res       = $this->rsaCheck($checkdata, $merchantSign);
        // dd($res);
        $params['merchantSign'] = $merchantSign;
        $params['merchantCert'] = $merchantCert;

        $res = $this->getWoData($params);

        //判断是否是数组，不是数组返回错误信息
        if (!is_array($res)) {
            $coupon->remark = $res;
            $coupon->status = 2;
            $coupon->save();
            return $res;
        }

        //判断是否成功
        $status           = ($res['rspCode'] == 'MKM00000') ? 1 : 2;
        $coupon->remark   = $res['rspMessage'];
        $coupon->status   = $status;
        $coupon->ticketId = $res['ticketId'] ?? '';
        $coupon->serialNo = $res['serialNo'] ?? '';
        $coupon->save();

        if ($status == 1) {
            $ret = [
                'logNo'      => $coupon->logNo,
                'rspMessage' => $res['rspMessage'],
            ];
            return $ret;
        } else {
            // PhoneTicket::dispatch($data);
            return $res['rspMessage'];
        }
    }

    /**
     * 查询券
     * @param  [type] $data     [description]
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function ticketQuery($data, $user_id)
    {
        $logNo = $data['logNo'];

        if (!$logNo) {
            return '缺少发券流水号';
        }

        $info = WoCoupon::where('logNo', $logNo)->first();

        if (!$info) {
            return '没有查询到信息';
        }

        if ($info->user_id != $user_id) {
            return '这不是您的券。';
        }

        $params = [
            'charset'      => $this->charset,
            'version'      => $this->version,
            'merchantCert' => '',
            'merchantSign' => '',
            'signType'     => $this->signType,
            'service'      => 'ticketQuery',
            'requestId'    => $this->getMsecTime(),
            'merchantId'   => $this->merchant_id,
            'logNo'        => $info->logNo,
            'channelType'  => $this->channelType,
        ];

        $merchantCert = $this->merchantCert();
        if (!$merchantCert) {
            return '证书文件有错误';
        }

        $merchantSign = $this->rsaSign($params);
        if (is_array($merchantSign)) {
            return $merchantSign['msg'];
        }

        $params['merchantSign'] = $merchantSign;
        $params['merchantCert'] = $merchantCert;
        $res                    = $this->getWoData($params);

        if (is_array($res)) {
            if ($res['rspCode'] == 'MKM00000') {
                $ret = [
                    'ticketState'   => $res['ticketState'],
                    'ticketBalance' => $res['ticketBalance'],
                    'ticketAmt'     => $res['ticketAmt'],
                    'expireDate'    => $res['expireDate'],
                    'ticketId'      => $res['ticketId'],
                    'ticketName'    => $res['ticketName'],
                    'activityId'    => $res['activityId'],
                    'logNo'         => $params['logNo'],
                    'mobile'        => $info->mobile,
                ];
                return $ret;
            } else {
                return $res['rspMessage'];
            }
        } else {
            return $res;
        }
    }

    /**
     * 退业务
     * @author 玄尘 2020-02-13
     * @param  [type] $data [description]
     * @param  [type] $user [description]
     * @return [type]       [description]
     */
    public function ticketBack($data, $user)
    {
        $ologNo = $data['ologNo'];

        $info = WoCoupon::where('logNo', $ologNo)->where('status', 1)->first();

        if (!$info) {
            return '没有查询到信息';
        }

        $logNo  = 'YSD' . $this->getMsecTime();
        $params = [
            'charset'         => $this->charset,
            'version'         => $this->version,
            'merchantSign'    => '',
            'merchantCert'    => '',
            'signType'        => $this->signType,
            'service'         => $data['service'],
            'requestId'       => $logNo,
            'merchantId'      => $this->merchant_id,
            'activityId'      => $info->activityId,
            'mobilepNo'       => $info->mobile,
            'ologNo'          => $data['ologNo'] ?? '',
            'logNo'           => $logNo,
            'channelType'     => $this->channelType,
            'transactionDate' => now()->format('Ymd'),
        ];

        if (!empty($data['ticketAmt'])) {
            // $params['ticketAmt'] = $data['ticketAmt'];
        }

        $merchantCert = $this->merchantCert();
        if (!$merchantCert) {
            return '证书文件有错误';
        }

        $couponData = [
            'user_id'    => $user->id,
            'number'     => $data['number'] ?? '',
            'service'    => $params['service'],
            'ticketAmt'  => $info->ticketAmt ?? '0.00',
            'mobile'     => $params['mobilepNo'],
            'ologNo'     => $params['ologNo'],
            'logNo'      => $params['logNo'],
            'activityId' => $params['activityId'],
        ];

        //记录
        $coupon = WoCoupon::create($couponData);

        //签名
        $merchantSign = $this->rsaSign($params);
        if (is_array($merchantSign)) {
            return $merchantSign['msg'];
        }

        $params['merchantSign'] = $merchantSign;
        $params['merchantCert'] = $merchantCert;

        $res = $this->getWoData($params);

        //判断是否是数组，不是数组返回错误信息
        if (!is_array($res)) {
            $coupon->remark = $res;
            $coupon->status = 2;
            $coupon->save();
            return $res;
        }

        //判断是否成功
        $status           = ($res['rspCode'] == 'MKM00000') ? 1 : 2;
        $coupon->remark   = $res['rspMessage'];
        $coupon->status   = $status;
        $coupon->ticketId = $res['ticketId'] ?? '';
        $coupon->serialNo = $res['serialNo'] ?? '';
        $coupon->save();

        if ($status == 1) {
            //退订成功
            // $info->ticketAmt = $info->ticketAmt . '/' . $res['ticketAmt'];
            $info->status = 3;
            $info->remark = $info->remark . '/已经退订';
            $info->save();

            $ret = [
                'alreadyconAmt' => $res['alreadyconAmt'],
                'ticketAmt'     => $res['ticketAmt'],
                'rspMessage'    => $res['rspMessage'],
            ];
            return $ret;

        } else {
            // TicketBack::dispatch($data);
            return $res['rspMessage'];
        }
    }
}
