<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/26
 * Time: 下午8:30
 */

namespace App\Http\Controllers\Manage\Finance;


use App\Modules\Finance\Services\WithdrawCashService;
use Illuminate\Http\Request;

class FinanceController extends BaseController
{

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取状态列表
     * @api {POST} getWithdrawCashStatus 获取状态列表
     * @apiGroup Finance
     * @apiName getWithdrawCashStatus
     * @apiSuccess   {Number}  code 状态码
     * @apiSuccess   {String}  name 状态名称.
     * @apiSuccess   {Number}  show 是否显示 0:隐藏; 1:显示
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":[
     *                  {
     *                      "code": 0,
     *                      "name": "待审核",
     *                      "show": 1
     *                  }
     *              ]
     *     }
     */
    public function getWithdrawCashStatus(Request $request) {
        $result = WithdrawCashService::getWithdrawCashStatusList(20);
        return response()->success($result);
    }




    public function outPayMerchantData(Request $request) {
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $start_time = self::getMinDay($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);

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

        $idsArray = [];
        if($channel_ids && count(explode(',', $channel_ids)) > 0) {
            $ids = explode(',', $channel_ids);
            foreach ($ids as $id) {
                if(is_numeric($id)) {
                    $idsArray[] = $id;
                }
            }
        }
        $sql = '';
        if($start_time && $end_time && count($idsArray) > 0) {
            if( date('Y-m-d H:i:s', strtotime($start_time)) == $start_time && date('Y-m-d H:i:s', strtotime($end_time)) == $end_time) {
                $idsStr = implode(",", $idsArray);

                //$sql = 'SELECT distribution_channel_id as "渠道ID", DATE(created_at) as "日期", SUM(price) as "美哒总金额", pay_merchant_id as "支付通道" FROM orders ';
                $sql = 'SELECT distribution_channel_id, DATE(created_at) as createDate, SUM(price) as price, pay_merchant_id FROM orders ';
                $sql = $sql.' WHERE distribution_channel_id in (' .$idsStr. ')';
                $sql = $sql.' AND `status` = "PAID"';
                $sql = $sql.' AND pay_merchant_id in (' .env('not_in_pay_merchant_id').')';
                $sql = $sql.' AND `status` = "PAID"';
                $sql = $sql.' AND created_at >= "'.$start_time .'"';
                $sql = $sql.' AND created_at <= "'.$end_time.'"';
                $sql = $sql.' GROUP BY DATE(created_at), distribution_channel_id';


            }
        }

        if($sql) {
            //dd($sql);
            $result = \DB::select($sql);
            //dd($result);
            //exit();
//            if($result) {
//                foreach($result as $item) {
//                    dd($item->distribution_channel_id);
//                }
//            }
//            exit();

            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("\"" . $item->distribution_channel_id .  "\",");
                    echo("\"" . iconv("UTF-8","GBK",trim($item->createDate)) .  "\",");
                    echo("\"" . iconv("UTF-8","GBK",trim($item->price)) .  "\",");
                    echo("\"" . iconv("UTF-8","GBK",trim($item->pay_merchant_id)) . "\"\r\n");
                }
            }
            exit();
        } else {
            echo("输入条件错误");
            exit();
        }


//                    WHERE distribution_channel_id in (\'369\', \'370\', \'374\', \'377\')
//                    AND `status` = \'PAID\'
//                    AND pay_merchant_id in (\'13\', \'14\')
//                    AND created_at >= \'2018-06-01 00:00:00\'
//                    AND created_at <= \'2018-06-07 23:59:59\'
//                    GROUP BY DATE(created_at), distribution_channel_id';
//        DB::select('IFNULL(financial_configs.is_frozen, 0) as is_frozen');

    }
}