using System; using System.Collections.Generic; using System.Linq; using Weee.Models.Paramemter; namespace Weee.DAL { // this class is designed for reading csv file in app_data for creating initial data in our database public class YearlyParameterBuilder : ParameterBuilder { private YearlyParameterCategory _category { get; set; } private List _types { get; set; } private List _areas { get; set; } private List _parameters { get; set; } public YearlyParameterBuilder(Categories cate) { _category = new YearlyParameterCategory() { Category = cate }; } public YearlyParameterBuilder BuildTypes(List TwNames, List CNNames, List ENNames) { if (_types == null) _types = new List(); var Names = CunstructMultiNames(TwNames, CNNames, ENNames); foreach (var Name in Names) { var newtype = new YearlyParameterType() { Category = _category, DisplayNameTW = Name.TWName, DisplayNameCN = Name.CNName, DisplayNameEN = Name.ENName }; _types.Add(newtype); } _category.Types = _types; return this; } public YearlyParameterBuilder BuildAreas(List TwNames, List CNNames, List ENNames) { if (_areas == null) _areas = new List(); var Names = CunstructMultiNames(TwNames, CNNames, ENNames); foreach (var Name in Names) { var newarea = new YearlyParameterArea() { Category = _category, DisplayNameTW = Name.TWName, DisplayNameCN = Name.CNName, DisplayNameEN = Name.ENName }; _areas.Add(newarea); } _category.Areas = _areas; return this; } public YearlyParameterBuilder BuildParameters(Dictionary> ToBeBuilded) { if (_areas == null || _areas.Count() == 0) throw new Exception("The Area Is Empty , please build Area first"); if (_types == null || _types.Count() == 0) throw new Exception("The Type Is Empty , please build Type first"); if (_parameters == null) _parameters = new List(); foreach (var year in ToBeBuilded.Keys) { var maxindex = ToBeBuilded[year].Count() > _areas.Count() * _types.Count() ? _areas.Count() * _types.Count() : ToBeBuilded[year].Count(); for (int i = 0; i < maxindex; i++) { var pvalue = ToBeBuilded[year].ToList()[i]; if (pvalue != null) { var toBeCreate = new YearlyParameter() { Year = year, Value = pvalue.Value }; toBeCreate.Area = _areas[i % _areas.Count()]; toBeCreate.Type = _types[i / _areas.Count()]; _parameters.Add(toBeCreate); _areas[i % _areas.Count()].Parameters.Add(toBeCreate); _types[i / _areas.Count()].Parameters.Add(toBeCreate); } } } return this; } public YearlyParameterCategory GetResults() { return _category; } } }