<?php
/**
 *
 * ============================================================================
 * * 版权所有 蜘蛛出行 * *
 * 网站地址: http://www.zhizhuchuxing.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 倪宗锋
 * PhpStorm CsCurl.php
 * Create By 2017/3/29 20:25 $
 */


namespace common\util\curl;


use common\models\OrderMain;
use common\util\CurlInterface;
use common\util\Util;

class CsCurl
{
    /**
     * Des:获取新路某天的各班次及票库存数据
     * Name: getGoBusStockForLine
     * @param $line_id
     * @param $date
     * @return array
     * @author 倪宗锋
     */
    public static function getGoBusStockForLine($line_id, $date)
    {
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $send_data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'request_time' => $time,
            'action' => 'get_run_ticket_array_by_date_line',
            'line_id' => $line_id,
            'run_date' => $date,
        ];
        $curlInterface = new CurlInterface($send_data, 4);
        $curlInterface->setBaseUrl('');
        $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
        if ($ticket_arr_cs['code'] == 0) {
            return Util::returnArrSu('', $ticket_arr_cs['data']);
        }
        return Util::returnArrEr('当前日期所有票已售完！');
    }

    /**
     * Des:获取新路某天的各班次及票库存数据
     * Name: getGoBusStockForLine
     * @param $start_area_id
     * @param $end_area_id
     * @param $date
     * @return array
     * @author 倪宗锋
     */
    public static function getGoBusStockForArea($start_area_id, $end_area_id, $date)
    {
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $send_data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'request_time' => $time,
            'action' => 'get_run_ticket_array_by_date_area',
            'start_area_id' => $start_area_id,
            'end_area_id' => $end_area_id,
            'run_date' => $date,
        ];
        $curlInterface = new CurlInterface($send_data, 4);
        $curlInterface->setBaseUrl('');
        $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
        if ($ticket_arr_cs['code'] == 0) {
            return Util::returnArrSu('', $ticket_arr_cs['data']);
        }
        return Util::returnArrEr('当前日期所有票已售完！');
    }

    /**
     * Des:获取主线路库存
     * Name: getLineSaleDate
     * @param string $line_id
     * @param string $start_area_id
     * @param string $end_area_id
     * @return array
     * @author 倪宗锋
     */
    public static function getLineSaleDate($line_id = '', $start_area_id = '', $end_area_id = '')
    {
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $send_data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'request_time' => $time,
            'action' => 'get_area_available_date_array',
            'start_area_id' => $start_area_id,
            'end_area_id' => $end_area_id,
            'line_id' => $line_id
        ];
        $curlInterface = new CurlInterface($send_data, 4);
        $curlInterface->setBaseUrl('');
        $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
        if (isset($ticket_arr_cs['code']) && $ticket_arr_cs['code'] == 0) {
            return Util::returnArrSu('', ['list' => $ticket_arr_cs['data']]);
        }
        return Util::returnArrSu('', ['list' => []]);
    }


    /**
     * Des:根据票种获取库存日历
     * Name: getBusSaleDate
     * @param $ticket_id
     * @return array
     * @author 倪宗锋
     */
    public static function getBusSaleDate($ticket_id)
    {
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $send_data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'request_time' => $time,
            'action' => 'get_ticket_available_date_array',
            'ticket_id' => $ticket_id,
        ];
        $curlInterface = new CurlInterface($send_data, 4);
        $curlInterface->setBaseUrl('');
        $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
        if ($ticket_arr_cs['code'] == 0) {
            return Util::returnArrSu('', ['list' => $ticket_arr_cs['data']]);
        }
        return Util::returnArrSu('', ['list' => []]);
    }

    /**
     * Des:获取巴士自由行底价
     * Name: getProdPrice
     * @param $params
     * @return array
     * @author 倪宗锋
     */
    public static function getFreeWalkPrice($params)
    {
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $send_data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'request_time' => $time,
            'sign' => $params['sign'],
            'org_id' => 164,
            'action' => 'get_tourist_pay_total',
            'prod_arr' => json_encode($params['prod_arr']),
            'date' => $params['start_date']
        ];
        $curlInterface = new CurlInterface($send_data, 4);
        $curlInterface->setBaseUrl('');
        $getPrice = $curlInterface->execute($siteConfig['adm_host1'] . '/api/wx', 'POST');
        if ($getPrice['code'] == 0) {
            $total_cost = ceil($getPrice['list']['extra_info']['total_cost']);//低价
            $total_money = ceil($getPrice['list']['total_money']);//零售价
            return Util::returnArrSu('', ['total_cost' => $total_cost, 'total_money' => $total_money]);
        }
        return Util::returnArrEr('获取底价失败！');
    }


    /**
     * Des:获取酒店底价
     * Name: getHotelLowPrice
     * @param $order_id
     * @return array
     * @author 倪宗锋
     */
    public static function getHotelLowPrice($order_id)
    {
        $orderMain = new OrderMain();
        $params = $orderMain->getHotelOrderInfo($order_id);
        $roomPrices = json_decode($params['total_details'], true);
        foreach ($roomPrices as &$val) {
            $val['RunDate'] = $val['date']; unset($val['date']);
            $val['Price'] = $params['prod_cnt'] * $val['price'];unset($val['price']);
        }
        $time = time();
        $siteConfig = Util::getSiteConfig();
        $data = [
            'auth_code' => Util::authCodeForCs($time),
            'user_key' => $siteConfig['adm_user_key'],
            'user' => $siteConfig['adm_user'],
            'user_id' => $siteConfig['adm_user_id'],
            'CreateUserID' => $siteConfig['adm_user_id'],
            'DistribID' =>  164,
            'request_time' => $time,
            'HotelID' => $params['hotel_id'],
            'RoomID' => $params['room_id'],
            'RoomNum' => $params['prod_cnt'],
            'CheckIn' => $params['start_date'],
            'CheckOut' => $params['end_date'],
            'RoomPrices' => $roomPrices,
            'IfCheckPrice' => 0,
            'org_id' => 164,
        ];
        $curlInterface = new CurlInterface($data, 4);
        $curlInterface->setBaseUrl('');
        $getPrice = $curlInterface->execute($siteConfig['adm_host1'] . '/hotel/api/get-low-price', 'POST');
        if ($getPrice['code'] == 0) {
            $total_cost = floatval($getPrice['data']['low_price']);//低价
            return Util::returnArrSu('', ['total_cost' => $total_cost]);
        }
        return Util::returnArrEr('获取底价失败！');
    }
}