<?php
/*
 * @Author: your name
 * @Date: 2020-01-15 11:28:41
 * @LastEditTime : 2020-01-17 11:00:42
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \information-system\application\third_party\updatetrain\models\station_model.php
 */


defined('BASEPATH') or exit('No direct script access allowed');

class station_model extends CI_Model
{

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


    /**
     * @description: 判断数据库里面是否有该站点的信息
     * @param {type}$code 三字码,$name 车站名 
     * @return: bool
     */
    function GetStationCount($code, $name)
    {
        $sql = " select top 1 * from TrainStation
        where TRS_Code=? and TRS_StationCN='" . $name . "'";
        //$name = iconv ( "utf-8", "gb2312//IGNORE", $name );
        $query = $this->HT->query($sql, array($code));
        if ($query->result()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @description: 通过比较中文名，三字码，判断HT的站点信息是否存在或变动
     * @param {type} 
     * @return: 0表示站点已存在， 1表示站点存在但是更新，2表示站点未存在
     */
    function CompareStation($code, $name)
    {

        $sql = " select top 1 * from TrainStation where TRS_Code=? and TRS_StationCN='" . $name . "'";
        //$name = iconv ( "utf-8", "gb2312//IGNORE", $name );
        $query = $this->HT->query($sql, array($code));
        if ($query->result()) {
            return 0;
        } else {
            $sql2 = " select top 1 * from TrainStation where TRS_StationCN='" . $name . "'";
            $query2 = $this->HT->query($sql2);
            if ($query2->result()) {
                return 1;
            } else {
                return 2;
            }
        }
    }


    /**
     * @description: 获取HT里面的站点信息列表
     * @param {type} 
     * @return: 
     */
    function GetStationList()
    {
        $sql = "select TRS_ID,TRS_StationCN,TRS_Station,TRS_Code FROM TrainStation where TRS_Use=1";
        $query = $this->HT->query($sql);
        return $query->result();
    }


    /**
     * @description: 增加新站点
     * @param {type} 
     * @return: 
     */
    function AddStation($data)
    {

        //TrainStation表
        $sql = "
            insert into TrainStation (TRS_StationCN,TRS_Station,LastEditTime,TRS_Code,TRS_Use,TRS_IsNew)
            values(?,?,getdate(),?,1,1) 
        ";
        $param = array($data["StationCN"], $data["Station"], $data["Code"]);
        $query = $this->HT->query($sql, $param);
        $TRS_ID = $this->HT->insert_id();

        //语种表TrainStation2,中英文都得添加
        $sql2 = " insert into TrainStation2 (TS2_Name,TS2_LGC,TS2_TRSID,LastEditTime)
        values(?,1,?,GETDATE())";
        $param = array($data["Station"], $TRS_ID);
        $query = $this->HT->query($sql2, $param);
        $sql2 = " insert into TrainStation2 (TS2_Name,TS2_LGC,TS2_TRSID,LastEditTime)
        values(?,2,?,GETDATE())";
        $param = array($data["StationCN"], $TRS_ID);
        $query = $this->HT->query($sql2, $param);
    }

    /**
     * @description: 更新已存在的站点名称的三字码
     * @param {type} 
     * @return: 
     */
    function UpdateStation($code, $name)
    {
        //$name = iconv("utf-8", "gb2312//IGNORE", $name);
        $sql = "update TrainStation set TRS_Code=? where TRS_StationCN='".$name."'";
        $param = array($code);
        $query = $this->HT->query($sql, $param);
    }


    function SendMail($fromName, $fromEmail, $toName, $toEmail, $subject, $body)
    {
        $toName = str_replace("'", "''", $toName);
        $body = str_replace("'", "''", $body);
        $sql = "INSERT INTO Email_AutomaticSend 
                  ( 
                    M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  
                    M_FromName, M_State 
                  ) 
                VALUES 
                  (  
                    ?, ?, N'{$toName}', ?, N'{$subject}', N'{$body}', ?, ?, 0
                  ) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toEmail, 'cht', 'information'));
        echo $query;

        //return $query;
    }
}

/* End of file station_model.php */
