<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午4:12
 */

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\WithdrawCashFinancialTransformer;
use App\Http\Controllers\Manage\Finance\Transformers\WithdrawCashTransformer;
use App\Modules\Finance\Services\CashAccountService;
use App\Modules\Finance\Services\FinanceService;
use App\Modules\Finance\Services\FinancialConfigService;
use App\Modules\Finance\Services\FinancialStatService;
use App\Modules\Finance\Services\WithdrawCashService;
use Illuminate\Http\Request;

class WithdrawCashController extends BaseController
{

    /**
     * @apiDefine Finance 结算提现模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 申请提现
     * @api {POST} addWithdrawCash 添加提现信息
     * @apiGroup Finance
     * @apiName addWithdrawCash
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiParam   {Number}  amount 提现金额.
     * @apiParam   {String}  [remark] 备注说明.
     * @apiSuccess   {Number}  enable_amount 可提现总额.
     * @apiSuccess   {Number}  withdraw_pending_amount 提现中金额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                  "enable_amount": 70,
     *                  "withdraw_pending_amount": 1100
     *              }
     *     }
     */
    function add_withdrawCash(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $amount = $request->has('amount') ? $request->input('amount') : '';
        if (!is_numeric($amount)) {
            return response()->error("PARAM_ERROR");
        }
        if ($amount < 100) {
            return response()->error("WITHDRAW_CASH_AMOUNT");
        }

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

        $financialStat = FinancialStatService::getFinancialStatSingle($distribution_channel_id);
        if (empty($financialStat) || $financialStat['enable_withdrawal_amount'] < $amount) {
            //可提现金额不够
            return response()->error("WITHDRAW_CASH_AMOUNT_INSUFFICIEN");
        }

        if (FinancialConfigService::isFrozenDistributionChannel($distribution_channel_id)) {
            //渠道被冻结
            return response()->error("WITHDRAW_CASH_AMOUNT_FROZEN");
        }

        //判断账户是否设置
        if (!CashAccountService::isCashAccountExits($distribution_channel_id)) {
            return response()->error("WITHDRAW_CASH_AMOUNT_ACCOUNT");
        }

        //判断今天是否已经提现
        if (WithdrawCashService::isWithdrawCashChannelToToday($distribution_channel_id)) {
            return response()->error("WITHDRAW_CASH_TODAY_USE");
        }


        WithdrawCashService::addWithdrawCash($distribution_channel_id, $amount, $remark);

        $financialStatUp = FinancialStatService::updateFinancialStatByWithdraw($distribution_channel_id, $amount);
        //修改可提现总额
        $enable_amount = $financialStatUp['enable_withdrawal_amount'];
        //修改提现中金额
        $withdraw_pending_amount = $financialStatUp['withdraw_pending_amount'];

        return response()->success(compact('enable_amount', 'withdraw_pending_amount'));
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改提现状态
     * @api {POST} updateWithdrawCash 修改提现状态信息
     * @apiGroup Finance
     * @apiName updateWithdrawCash
     * @apiParam   {Number}  id 提现id.
     * @apiParam   {Number}  status 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {String}  remark 备注说明.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              }
     *     }
     */
    function update_withdrawCash(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        if (!is_numeric($id)) {
            return response()->error("PARAM_ERROR");
        }
//        $userId = $request->has('user_id') ? $request->input('user_id') : '';
//        if(!is_numeric($userId)) {
//            return response()->error("PARAM_ERROR");
//        }
        $userId = $this->getLoginUserId();


        $remark = $request->has('remark') ? $request->input('remark') : '';
        $statusIn = $request->has('status') ? $request->input('status') : '';
        if (!is_numeric($statusIn)) {
            return response()->error("PARAM_ERROR");
        }

        //审核通过 2， 审核失败 9， 待打款 10， 自动待打款  11  人工待打款 12
        if ($statusIn == 2 || $statusIn == 9 || $statusIn == 10 || $statusIn == 12) {
        } else {
            return response()->error("PARAM_ERROR");
        }

        $withdrawCash = WithdrawCashService::getWithdrawCash($id);
        if (empty($withdrawCash)) {
            return response()->error("PARAM_ERROR");
        }

        if (WithdrawCashService::isEditWithdrawCashStatus($id)) {
            return response()->error("WITHDRAW_CASH_AMOUNT_NO_CHANGE");
        }
        if ($statusIn == 2) {
            //已审核 , 私
            if ($withdrawCash['is_company'] == 0) {
                $statusIn = 11;//,'name' => "自动待打款"
            }
            if ($withdrawCash['is_company'] == 1) {
                //对公，直接 人工打款
                $statusIn = 12;//,12,'name' => "人工待打款"
            }
        }
        WithdrawCashService::updateWithdrawCashStatus($id, $userId, $statusIn, $remark);
        return response()->success();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取提现列表
     * @api {GET} withdrawCashs 获取提现列表信息
     * @apiGroup Finance
     * @apiName withdrawCashs
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiSuccess   {Number}  channel_id 渠道ID.
     * @apiSuccess   {String}  channel_name 渠道名称.
     * @apiSuccess   {Number}  amount 提现金额.
     * @apiSuccess   {Number}  tallage 扣税金额.
     * @apiSuccess   {String}  status 状态.
     * @apiSuccess   {String}  remark 备注.
     * @apiSuccess   {Number}  check_user_id 审核人ID.
     * @apiSuccess   {String}  check_user_name 审核人姓名.
     * @apiSuccess   {String}  created_time 提现时间.
     * @apiSuccess   {String}  updated_time 最后更新时间.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "list": [
     *                           {
     *                                       "channel_id": 2,
     *                                       "channel_name": "渠道二",
     *                                       "amount": "330.00",
     *                                       "tallage": "11",
     *                                       "status": "待审核",
     *                                       "remark": null,
     *                                       "check_user_id": null,
     *                                       "check_user_name": null,
     *                                       "created_time": "2017-12-01 16:58:55",
     *                                       "updated_time": "2017-12-01 16:58:59"
     *                               }
     *                       ],
     *                   "meta": {
     *                       "total": 2,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function get_list(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $statusIn = $request->has('status') ? $request->input('status') : '';
        $result = WithdrawCashService::getList($distribution_channel_id, $distribution_channel_name, $start_time, $end_time, $statusIn);
        return response()->pagination(new WithdrawCashTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 财务对账
     * @api {get} WithdrawCash/financialCounting 财务对账
     * @apiGroup Finance
     * @apiName WithdrawCash/financialCounting
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [account_name] 账户名称
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_frozen] 0:正常状态； -1:冻结状态.
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     * @apiSuccess   {Number}  id 提现ID.
     * @apiSuccess   {Number}  channel_id 渠道ID.
     * @apiSuccess   {String}  channel_name 渠道名称.
     * @apiSuccess   {Number}  amount 提现金额.
     * @apiSuccess   {Number}  pay_amount 打款金额.
     * @apiSuccess   {String}  status 打款状态
     * @apiSuccess   {String}  remark 备注
     * @apiSuccess   {String}  check_user_name 审核人名称
     * @apiSuccess   {Number}  check_user_id 审核人ID
     * @apiSuccess   {String}  account_name 银行卡户主名称
     * @apiSuccess   {String}  identity_card 银行卡户主身份证
     * @apiSuccess   {String}  card_number 银行卡账号
     * @apiSuccess   {String}  account_bank 银行支行
     * @apiSuccess   {String}  bank 银行名称
     * @apiSuccess   {String}  province 银行地址
     * @apiSuccess   {String}  created_time 提现时间.
     * @apiSuccess   {String}  updated_time 审核时间
     * @apiSuccess   {Number}  tallage 税
     * @apiSuccess   {Number}  is_frozen 0:正常状态； -1:冻结状态.
     * @apiSuccess   {String}  fc_remark 冻结备注.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "list": [
     *                           {
     *                              "id": 14,
     *                              "channel_id": 2,
     *                              "channel_name": "渠道二",
     *                              "amount": "330.00",
     *                              "pay_amount": "220.00",
     *                              "status": "已打款",
     *                              "remark": "的发生的饭",
     *                              "check_user_name": "波哥的锅",
     *                              "check_user_id": 2,
     *                              "account_name": "宋晓",
     *                              "identity_card": "33038119930901821X",
     *                              "card_number": "6222520177654916",
     *                              "account_bank": "钱江支行",
     *                              "bank": "杭州银行",
     *                              "province": "北京市",
     *                              "created_time": "2017-11-30 16:24:25",
     *                              "updated_time": "2017-11-30 16:24:27",
     *                              "tallage": "110",
     *                              "is_frozen": 0,
     *                              "fc_remark": ""
     *                            }
     *                       ],
     *                   "meta": {
     *                       "total": 2,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function financial_counting(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';
        $status = $request->has('status') ? $request->input('status') : '';

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

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'is_frozen' => $isFrozen,
            'account_name' => $account_name,
            'search_name' => $search_name,
            'is_company' => $is_company,
        ];
        $result = WithdrawCashService::getFinancialCounting($params, $status);

        return response()->pagination(new WithdrawCashFinancialTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 财务对账导出
     * @api {get} WithdrawCash/financialCountingExport 财务对账导出
     * @apiGroup Finance
     * @apiName WithdrawCash/financialCountingExport
     * @apiParam   {String}  [account_name] 账户名称
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_frozen] 0:正常状态； -1:冻结状态.
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     */
    function financial_counting_export(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';


        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';
        $status = $request->has('status') ? $request->input('status') : '';

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

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'is_frozen' => $isFrozen,
            'account_name' => $account_name,
            'search_name' => $search_name,
            'is_company' => $is_company,
        ];
        $result = WithdrawCashService::getFinancialCounting($params, $status, true);


        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "财务对账" . date("YmdHis") . ".csv");
        echo iconv("UTF-8", "GBK", "\"操作日期\",\"渠道ID\",\"渠道名称\",\"私/公\",\"姓名\",\"打款金额(元)\",\"打款状态\",\"银行卡\",\"交易银行\",\"开户支行\"\r\n");
        if ($result) {
            foreach ($result as $item) {

                echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->updated_at))) . "\",");
                echo("\"" . ($item->channel_id) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", $item->channel_name) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", $item->is_company) . "\",");
                echo("\"" . mb_convert_encoding($item->account_name, "GBK") . "\",");
                echo("\"" . ($item->amount - $item->tallage) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", $item->wc_status) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", "`" . ($item->card_number)) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", $item->bank) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", $item->account_bank) . "\"\r\n");
            }
        }
        exit();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 财务审核
     * @api {get} WithdrawCash/financialAudit 财务审核
     * @apiGroup Finance
     * @apiName WithdrawCash/financialAudit
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [account_name] 账户名称
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_frozen] 0:正常状态； -1:冻结状态.
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     * @apiSuccess   {Number}  id 提现ID.
     * @apiSuccess   {Number}  channel_id 渠道ID.
     * @apiSuccess   {String}  channel_name 渠道名称.
     * @apiSuccess   {Number}  amount 提现金额.
     * @apiSuccess   {Number}  pay_amount 打款金额.
     * @apiSuccess   {String}  status 打款状态
     * @apiSuccess   {String}  remark 备注
     * @apiSuccess   {String}  check_user_name 审核人名称
     * @apiSuccess   {Number}  check_user_id 审核人ID
     * @apiSuccess   {String}  account_name 银行卡户主名称
     * @apiSuccess   {String}  identity_card 银行卡户主身份证
     * @apiSuccess   {String}  card_number 银行卡账号
     * @apiSuccess   {String}  account_bank 银行支行
     * @apiSuccess   {String}  bank 银行名称
     * @apiSuccess   {String}  province 银行地址
     * @apiSuccess   {String}  created_time 提现时间.
     * @apiSuccess   {String}  updated_time 审核时间
     * @apiSuccess   {Number}  tallage 税
     * @apiSuccess   {Number}  is_frozen 0:正常状态； -1:冻结状态.
     * @apiSuccess   {String}  fc_remark 冻结备注.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "list": [
     *                           {
     *                              "id": 14,
     *                              "channel_id": 2,
     *                              "channel_name": "渠道二",
     *                              "amount": "330.00",
     *                              "pay_amount": "220.00",
     *                              "status": "已打款",
     *                              "remark": "的发生的饭",
     *                              "check_user_name": "波哥的锅",
     *                              "check_user_id": 2,
     *                              "account_name": "宋晓",
     *                              "identity_card": "33038119930901821X",
     *                              "card_number": "6222520177654916",
     *                              "account_bank": "钱江支行",
     *                              "bank": "杭州银行",
     *                              "province": "北京市",
     *                              "created_time": "2017-11-30 16:24:25",
     *                              "updated_time": "2017-11-30 16:24:27",
     *                              "tallage": "110",
     *                              "is_frozen": 0,
     *                              "fc_remark": ""
     *                            }
     *                       ],
     *                   "meta": {
     *                       "total": 2,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function financial_audit(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';
        $status = $request->has('status') ? $request->input('status') : '';

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

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'is_frozen' => $isFrozen,
            'account_name' => $account_name,
            'search_name' => $search_name,
            'is_company' => $is_company,
        ];
        $result = WithdrawCashService::getFinancialAudit($params, $status);

        return response()->pagination(new WithdrawCashFinancialTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 财务审核导出
     * @api {get} WithdrawCash/financialAuditExport 财务审核导出
     * @apiGroup Finance
     * @apiName WithdrawCash/financialAuditExport
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [account_name] 账户名称
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_frozen] 0:正常状态； -1:冻结状态.
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传，获取所有状态)
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     */
    function financial_audit_export(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd', strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd', strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';
        $status = $request->has('status') ? $request->input('status') : '';

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

        $params = [
            'channel_id' => $distribution_channel_id,
            'channel_name' => $distribution_channel_name,
            'start_date' => $start_time,
            'end_date' => $end_time,
            'is_frozen' => $isFrozen,
            'account_name' => $account_name,
            'search_name' => $search_name,
            'is_company' => $is_company,
        ];
        $result = WithdrawCashService::getFinancialAudit($params, $status, true);


        //mb_convert_encoding()
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "财务审核" . date("YmdHis") . ".csv");
        echo iconv("UTF-8", "GBK", "\"创建日期\",\"操作日期\",\"渠道ID\",\"渠道名称\",\"私/公\",\"姓名\",\"打款金额(元)\",\"手续费\",\"备注\",\"打款状态\",\"银行卡\",\"交易银行\",\"开户支行\"\r\n");
        if ($result) {
            foreach ($result as $item) {
                echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->created_at))) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->updated_at))) . "\",");
                echo("\"" . $item->channel_id . "\",");
                echo("\"" . iconv("UTF-8", "GBK", trim($item->channel_name)) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", trim($item->is_company)) . "\",");
                echo("\"" . mb_convert_encoding(trim($item->account_name), "GBK") . "\",");
                echo("\"" . ($item->amount - $item->tallage) . "\",");
                echo("\"" . ($item->tallage) . "\",");
                echo("\"" . mb_convert_encoding(trim($item->remark), "GBK") . "\",");
                echo("\"" . mb_convert_encoding(trim($item->wc_status), "GBK") . "\",");
                echo("\"" . "`" . ($item->card_number) . "\",");
                echo("\"" . mb_convert_encoding(trim($item->bank), "GBK") . "\",");
                echo("\"" . mb_convert_encoding(trim($item->account_bank), "GBK") . "\"\r\n");
            }
        }
        exit();

    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 对账统计数据
     * @api {get} WithdrawCash/dataStatistics 对账统计数据
     * @apiGroup Finance
     * @apiName WithdrawCash/dataStatistics
     * @apiSuccess   {Number}  yesterday_amount_audit_success 昨日已审核金额
     * @apiSuccess   {Number}  all_amount_audit_success 所有已审核金额
     * @apiSuccess   {Number}  yesterday_amount_apply 昨日申请提现金额
     * @apiSuccess   {Number}  all_amount_apply 所有申请提现金额
     * @apiSuccess   {Number}  yesterday_amount_audit_fail 昨日审核不通过
     * @apiSuccess   {Number}  all_amount_audit_fail 所有审核不通过
     * @apiSuccess   {Number}  yesterday_amount_wait 昨日待打款金额
     * @apiSuccess   {Number}  all_amount_wait 所有待打款金额
     * @apiSuccess   {Number}  yesterday_amount_apply_success 昨日成功提现金额
     * @apiSuccess   {Number}  all_amount_apply_success 所有成功提现金额
     * @apiSuccess   {Number}  yesterday_amount_apply_person_success 昨日对私成功提现金额
     * @apiSuccess   {Number}  all_amount_apply_person_success 所有对私成功提现金额
     * @apiSuccess   {Number}  yesterday_amount_apply_company_success 昨日对公成功提现金额
     * @apiSuccess   {Number}  all_amount_apply_company_success 所有对公成功提现金额
     * @apiSuccess   {Number}  yesterday_apply_num 昨日累积提现笔数
     * @apiSuccess   {Number}  all_apply_num 所有累积提现笔数
     * @apiSuccess   {Number}  yesterday_tallage_count 昨日累积提现手续费
     * @apiSuccess   {Number}  all_stallage_count 所有累积提现手续费
     * @apiSuccess   {Number}  yesterday_payment_count 昨日累积打款金额
     * @apiSuccess   {Number}  all_payment_count 所有累积打款金额
     * @apiSuccess   {Number}  all_amount_apply_success_count 所有累积已提现金额
     * @apiSuccess   {Number}  all_amount_apply_count 所有累积可提现金额
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "yesterday_amount_audit_success": "660.00",
     *                   "all_amount_audit_success": "660.00",
     *                   "yesterday_amount_apply": "660.00",
     *                   "all_amount_apply": "660.00",
     *                   "yesterday_amount_audit_fail": "440.00",
     *                   "all_amount_audit_fail": "440.00",
     *                   "yesterday_amount_wait": "440.00",
     *                   "all_amount_wait": "440.00",
     *                   "yesterday_amount_apply_success": "220.00",
     *                   "all_amount_apply_success": "220.00",
     *                   "yesterday_apply_num": 3,
     *                   "all_apply_num": 3,
     *                   "yesterday_tallage_count": 3,
     *                   "all_stallage_count": 3,
     *                   "yesterday_payment_count":200,
     *                   "all_payment_count":200,
     *                   "all_amount_apply_success_count": 3,
     *                   "all_amount_apply_count": 3,
     *              }
     *     }
     */
    function data_statistics(Request $request)
    {
        $dataWithdrawCash = FinanceService::getDataStatisticsWithdrawCash();

        //昨日已审核金额
        $yesterday_amount_audit_success = $dataWithdrawCash['yesterday_amount_audit_success'];
        //所有已审核金额
        $all_amount_audit_success = $dataWithdrawCash['all_amount_audit_success'];
        //昨日申请提现金额
        $yesterday_amount_apply = $dataWithdrawCash['yesterday_amount_apply'];
        //所有申请提现金额
        $all_amount_apply = $dataWithdrawCash['all_amount_apply'];
        //昨日审核不通过
        $yesterday_amount_audit_fail = $dataWithdrawCash['yesterday_amount_audit_fail'];
        //所有审核不通过
        $all_amount_audit_fail = $dataWithdrawCash['all_amount_audit_fail'];

        //昨日待打款
        $yesterday_amount_wait = $dataWithdrawCash['yesterday_amount_wait'];
        //所有待打款
        $all_amount_wait = $dataWithdrawCash['all_amount_wait'];
        //所有待笔数
        $all_amount_wait_num = $dataWithdrawCash['all_amount_wait_num'];

        //昨日对私成功提现金额
        $yesterday_amount_apply_person_success = $dataWithdrawCash['yesterday_amount_apply_person_success'];
        //所有对私成功提现金额
        $all_amount_apply_person_success = $dataWithdrawCash['all_amount_apply_person_success'];
        //昨日对公成功提现金额
        $yesterday_amount_apply_company_success = $dataWithdrawCash['yesterday_amount_apply_company_success'];
        //所有对公成功提现金额
        $all_amount_apply_company_success = $dataWithdrawCash['all_amount_apply_company_success'];


        //昨日成功提现金额
        $yesterday_amount_apply_success = $dataWithdrawCash['yesterday_amount_apply_success'];
        //所有成功提现金额
        $all_amount_apply_success = $dataWithdrawCash['all_amount_apply_success'];
        //昨日累积提现笔数
        $yesterday_apply_num = $dataWithdrawCash['yesterday_apply_num'];
        //所有累积提现笔数
        $all_apply_num = $dataWithdrawCash['all_apply_num'];
        //昨日累积提现手续费
        $yesterday_tallage_count = $dataWithdrawCash['yesterday_tallage_count'];
        //所有累积提现手续费
        $all_stallage_count = $dataWithdrawCash['all_stallage_count'];


        $dataPayment = FinanceService::getDataStatisticsPayment();
        //昨日累积打款金额
        $yesterday_payment_count = $dataPayment['yesterday_payment_count'];
        //所有累积打款金额
        $all_payment_count = $dataPayment['all_payment_count'];


        $dataFinancialStat = FinanceService::getDataStatisticsFinancialStat();
        //所有累积已提现金额
        $all_amount_apply_success_count = $dataFinancialStat['all_amount_apply_success_count'];
        //所有累积可提现金额
        $all_amount_apply_count = $dataFinancialStat['all_amount_apply_count'];

        return response()->success(
            compact(
                'yesterday_amount_audit_success',
                'all_amount_audit_success',
                'yesterday_amount_apply',
                'all_amount_apply',
                'yesterday_amount_audit_fail',
                'all_amount_audit_fail',
                'yesterday_amount_wait',
                'all_amount_wait',
                'all_amount_wait_num',
                'yesterday_amount_apply_success',
                'all_amount_apply_success',
                'yesterday_amount_apply_person_success',
                'all_amount_apply_person_success',
                'yesterday_amount_apply_company_success',
                'all_amount_apply_company_success',
                'yesterday_apply_num',
                'all_apply_num',
                'yesterday_tallage_count',
                'all_stallage_count',
                'yesterday_payment_count',
                'all_payment_count',
                'all_amount_apply_success_count',
                'all_amount_apply_count'
            ));
    }

    public function updateWithdrawType(Request $request)
    {
        $id = $request->input('id', '');
        $param = $request->input('param', '');
        $param_value = $request->input('param_value', '');
        if (empty($id)) {
            return response()->error('PARAM_EMPTY');
        }
        $withdrawCash = WithdrawCashService::getWithdrawCash($id);
        if (!$withdrawCash) {
            return response()->error('PARAM_ERROR');
        }
        if ($param == 'is_company') {
            $withdrawCash->is_company = $param_value;
            $withdrawCash->save();
        }
        //WithdrawCashService::updateWithdrawCashType($id,);
        return response()->success();
    }

    /**
     * 更新打款状态
     * id 条目id
     * status 状态(10：待打款)
     * @param Request $request
     * @return mixed
     */
    function updatePaymentStatus(Request $request)
    {
        $id = $request->input('id', '');
        if (empty($id)) {
            return response()->error('PARAM_EMPTY');
        }
        $withdrawCash = WithdrawCashService::getWithdrawCash($id);
        if (!$withdrawCash) {
            return response()->error('PARAM_ERROR');
        }

        $status = WithdrawCashService::getWithdrawCashStatusStr(10);
        $withdrawCash->status = $status;
        $withdrawCash->save();
        //WithdrawCashService::updateWithdrawCashType($id,);
        return response()->success();
    }
}