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


namespace common\util;


use common\models\ProdCategory;
use common\models\ProdMain;

class EntranceHotelService
{

    /**
     * Des:获取酒店产品初始化数据
     * Name: getInitialize
     * @param $prod_cate_id
     * @return array
     * @author 付泓程
     */
    public function getInitialize($prod_cate_id)
    {
        if (Util::checkPattern('intVal', $prod_cate_id) == false) {
            return Util::returnArrEr('params error');
        }
        /**获取产品信息*/
        $prodCate = new ProdCategory();
        $prodInfo = $prodCate->getProdDetail($prod_cate_id);
//        return Util::returnArrSu('fda',$prodInfo);
        if (!isset($prodInfo['category_id']) || $prodInfo['category_id'] != 3) {
            return Util::returnArrEr('该产品不是酒店产品！');
        }
        /**获取产品的价格日历*/
        $getDatePriceList = $this->getDatePriceList($prodInfo);
        if ($getDatePriceList['flag'] == false) {
            return $getDatePriceList;
        }
        $datePriceList = $getDatePriceList['data'];
        $prodInfo['date_list'] = $datePriceList;
        $keys = array_keys($datePriceList);//将数组的健组成一个数组
        $date = $keys[0];//取健的第一个值为默认日期
        $prodInfo['date'] = $date;
        /**获取产品的价格列表*/
        $getProdArr = $this->getProdArr($prodInfo, $date);
        if ($getProdArr['flag'] == false) {
            return $getProdArr;
        }
        $prodInfo['prod_arr'] = $getProdArr['data'];
        return Util::returnArrSu('', $prodInfo);
    }

    /**
     * Des:获取价格日历
     * Name: getDatePriceList
     * @param $prodInfo
     * @return array
     * @author 付泓程
     */
    public function getDatePriceList($prodInfo)
    {
        if (empty($prodInfo['sign']) == false) {//如果不是cs的巴士
            $siteConfig = Util::getSiteConfig();
            $time = time();
            $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' => 'gate_ticket_mini_value',
                'sign' => $prodInfo['sign'],
            ];

            $curl = new CurlInterface($data, 4);
//            $result = $curl->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
//            if (!isset($result['code']) || $result['code'] != '0' || count($result['list']) == 0) {
//                return Util::returnArrEr('产品已售完！');
//            }
            $result = ["code"=>"0","info"=>"","list"=>[
                                                    ["ticket_date"=>"2017-05-09","ticket_time"=>"","mini_price"=>"509.00"],
                                                    ["ticket_date"=>"2017-05-12","ticket_time"=>"","mini_price"=>"512.00"],
                                                    ["ticket_date"=>"2017-05-15","ticket_time"=>"","mini_price"=>"515.00"]
                                                ]
                    ];
            $datePriceList = [];
            foreach ($result['list'] as $val) {
                $datePriceList[$val['ticket_date']] = $val['mini_price'];
            }
        } else {
            $endDate = strtotime(date('Y-m-01') . " +4 month -1 day");//获取第三个月的最后一天
            $_time = range(strtotime(date('Y-m-d')), $endDate, 24 * 60 * 60);
            $_time = array_map(create_function('$v', 'return date("Y-m-d", $v);'), $_time);
            $datePriceList = array_fill_keys($_time, $this->dealFloat($prodInfo['show_price']));
        }
        return Util::returnArrSu('', $datePriceList);

    }

    /**
     * Des:获取品类的产品列表
     * Name: getProdArr
     * @param $prodInfo
     * @param $date
     * @return array
     * @author 付泓程
     */
    public function getProdArr($prodInfo, $date)
    {
        $prodMain = new ProdMain();
//        $prodArray = $prodMain->getProdArr($prodInfo['pro_cate_id']);//获取本地存储的酒店品类下的酒店房型产品
        if (empty($prodInfo['sign']) == false) {
            $result = $this->getCsPro($prodInfo, $date);
            if (!isset($result['code']) || $result['code'] != '0' || count($result['product_list']) == 0) {
                return Util::returnArrEr('系统繁忙，请稍后再试！');
            }
            //对数据进行整合
//            $csProdArr = $result['product_list'];
//            foreach ($prodArray as $key => $val) {
//                $prod = false;
//                foreach ($csProdArr as $csVal) {
//                    if ($csVal['prod_id'] == $val['bus_id']) {
//                        $prod = true;
//                        $prodArray[$key]['prod_count'] = $csVal['ticket_num'];
//                        $prodArray[$key]['prod_price'] = intval(ceil($csVal['ticket_price'])) ;
//                        break;
//                    }
//                }
//                if ($prod == false) {//当这个产品在cs那边不存在的话 则删除该产品
//                    unset($prodArray[$key]);
//                }
//            }
            $prodArray = array_values($result['product_list']);
        }
        return Util::returnArrSu('', $prodArray);
    }

    /**
     * Des:获取品类某天的产品列表
     * Name: getProdArrForDate
     * @param $params
     * @return array
     * @author 付泓程
     */
    public function  getProdArrForDate($params)
    {
        if (Util::checkPattern('intVal', $params['prod_cate_id']) == false) {
            return Util::returnArrEr('params error');
        }
        /**获取产品品类信息*/
        $prodCate = new ProdCategory();
        $prodInfo = $prodCate->getProdDetail($params['prod_cate_id']);
        if (!isset($prodInfo['category_id']) || $prodInfo['category_id'] != 3) {
            return Util::returnArrEr('该产品不是酒店产品！');
        }
        /**获取的品类的产品列表*/
        $getProdArr = $this->getProdArr($prodInfo, $params['date']);
        if ($getProdArr['flag'] == false) {
            return $getProdArr;
        }
        $return['prod_arr'] = $getProdArr['data'];
        return Util::returnArrSu('', $return);
    }

    /**
     * Des:获取cs酒店产品
     * Name: getCsPro
     * @param $prodInfo
     * @param $date
     * @return array
     * @author 付泓程
     */
    public function getCsPro($prodInfo, $date)
    {
        $siteConfig = Util::getSiteConfig();
        $time = time();
        $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' => 'gate_ticket_date_run',
            'sign' => $prodInfo['sign'],
            'date' => $date
        ];
//        $curl = new CurlInterface($data, 4);
//        $result = $curl->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');//获取cs的门票产品
        $curl = new CurlInterface($data,4);
        $result = $curl->execute('http://cs.zhizhuchuxing.com/zz-jd/st-ht/HTcontrol.php?type=order_ChooseProduct&key_word=&room_name=&current_page=1&page_size=25','GET');
//        $result = [];
        return $result;
    }


    /**
     * Function Description:处理价格
     * Function Name: dealFloat
     * @param $float
     *
     * @return float
     *
     * @author LUOCJ
     */
    public function dealFloat($float)
    {
        $int = floor($float);
        if ($float - $int == 0) {
            $float = $int;
        }
        return $float;
    }
}