using AnfleCrawler.Common;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace AnfleCrawler.Repository{ public class CsvRepository : Disposable, IRepository { //public static void Save(IEnumerable <楼盘> set) //{ // char xChar = '卐'; // using (var writer1 = new CsvFileWriter("楼盘.txt", fieldChar: xChar)) // using (var writer2 = new CsvFileWriter("楼栋.txt", fieldChar: xChar)) // using (var writer3 = new CsvFileWriter("房间.txt", fieldChar: xChar)) // { // Type type = typeof(楼盘); // var props = type.GetProperties().Where(p => p.Name != "楼栋").ToArray(); // foreach (var louPan in set) // { // var row = new CsvRow(); // row.Add(props[0].GetValue(louPan)); // for (int i = 1; i < props.Length; i++) // { // object val = props[i].GetValue(louPan); // row.Add(val); // } // writer1.WriteRow(row); // } // } //} private CsvFileWriter _lpWriter, _ldWriter, _fjWriter; private Type[] _types = new Type[] { typeof(HousesEntity), typeof(BuildingEntity), typeof(RoomEntity) }; private Dictionary _props; private IRepository _sync; public CsvRepository(string prefix, IRepository sync = null) { char xChar = '卐'; _lpWriter = new CsvFileWriter(string.Format("{0}楼盘.txt", prefix), true, fieldChar: xChar); _ldWriter = new CsvFileWriter(string.Format("{0}楼栋.txt", prefix), true, fieldChar: xChar); _fjWriter = new CsvFileWriter(string.Format("{0}房间.txt", prefix), true, fieldChar: xChar); _props = new Dictionary (); InitProps(); _sync = sync; } protected override void DisposeInternal(bool disposing) { if (disposing) { _lpWriter.Dispose(); _ldWriter.Dispose(); _fjWriter.Dispose(); } } private void InitProps() { foreach (var type in _types) { _props.Add(type, type.GetProperties()); } } void IRepository.SaveProxy(ProxyEntity entity) { if (_sync != null) { _sync.SaveProxy(entity); } } public HousesEntity LoadHouses(Guid hashKey) { if (_sync != null) { return _sync.LoadHouses(hashKey); } return new HousesEntity() { RowID = hashKey, }; } public BuildingEntity LoadBuilding(Guid hashKey, Guid relationID) { if (_sync != null) { return _sync.LoadBuilding(hashKey, relationID); } return new BuildingEntity() { RowID = hashKey, RelationID = relationID, }; } public RoomEntity LoadRoom(Guid hashKey, Guid relationID) { if (_sync != null) { return _sync.LoadRoom(hashKey, relationID); } return new RoomEntity() { RowID = hashKey, RelationID = relationID, }; } public void Save(HousesEntity entity) { if (_sync != null) { _sync.Save(entity); } lock (_lpWriter) { var props = _props[_types[0]].Where(p => p.Name != "楼栋").ToArray(); var row = new CsvRow(); row.Add(props[0].GetValue(entity)); for (int i = 1; i < props.Length; i++) { object val = props[i].GetValue(entity); row.Add(val); } _lpWriter.WriteRow(row); } } public void Save(BuildingEntity entity) { if (_sync != null) { _sync.Save(entity); } var vProps = new string[] { "楼盘", "房间" }; var props = _props[_types[1]].Where(p => !vProps.Contains(p.Name)).ToArray(); var row = new CsvRow(); row.Add(props[0].GetValue(entity)); for (int i = 1; i < props.Length; i++) { object val = props[i].GetValue(entity); row.Add(val); } _ldWriter.WriteRow(row); } public void Save(RoomEntity entity) { if (_sync != null) { _sync.Save(entity); } Type type = entity.GetType(); var props = _props[_types[2]].Where(p => p.Name != "楼栋").ToArray(); var row = new CsvRow(); row.Add(props[0].GetValue(entity)); for (int i = 1; i < props.Length; i++) { object val = props[i].GetValue(entity); row.Add(val); } _fjWriter.WriteRow(row); } public void SavePrice(CategoryPriceEntity entity) { throw new NotSupportedException(); } public void SaveHouselisting(HouselistingEntity entity) { throw new NotImplementedException(); } public Guid SaveDiscount(DiscountEntity entity) { throw new NotSupportedException(); } public Guid SaveDiscountInfo(DiscountInfoEntity entity) { throw new NotSupportedException(); } public void SaveSchool(SchoolEntity entity) { throw new NotImplementedException(); } public void SaveSchoolHouses(SchoolHousesEntity entity) { throw new NotImplementedException(); } public void SaveSchoolHouselisting(SchoolHouselistingEntity entity) { throw new NotImplementedException(); } }} 楼盘>