// 向服务器发送故障或调度命令
  public boolean sendScheduleORErrorCommandToServer(
      int CommandType, int teamID, String name, String content) {

    // hu 2010-11-2 修改
    // ScheduleErrorMessage sMsg = new ScheduleErrorMessage();
    CommonMessage cMsg = new CommonMessage();
    cMsg.setTeamID(teamID);
    if (name.equalsIgnoreCase("TDCS")) {
      cMsg.setCommandType(Constants.TYPE_DDZR_TO_ZNTDCS_ASYN);
    } else if (name.equalsIgnoreCase("CTC")) {
      cMsg.setCommandType(Constants.TYPE_DDZR_TO_ZNCTC_ASYN);
    } else if (name.equalsIgnoreCase("电务维修机")) { // 电务
      cMsg.setCommandType(Constants.TYPE_DDZR_TO_DW_ASYN);
    } else {
      cMsg.setCommandType(Constants.TYPE_DDZR_TO_SICS_ASYN);
      cMsg.setStationName(name);
    }

    ScheduleErrorMessage sMsg = new ScheduleErrorMessage();
    sMsg.setContent(content);
    sMsg.setTeamID(teamID);

    cMsg.setMeseageName("ScheduleErrorMessage");
    cMsg.setMessage(JsonUtil.bean2json(sMsg));

    databaseAccessHandler.sendCommonMessageToServer(cMsg);

    return true;
  }
  public boolean TDCSCommandForRun(String vrTime, String timeStep) { // 此方法对应于教师TDCS界面中选取命令的处理

    TDCSCommandMessage sMsg = new TDCSCommandMessage();

    // AbstractMessage类中定义的字段 通信类别  此数据报所表示的是客户端和服务器代码进行进一步处理的依据
    sMsg.setCommandType(Constants.TYPE_CLIENT_EXPERIMENT_RUN); // 运行
    // sMsg.setCommandType(Constants.TYPE_CLIENT_EXPERIMENT_CLOSE);关闭实验
    // Constants.TYPE_TEACHER_TDCS_START);//启动

    // 设置当前区段名称
    sMsg.setDistrictName(baseParam.getCurrentDistrictName());

    // 下面设置默认的工作模式
    sMsg.setRunMode(Constants.RUN_MODE_AUTO); // 自律模式
    sMsg.setSubjectName(Constants.EXPERIMENT_MODE_TDSI); // 综合实验

    // 设置虚拟时间
    if ((vrTime == null) || (vrTime.length() == 0)) timeStep = "00:00"; // 默认值
    sMsg.setVrTime(vrTime);

    // 设置时间步长
    if ((timeStep == null) || (timeStep.length() == 0)) timeStep = "2"; // 默认值
    sMsg.setTimeStep(timeStep);

    // 向服务器发送开始实验消息
    TDCSCommandMessage rMsg = databaseAccessHandler.TDCSRunCommandToServer(sMsg);

    if (rMsg == null) return false;
    else // 成功
    {
      utilForCurrentTimeAxis.displayTimeOnToolBar(
          rMsg.getVrTime(), rMsg.getCurrentTime(), rMsg.getTimeStep());
      return true;
    }
  }
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // 根据区段获得属于区段内的所有车站信息  并排序
  public void getAllStationInfoByDistrict(String districtName) {

    StationDistrictRelation tableName = new StationDistrictRelation(); // 设置要进行操作的数据库表名
    String sqlStr =
        "select * from stationdistrictrelation where District_name = '" + districtName + "'";

    // 获取区段内所有车站信息
    List<StationDistrictRelation> list =
        databaseAccessHandler.sqlQuery(tableName, "StationDistrictRelation", sqlStr);

    List<StationDistrictRelation> sortedStationDistrictRelationList =
        new ArrayList<StationDistrictRelation>();

    // 对区段内所有车站信息进行排序 : 首站->终点站
    sortedStationDistrictRelationList = this.sortStations(list);

    baseParam.setSortedStationDistrictRelationList(sortedStationDistrictRelationList);
    if (sortedStationDistrictRelationList == null || sortedStationDistrictRelationList.size() == 0)
      return;

    int len = sortedStationDistrictRelationList.size();
    List<String> stationNameList = new ArrayList<String>();
    // 计算区段内站的总距离 和提取车站名
    int sumStationDistance = 0;
    for (int i = 0; i < len; i++) {
      StationDistrictRelation sdr = sortedStationDistrictRelationList.get(i);
      sumStationDistance = sumStationDistance + sdr.getPredistance();
      stationNameList.add(sdr.getStation_name()); // 提取车站名
    }

    BaseParam.setSortedStationNameList(stationNameList);

    baseParam.setSumStationDistance(sumStationDistance);
  }
  // ********************************************************************************************************//
  ////////////////// 以下代码是供发有关命令到服务器使用//////////////////////////////
  // 从服务器获取组的名称或组内成员名称
  public String[] getTeamORMemberNameFromServer(int flag, String teamID) {

    TDCSCommandMessage sMsg = new TDCSCommandMessage();
    sMsg.setDistrictName(teamID);
    sMsg.setCommandType(flag);

    return databaseAccessHandler.getTeamORMemberNameFromServer(sMsg);
  }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
 // 根据区段 从TrainDistrictRelation表获得属于区段内的所有计划车次信息 仅含车次信息  目前不用
 private List<TrainDistrictRelation> getAllTrainDistrictRelationByDistrict(String districtName) {
   TrainDistrictRelation tableName = new TrainDistrictRelation(); // 设置要进行操作的数据库表名
   String sqlStr =
       "select * from Traindistrictrelation where District_name = '" + districtName + "'";
   List<TrainDistrictRelation> trainNameList =
       databaseAccessHandler.sqlQuery(tableName, "TrainDistrictRelation", sqlStr);
   return trainNameList;
 }
 private List<Train> getAllTrainsByDistrict(String districtName) {
   Train tableName = new Train(); // 设置要进行操作的数据库表名
   String sqlStr =
       "select * from Train where Train.Train_name in "
           + "(select Train_name from Traindistrictrelation where District_name='"
           + districtName
           + "')";
   List<Train> trainList = databaseAccessHandler.sqlQuery(tableName, "Train", sqlStr);
   return trainList;
 }
  //////////////////// 以上的代码是写数据到数据库中//////////////////////////////
  // ********************************************************************************************************//
  //////////////////// 以下的代码是从数据库中读取内容//////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////
  // 获取所有区段信息
  public void getAllDistrictInfo() {
    District tableName = new District();
    String sqlStr = "select * from District";

    List<District> list = databaseAccessHandler.sqlQuery(tableName, "District", sqlStr);

    baseParam.setAllDistrictName(list);

    // 测试代码
    // getAllPlanTrainInfoByDistrict("北京-呼和浩特");
  }
  public void TDCSCommandForStop() { // 关闭实验

    TDCSCommandMessage sMsg = new TDCSCommandMessage();

    // AbstractMessage类中定义的字段 通信类别  此数据报所表示的是客户端和服务器代码进行进一步处理的依据
    sMsg.setCommandType(Constants.TYPE_CLIENT_EXPERIMENT_CLOSE); // 关闭实验

    // 设置当前区段名称
    sMsg.setDistrictName(baseParam.getCurrentDistrictName());

    databaseAccessHandler.TDCSCloseCommandToServer(sMsg);
  }
  // 根据区段获得属于区段内的所有计划车次信息
  public void getAllPlanTrainInfoByDistrict(String districtName) {

    // ORDER BY 记录将按照默认的升序进行排列(即:从1到9,从a到z)使用DESC 数据按照降序排列
    Plan tableName = new Plan(); // 设置要进行操作的数据库表名
    String sqlStr =
        "select * from plan where District_name = '"
            + districtName
            + "' ORDER BY Plan_arrivestationtime,Plan_leavestationtime";

    // 从plan表中获取区段内所有车次信息 已经按照到站和离站时间排好序
    List<Plan> planTrainList = databaseAccessHandler.sqlQuery(tableName, "Plan", sqlStr);

    //////////////////////////////////////////////////////////////////
    // 从TrainDistrictRelation表中获取区段内所有车次信息,仅有车次信息  原来的
    // List<TrainDistrictRelation> trainNameList =
    // getAllTrainDistrictRelationByDistrict(districtName);
    // 从Train表中获取区段内所有车次信息
    List<Train> trainNameList = getAllTrainsByDistrict(districtName);

    if (trainNameList == null || trainNameList.size() == 0) return;

    // 对trainNameList进行加工,方便getTrainDirectionByTrainName方法的执行,提高效率
    Iterator<Train> it = trainNameList.iterator();
    while (it.hasNext()) {
      Train data = (Train) it.next();
      trainNameDirectionMap.put(data.getTrain_name(), data.getTrain_direction());
    }
    baseParam.setTrainNameDirectionMap(trainNameDirectionMap);

    // 对每一个车站,获取经过的所有车次信息,并自动排序
    sortPlanTrainForStation(planTrainList);

    // 转换格式
    int len = trainNameList.size();
    // 每次执行,仅对一个车次的信息的进行操作
    for (int i = 0; i < len; i++) {
      String trainName = trainNameList.get(i).getTrain_name();

      // 依据车次trainName按照车站的先后顺序对本车次进行排序  首站->终点站
      List<Plan> sortPlanTrainList = sortPlanTrainList(planTrainList, trainName);

      // 依据车次trainName,对排好序的车次详细信息进行加工,使之用于屏幕绘图
      setTrainPlanFromatList(sortPlanTrainList, trainName);
    }

    // 将车次信息添加到tdcsDataset的TrainList中
    for (int i = 0; i < len; i++) {
      String trainName = trainNameList.get(i).getTrain_name();
      trainNameExist(trainName); // 判断trainName是否已经存在
    }
  }
  // 发送改动后的车次信息到服务器
  public void TDCSCommandForTrain() {
    TeamTdcsRsbMessage sMsg = new TeamTdcsRsbMessage();

    switch (BaseParam.getOperationType()) {
      case Constants.TDCS_TRAIN_TYPE_DELETE: // 删除车次
        sMsg.setCommandType(Constants.TYPE_CLIENT_ZNTDCS_DELETE);
        break;
      case Constants.TDCS_TRAIN_TYPE_NEW: // 添加新车次
        sMsg.setCommandType(Constants.TYPE_CLIENT_ZNTDCS_ADD);
        break;
      case Constants.TDCS_MENU_TOOL_RECTANGLE_ADJUST: // 调整车次的发车或到站时间
        sMsg.setCommandType(Constants.TYPE_CLIENT_ZNTDCS_ADJUST);
        break;
    }
    sMsg.setTeamID(BaseParam.getTeamID()); // 组编号

    // 车次信息
    List<TDCSPlan> trainPlan = BaseParam.getTdcsTrainList();
    sMsg.setTrainPlan(JsonUtil.list2json(trainPlan));

    databaseAccessHandler.TDCSCommandForTrainToServer(sMsg);
  }
  /*
   本方法是针对教师的,如果对学生,只需将plan表更改为dispatch表,当然需要增加一些字段
  */
  public boolean batchUpdateFromTDCSForTeacher(
      Map<String, PlanForStatics> insertTrainDistrictMap,
      Map<String, PlanForStatics> deleteTrainDistrictMap,
      Map<TrainStation, PlanForStatics> insertPlanMap, // plan表插入
      Map<TrainStationTime, TrainPlan> updatePlanMap, // plan表更新
      String firstStation,
      String lastStation) {
    int insertLen = insertTrainDistrictMap.size();
    int deleteLen = deleteTrainDistrictMap.size();
    int insertPlanLen = insertPlanMap.size();
    int updatePlanLen = updatePlanMap.size();

    String[] sqlArray =
        new String[(deleteLen * 3) + (insertLen * 2) + insertPlanLen + updatePlanLen];

    int rowNumber = 0;
    String zeroTimeStr = "00:00:00";

    // 组装插入记录
    if (insertLen != 0) { // 存在元素
      for (String key : insertTrainDistrictMap.keySet()) {
        PlanForStatics info = insertTrainDistrictMap.get(key);

        /*表TrainDistrictRelation  关键字: Train_name  车次名称, District_name 区段名称 */
        String inertStr =
            "insert into TrainDistrictRelation(District_name,Train_name) Values('"
                + info.getDistrictName()
                + "','"
                + key
                + "');";
        sqlArray[rowNumber++] = inertStr;

        /*Train表  关键字:Train_name
        Train_name 车次名称,Train_direction 车次方向(上行0和下行1)Train_maxspeed 车最大速度'
        Train_startstationname始发站名称',Train_endstationname 到达站名称'*/
        inertStr =
            "insert into Train(Train_name,Train_direction,Train_startstationname,Train_endstationname) Values('"
                + key
                + "',"
                + info.getTrainDirection()
                + ",'"
                + firstStation
                + "','"
                + lastStation
                + "');";
        sqlArray[rowNumber++] = inertStr;

        //	System.out.println(inertStr);
      }
    }

    // 对于plan需要单独处理,因为plan表所保存的内容是以车站和车次为关键字,即同一个车次在不同车站的状态都要保存
    if (insertPlanLen != 0) { // 存在元素
      for (TrainStation key : insertPlanMap.keySet()) {
        /*Plan表     关键字:Train_name   Station_name
        Train_name 车次名称, District_name 区段名称' Prestation_name'前站站名',Station_name 本站站名',
                 Plan_arrivestationtime 到站时间', Plan_leavestationtime 离站时间',*/

        String trainName = key.getTrainName();
        PlanForStatics info = insertPlanMap.get(key);

        String arriveTime = info.getArriveTime();
        String leaveTime = info.getLeaveTime();
        if (leaveTime == null) leaveTime = zeroTimeStr;
        if (arriveTime == null) arriveTime = zeroTimeStr;

        String inertStr =
            "insert into Plan(District_name,Train_name,Prestation_name,Station_name,Plan_arrivestationtime,Plan_leavestationtime) Values('"
                + info.getDistrictName()
                + "','"
                + trainName
                + "','"
                + info.getPrestationName()
                + "','"
                + info.getStationName()
                + "','"
                + arriveTime
                + "','"
                + leaveTime
                + "')";
        sqlArray[rowNumber++] = inertStr;

        //	System.out.println(inertStr);
      }
    }

    // 对于plan需要单独处理,因为plan表所保存的内容是以车站和车次为关键字,即同一个车次在不同车站的状态都要保存
    if (updatePlanLen != 0) { // 存在元素
      for (TrainStationTime key : updatePlanMap.keySet()) {
        int timeType = key.getTypeTime();

        TrainPlan value = updatePlanMap.get(key);
        String fixStr =
            "' where Train_name='"
                + key.getTrainName()
                + "' and Station_name='"
                + key.getStationName()
                + "';";
        String updateStr = "";

        if (timeType == Constants.TDCS_TIME_TYPE_ARRIVEAL) // 到站时间
        {
          updateStr = "update Plan set Plan_arrivestationtime='" + value.getTime() + fixStr;
        } else if (timeType == Constants.TDCS_TIME_TYPE_LEAVE) // 离站时间
        {
          updateStr = "update Plan set Plan_leavestationtime='" + value.getTime() + fixStr;
        }
        sqlArray[rowNumber++] = updateStr;

        //	System.out.println(updateStr);
      }
    }

    // 组装删除记录
    if (deleteLen != 0) { // 存在元素

      for (String key : deleteTrainDistrictMap.keySet()) {
        PlanForStatics info = deleteTrainDistrictMap.get(key);

        // TrainDistrictRelation表
        String deleteStr =
            "delete from TrainDistrictRelation where District_name='"
                + info.getDistrictName()
                + "' and Train_name='"
                + key
                + "';";
        sqlArray[rowNumber++] = deleteStr;

        // Train表
        deleteStr = "delete from Train where Train_name='" + key + "';";
        sqlArray[rowNumber++] = deleteStr;

        // Plan表
        deleteStr = "delete from Plan where Train_name='" + key + "';";
        sqlArray[rowNumber++] = deleteStr;

        // System.out.println(deleteStr);
      }
    }
    // NODEFINE 表示无须指定表名,实际上此字段可以去掉。
    boolean result = databaseAccessHandler.updateQuery("NODEFINE", sqlArray);
    // System.out.println("result::"+result);
    return result;
  }