262 lines
10 KiB
C#
262 lines
10 KiB
C#
using System;
|
||
using System.Data.Entity;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Web.Mvc;
|
||
using Weee.Models;
|
||
using Weee.Models.Paramemter;
|
||
using Weee.DAL;
|
||
using Weee.Models.ExtensionMethods;
|
||
using Resources;
|
||
using DocumentFormat.OpenXml.EMMA;
|
||
|
||
namespace Weee.Areas.Admin.Controllers
|
||
{
|
||
public class YearlyParameterCategoryController : AdminControllerBase
|
||
{
|
||
|
||
public YearlyParameterCategoryController(WeeeDataContext db)
|
||
:base(db)
|
||
{
|
||
}
|
||
#region Area action
|
||
public ActionResult CreateArea(Categories c)
|
||
{
|
||
//var qry = (from a in uow.yearlyParameterRepository.Get()
|
||
// select a).AsQueryable();
|
||
ViewBag.Category = c;
|
||
return View();
|
||
}
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public ActionResult CreateArea(Categories c,YearlyParameterArea Area)
|
||
{
|
||
if (ModelState.IsValid)
|
||
{
|
||
db.YearlyParameterCategories.Where(x=>x.Category==c).First().Areas.Add(Area);
|
||
db.SaveChanges();
|
||
return RedirectToAction("index", "YearlyParameter", new { c = c });
|
||
}
|
||
ViewBag.Category = c;
|
||
return View();
|
||
}
|
||
|
||
|
||
|
||
public ActionResult EditArea(int? a)
|
||
{
|
||
if (a == null) return RedirectToAction("index", "home");
|
||
var Model = db.YearlyParameterAreas.Find(a.Value);
|
||
if (Model == null) return RedirectToAction("index", "home");
|
||
ViewBag.Category = Model.Category.Category;
|
||
return View(Model);
|
||
}
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public ActionResult EditArea(YearlyParameterArea Model)
|
||
{
|
||
var Category = db.YearlyParameterCategories.Single(x => x.ID == Model.CategoryID).Category;
|
||
if (ModelState.IsValid)
|
||
{
|
||
|
||
db.Entry(Model).State = EntityState.Modified;
|
||
db.SaveChanges();
|
||
return RedirectToAction("index", "YearlyParameter", new { c = Category });
|
||
}
|
||
ViewBag.Category = Category;
|
||
return View(Model);
|
||
}
|
||
public ActionResult DeleteArea(int a, Categories category)
|
||
{
|
||
var Area = db.YearlyParameterAreas.Find(a);
|
||
if (Area != null)
|
||
{
|
||
//begin DL-21
|
||
var yearParameters = db.YearlyParameters.Where(x => x.AreaID == Area.ID).ToList();
|
||
this.RemoveYearlyParameters(yearParameters);
|
||
db.YearlyParameterAreas.Remove(Area);
|
||
db.SaveChanges();
|
||
}
|
||
return RedirectToAction("index", "YearlyParameter", new { c = category });
|
||
}
|
||
#endregion
|
||
|
||
#region Type action
|
||
public ActionResult CreateType(Categories c)
|
||
{
|
||
ViewBag.Category = c;
|
||
return View();
|
||
}
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public ActionResult CreateType(Categories c, YearlyParameterType Model)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(Model.DisplayNameCN))
|
||
ModelState.AddModelError("DisplayNameCN", "簡中不得為空白");
|
||
if (string.IsNullOrWhiteSpace(Model.DisplayNameEN))
|
||
ModelState.AddModelError("DisplayNameEN", "英文不得為空白");
|
||
ViewBag.Category = c;
|
||
if (!ModelState.IsValid)
|
||
return View();
|
||
dealWithWarmGasType(Model);
|
||
if (Model.oriHeatValue.ToString().Trim().Equals(""))
|
||
{
|
||
Model.oriHeatValue = null;
|
||
}
|
||
if (ModelState.IsValid)
|
||
{
|
||
db.YearlyParameterCategories.Where(x => x.Category == c).First().Types.Add(Model);
|
||
db.SaveChanges();
|
||
//return View(Model);
|
||
return RedirectToAction("index", "YearlyParameter", new { c = c });
|
||
}
|
||
return View();
|
||
}
|
||
|
||
|
||
public ActionResult EditType(int? t)
|
||
{
|
||
if (t == null) return RedirectToAction("index", "home");
|
||
var Model = db.YearlyParameterTypes.Include(x=>x.Category).Single(x=>x.ID==t.Value);
|
||
if (Model == null) return RedirectToAction("index", "home");
|
||
ViewBag.Category = Model.Category.Category;
|
||
return View(Model);
|
||
}
|
||
private string purgeTW(string source)
|
||
{
|
||
string ret=source;
|
||
ret = ret.Replace("固定源 - ", "");
|
||
ret = ret.Replace("固定源", "");
|
||
ret = ret.Trim();
|
||
ret = ret.Replace("移動源 - ", "");
|
||
ret = ret.Replace("移動源", "");
|
||
ret = ret.Trim();
|
||
return ret;
|
||
}
|
||
private string purgeCN(string source)
|
||
{
|
||
string ret = source;
|
||
ret = ret.Replace("固定源 - ", "");
|
||
ret = ret.Replace("固定源", "");
|
||
ret = ret.Trim();
|
||
ret = ret.Replace("移动源 - ", "");
|
||
ret = ret.Replace("移动源", "");
|
||
ret = ret.Trim();
|
||
return ret;
|
||
}
|
||
private string purgeEN(string source)
|
||
{
|
||
string ret = source;
|
||
ret = ret.Replace("stationary", "");
|
||
ret = ret.Replace("mobile", "");
|
||
ret = ret.Trim();
|
||
return ret;
|
||
}
|
||
//溫室氣體排放型態處理
|
||
private void dealWithWarmGasType(YearlyParameterType Model)
|
||
{
|
||
switch (Model.warmGasType)
|
||
{
|
||
case WarmGasType.固定源:
|
||
Model.DisplayNameTW = purgeTW(Model.DisplayNameTW);
|
||
Model.DisplayNameCN = purgeCN(Model.DisplayNameCN);
|
||
Model.DisplayNameEN = purgeEN(Model.DisplayNameEN);
|
||
Model.DisplayNameTW = "固定源 - " + Model.DisplayNameTW;
|
||
Model.DisplayNameCN = "固定源 - " + Model.DisplayNameCN;
|
||
Model.DisplayNameEN = "stationary " + Model.DisplayNameEN;
|
||
break;
|
||
case WarmGasType.移動源:
|
||
Model.DisplayNameTW = purgeTW(Model.DisplayNameTW);
|
||
Model.DisplayNameCN = purgeCN(Model.DisplayNameCN);
|
||
Model.DisplayNameEN = purgeEN(Model.DisplayNameEN);
|
||
Model.DisplayNameTW = "移動源 - " + Model.DisplayNameTW;
|
||
Model.DisplayNameCN = "移动源 - " + Model.DisplayNameCN;
|
||
Model.DisplayNameEN = "mobile " + Model.DisplayNameEN;
|
||
break;
|
||
case WarmGasType.逸散:
|
||
Model.DisplayNameTW = purgeTW(Model.DisplayNameTW);
|
||
Model.DisplayNameCN = purgeCN(Model.DisplayNameCN);
|
||
Model.DisplayNameEN = purgeEN(Model.DisplayNameEN);
|
||
if (Model.DisplayNameEN.Trim().ToUpper().CompareTo("CO2") != 0)
|
||
ModelState.AddModelError("", "僅CO2為逸散型態!");
|
||
break;
|
||
case WarmGasType.製程:
|
||
Model.DisplayNameTW = purgeTW(Model.DisplayNameTW);
|
||
Model.DisplayNameCN = purgeCN(Model.DisplayNameCN);
|
||
Model.DisplayNameEN = purgeEN(Model.DisplayNameEN);
|
||
break;
|
||
}
|
||
}
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public ActionResult EditType(YearlyParameterType Model)
|
||
{
|
||
var Category = db.YearlyParameterCategories.Single(x => x.ID == Model.CategoryID).Category;
|
||
dealWithWarmGasType(Model);
|
||
if (Model.oriHeatValue.ToString().Trim().Equals(""))
|
||
{
|
||
Model.oriHeatValue = null;
|
||
}
|
||
ViewBag.Category = Category;
|
||
if (ModelState.IsValid)
|
||
{
|
||
|
||
db.Entry(Model).State = EntityState.Modified;
|
||
db.SaveChanges();
|
||
//return View(Model);
|
||
return RedirectToAction("index", "YearlyParameter", new { c = Category });
|
||
}
|
||
return View(Model);
|
||
}
|
||
public ActionResult DeleteType(int t, Categories category)
|
||
{
|
||
var Type = db.YearlyParameterTypes.Find(t);
|
||
if (Type != null)
|
||
{
|
||
var yearParameters = db.YearlyParameters.Where(x => x.TypeID == Type.ID).ToList();
|
||
List<int> yearlyParaIds = yearParameters.Select(x=>x.ID).ToList();
|
||
var Water=db.ProductLCAFabSurveyForm_WaterUsage.Where(x=>yearlyParaIds.Any(y=>y==x.ParameterID)).ToList();
|
||
if (Water != null && Water.Count > 0)
|
||
db.ProductLCAFabSurveyForm_WaterUsage.RemoveRange(Water);
|
||
var Water2 = db.LCARiskAssmtSurveyForm_WaterUsage.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
||
if (Water2 != null && Water.Count > 0)
|
||
db.LCARiskAssmtSurveyForm_WaterUsage.RemoveRange(Water2);
|
||
this.RemoveYearlyParameters(yearParameters);
|
||
db.YearlyParameterTypes.Remove(Type);
|
||
db.SaveChanges();
|
||
}
|
||
return RedirectToAction("index", "YearlyParameter", new { c = category });
|
||
}
|
||
#endregion
|
||
|
||
protected void RemoveYearlyParameters(List<YearlyParameter> parameters)
|
||
{
|
||
foreach (var parameter in parameters)
|
||
{
|
||
db.ProductLCAFabSurveyForm_WaterUsage.RemoveRange(
|
||
db.ProductLCAFabSurveyForm_WaterUsage.Where(x => x.ParameterID == parameter.ID));
|
||
db.LCACommonSurveyForm_GasolineEquipment.RemoveRange(
|
||
db.LCACommonSurveyForm_GasolineEquipment.Where(x => x.ParameterID == parameter.ID));
|
||
db.LCACommonSurveyForm_Kitchen.RemoveRange(
|
||
db.LCACommonSurveyForm_Kitchen.Where(x => x.ParameterID == parameter.ID));
|
||
db.LCACommonSurveyForm_PowerUsage.RemoveRange(
|
||
db.LCACommonSurveyForm_PowerUsage.Where(x => x.ParameterID == parameter.ID));
|
||
db.LCACommonSurveyForm_SteamUsage.RemoveRange(
|
||
db.LCACommonSurveyForm_SteamUsage.Where(x => x.ParameterID == parameter.ID));
|
||
db.LCACommonSurveyForm_Vehicle.RemoveRange(
|
||
db.LCACommonSurveyForm_Vehicle.Where(x => x.ParameterID == parameter.ID));
|
||
}
|
||
db.YearlyParameters.RemoveRange(parameters);
|
||
}
|
||
|
||
protected override void Dispose(bool disposing)
|
||
{
|
||
if (disposing)
|
||
{
|
||
db.Dispose();
|
||
}
|
||
base.Dispose(disposing);
|
||
}
|
||
}
|
||
} |