demo20230512/Migrations/Configuration.cs
2023-05-12 10:20:28 +08:00

613 lines
28 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace Weee.Migrations
{
using CScommon;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using Weee.DAL;
using Weee.Models;
using Weee.Models.Paramemter;
internal sealed class Configuration : DbMigrationsConfiguration<Weee.DAL.WeeeDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;// true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "Weee.DAL.WeeeDataContext";
}
protected override void Seed(WeeeDataContext context)
{
context.AssessmentReportVersion.AddOrUpdate(x => x.ARversionGuid,
new AssessmentReportVersion()
{
ARversionGuid = new Guid("47A4251D-875B-4B99-AA32-B7C206E6873F"),
ARversion = "AR4",
createTime = new DateTime(2022, 12, 22),
createBy = "admin"
}, new AssessmentReportVersion()
{
ARversionGuid = new Guid("5191B6FB-F39F-4FAD-B746-007366744EB3"),
ARversion = "AR5",
createTime = new DateTime(2022, 12, 22),
createBy = "admin"
}, new AssessmentReportVersion()
{
ARversionGuid = new Guid("64B200BA-4D85-4232-BD25-2DC9FA721D34"),
ARversion = "AR6",
createTime = new DateTime(2022, 12, 22),
createBy = "admin"
});
using (var UserStore = new UserStore<User>(context))
using (var RoleStore = new RoleStore<IdentityRole>(context))
using (var UserManager = new UserManager<User>(UserStore))
using (var RoleManager = new RoleManager<IdentityRole>(RoleStore))
{
if (context.Users.Count() == 0 && context.Parameters.Count() == 0)// run following seed only if the database is totally new
{
#region seed admin and role
if (!RoleManager.RoleExists(ProgramConstants.admin))
{
var roleresult = RoleManager.Create(new IdentityRole(ProgramConstants.admin));
}
if (!RoleManager.RoleExists(ProgramConstants.normalcompany))
{
RoleManager.Create(new IdentityRole(ProgramConstants.normalcompany));
}
if (!RoleManager.RoleExists(ProgramConstants.certification))
{
RoleManager.Create(new IdentityRole(ProgramConstants.certification));
}
var u = new User()
{
Name = "admin",
Email = "haha@ha.haha",
IsSystemAdmin = true
};
u.UserName = ProgramConstants.admin;
var adminresult = UserManager.Create(u, ProgramConstants.DefaultPassword);
if (adminresult.Succeeded)
{
var result = UserManager.AddToRole(u.Id, ProgramConstants.admin);
}
#endregion
#region initial parameter
CreateYearlyParameter(context, Categories.Electric, "electric.csv");
CreateYearlyParameter(context, Categories.ElectricOrganization, "electric.csv");
CreateYearlyParameter(context, Categories.Water, "water.csv");
CreateYearlyParameter(context, Categories.Vehicle, "Vehicle.csv");
//CreateYearlyParameter(context, Categories.VehicleOrganization, "Vehicle.csv");
CreateYearlyParameter(context, Categories.GasolineEquipment, "GasolineEquipment.csv");
CreateYearlyParameter(context, Categories.Kitchen, "Kitchen.csv");
CreateYearlyParameter(context, Categories.Steam, "Steam.csv");
CreateYearlyParameter(context, Categories.SteamOrganization, "Steam.csv");
CreateNonYearlyParameter(context, Categories.FireEquipment, "FireEquipment.csv");
CreateNonYearlyParameter(context, Categories.Septic, "Septic.csv");
CreateNonYearlyParameter(context, Categories.OtherCompound, "OtherCompound.csv");
CreateNonYearlyParameter(context, Categories.RefrigerantGWP, "RefrigerantGWP.csv");
CreateNonYearlyParameter(context, Categories.Refrigerant, "Refrigerant.csv");
CreateNonYearlyParameter(context, Categories.Waste, "waste.csv");
#endregion
#region seed pcr
//for (var i = 0; i < 10; i++)
//{
// var temp = new PCR()
// {
// DisplayNameTW = "hello",
// Percentage = ((decimal)rnd.Next(0, 1000)) / 10
// };
// var ids = context.SimaproParameters.Select(x => x.ID).ToList();
// var count = ids.Count();
// var numbers = ids.OrderBy(x => rnd.Next()).Take(5).ToList();
// foreach (var number in numbers)
// {
// temp.SimaproPCRs.Add(new SimaproPCR() { Parameter = context.SimaproParameters.First(x => x.ID == number) });
// }
// context.PCRs.Add(temp);
//}
//context.SaveChanges();
#endregion
RandomSeedComapny(context);
context.SaveChanges();
}
//seed from transport csv because it is null
if (context.NonYearlyParameterCategories.Where(x => x.Category == Categories.Transport).SingleOrDefault() == null)
{
CreateNonYearlyParameter(context, Categories.Transport, "Transport.csv");
context.SaveChanges();
}
if (context.NonYearlyParameterCategories.Where(x => x.Category == Categories.WasteWater).SingleOrDefault() == null)
{
CreateNonYearlyParameter(context, Categories.WasteWater, "WasteWater.csv");
context.SaveChanges();
}
if (!context.LCARiskAssmtSurveyForm_LifecycleAssmtItems.Any())
{
CreateLifecycleAssmtItem(context, "LifecycleAssmtItem.csv");
context.SaveChanges();
}
}
base.Seed(context);
}
#region private function
private static void CreateYearlyParameter(WeeeDataContext context, Categories cate, string filename)
{
using (var file = new System.IO.StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/" + filename)))
{
var ElectricBuilder = new YearlyParameterBuilder(cate);
var typeTwNames = file.ReadLine().Split(',').ToList();
var typeCnNames = file.ReadLine().Split(',').ToList();
var typeEnNames = file.ReadLine().Split(',').ToList();
var areaTwNames = file.ReadLine().Split(',').ToList();
var areaCnNames = file.ReadLine().Split(',').ToList();
var areaEnNames = file.ReadLine().Split(',').ToList();
string line;
var parameters = new Dictionary<int, IEnumerable<decimal?>>();
int baseyear = 2006;
while ((line = file.ReadLine()) != null)
{
var row = line.Split(',').ToList();
var decimalrow = new List<decimal?>();
foreach (var item in row)
{
decimal parsed;
if (decimal.TryParse(item, out parsed))
decimalrow.Add(parsed);
else
decimalrow.Add(null);
}
parameters.Add(baseyear, decimalrow);
baseyear++;
}
var buildresult = ElectricBuilder
.BuildTypes(typeTwNames, typeCnNames, typeEnNames)
.BuildAreas(areaTwNames, areaCnNames, areaEnNames)
.BuildParameters(parameters)
.GetResults();
context.YearlyParameterCategories.Add(buildresult);
}
}
private static void CreateNonYearlyParameter(WeeeDataContext context, Categories cate, string filename)
{
using (var file = new System.IO.StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/" + filename)))
{
var Builder = new NonYearlyParameterBuilder(cate);
string line;
var dataset = new List<List<string>>();
while ((line = file.ReadLine()) != null)
{
var toadd = line.Split(',').ToList();
if (toadd.Count == 4) dataset.Add(toadd);
}
var typeTwNames = dataset.Select(x => x[0]).ToList();
var typeCnNames = dataset.Select(x => x[1]).ToList();
var typeEnNames = dataset.Select(x => x[2]).ToList();
var parameters = dataset.Select(x => x[3]).ToList();
//extra handle for waste ,waste water, and transport , replace '_' to ','
if (cate == Categories.Waste || cate == Categories.WasteWater || cate == Categories.Transport)
{
for (int i = 0; i < typeTwNames.Count; i++)
{
typeTwNames[i] = typeTwNames[i].Replace('_', ',');
typeCnNames[i] = typeCnNames[i].Replace('_', ',');
typeEnNames[i] = typeEnNames[i].Replace('_', ',');
}
}
var decimalrow = new List<decimal>();
foreach (var item in parameters)
{
decimal parsed;
if (decimal.TryParse(item, out parsed))
decimalrow.Add(parsed);
}
var result = Builder
.BuildTypes(typeTwNames, typeCnNames, typeEnNames)
.BuildParameters(decimalrow)
.GetResults();
context.NonYearlyParameterCategories.Add(result);
}
}
//remove this when go online
private void RandomSeedComapny(WeeeDataContext context)
{
Random rnd = new Random();
var UserManager = new UserManager<User>(new UserStore<User>(context));
var NormalCompanyNumber = 1;// rnd.Next(5, 10);
for (int i = 0; i < NormalCompanyNumber; i++)
{
var company = new NormalCompany()
{
Name = "company " + i,
Address = "Taipei" + i,
Description = "Hello" + i,
VATNumber = "ccccVAT" + i,
CEOName = "a ceo" + i,
EnglishName= "company " + i,
NumberOfEmployees=1
};
var UserNumber = rnd.Next(1, 3);
for (int j = 0; j < UserNumber; j++)// 建立一般測試使用者
{
var u = new User() { UserName = "tester" + i.ToString() + j.ToString(), Name = "normal tester" + rnd.Next(0, 1000).ToString()
, Email = "haha@ha.haha", IsCompanyAdmin = j == 0 };
UserManager.Create(u, ProgramConstants.DefaultPassword);
company.Users.Add(u);
var uat = new UserAccountType() { UserId=u.Id, AccountType=ACCOUNT_TYPE.FULL_FUNCTION, StartTime=DateTime.Now, EndTime=DateTime.Now.AddYears(2)
, Enabled=true };
context.UserAccountType.Add(uat);
}
var FabNumber = rnd.Next(9, 12);
for (int j = 0; j < FabNumber; j++)
{
var f = new Fab() { Name = " 工廠 " + j, Phone = "1234", Address = "haha", IsIso14064Passed = rnd.Next(0, 1) == 0 };
company.Fabs.Add(f);
}
var ProdNumber = rnd.Next(5, 8);
for (int j = 0; j < ProdNumber; j++)
{
var p = new Product() { Name = " 產品 " + j, SpecDescription = "haha", Weight = rnd.Next(5, 10), FunctionalUnit="kg" };
company.Products.Add(p);
}
context.Companies.Add(company);
}
var CerificateCompanyNumber = 1;// rnd.Next(2, 4);
for (int i = 0; i < CerificateCompanyNumber; i++)
{
var company = new CertificationCompany()
{
Name = "其他查證單位" + i,
Address = "Taipei" + i,
Description = "Hello" + i,
VATNumber = "ccccVAT" + i,
CEOName = "a ceo" + i,
EnglishName = "其他查證單位 " + i,
NumberOfEmployees = 1
};
var UserNumber = rnd.Next(1, 3);
for (int j = 0; j < UserNumber; j++)// 建立查證測試使用者
{
var u = new User() { UserName = "testerc" + i.ToString() + j.ToString(), Name = "certi tester" + rnd.Next(0, 1000).ToString(), Email = "haha@ha.haha" };
UserManager.Create(u, ProgramConstants.DefaultPassword);
company.Users.Add(u);
}
context.Companies.Add(company);
}
//valid company1.Fabs.Add(new Fab() { Name = "test company1 fab", Phone = "1234", Address = "haha", IsIso14064Passed = false });
//invalid company1.Fabs = new List<Fab>();
}
private List<SimaproParameterCategory> SimapeoParameterSeeds()
{
var returnvalue = new List<SimaproParameterCategory>()
{
new SimaproParameterCategory()
{
DisplayNameTW = "TestingSheet",
SubTypes = new List<SimaproParameterType>()
{
new SimaproParameterType()
{
DisplayNameTW = "SubType1"
},
new SimaproParameterType()
{
DisplayNameTW = "SubType2"
}
},
SimaproVersion = new SimaproVersion()
{
Version = "v0.1"
}
},
new SimaproParameterCategory()
{
DisplayNameTW = "TestingSheet2",
SubTypes = new List<SimaproParameterType>()
{
new SimaproParameterType()
{
DisplayNameTW = "SubType3"
},
new SimaproParameterType()
{
DisplayNameTW = "SubType4"
},
new SimaproParameterType()
{
DisplayNameTW = "SubType5"
},
new SimaproParameterType()
{
DisplayNameTW = "SubType6"
}
},
SimaproVersion = new SimaproVersion()
{
Version = "v0.2"
}
}
};
return returnvalue;
}
//seed 來源:Unlock_psd-003-04001 工作表2
// 化糞池: 總工時 sheet
private List<YearlyParameterCategory> YearlyParamemterSeeds()
{
var returnvalue = new List<YearlyParameterCategory>
{
new YearlyParameterCategory(Categories.Electric)
{
Areas=new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("華北地區"),
new YearlyParameterArea("東北地區"),
new YearlyParameterArea("華東地區"),
new YearlyParameterArea("華中地區"),
new YearlyParameterArea("南方地區")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("Default")
}
},new YearlyParameterCategory(Categories.ElectricOrganization)
{
Areas=new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("華北地區"),
new YearlyParameterArea("東北地區"),
new YearlyParameterArea("華東地區"),
new YearlyParameterArea("華中地區"),
new YearlyParameterArea("南方地區")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("Default")
}
},
new YearlyParameterCategory(Categories.Water)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("Default")
}
},
//new YearlyParameterCategory(Categories.VehicleOrganization)
//{
// Areas = new List<YearlyParameterArea>
// {
// new YearlyParameterArea("台灣"),
// new YearlyParameterArea("大陸")
// },
// Types = new List<YearlyParameterType>
// {
// new YearlyParameterType("移動源汽油"),
// new YearlyParameterType("移動源柴油")
// }
//}
//,
new YearlyParameterCategory(Categories.Vehicle)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("移動源汽油"),
new YearlyParameterType("移動源柴油")
}
},
new YearlyParameterCategory(Categories.GasolineEquipment)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("固定源柴油"),
new YearlyParameterType("移動源柴油")
}
},
new YearlyParameterCategory(Categories.Kitchen)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("無煤煙"),
new YearlyParameterType("天然氣"),
new YearlyParameterType("液化石油氣"),
new YearlyParameterType("固定源汽油"),
new YearlyParameterType("重油")
}
},new YearlyParameterCategory(Categories.SteamOrganization)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("Default")
}
},
new YearlyParameterCategory(Categories.Steam)
{
Areas = new List<YearlyParameterArea>
{
new YearlyParameterArea("台灣"),
new YearlyParameterArea("大陸")
},
Types = new List<YearlyParameterType>
{
new YearlyParameterType("Default")
}
}
};
return returnvalue;
}
private List<NonYearlyParameterCategory> NonYearlyParameterSeeds()
{
var Types = new List<NonYearlyParameterCategory>
{
new NonYearlyParameterCategory(Categories.Refrigerant)
{
Types = new List<NonYearlyParameterType>
{
new NonYearlyParameterType("家用冰箱",0.003m),
new NonYearlyParameterType("飲水機",0.003m),
new NonYearlyParameterType("除濕機",0.003m),
new NonYearlyParameterType("餐廳冷藏櫃",0.08m),
new NonYearlyParameterType("恆溫除濕機",0.08m),
new NonYearlyParameterType("冰水主機",0.085m),
new NonYearlyParameterType("分離式主機",0.055m),
new NonYearlyParameterType("公務車",0.15m),
new NonYearlyParameterType("家用冷凍冷藏設備",0.003m),
new NonYearlyParameterType("獨立商用冷凍冷藏設備",0.08m)
}
},
new NonYearlyParameterCategory(Categories.FireEquipment)
{
Types=new List<NonYearlyParameterType>
{
new NonYearlyParameterType("CO2",1m),
new NonYearlyParameterType("FM-200",3220m),
new NonYearlyParameterType("Halon1211",1890m),
new NonYearlyParameterType("Halon1301",7140m),
new NonYearlyParameterType("Halon2402",1640m)
}
},
new NonYearlyParameterCategory(Categories.OtherCompound)
{
Types=new List<NonYearlyParameterType>
{
new NonYearlyParameterType("Carbon tetrachloride",1400m),
new NonYearlyParameterType("Methyl bromide",5m),
new NonYearlyParameterType("Methyl chloroform",146m),
new NonYearlyParameterType("Sulfur hexafluoride",22800m),
new NonYearlyParameterType("Nitrogen trifluoride",17200m)
}
},
new NonYearlyParameterCategory(Categories.RefrigerantGWP)
{
Types=new List<NonYearlyParameterType>
{
new NonYearlyParameterType("R-11",4750m),
new NonYearlyParameterType("R-12",10900m),
new NonYearlyParameterType("R-13",14400m),
new NonYearlyParameterType("R-113",6130m),
new NonYearlyParameterType("R-114",10000m)
}
},
//此兩個category的parameter 本身就需要binding , 較為特別 , 需重新考慮是否加在此處
//new NonYearlyParameterCategory("汽柴油密度")
//{
// Parameters=new List<NonYearlyParameter>
// {
// new NonYearlyParameter("柴油密度",0.84m),
// new NonYearlyParameter("汽油密度",0.73m),
// new NonYearlyParameter("LPG密度",0.54m)
// }
//},
new NonYearlyParameterCategory(Categories.Septic)
{
Types=new List<NonYearlyParameterType>
{
new NonYearlyParameterType("排放因子",0.6m),
new NonYearlyParameterType("汙水濃度",200m),
new NonYearlyParameterType("每人每天汙水量",125m),
new NonYearlyParameterType("化糞池處理效率",0.85m),
new NonYearlyParameterType("單位換算",0.000001m),
new NonYearlyParameterType("GWP",25m)
}
},
new NonYearlyParameterCategory(Categories.Transport)
{
Types=new List<NonYearlyParameterType>
{
new NonYearlyParameterType("air,aircraft, freight/RER S",0.6m),
new NonYearlyParameterType("air,aircraft, freight, intercontinental/RER S",200m),
new NonYearlyParameterType("sea,transoceanic freight ship/OCE S",125m),
new NonYearlyParameterType("sea,barge tanker/RER S",125m),
new NonYearlyParameterType("road,lorry 3.5-7.5t, EURO5/RER S",0.85m),
new NonYearlyParameterType("road,lorry 7.5-16t, EURO5/RER S",0.85m)
}
}
};
return Types;
}
private static void CreateLifecycleAssmtItem(WeeeDataContext context, string filename)
{
if (HttpContext.Current == null)
{
return;
}
using (var file = new System.IO.StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/" + filename)))
{
string line;
while ((line = file.ReadLine()) != null)
{
var array = line.Split(',').ToList();
var item = new LCARiskAssmtSurveyForm_LifecycleAssmtItem()
{
LifeCycleStage = int.Parse(array[0]),
DisplayNameTW = array[1],
DisplayNameCN = array[2],
DisplayNameEN = array[3]
};
context.LCARiskAssmtSurveyForm_LifecycleAssmtItems.Add(item);
}
}
}
private enum unitsfortest
{
kg,
m2,
cm3,
p,
m
}
#endregion
}
}