<?php
namespace console\controllers;

use common\models\BusTicket;
use common\models\FxCommissionInfo;
use common\models\OrderMain;
use common\models\ProdCategory;
use common\util\CurlInterface;
use common\util\Util;
use yii\base\Exception;
use yii\console\Controller;

/**
 * @author 付泓程
 */
class SyncHotelController extends Controller
{
    /**
     * Des:更新酒店数据
     * Name: actionUptHotel
     * @author 付泓程
     */
    public function actionUptHotel(){
        $siteConfig = Util::getSiteConfig();
        $add_order_str = '';
        $del_order_str = '';
        $prod_category = new ProdCategory();
        //0.查当前数据库的酒店信息
        $ticket_arr = $prod_category->getEntranceHotel();
        //1.cs查票种信息，存缓存
//        $time = time();
//        $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' => 'gate_ticket_all',
//            'ticket_type' => 'bus'
//        ];
//        $curlInterface = new CurlInterface($send_data, 4);
//        $curlInterface->setBaseUrl('');
//        $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
        $ticket_arr_cs = ["code"=>0,"info"=>"","list"=>["1111"=>[
            "sign"=>"1111",
            "prod_id"=>"111100",
            "name"=>"测试酒店名称1",
            "sub_ticket"=>[
                ["prod_id"=>"111100","prod_name"=>"1111大床房[无早]"],
                ["prod_id"=>"111101","prod_name"=>"1111大床房[单早]"],
                ["prod_id"=>"111103","prod_name"=>"1111大床房[双早]"]
            ]
        ],
            "2222"=>[
                "sign"=>"2222",
                "prod_id"=>"222200",
                "name"=>"测试酒店名称2",
                "sub_ticket"=>[
                    ["prod_id"=>"222200","prod_name"=>"2222大床房[无早]"],
                    ["prod_id"=>"222201","prod_name"=>"2222大床房[单早]"],
                    ["prod_id"=>"222203","prod_name"=>"2222大床房[双早]"]
                ]
            ]
        ]
        ];
        if ($ticket_arr_cs['code'] == 0) {
            //新产品则添加
            $new_bus_arr_add = array_diff_key($ticket_arr_cs['list'], $ticket_arr);//新增的酒店-房型
            foreach ($new_bus_arr_add as $key => $val) {
                $add_order_str .= ',' . $key;
            }
            if ($new_bus_arr_add) {
                $istBusTicket = $prod_category->addEntranceHotelFormCs($new_bus_arr_add);
                if (!$istBusTicket) {
                    file_put_contents(ROOT_PATH . '/console/log/hotelroom/entrancehotelroom.log', date("Y-m-d H:i:s") . '新增酒店房型失败' . PHP_EOL, FILE_APPEND);
                }
            };
            //不再使用的产品则删除
            $new_bus_arr_del = array_diff_key($ticket_arr, $ticket_arr_cs['list']);//取消售卖的票
            foreach ($new_bus_arr_del as $key => $val) {
                $del_order_str .= ',' . $key;
            }
            if ($new_bus_arr_del) {
                $delBusTicket = $prod_category->delEntranceHotelFormCs($new_bus_arr_del);
                if (!$delBusTicket) {
                    file_put_contents(ROOT_PATH . '/console/log/hotelroom/entrancehotelroom.log', date("Y-m-d H:i:s") . '删除酒店房型失败' . PHP_EOL, FILE_APPEND);
                }
            } else {
                file_put_contents(ROOT_PATH . '/console/log/hotelroom/entrancehotelroom.log', date("Y-m-d H:i:s") . '无取消酒店房型' . PHP_EOL, FILE_APPEND);
            }
            //重新激活产品
            $new_bus_arr_inter = array_intersect_key($ticket_arr, $ticket_arr_cs['list']);//取消售卖的票
            if(count($new_bus_arr_inter) >= 1) {
                $prod_category->interEntranceHotelFormCs($new_bus_arr_inter);
            }
        } else {
            file_put_contents(ROOT_PATH . '/console/log/hotelroom/entrancehotelroom.log', date("Y-m-d H:i:s") . '接口错误' . PHP_EOL, FILE_APPEND);
        }
        file_put_contents(ROOT_PATH . '/console/log/hotelroom/entrancehotelroom.log', date("Y-m-d H:i:s") . '更新完成，今日新增酒店房型' . $add_order_str . '  今日删除酒店房型' . $del_order_str . PHP_EOL, FILE_APPEND);

    }


}
