demo20230512/DAL/YearlyParameterBuilder.cs
2023-05-12 10:20:28 +08:00

96 lines
3.6 KiB
C#

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<YearlyParameterType> _types { get; set; }
private List<YearlyParameterArea> _areas { get; set; }
private List<YearlyParameter> _parameters { get; set; }
public YearlyParameterBuilder(Categories cate)
{
_category = new YearlyParameterCategory() { Category = cate };
}
public YearlyParameterBuilder BuildTypes(List<string> TwNames, List<string> CNNames, List<string> ENNames)
{
if (_types == null) _types = new List<YearlyParameterType>();
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<string> TwNames, List<string> CNNames, List<string> ENNames)
{
if (_areas == null) _areas = new List<YearlyParameterArea>();
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<int, IEnumerable<decimal?>> 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<YearlyParameter>();
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;
}
}
}