<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 上午11:36
 */

namespace App\Http\Controllers\Channel\Finance;


use App\Http\Controllers\Channel\Finance\Transformers\CashAccountTransformer;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\Finance\Services\BankService;
use App\Modules\Finance\Services\CashAccountService;
use Illuminate\Http\Request;
use Cache;
use Redis;
use App\Libs\SMS;
use Log;
use App\Libs\AliSMS;

class CashAccountsController extends BaseController
{
    /**
     * @apiDefine Finance 结算提现模块
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取银行账号
     * @api {GET} getCashAccount 获取银行账号
     * @apiGroup Finance
     * @apiName getCashAccount
     * @apiSuccess   {String}  account_name 姓名.
     * @apiSuccess   {String}  identity_card 身份证号码.
     * @apiSuccess   {String}  card_number 银行卡号码.
     * @apiSuccess   {String}  account_bank 开户支行名称.
     * @apiSuccess   {Number}  bank_id 银行ID.
     * @apiSuccess   {String}  bank 交易银行.
     * @apiSuccess   {String}  phone 电话号码.
     * @apiSuccess   {String}  province 省份.
     * @apiSuccess   {Number}  status 省份.状态; -1:禁止编辑; 0:默认可编辑
     * @apiSuccess   {Number}  is_company 0:对私账户; 1:对公账户
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *              {
     *                   "account_bank": "钱江支行",
     *                   "account_name": "宋晓",
     *                   "bank_id": 1,
     *                   "bank": "杭州银行",
     *                   "card_number": "6222520177654916",
     *                   "identity_card": "33038119930901821X",
     *                   "province": "北京市",
     *                   "status": -1,
     *                   "phone": "13688998899",
     *                   "is_company": 0,
     *                   "updated_time": "2017-11-20 14:54:50"
     *               }
     *     }
     */
    function get_cashAccount(Request $request) {
        $distribution_channel_id = $this->getChannelId();
        $distribution_channel_name = $this->getChannelName();
        $distribution_channel_name = '';


        $cashAccount = CashAccountService::getCashAccountSingle($distribution_channel_id);
        return response()->item(new CashAccountTransformer(), $cashAccount);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加更新银行账号
     * @api {POST} addCashAccount 添加银行账号
     * @apiGroup Finance
     * @apiName addCashAccount
     * @apiParam   {Number}  [is_company] 0:对私账户; 1:对公账户
     * @apiParam   {Number}  bank_id 银行ID.
     * @apiParam   {String}  account_bank 开户支行名称.
     * @apiParam   {String}  account_name 对私账户:开户人名称;  对公账户:持卡人名称
     * @apiParam   {String}  card_number 银行卡号码.
     * @apiParam   {String}  [phone] 电话号码.
     * @apiParam   {String}  [identity_card] 对私账户:开户人身份证号码;   对公账户:持卡人身份证号码
     * @apiParam   {String}  [province] 省份.
     * @apiParam   {String}  [sms_code] 验证码,修改的时候必传.
     *
     * @apiSuccess   {String}  account_name 姓名.
     * @apiSuccess   {String}  identity_card 身份证号码.
     * @apiSuccess   {String}  card_number 银行卡号码.
     * @apiSuccess   {String}  account_bank 开户支行名称.
     * @apiSuccess   {Number}  bank_id 银行ID.
     * @apiSuccess   {String}  bank 交易银行.
     * @apiSuccess   {String}  phone 电话号码.
     * @apiSuccess   {String}  province 省份.
     * @apiSuccess   {Number}  status 省份.状态; -1:禁止编辑; 0:默认可编辑
     * @apiSuccess   {Number}  is_company 0:对私账户; 1:对公账户
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *              {
     *                   "account_bank": "钱江支行",
     *                   "account_name": "宋晓",
     *                   "bank_id": 1,
     *                   "bank": "杭州银行",
     *                   "card_number": "6222520177654916",
     *                   "identity_card": "33038119930901821X",
     *                   "province": "北京市",
     *                   "status": -1,
     *                   "phone": "13688998899",
     *                   "is_company": 0,
     *                   "updated_time": "2017-11-20 14:54:50"
     *               }
     *     }
     */
    function add_cashAccount(Request $request) {
        $distribution_channel_id = $this->getChannelId();
        $channel_id = $distribution_channel_id;
        $distribution_channel_name = $this->getChannelName();
        $distribution_channel_name = '';


        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }
        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        if(empty($account_name)) {
            return response()->error("PARAM_EMPTY");
        }
        $identity_card = $request->has('identity_card') ? $request->input('identity_card') : '';
//        if(empty($identity_card)) {
//            return response()->error("PARAM_EMPTY");
//        }
        $card_number = $request->has('card_number') ? $request->input('card_number') : '';
        if(empty($card_number)) {
            return response()->error("PARAM_EMPTY");
        }
        $account_bank = $request->has('account_bank') ? $request->input('account_bank') : '';
//        if(empty($account_bank)) {
//            return response()->error("PARAM_EMPTY");
//        }
        $bank_id = $request->has('bank_id') ? $request->input('bank_id') : '';
        if(!is_numeric($bank_id)) {
            return response()->error("PARAM_ERROR");
        }

