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

class Send_operation_model extends CI_Model {

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

    public function daytour_order_ready_for_send($COLI_ID="", $date, $time_flag="no_send_state")
    {
        $today = date('Y-m-d 16:00:00');
        switch ($time_flag) {
            case 'try1':
                $send_state = " ";
                break;
            case 'try2':
                $send_state = " and (TPSL_sendState=1 OR TPSL_logTime > '$today') ";
                break;
            case 'handle':
                $send_state = " and (TPSL_sendState=1) ";
                break;

            case '':
            default:
                $send_state = "";
                break;
        }
        $send_state_sql = " and not exists (
                        select TPSL_SN from InfoManager.dbo.trippest_sms_log
                        where TPSL_COLI_SN=coli.COLI_SN
                        $send_state
                        ) ";
        $top = " TOP 1 ";
        $sms_state = "";
        if ($time_flag == "no_send_state") {
            $send_state_sql = "";
            $top = "";
            $sms_state = " ,(SELECT top 1 TPSL_sendState from InfoManager.dbo.trippest_sms_log
                                where TPSL_COLI_SN=coli.COLI_SN
                                    and TPSL_sendContent like '%'+CONVERT(VARCHAR(10),CONVERT(DATE, COLD_StartDate))+'%'
                                order by TPSL_sendState desc
                            ) as send_state";
        }
        $search_sql = " AND COLD_StartDate ='$date' ";
        if ($COLI_ID !== "") {
            $search_sql = " AND COLI_ID='" . $COLI_ID . "'";
        }
        $sql = "SELECT $top
                    COLI_GroupCode,COLI_SN,COLI_ID
                    $sms_state
                    ,GUT_POST,GUT_TEL
                    ,PAG_Code
                    ,g.GUT_FirstName+' '+g.GUT_LastName guest_name
                    ,(
                        select top 1 GCOD_dutyName+'@'+GCOD_dutyTel+'@'+convert(varchar(20),GCOD_creatTime)+'@'+convert(varchar(20),GCI_createTime)
                        from GroupCombineInfo
                        inner join GroupCombineOperationDetail on GCOD_GCI_combineNo=GCI_combineNo
                            AND GCI_combineNo not IN ('cancel','forbidden')
                        where GCI_GRI_SN=COLI_GRI_SN
                            AND GCOD_operationType='guiderOperations'
                    ) as gcod
                    ,(
                        select top 1 TGI2_Name+'@'+TGI_Mobile from Eva_ObjectInfo
                        left join
                            ( select TGI_SN,TGI_Mobile,TGI2_Name from TouristGuideInfo tgi
                                left join TouristGuideInfo2 tgi2 on TGI2_TGI_SN=TGI_SN and TGI2_LGC=1
                            ) as tgi_info on tgi_info.TGI_SN=EOI_ObjSN
                        where EOI_GRI_SN=COLI_GRI_SN and EOI_Type=3
                    ) as eva
                    ,COLD_StartDate
                from BIZ_ConfirmLineInfo coli
                inner join BIZ_ConfirmLineDetail cold on COLI_SN=COLD_COLI_SN
                INNER JOIN BIZ_GUEST g ON g.GUT_SN=COLI_GUT_SN
                    and GUT_TEL is not null and GUT_TEL<>'' and GUT_POST<>''
                INNER JOIN BIZ_PackageInfo pag ON pag.PAG_SN=COLD_ServiceSN
                WHERE 1=1
                    and COLI_State not in (30,40,50)
                    $search_sql
                    AND '39009'<>PAG_ExtendType
                    $send_state_sql
                    and (exists (
                        select top 1 1 from GroupCombineInfo
                        inner join GroupCombineOperationDetail on GCOD_GCI_combineNo=GCI_combineNo
                            AND GCI_combineNo not IN ('cancel','forbidden')
                        where GCI_GRI_SN=COLI_GRI_SN
                            AND GCOD_operationType='guiderOperations'
                        )
                        OR exists (
                            select top 1 1 from Eva_ObjectInfo
                            where EOI_GRI_SN=COLI_GRI_SN and EOI_Type=3
                        )
                    )
                ORDER BY COLD_StartDate, COLI_SN";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function insert_trippest_sms_log($column)
    {
        $this->info->insert("trippest_sms_log", $column);
        return $this->info->insert_id();
    }

    public function update_trippest_sms_log($where, $column)
    {
        if ($where == "" || empty($column)) {
            return false;
        }
        $update_str = $this->info->update_string('trippest_sms_log', $column, $where);
        $update_exc = $this->info->query($update_str);
        return $update_exc;
    }

    /** 暂时不用了 */
    public function if_order_sent($COLI_SN, $msgType='guide')
    {
        $sql = "SELECT *
                  FROM [infomanager].[dbo].[trippest_sms_log]
                  where [TPSL_COLI_SN]=$COLI_SN
                    and [TPSL_sendState]=1
                    and TPSL_msgType='$msgType'
                ";
        return $this->info->query($sql)->row();
    }

    /*
     * 发送邮件
     */
    function SendMail($fromName, $fromEmail, $toName, $toEmail, $subject, $body) {
        $sql = "INSERT INTO tourmanager.dbo.Email_AutomaticSend \n"
                . "  ( \n"
                . " M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  \n"
                . " M_FromName, M_State \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . " ?, ?, ?, ?, ?, N?, ?, ?, 0 \n"
                . "  ) ";
        $query = $this->info->query($sql,
            array(substr($fromName, 0, 127), $fromEmail, substr($toName, 0, 127), $toEmail, $subject, $body, 'trippest SMS send fail', 'chinahighlighgts')
            );
        return $query;
    }

    function order_sms_list($COLI_SN=0)
    {
        $sql = "SELECT *
                  FROM [infomanager].[dbo].[trippest_sms_log]
                  where [TPSL_COLI_SN]=$COLI_SN
                order by TPSL_logTime ";
        return $this->HT->query($sql)->result();
    }

}

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