<?php

namespace App\Http\Controllers\Channel\Ali;

use App\Http\Controllers\Channel\BaseController;
use App\Modules\Ali\Services\AliSendOrderService;
use App\Modules\User\Models\Aliuser;
use App\Modules\Ali\Models\AliSendOrder;
use App\Modules\Ali\Models\AliOrder;
use Hashids;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Http\Controllers\Channel\Ali\Transformers\AliSendOrderTransformer;

class AliSendOrderController extends BaseController
{
    /**
     * @apiDefine sendOrder 派单
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} getSendOrders 获取派单列表
     * @apiGroup sendOrder
     * @apiName getSendOrders
     * @apiParam{Number}  [sendOrder_id]  派单id(可不传)
     * @apiParam{String}  [start_time] 开始时间(可不传)
     * @apiParam{String}  [end_time] 结束时间(可不传)
     * @apiParam{String}  [pre_send_date_start] 预计发布时间开始(可不传)
     * @apiParam{String}  [pre_send_date_end] 预计发布时间结束(可不传)
     * @apiSuccess {Number}  id 派单id.
     * @apiSuccess {String}  distribution_channel_id 渠道id
     * @apiSuccess {String}  name 渠道名称
     * @apiSuccess {String}  updated_at 更新时间
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccess {String}  pre_send_date 预计发布时间
     * @apiSuccess {Number}  remark 备注
     * @apiSuccess {Number}  star_num 星星数
     * @apiSuccess {Number}  cost 成本
     * @apiSuccess {Number}  totalChargeAmount 充值总额数
     * @apiSuccess {Number}  totalOrderNum 订单总数
     * @apiSuccess {Number}  sucOrderNum 成功订单数
     * @apiSuccess {Number}  addUserNum 新增用户
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *
     *         "data": [
     *           {
     *           }

     */
    function getSendOrders(Request $request)
    {
        $distribution_channel_id = $this->getChannelId();
        $name = $request->has('name') ? $request->input('name') : '';
        $id = $sendOrderId = $request->has('sendOrder_id') ? $request->input('sendOrder_id') : '';
        $promotion_type = $request->has('promotion_type') ? $request->input('promotion_type') : '';
        $pre_send_date_end = $request->has('pre_send_date_end') ? $request->input('pre_send_date_end') : '';
        $pre_send_date_start = $request->has('pre_send_date_start') ? $request->input('pre_send_date_start') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? $request->input('start_time') : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Y-m-d H:i:s', strtotime($request->input('end_time')) + 86400 - 1) : '';
        $data = array();
        $data = AliSendOrderService::search(compact(
            'distribution_channel_id',
            'name',
            'pre_send_date_end',
            'pre_send_date_start',
            'promotion_type',
            'pre_send_date',
            'start_time',
            'end_time',
            'id'
        ));

        foreach ($data as $item) {
            $sendOrderId = $item->id;
            
            //充值总额数
            $item->totalChargeAmount = AliOrder::getTotalChargeAmount($sendOrderId,$distribution_channel_id);
            //订单总数
            $item->totalOrderNum = AliOrder::getTotalOrderNum($sendOrderId,$distribution_channel_id);
            //成功订单数
            $item->sucOrderNum = AliOrder::getSucOrderNum($sendOrderId,$distribution_channel_id);
            //新增用户
            $item->addUserNum = Aliuser::getAddUserNum($sendOrderId,$distribution_channel_id);
            
            // 海报
            if($item->promotion_type == 'POSTER_EXTEND'){
            	$item->promotion_type_name = '海报推广';
            	$item->promotion_url = 'http://channel.zhuishuyun.com/xcx/yun?channel_id='.$distribution_channel_id.'&xcx_send_order_id='.$sendOrderId;
            }
            // U参
            else{
            	$item->promotion_type_name = '参数推广';
            	$item->promotion_url = 'pages/index/index?xcx_send_order_id='.$sendOrderId;
            }
            \Log::info($item);
          
        }
        return response()->pagination(new AliSendOrderTransformer, $data);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 派单列表导出
     * @api {GET} sendOrder/exportSendOrders 派单列表导出
     * @apiGroup sendOrder
     * @apiName exportSendOrders
     * @apiParam{Number}  [sendOrder_id]  派单id(可不传)
     * @apiParam{Number}  [book_id]  书本id(可不传)
     * @apiParam{String}  [book_name] 书名(可不传)
     * @apiParam{String}  [name]  派单名称(可不传)
     * @apiParam{String}  [pre_send_date_start] 预计发布时间开始(可不传)
     * @apiParam{String}  [pre_send_date_end] 预计发布时间结束(可不传)
     * @apiParam{String}  [start_time] 开始时间(可不传)
     * @apiParam{String}  [end_time] 结束时间(可不传)
     * @apiParam{String}  [promotion_point] 派单源 微信(默认)or浏览器(微信:1,浏览器:2)
     * @apiSuccess {Number}  id 派单id.
     * @apiSuccess {String}  distribution_channel_id 渠道id
     * @apiSuccess {String}  entrance 入口
     * @apiSuccess {String}  name 渠道名称
     * @apiSuccess {Number}  book_id 书本id
     * @apiSuccess {String}  channel_type 渠道类型
     * @apiSuccess {Number}  document_cover_id  文案封面id
     * @apiSuccess {Number}  headline_id  标题id
     * @apiSuccess {Number}  body_template_id ID. 内容模板id
     * @apiSuccess {Number}  original_guide_id  原文引导id
     * @apiSuccess {Number}  subscribe_chapter_id 关注章节ID
     * @apiSuccess {String}  book_name 图书名称
     * @apiSuccess {String}  chapter_name 章节名称
     * @apiSuccess {Number}  cost 成本
     * @apiSuccess {String}  updated_at 更新时间
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccess {String}  subscribe_chapter_name 关注章节名称
     * @apiSuccess {String}  subscribe_chapter_seq 关注章节序号
     * @apiSuccess {Number}  remark 备注
     * @apiSuccess {Number}  star_num 星星数
     * @apiSuccess {Number}  totalChargeAmount 充值总额数
     * @apiSuccess {Number}  registerNum 注册数
     * @apiSuccess {Number}  payUserNum 付费用户数
     * @apiSuccess {Number}  clickNum 点击数数
     * @apiSuccess {Number}  fansNum 粉丝数
     * @apiSuccess {Number}  promotion_page_id  推广页面id
     * @apiSuccess {String}  redirect_url 重定向url
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *
     *         "data": [
     *           {
     *           }
     *           ]
     *     }
     */
    function exportSendOrders(Request $request)
    {
        set_time_limit(0);
        $distribution_channel_id = $this->getChannelId();
        exit();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 生成推广的派单
     * @api {POST} createOrder 生成推广的派单
     * @apiGroup sendOrder
     * @apiName createOrder
     * @apiParam{Number} cost 成本（可选）.
     * @apiParam{String} name 派单渠道名称.
     * @apiParam{String} promotion_type 推广类型[POSTER_EXTEND、PARAM_EXTEND]. 海报推广、参数推广
     * @apiSuccess {Number}  id 派单id（推广U参）
     * @apiSuccess {String}  name 派单名称
     * @apiSuccessExample {json} Success-Response:
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{"id":5，"name":"派单名称", "promotion_url":"promotion_url"}
     *     }
     */
    function createOrder(Request $request)
    {
        $name = $request->has('name') ? $request->input('name') : '';
        $cost = $request->has('cost') ? $request->input('cost') : 0;
        $promotion_type = $request->has('promotion_type') ? $request->input('promotion_type') : '';

        if (!$name || !$promotion_type) return response()->error('PARAM_EMPTY');

        $distribution_channel_id = $this->getChannelId();
        $sendOrder = AliSendOrderService::createOrder(compact('name', 'promotion_type', 'cost', 'distribution_channel_id'));
        $sendOrderId = $sendOrder->id;
        // 海报
        if($promotion_type == 'POSTER_EXTEND'){
        	$promotion_url = 'http://channel.zhuishuyun.com/xcx/yun?channel_id='.$distribution_channel_id.'&xcx_send_order_id='.$sendOrderId;
        }
        // U参
        else{
        	$promotion_url = 'pages/index/index?xcx_send_order_id='.$sendOrderId;
        }

        return response()->success(['id' => $sendOrder->id, 'name' => $name,'promotion_url'=>$promotion_url]);
    }

}
