2014-11-21 20:32:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2014-11-27 23:25:36 +08:00
|
|
|
|
using System.Runtime.Serialization;
|
2014-11-21 20:32:36 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using TrainInfomationProviderService.StationInfo;
|
|
|
|
|
using TrainInfomationProviderService.StationInfo.Entities;
|
|
|
|
|
|
|
|
|
|
namespace TrainInfomationProviderService.TrainInfo.Entities
|
|
|
|
|
{
|
|
|
|
|
public class Train : IEquatable<Train>
|
|
|
|
|
{
|
2015-05-11 19:27:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建 <see cref="Train" /> 的新实例(Train)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Train()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-21 20:32:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// ID
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonProperty("i")]
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列车编号
|
|
|
|
|
/// </summary>
|
2015-05-11 19:27:05 +08:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public string Code
|
|
|
|
|
{
|
|
|
|
|
get { return _code ?? (_code = Codes.JoinAsString("/")); }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value.IsNullOrEmpty() || Codes.Contains(value))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Codes.Contains(value))
|
|
|
|
|
Codes.Add(value);
|
|
|
|
|
_code = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JsonProperty("cl")]
|
|
|
|
|
public HashSet<string> Codes { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
2014-11-21 20:32:36 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发站
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonProperty("f")]
|
|
|
|
|
public string From { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 到站
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonProperty("t")]
|
|
|
|
|
public string To { get; set; }
|
|
|
|
|
|
|
|
|
|
[JsonProperty("s")]
|
|
|
|
|
public List<TrainStop> TrainStops { get; set; }
|
|
|
|
|
|
2014-11-27 23:25:36 +08:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public Dictionary<string, TrainStop> TrainStopMap { get; private set; }
|
|
|
|
|
|
2014-12-01 02:17:13 +08:00
|
|
|
|
|
|
|
|
|
string _trainHash;
|
2015-05-11 19:27:05 +08:00
|
|
|
|
|
2014-12-01 02:17:13 +08:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public string TrainHash
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_trainHash))
|
|
|
|
|
{
|
|
|
|
|
_trainHash = CalculateTrainHash();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _trainHash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string CalculateTrainHash()
|
|
|
|
|
{
|
2015-05-11 19:27:05 +08:00
|
|
|
|
var data = Id + TrainStops.Select(s => s.Code + (s.Arrive == null ? "" : ((int)s.Arrive.Value.TotalSeconds).ToString()) + ">" + (s.Left == null ? "" : ((int)s.Left.Value.TotalSeconds).ToString())).JoinAsString("|");
|
2014-12-01 02:17:13 +08:00
|
|
|
|
return data.ToLower().MD5();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 23:25:36 +08:00
|
|
|
|
[OnDeserialized]
|
2014-12-08 01:06:01 +08:00
|
|
|
|
internal void Init(StreamingContext ctx)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
internal void Init()
|
2014-11-27 23:25:36 +08:00
|
|
|
|
{
|
|
|
|
|
TrainStopMap = TrainStops == null ? null : TrainStops.Where(s => !string.IsNullOrEmpty(s.Code)).ToDictionary(s => s.Code, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-21 20:32:36 +08:00
|
|
|
|
#region Implementation of IEquatable<Train>
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 指示当前对象是否等于同一类型的另一个对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// 如果当前对象等于 <paramref name="other"/> 参数,则为 true;否则为 false。
|
|
|
|
|
/// </returns>
|
|
|
|
|
/// <param name="other">与此对象进行比较的对象。</param>
|
|
|
|
|
public bool Equals(Train other)
|
|
|
|
|
{
|
|
|
|
|
return Key == other.Key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _key;
|
2014-12-01 02:17:13 +08:00
|
|
|
|
public string Key
|
2014-11-21 20:32:36 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_key))
|
|
|
|
|
{
|
2015-05-11 19:27:05 +08:00
|
|
|
|
//MOD: 仅用ID,不使用车次编号。
|
|
|
|
|
_key = Id + From + To;
|
2014-11-21 20:32:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用作特定类型的哈希函数。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// 当前 <see cref="T:System.Object"/> 的哈希代码。
|
|
|
|
|
/// </returns>
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return Key.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 确定指定的 <see cref="T:System.Object"/> 是否等于当前的 <see cref="T:System.Object"/>。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// 如果指定的对象等于当前对象,则为 true;否则为 false。
|
|
|
|
|
/// </returns>
|
|
|
|
|
/// <param name="obj">要与当前对象进行比较的对象。</param>
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj == null || !(obj is Train))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return Key == (obj as Train).Key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-11-27 23:25:36 +08:00
|
|
|
|
|
|
|
|
|
char? _trainClass;
|
2015-05-11 19:27:05 +08:00
|
|
|
|
string _code;
|
2014-11-27 23:25:36 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列车等级
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public char TrainClass
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_trainClass == null)
|
|
|
|
|
{
|
|
|
|
|
if (Code[0] == 'G' || Code[0] == 'D' || Code[0] == 'Z' || Code[0] == 'T')
|
|
|
|
|
_trainClass = Code[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _trainClass ?? '*';
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-28 19:04:24 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否是高速列车
|
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public bool IsHighSpeedClass
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var c = TrainClass;
|
|
|
|
|
return c == 'G' || c == 'D' || c == 'C';
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-21 20:32:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|