<?php

namespace App\Api\Controllers;

use App\Facades\Wo\Log as LogFacade;
use Illuminate\Http\Request;
use Wo;

class WoController extends Controller
{

    /**
     * 180元的话费券  一次发放 活动编号 97LJ202018
     * @author 玄尘 2020-03-09
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function grant180(Request $request)
    {
        $type = 'phoneTicket';

        $log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志

        $res = $this->checkSign($request);
        if (!is_array($res)) {
            return $this->error($res, $log);
        }

        $validator = \Validator::make($res, [
            'mobile' => 'required',
        ], [
            'mobile.required' => '缺少手机号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        $res['user']      = $this->user;
        $res['activity']  = '97LJ202018';
        $res['ticketAmt'] = '180';
        $res['type']      = $type;

        $res = Wo::ticketGrant($res);

        if (is_string($res)) {
            return $this->error($res, $log);
        }

        return $this->success($res, $log);
    }

    /**
     * 电子提货券 一共600 每期发放25元  活动编号 97LJ202025
     * @author 玄尘 2020-03-09
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function grant25(Request $request)
    {

        $type = 'pickTicket25';

        $log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志

        $res = $this->checkSign($request);
        if (!is_array($res)) {
            return $this->error($res, $log);
        }

        $validator = \Validator::make($res, [
            'mobile' => 'required',
        ], [
            'mobile.required' => '缺少手机号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        $res['user']      = $this->user;
        $res['activity']  = '97LJ202025';
        $res['ticketAmt'] = '25';
        $res['type']      = $type;

        $res = Wo::ticketGrant($res);

        if (is_string($res)) {
            return $this->error($res, $log);
        }
        return $this->success($res, $log);
    }

    /**
     * 电子提货券 每次100元 活动编号 97LJ202010
     * @author  玄尘 2020-03-09
     * @param Request $request [description]
     * @return  [type]           [description]
     */
    public function grant100(Request $request)
    {
        $type = 'pickTicket100';

        $log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志

        $res = $this->checkSign($request);
        if (!is_array($res)) {
            return $this->error($res, $log);
        }

        $validator = \Validator::make($res, [
            'mobile' => 'required',
        ], [
            'mobile.required' => '缺少手机号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        $res['user']      = $this->user;
        $res['activity']  = '97LJ202010';
        $res['ticketAmt'] = '100';
        $res['type']      = $type;

        $res = Wo::ticketGrant($res);

        if (is_string($res)) {
            return $this->error($res, $log);
        }

        return $this->success($res, $log);

    }

    /**
     * 10元购物券
     * @author  玄尘 2020-06-11
     * @param Request $request [description]
     * @return  [type]           [description]
     */
    public function grant10(Request $request)
    {

        $type = 'pickTicket10';

        $log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志

        $res = $this->checkSign($request);
        if (!is_array($res)) {
            return $this->error($res, $log);
        }

        $validator = \Validator::make($res, [
            'mobile' => 'required',
        ], [
            'mobile.required' => '缺少手机号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        $res['user']      = $this->user;
        $res['activity']  = '97LJ202001';
        $res['ticketAmt'] = '10';
        $res['type']      = $type;

        $res = Wo::ticketGrant($res);

        if (is_string($res)) {
            return $this->error($res, $log);
        }
        return $this->success($res, $log);
    }

    //退业务
    public function cancel(Request $request)
    {
        $log = LogFacade::insert($request->url(), 'POST', $request->all(), 'refund'); //添加日志

        $postData = $this->checkSign($request);
        if (!is_array($postData)) {
            return $this->error($postData, $log);
        }

        $validator = \Validator::make($postData, [
            'logNo' => 'required',
        ], [
            'logNo.required' => '缺少发券流水号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        //查询状态
        $res = Wo::ticketQuery($postData, $this->user->id);
        if (is_string($res)) {
            return $this->error($res, $log);
        }

        $ConfigTicketState     = config('wo.ticketState');
        $ConfigTicketStateText = config('wo.ticketStateText');
        $ticketState           = $res['ticketState'];

        if (!in_array($ticketState, ['A', 'U'])) {
            $msg = (array_key_exists($ticketState, $ConfigTicketStateText)) ? $ConfigTicketStateText[$ticketState] : '未知状态';
            return $this->error($msg, $log);
        }

        //判断数组中是否存在下标
        if (!array_key_exists($ticketState, $ConfigTicketState)) {
            return $this->error('未知状态', $log);
        }

        $postData['service'] = $ConfigTicketState[$ticketState];
        $postData['ologNo']  = $postData['logNo'];
        $postData['user']    = $this->user;

        //退业务
        $res = Wo::ticketBack($postData, $this->user);

        if (is_string($res)) {
            return $this->error($res, $log);
        }
        return $this->success($res, $log);
    }

    /**
     * 查询状态
     * @author 玄尘 2020-03-13
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function query(Request $request)
    {
        $log = LogFacade::insert($request->url(), 'POST', $request->all(), 'refund'); //添加日志

        $postData = $this->checkSign($request);
        if (!is_array($postData)) {
            return $this->error($postData, $log);
        }

        $validator = \Validator::make($postData, [
            'logNo' => 'required',
        ], [
            'logNo.required' => '缺少发券流水号',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), $log);
        }

        //查询状态
        $res = Wo::ticketQuery($postData, $this->user->id);
        if (is_string($res)) {
            return $this->error($res, $log);
        }
        return $this->success($res, $log);
    }

}
