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


namespace common\util;


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

class EntranceTicketService
{

    /**
     * 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;
    }

    /**
     * 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);
        if (!isset($prodInfo['category_id']) || $prodInfo['category_id'] != 2) {
            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('产品已售完！');
            }
            $datePriceList = [];
            foreach ($result['list'] as $val) {
                $datePriceList[$val['ticket_date']] = $val['mini_price'];
            }

            //判断售卖截止时间差
            $s_time_h = $prodInfo['close_sale_time'];
            $_time_new = array();
            foreach ($datePriceList as $k => $v) {
                //得到能卖的时间
//                $v.=" 00:00:00";
                $s_time = strtotime($k) + $s_time_h * 60 * 60;
                //能卖的时间和当前时间比较
                $currdate = time();
                if ($s_time > $currdate) {
                    $_time_new[$k] = $v;
                }
            }
            $datePriceList = $_time_new;


        } 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);
            //判断售卖截止时间差
            $s_time_h = $prodInfo['close_sale_time'];
            $_time_new = array();
            foreach ($_time as $k => $v) {
                //得到能卖的时间
//                $v.=" 00:00:00";
                $s_time = strtotime($v) + $s_time_h * 60 * 60;
                //能卖的时间和当前时间比较
                $currdate = time();
                if ($s_time > $currdate) {
                    $_time_new[] = $v;
                }
            }
            $datePriceList = array_fill_keys($_time_new, $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['list']) == 0) {
                return Util::returnArrEr('系统繁忙，请稍后再试！');
            }
            //对数据进行整合
            $csProdArr = $result['list'];
            foreach ($prodArray as $key => $val) {
                $prod = false;
                foreach ($csProdArr as $csVal) {
                    if ($csVal['prod_id'] == $val['bus_id']) {
                        if (Util::checkPattern('intVal', $csVal['ticket_num'])) {
                            $prod = true;
                            $prodArray[$key]['prod_count'] = $csVal['ticket_num'];
                            $prodArray[$key]['prod_price'] = $csVal['ticket_price'];
                            break;
                        }
                    }
                }
                if ($prod == false) {//当这个产品在cs那边不存在的话 则删除该产品
                    unset($prodArray[$key]);
                }
            }
            $prodArray = array_values($prodArray);
        }
        foreach($prodArray as &$v){
            $v['prod_price'] = $this->dealFloat($v['prod_price']);
        }
        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'] != 2) {
            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的门票产品
        return $result;
    }

    /**
     * Des:获取cs系统门票订单产品价格
     * Name: getOrderPriceFromCs
     * @param $prodInfo
     * @param $date
     * @param $order_prod_arr
     * @return array
     * @author 倪宗锋
     */
    public function getOrderPriceFromCs($prodInfo, $date, $order_prod_arr)
    {
        $prodMain = new ProdMain();
        $prodArray = $prodMain->getProdArr($prodInfo['pro_cate_id']);//获取本地存储的门票品类下的门票产品
        $result = $this->getCsPro($prodInfo, $date);
        if (!isset($result['code']) || $result['code'] != '0' || count($result['list']) == 0) {
            return Util::returnArrEr('系统繁忙，请稍后再试！');
        }
        //对数据进行整合
        $csProdArr = $result['list'];
        $csOrderProdParams = [];
        /**将数据拼接成cs产品ID对应产品数量的格式**/
        foreach ($order_prod_arr as $orderProd) {
            foreach ($prodArray as $val) {
                if ($orderProd['prod_id'] == $val['prod_id']) {
                    $csOrderProdParams[$val['bus_id']] = [
                        'prod_id' => $val['bus_id'],
                        'prod_cnt' => $orderProd['prod_count']
                    ];
                    break;
                }
            }
        }
        /**根据cs产品格式计算价格**/
        $price = 0;
        foreach ($csProdArr as $csPro) {
            if (isset($csOrderProdParams[$csPro['prod_id']])) {
                $theProd = $csOrderProdParams[$csPro['prod_id']];
                $price += $theProd['prod_cnt'] * ceil($csPro['ticket_price']);
            }
        }
        return Util::returnArrSu('', $price);
    }

}