<?php

namespace App\Http\Controllers\Channel\Finance;

use App\Http\Controllers\Channel\Finance\Transformers\OfficialAccountBillsTransformer;
use App\Modules\Finance\Services\OfficialAccountBillsService;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

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


    /**
     * @apiVersion 1.0.0
     * @apiDescription 服务号结算详情
     * @api {GET} OfficialAccountBills/getList 服务号结算详情
     * @apiGroup Finance
     * @apiName OfficialAccountBills-getList
     * @apiParam     {String}  start_time 开始时间.
     * @apiParam     {String}  end_time 结束时间.
     * @apiParam     {String}  export  导出(值给1就行export=1)
     * @apiSuccess   {String}  data.list.recharge_amount 充值金额.
     * @apiSuccess   {String}  data.list.date 日期.
     * @apiSuccess   {String}  data.list.nickname  服务号
     * @apiSuccess   {String}  data.meta.channel_recharge_amount total  渠道累计充值
     * @apiSuccess   {String}  data.meta.recharge_amount_in_30_days 30日内充值
     * @apiSuccess   {String}  data.meta.recharge_amount_in_60_days 60日内充值
     * @apiSuccess   {String}  data.meta.recharge_amount_in_90_days 90日内充值
     * @apiSuccess   {String}  data.meta.new_fans_num 新关粉丝数
     * @apiSuccess   {String}  data.meta.per_page per_page
     * @apiSuccess   {String}  data.meta.current_page current_page
     * @apiSuccess   {String}  data.meta.last_page last_page
     * @apiSuccess   {String}  data.meta.next_page_url next_page_url
     * @apiSuccess   {String}  data.meta.prev_page_url prev_page_url
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              list:[
     *                  {
     *                       recharge_amount: "3145.00",
     *                       date: "2018-04-13",
     *                       appid: "wx1d6855c00d8e6500",
     *                       nickname: "南梦繁花",
     *                       channel_recharge_amount: "27680.00",
     *                       recharge_amount_in_30_days: 111,
     *                       recharge_amount_in_60_days: 111,
     *                       recharge_amount_in_90_days: 111,
     *                       new_fans_num: 40
     *                    },...
     *                ]
     *             meta:{}
     *             }
     *
     */
    public function getList(Request $request)
    {
        $start_time = $request->input('start_time', date('Y-m-d'));
        $end_time = $request->input('end_time', date('Y-m-d', time() + 86400));
        $distribution_channel_id = $this->getChannelId();
        $official = $this->getOfficialAccount($distribution_channel_id);
        if (!$official)
            return response()->success();
        if ($request->input('export')) {
            $data = OfficialAccountBillsService::getBillsByOfficialAcount($distribution_channel_id, $start_time, $end_time, true);
//            $data = OrderService::OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,500);
        } else {
//            $data = OrderService::OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time);
            $data = OfficialAccountBillsService::getBillsByOfficialAcount($distribution_channel_id, $start_time, $end_time, false);
        }

//        if ($data) {
//            foreach ($data as &$v) {
//                $v->nickname = isset($official[$v->appid]) ? $official[$v->appid] : '未知';
//                $amount = OrderService::OfficialAccountOrderSum($distribution_channel_id, $start_time, $end_time, $v->appid);
//                $v->total_amount = $amount;
//            }
//        }

        if ($request->input('export')) {
            $filename = date('YmdHis') . '.csv';

            Storage::append($filename, mb_convert_encoding("日期,服务号,充值金额,总额,新关粉丝数,30天内充值,60天内充值,90天内充值", 'gbk'));
            $str = '';
            if ($data) {
                foreach ($data as $val) {
                    $val->new_fans_num = $val->new_fans_num ? $val->new_fans_num : 0;
                    $val->recharge_amount_in_30_days = $val->recharge_amount_in_30_days ? $val->recharge_amount_in_30_days : 0;
                    $val->recharge_amount_in_60_days = $val->recharge_amount_in_60_days ? $val->recharge_amount_in_60_days : 0;
                    $val->recharge_amount_in_90_days = $val->recharge_amount_in_90_days ? $val->recharge_amount_in_90_days : 0;

                    $str .= "{$val->date},{$val->nickname},{$val->recharge_amount},{$val->channel_recharge_amount},{$val->new_fans_num},{$val->recharge_amount_in_30_days},{$val->recharge_amount_in_60_days},{$val->recharge_amount_in_90_days}\r\n";
                }
            }

            Storage::append($filename, mb_convert_encoding($str, 'gbk'));
            return response()->download(storage_path('app/' . $filename))->deleteFileAfterSend(true);
        }

        return response()->pagination(new OfficialAccountBillsTransformer(), $data);
    }


    private function getOfficialAccount($distribution_channel_id)
    {
        if (empty($distribution_channel_id)) return [];
        $res = DB::table('official_accounts')->select('nickname', 'appid')->where('distribution_channel_id', $distribution_channel_id)->get();
        if ($res) {
            $data = [];
            foreach ($res as $v) {
                $data[$v->appid] = $v->nickname;
            }
            return $data;
        }
        return [];
    }
}
