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

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\LiquidationTransformer;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Finance\Services\LiquidatedStatService;
use App\Modules\Finance\Services\LiquidationService;
use App\Modules\Trade\Services\PayMerchantService;
use Illuminate\Http\Request;

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

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加清算支付渠道金额
     * @api {POST} Liquidated/add 添加清算支付渠道金额
     * @apiGroup Finance
     * @apiName Liquidated/add
     * @apiParam   {String}  pay_merchant_source 支付渠道
     * @apiParam   {Number}  amount 清算金额
     * @apiParam   {String}  [remark] 备注说明.
     * @apiSuccess   {Number}  amount 清算金额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function add_liquidated(Request $request) {
        $pay_merchant_source = $request->has('pay_merchant_source') ? $request->input('pay_merchant_source') : '';
        if(empty($pay_merchant_source)) {
            return response()->error("PARAM_ERROR");
        }
        $amount = $request->has('amount') ? $request->input('amount') : '';
        if(!is_numeric($amount) || (float)$amount <= 0) {
            return response()->error("PARAM_ERROR");
        }
        $remark = $request->has('remark') ? $request->input('remark') : '';
        $remark = "[支付渠道清算]{".$remark."}";

        $liquidatedStat = LiquidatedStatService::getLiquidatedStatSingleBySource($pay_merchant_source);
        if(empty($liquidatedStat)) {
            return response()->error("PARAM_ERROR");
        }

        if($liquidatedStat['amount'] < $amount) {
            return response()->error("LIQUIDATED_STAT_AMOUNT_WITHOUT");
        }
        //添加一条清算记录
        LiquidationService::addLiquidation(0, $pay_merchant_source, -$amount, 0, $remark);
        //更新支付通道清算金额
        LiquidatedStatService::createOrUpdate($pay_merchant_source, -$amount);

        return response()->success();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取清算明细表列表
     * @api {GET} Liquidated/list 获取清算明细表列表
     * @apiGroup Finance
     * @apiName Liquidated/list
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [pay_merchant_source] 支付通道渠道(可不传，获取所有通道)
     * @apiParam   {String}  [search_name] 搜索名称(可不传)
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiSuccess   {Number}  id 清算ID
     * @apiSuccess   {Number}  channel_id 渠道ID
     * @apiSuccess   {String}  channel_name 渠道名称
     * @apiSuccess   {String}  pay_merchant_source 渠道支付通道
     * @apiSuccess   {String}  pay_merchant_source_name 渠道支付通道
     * @apiSuccess   {Number}  amount 清算金额.
     * @apiSuccess   {Number}  payment_id 打款ID
     * @apiSuccess   {String}  date 清算时间
     * @apiSuccess   {String}  remark 清算备注
     * @apiSuccess   {String}  phone 渠道联系
     * @apiSuccess   {String}  created_time
     * @apiSuccess   {String}  pay_merchant_id
     * @apiSuccess   {String}  nickname
     * @apiSuccess   {String}  person_in_charge_name 渠道负责人
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *              [
     *                  {
     *                      "id": 21,
     *                      "channel_id": 11,
     *                      "channel_name": "weixiao",
     *                      "pay_merchant_source": "ALIPAY",
     *                      "pay_merchant_source_name": "通联支付",
     *                      "amount": "11.0000",
     *                      "payment_id": 0,
     *                      "date": "2017-12-07 00:00:00",
     *                      "remark": "每日结算",
     *                      "phone": "13732291699",
     *                      "created_time": "2017-12-07 18:22:29",
     *                      "pay_merchant_id": 0,
     *                      "nickname": "是的方式",
     *                      "person_in_charge_name": "大波哥"
     *                  }
     *              ]
     *     }
     */
    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') : '';
        $pay_merchant_source = $request->has('pay_merchant_source') ? $request->input('pay_merchant_source') : '';
        $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);

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

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'pay_merchant_source'=>$pay_merchant_source,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
            'search_name'=>$search_name,
        ];

        $result = LiquidationService::getLiquidationList($params);

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

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取清算明细表列表导出
     * @api {GET} Liquidated/listExport 获取清算明细表列表导出
     * @apiGroup Finance
     * @apiName Liquidated/listExport
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [pay_merchant_source] 支付通道渠道(可不传，获取所有通道)
     * @apiParam   {String}  [search_name] 搜索名称(可不传)
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     *
     */
    function get_list_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') : '';
        $pay_merchant_source = $request->has('pay_merchant_source') ? $request->input('pay_merchant_source') : '';
        $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);

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

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'pay_merchant_source'=>$pay_merchant_source,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
            'search_name'=>$search_name,
        ];

        $result = LiquidationService::getLiquidationList($params, 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->date))) .  "\",");
                echo("\"" . $item->distribution_channel_id .  "\",");
                echo("\"" . iconv("UTF-8","GBK",$item->distribution_channel_name) .  "\",");
                echo("\"" . iconv("UTF-8","GBK",ChannelService::getSourceName(isset($item->pay_merchant_source) ? $item->pay_merchant_source : "")) .  "\",");
                echo("\"" . ($item->amount) .  "\",");
                echo("\"" . iconv("UTF-8","GBK",$item->remark) .  "\"\r\n");
            }
        }
        exit();
    }

}