        $bankModel = BankService::getBankSingle($bank_id);
        if(empty($bankModel)) {
            return response()->error("PARAM_ERROR");
        }
        $is_company = $request->has('is_company') ? $request->input('is_company') : '';

        $bank = $request->has('bank') ? $request->input('bank') : '';
        if(empty($bank)) {
            $bank = $bankModel['name'];
        }

        $province = $request->has('province') ? $request->input('province') : '';
        $phone = $request->has('phone') ? $request->input('phone') : '';

        $cashAccount = CashAccountService::getCashAccountSingle($distribution_channel_id);

        if(empty($cashAccount)) {
            $cashAccount = CashAccountService::addCashAccount($distribution_channel_id, $account_name, $identity_card, $card_number, $account_bank, $bank_id, $bank, $province, $is_company, $phone);
        } else {
            $channel = ChannelService::getById($distribution_channel_id);
            if(!$channel) {
                return response()->error('PARAM_ERROR');
            }
            $channelUser = ChannelUserService::getById($channel->channel_user_id);
            if(!$channelUser) {
                return response()->error('PARAM_ERROR');
            }
            $phoneCodeNumber = $channelUser->phone;

            $phoneCode = Redis::get('sms_'.$channel_id."_".$phoneCodeNumber);
            if(!$phoneCode) {
                $phoneCode = Cache::get('sms_'.$channel_id."_".$phoneCodeNumber);
            }
            $exist_sms_code = $phoneCode;
            $sms_code = $request->has('sms_code') ? $request->input('sms_code') : '';

            Log::info("======修改渠道提现信息验证码=====[".'sms_'.$channel_id."_".$phoneCodeNumber."_".$exist_sms_code."]======输入验证码 [ ".$sms_code ." =====".date("y-m-d H:i:s"."\n"));

            if(empty($exist_sms_code) || empty($sms_code)) {
                return response()->error('PHONE_CODE_ERROR');
            }

            if($sms_code != $exist_sms_code) {
                return response()->error('VERIFY_CODE_ERROR');
            }

            $cashAccount = CashAccountService::updateCashAccount($distribution_channel_id, $account_name, $identity_card, $card_number, $account_bank, $bank_id, $bank, $province, $is_company, $phone);
            Cache::forget('sms_'.$channel_id."_".$phoneCodeNumber);
            Redis::del('sms_'.$channel_id."_".$phoneCodeNumber);

//            if(CashAccountService::isEditCashAccount($distribution_channel_id)) {
//                //-1 禁止修改
//                return response()->error("FORBID_CHANGE");
//            } else {
//            }
        }
        return response()->item(new CashAccountTransformer(), $cashAccount);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取修改银行账号信息验证码
     * @api {GET} getCashAccountSms 获取修改银行账号信息验证码
     * @apiGroup Finance
     * @apiName getCashAccountSms
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":[]
     *     }
     */
    function getCashAccountSms(Request $request) {
        $channel_id = $this->getChannelId();
        $userId = $this->getChannelUserId();
        if($userId) {
            $user = ChannelUserService::getById($userId);
            if($user) {
                $phone = $user->phone;
                if(Cache::get('sms_'.$channel_id."_".$phone)) {
                    return response()->error('SMS_GET_TOO_OFTEN');
                }
                $code = rand(1000,9999);
                $request->session()->put('sms_code', $code);
                Cache::put('sms_'.$channel_id."_".$phone, $code, 10);
                Log::info("======修改渠道提现信息验证码=====[".'sms_'.$channel_id."_".$phone."_".$code."]===========".date("y-m-d H:i:s"."\n"));
                Redis::setex('sms_'.$channel_id."_".$phone, 10 * 60, $code);
                $param = array('code'=>$code);
                if(AliSMS::send($phone,'update_withdraw_cash',$param)){
                    return response()->success();
                }else{
                    return response()->error('SMS_SEND_ERROR');
                }
            }
        }
        return response()->error('SMS_SEND_ERROR');
    }

}