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

namespace App\Http\Controllers\Manage\Finance;

use App\Http\Controllers\Manage\Finance\Transformers\CommissionRateTransformer;
use App\Modules\Finance\Services\CommissionRateService;
use Illuminate\Http\Request;

class CommissionRateController extends BaseController
{

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

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取佣金结算比例列表
     * @api {GET} commissionRates 佣金结算比例列表
     * @apiGroup Finance
     * @apiName commissionRates
     * @apiParam   {Number}  channel_id 渠道id.
     * @apiSuccess   {Number}  begin_amount 起始金额.
     * @apiSuccess   {Number}  end_amount 结束金额.
     * @apiSuccess   {Number}  rate 比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *             [
     *              {
     *                  "id": 1,
     *                  "begin_amount": 0,
     *                  "end_amount": 1000,
     *                  "rate": "0.10"
     *               }
     *              ]
     *     }
     */
    function get_list(Request $request) {
        if(self::checkParamValueNumber($request, ['channel_id'])) {
            return response()->error("PARAM_ERROR");
        }
        $distribution_channel_id = $request->has('channel_id') ? $request->input('channel_id') : '';

        $result = CommissionRateService::getCommissionRateList($distribution_channel_id);

        return response()->collection(new CommissionRateTransformer(), $result);

    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加佣金结算比例
     * @api {POST} addCommissionRate 添加结算比例
     * @apiGroup Finance
     * @apiName addCommissionRate
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiParam   {Number}  begin_amount 起始金额.
     * @apiParam   {Number}  [end_amount] 结束金额.
     * @apiParam   {Number}  rate 比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function add_commissionRate(Request $request) {
        if(self::checkParamValueNumber($request, ['distribution_channel_id', 'begin_amount', 'rate'])) {
            return response()->error("PARAM_ERROR");
        }
        $distribution_channel_id = $request->has('channel_id') ? $request->input('channel_id') : '';
        $begin_amount = $request->has('begin_amount') ? $request->input('begin_amount') : '';
        $end_amount = $request->has('end_amount') ? $request->input('end_amount') : '';
        $rate = $request->has('rate') ? $request->input('rate') : '';

        if(is_numeric($rate) && ($rate >= 1 || $rate < 0.1)) {
            return response()->error("COMMISSION_RATE_WITHOUT");
        }

        CommissionRateService::addCommissionRate($distribution_channel_id, $begin_amount, $end_amount, $rate);

        return response()->success();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改佣金结算比例
     * @api {POST} changeCommissionRate 修改佣金结算比例
     * @apiGroup Finance
     * @apiName changeCommissionRate
     * @apiParam   {Number}  id 佣金结算 id.
     * @apiParam   {Number}  [channel_id] 渠道id.
     * @apiParam   {Number}  [begin_amount] 起始金额.
     * @apiParam   {Number}  [end_amount] 结束金额.
     * @apiParam   {Number}  [rate] 比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function change_commissionRate(Request $request) {
        if(self::checkParamValueNumber($request, ['id'])) {
            return response()->error("PARAM_ERROR");
        }
        $id = $request->has('id') ? $request->input('id') : '';
        $distribution_channel_id = $request->has('channel_id') ? $request->input('channel_id') : '';
        $begin_amount = $request->has('begin_amount') ? $request->input('begin_amount') : '';
        $end_amount = $request->has('end_amount') ? $request->input('end_amount') : '';
        $rate = $request->has('rate') ? $request->input('rate') : '';

        if(is_numeric($rate) && ($rate >= 1 || $rate < 0.1)) {
            return response()->error("COMMISSION_RATE_WITHOUT");
        }

        CommissionRateService::updateCommissionRate($id, $begin_amount, $end_amount, $rate, $distribution_channel_id);
        return response()->success();
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取当前渠道佣金比例
     * @api {GET} getCommissionRate 获取当前渠道佣金比例
     * @apiGroup Finance
     * @apiName getCommissionRate
     * @apiParam {Number}  channel_id 分销渠道ID.
     * @apiSuccess {Number}  rate 当前佣金比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *             "rate": "0.60"
     *         }
     *     }
     */
    function get_commissionRate(Request $request) {
        if($this->checkParamValueEmpty($request, ['channel_id'])) {
            return response()->error("PARAM_EMPTY");
        }
        if($this->checkParamValueNumber($request, ['channel_id'])) {
            return response()->error("PARAM_ERROR");
        }
        $rate = CommissionRateService::getRateCommissionRate($request->input('channel_id'));
        return response()->success(compact('rate'));
    }
}