<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Tulanduo_sync_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    /*!
     * 查询图兰朵订单id是否已存在
     * @param  string     $vendorOrderIds [description]
     */
    public function get_exists_vendorOrderId($vendorOrderIds="")
    {
        $sql = "SELECT GCI_VendorOrderId FROM GroupCombineInfo
                WHERE GCI_VendorOrderId IN ($vendorOrderIds) ";
        return $this->HT->query($sql)->result();
    }
    /*!
     * 从图兰朵同步历史数据的日期偏移
     * 获取HT内图兰朵订单的最老出发日期
     * * 由于获取列表时根据发团日期或得到更早时间的发团日期
     * * 因此这里取最早的10个, 找出不连续的为滚动日期的开始
     */
    public function get_oldest_offset()
    {
        $ret_date = "";
        $sql = "SELECT DISTINCT TOP 10 CAST(GCI_travelDate as DATE) old_date from GroupCombineInfo
                order by old_date asc";
        $all_date = $this->HT->query($sql)->result();
        $all_date_arr = array_map(function($ele)
        {
            return $ele->old_date;
        }, $all_date);
        for ($i=count($all_date_arr)-1; $i > 0; $i--) {
            $d1 = new DateTime($all_date_arr[$i]);
            $d2  = new DateTime($all_date_arr[$i-1]);
            $date_diff  = $d2->diff($d1);
            if (intval($date_diff->format('%R%a')) > 1 && !in_array($all_date_arr[$i], $this->empty_date()) ) {
                $ret_date = $all_date_arr[$i];
                break;
            }
        }
        if ($ret_date==="") {
            $ret_date = $all_date_arr[0];
        }
        return $ret_date;
    }
    private function empty_date()
    {
        return array(
            "2018-02-06"    // 2018-02-05 没有团
            ,"2017-01-17"
        );
    }
    /*!
     * 图兰朵订单在HT内的信息
     * @param  [type]     $code          [description]
     * @param  [type]     $vendorOrderId [description]
     */
    public function get_vendorOrder_HTinfo($code, $vendorOrderId=NULL)
    {
        # code...
    }


}

/* End of file tulanduo_sync_model.php */
/* Location: ./webht/third_party/trippestOrderSync/models/tulanduo_sync_model.php */
