using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Serialization;
using NewLife;
using NewLife.Data;
using NewLife.Log;
using NewLife.Model;
using NewLife.Reflection;
using NewLife.Threading;
using NewLife.Web;
using XCode;
using XCode.Cache;
using XCode.Configuration;
using XCode.DataAccessLayer;
using XCode.Membership;
using HtmlAgilityPack;
namespace StockData.Entity
{
/// <summary>股票基础信æ¯</summary>
public partial class StockBaseInfo : Entity<StockBaseInfo>
{
#region 对象æ“作
static StockBaseInfo()
{
// ç´¯åŠ å—段
//Meta.Factory.AdditionalFields.Add(__.Logins);
// 过滤器 UserModuleã€TimeModuleã€IPModule
}
/// <summary>验è¯æ•°æ®ï¼Œé€šè¿‡æŠ›å‡ºå¼‚常的方å¼æ示验è¯å¤±è´¥ã€‚</summary>
/// <param name="isNew">是å¦æ’å…¥</param>
public override void Valid(Boolean isNew)
{
// 如果没有è„æ•°æ®ï¼Œåˆ™ä¸éœ€è¦è¿›è¡Œä»»ä½•å¤„ç†
if (!HasDirty) return;
// 在新æ’入数æ®æˆ–者修改了指定å—段时进行修æ£
}
///// <summary>首次连接数æ®åº“æ—¶åˆå§‹åŒ–æ•°æ®ï¼Œä»…用于实体类é‡è½½ï¼Œç”¨æˆ·ä¸åº”该调用该方法</summary>
//[EditorBrowsable(EditorBrowsableState.Never)]
//protected override void InitData()
//{
// // InitData一般用于当数æ®è¡¨æ²¡æœ‰æ•°æ®æ—¶æ·»åŠ 一些默认数æ®ï¼Œè¯¥å®žä½“类的任何第一次数æ®åº“æ“作都会触å‘该方法,默认异æ¥è°ƒç”¨
// if (Meta.Count > 0) return;
// if (XTrace.Debug) XTrace.WriteLine("开始åˆå§‹åŒ–StockBaseInfo[股票基础信æ¯]æ•°æ®â€¦â€¦");
// var entity = new StockBaseInfo();
// entity.Code = "abc";
// entity.Name = "abc";
// entity.Exchange = "abc";
// entity.StartDate = DateTime.Now;
// entity.CreateDate = DateTime.Now;
// entity.Insert();
// if (XTrace.Debug) XTrace.WriteLine("完æˆåˆå§‹åŒ–StockBaseInfo[股票基础信æ¯]æ•°æ®ï¼"
//}
///// <summary>å·²é‡è½½ã€‚基类先调用Valid(true)验è¯æ•°æ®ï¼Œç„¶åŽåœ¨äº‹åŠ¡ä¿æŠ¤å†…调用OnInsert</summary>
///// <returns></returns>
//public override Int32 Insert()
//{
// return base.Insert();
//}
///// <summary>å·²é‡è½½ã€‚在事务ä¿æŠ¤èŒƒå›´å†…处ç†ä¸šåŠ¡ï¼Œä½äºŽValid之åŽ</summary>
///// <returns></returns>
//protected override Int32 OnDelete()
//{
// return base.OnDelete();
//}
#endregion
#region 扩展属性
#endregion
#region 扩展查询
/// <summary>æ ¹æ®è‚¡ç¥¨ç¼–ç 查找</summary>
/// <param name="code">股票编ç </param>
/// <returns>实体对象</returns>
public static StockBaseInfo FindByCode(String code)
{
if (code.IsNullOrEmpty()) return null;
// 实体缓å˜
if (Meta.Count < 1000) return Meta.Cache.Entities.FirstOrDefault(e => e.Code == code);
// å•å¯¹è±¡ç¼“å˜
//return Meta.SingleCache[code];
return Find(_.Code == code);
}
/// <summary>æ ¹æ®å称查找</summary>
/// <param name="name">å称</param>
/// <returns>实体列表</returns>
public static IList<StockBaseInfo> FindByName(String name)
{
// 实体缓å˜
if (Meta.Count < 1000) return Meta.Cache.Entities.Where(e => e.Name == name).ToList();
return FindAll(_.Name == name);
}
#endregion
#region 高级查询
#endregion
#region 业务æ“作
/// <summary>
/// 获å–所有股票代ç å’Œå称
/// </summary>
public static void ReadAllStockBaseInfo()
{
//上海:/html[1]/body[1]/div[9]/div[2]/div[1]/ul[1]
//下级是li列表 ,Text值就是股票å称和代ç XXX()
//深圳:上海:/html[1]/body[1]/div[9]/div[2]/div[1]/ul[2]
string url = @"http://quote.eastmoney.com/stocklist.html";
HtmlWeb htmlweb = new HtmlWeb();
htmlweb.OverrideEncoding = Encoding.GetEncoding(936);
HtmlDocument doc = htmlweb.Load(url);
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{"上海",@"/html[1]/body[1]/div[9]/div[2]/div[1]/ul[1]" },
{"深圳",@"/html[1]/body[1]/div[9]/div[2]/div[1]/ul[2]" }
};
#region 获å–
Dictionary<String, StockBaseInfo> list = new Dictionary<string, StockBaseInfo>();
foreach (var item in dic)
{
var res = doc.DocumentNode.SelectSingleNode(item.Value).SelectNodes(@"li");
if (res.Count > 0)
{
foreach (var node in res)
{
var name = node.InnerText.Trim();
if (name.IsNullOrEmpty()) continue;
var str = name.Split('(', ')');
if (str.Length < 2) continue;
StockBaseInfo et = new StockBaseInfo()
{
Code = str[1],
Name = str[0],
Exchange = item.Key,
StartDate = new DateTime(2000, 1, 1),
CreateDate = DateTime.Now
};
if(!list.ContainsKey(et.Code))
{
list.Add(et.Code,et);
}
}
}
}
list.ToValueArray().Insert(true);
#endregion
}
#endregion
}
}
|