593 lines
30 KiB
C#
593 lines
30 KiB
C#
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Web.Mvc;
|
|
using PagedList;
|
|
using Weee.Areas.Admin.ViewModels;
|
|
using Weee.DAL;
|
|
using Weee.Models.Paramemter;
|
|
using Weee.Models.ExtensionMethods;
|
|
using Weee.Supports;
|
|
using Resources;
|
|
using ClosedXML.Excel;
|
|
using System;
|
|
using System.IO;
|
|
using System.Net.Mime;
|
|
using Resources.Helper;
|
|
using System.Collections.Generic;
|
|
using Weee.ViewModels;
|
|
using System.Web;
|
|
using CScommon;
|
|
using System.Globalization;
|
|
|
|
namespace Weee.Areas.Admin.Controllers
|
|
{
|
|
public class ParameterController : AdminControllerBase
|
|
{
|
|
public ParameterController(WeeeDataContext db) : base(db)
|
|
{
|
|
}
|
|
|
|
private ParameterIndexViewModel loadViewModel(Categories? c, int page = 1, string searchString = "")
|
|
{
|
|
var nonYearlyParameterTypes = db.NonYearlyParameterTypes.Include(n => n.Category)
|
|
.Include(x => x.Parameters)
|
|
.OrderBy(x => x.Category.ID)
|
|
.ToList();
|
|
if (c != null) nonYearlyParameterTypes = nonYearlyParameterTypes.Where(x => x.Category.Category == c).ToList();
|
|
|
|
var dataFilter = new DataFilter();
|
|
var filteredNonYearlyParameterTypes = dataFilter.FilterNonYearlyParameterType(nonYearlyParameterTypes, searchString);
|
|
|
|
foreach (var type in filteredNonYearlyParameterTypes)
|
|
{
|
|
type.Parameters = type.Parameters.OrderByDescending(x => x.CreateTime).ToList();
|
|
}
|
|
|
|
var viewModel = new ParameterIndexViewModel()
|
|
{
|
|
FilteredParameterTypes = filteredNonYearlyParameterTypes,
|
|
PagedParameterTypes = filteredNonYearlyParameterTypes.ToPagedList(page, 10),
|
|
Category = c,
|
|
CategoryName = c.DisplayString(),
|
|
Page = page,
|
|
SearchString = searchString
|
|
};
|
|
return viewModel;
|
|
}
|
|
|
|
[HttpGet]
|
|
public ActionResult Index(Categories? c, int page = 1, string searchString = "")
|
|
{
|
|
var viewModel = loadViewModel(c, page, searchString);
|
|
if (TempData["ErrMsg"] != null && !string.IsNullOrWhiteSpace(TempData["ErrMsg"].ToString()))
|
|
viewModel.errMsg = TempData["ErrMsg"].ToString();
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Index(ParameterIndexViewModel oriViewModel)
|
|
{
|
|
if (oriViewModel.SearchString == null) oriViewModel.SearchString = "";
|
|
ParameterIndexViewModel viewModel = loadViewModel(oriViewModel.Category, oriViewModel.Page, oriViewModel.SearchString);
|
|
switch (oriViewModel.cmd)
|
|
{
|
|
case "upload":
|
|
viewModel.uploadExcelFile = oriViewModel.uploadExcelFile;
|
|
var errMsg = processExcelUpload(viewModel);
|
|
viewModel = loadViewModel(oriViewModel.Category, oriViewModel.Page, oriViewModel.SearchString);
|
|
if (!string.IsNullOrEmpty(errMsg))
|
|
TempData["ErrMsg"] = viewModel.errMsg;
|
|
return RedirectToAction("Index", "Parameter", new { c = oriViewModel.Category });
|
|
break;
|
|
case "download":
|
|
return processExcelDownload(viewModel);
|
|
}
|
|
return View(viewModel);
|
|
}
|
|
private FileStreamResult processExcelDownload(ParameterIndexViewModel viewModel)
|
|
{
|
|
XLWorkbook excelWorkbook = new XLWorkbook(); // 寫資料進excelWorkbook
|
|
string sheetName = viewModel.CategoryName.Replace(' ', '_');
|
|
var worksheet = excelWorkbook.Worksheets.Add(sheetName);
|
|
int rowTh;
|
|
|
|
var provider = ModelMetadataProviders.Current;
|
|
worksheet.Cell($"A1").Value = "ID";
|
|
worksheet.Cell($"B1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameterType), "DisplayNameTW").DisplayName;
|
|
worksheet.Cell($"C1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameterType), "DisplayNameCN").DisplayName;
|
|
worksheet.Cell($"D1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameterType), "DisplayNameEN").DisplayName;
|
|
worksheet.Cell($"E1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "Value").DisplayName;
|
|
worksheet.Cell($"F1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "CO2Value").DisplayName;
|
|
worksheet.Cell($"G1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "CH4Value").DisplayName;
|
|
worksheet.Cell($"H1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "N2OValue").DisplayName;
|
|
worksheet.Cell($"I1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "HFCsValue").DisplayName;
|
|
worksheet.Cell($"J1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "PFCsValue").DisplayName;
|
|
worksheet.Cell($"K1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "SF6Value").DisplayName;
|
|
worksheet.Cell($"L1").Value = provider.GetMetadataForProperty(null, typeof(NonYearlyParameter), "NF3Value").DisplayName;
|
|
|
|
rowTh = 2;
|
|
foreach (NonYearlyParameterType typeRec in viewModel.FilteredParameterTypes)
|
|
{
|
|
worksheet.Cell($"A{rowTh}").Value = $"{typeRec.ID}";
|
|
worksheet.Cell($"B{rowTh}").Value = $"{typeRec.DisplayNameTW}";
|
|
worksheet.Cell($"C{rowTh}").Value = $"{typeRec.DisplayNameCN}";
|
|
worksheet.Cell($"D{rowTh}").Value = $"{typeRec.DisplayNameEN}";
|
|
var para = typeRec.Parameters.First();
|
|
worksheet.Cell($"E{rowTh}").Value = para.Value;
|
|
worksheet.Cell($"F{rowTh}").Value = para.CO2Value;
|
|
worksheet.Cell($"G{rowTh}").Value = para.CH4Value;
|
|
worksheet.Cell($"H{rowTh}").Value = para.N2OValue;
|
|
worksheet.Cell($"I{rowTh}").Value = para.HFCsValue;
|
|
worksheet.Cell($"J{rowTh}").Value = para.PFCsValue;
|
|
worksheet.Cell($"K{rowTh}").Value = para.SF6Value;
|
|
worksheet.Cell($"L{rowTh}").Value = para.NF3Value;
|
|
rowTh++;
|
|
}
|
|
|
|
string myName = Server.UrlEncode(Resources.Resource.StaticLabelNonYearlyParameterMaintain.Replace(' ', '_')
|
|
+ $"_{sheetName}_" + DateTime.Today.ToString("yyyyMMdd") + ".xlsx");
|
|
MemoryStream stream = new MemoryStream();
|
|
excelWorkbook.SaveAs(stream);
|
|
stream.Position = 0;
|
|
|
|
Response.Clear();
|
|
Response.Buffer = true;
|
|
Response.AddHeader("content-disposition", "attachment; filename=" + myName);
|
|
Response.ContentType = "application/vnd.ms-excel";
|
|
Response.BinaryWrite(stream.ToArray());
|
|
Response.End();
|
|
return new FileStreamResult(stream, MediaTypeNames.Application.Octet);
|
|
}
|
|
|
|
private string processExcelUpload(ParameterIndexViewModel sour)
|
|
{
|
|
string err = "";
|
|
try
|
|
{
|
|
var exlVmList = GetExcelData(sour.uploadExcelFile);
|
|
exlVmList = exlVmList.Where(x => x.Value > 0).ToList();
|
|
if (exlVmList != null && exlVmList.Count() > 0)
|
|
{
|
|
if (sour == null || sour.Category == null)
|
|
throw new Exception("Category不可為空");
|
|
else
|
|
exlVmList.ForEach(x => { x.category = sour.Category; });
|
|
if (CkExcelData(exlVmList, out string ckErr))
|
|
SaveExelData(exlVmList);
|
|
else
|
|
throw new Exception(ckErr);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex = CScommon.Exceptions.inner(ex);
|
|
err = ex.Message;
|
|
}
|
|
return err;
|
|
}
|
|
|
|
private List<ParameterExcelViewModel> GetExcelData(HttpPostedFileBase uploadExcelFile)
|
|
{
|
|
var re = new List<ParameterExcelViewModel>();
|
|
if (uploadExcelFile == null || uploadExcelFile.InputStream == null || uploadExcelFile.InputStream.Length == 0)
|
|
throw new Exception($"未指定上傳檔案!");
|
|
else
|
|
{
|
|
string extesion = uploadExcelFile.FileName.Substring(uploadExcelFile.FileName.LastIndexOf(".") + 1);
|
|
if (extesion.ToLower() != "xlsx")
|
|
throw new Exception("僅支持xlsx類型!");
|
|
|
|
using (XLWorkbook workBook = new XLWorkbook(uploadExcelFile.InputStream))
|
|
{
|
|
IXLWorksheet workSheet = workBook.Worksheet(1);
|
|
if (workSheet == null)
|
|
throw new Exception($"檔案錯誤, sheet 不存在!");
|
|
int ith = 0;
|
|
foreach (IXLRow row in workSheet.Rows())
|
|
{
|
|
ith++;
|
|
if (ith == 1)
|
|
continue; // skip first row
|
|
var exlIt = new ParameterExcelViewModel();
|
|
//ID
|
|
string cellA = workSheet.Cell($"A{ith}").Value.ToString().Trim();
|
|
int ID = 0;
|
|
int.TryParse(cellA, out ID);
|
|
exlIt.ID = ID;
|
|
//繁中
|
|
string cellB = workSheet.Cell($"B{ith}").Value.ToString().Trim();
|
|
exlIt.TWNm = cellB;
|
|
//簡體
|
|
string cellC = workSheet.Cell($"C{ith}").Value.ToString().Trim();
|
|
exlIt.CNNm = cellC;
|
|
//英文
|
|
string cellD = workSheet.Cell($"D{ith}").Value.ToString().Trim();
|
|
exlIt.ENNm = cellD;
|
|
//值
|
|
string cellE = workSheet.Cell($"E{ith}").Value.ToString().Trim();
|
|
exlIt.Value = GetFromExcel(cellE);
|
|
//CO2值
|
|
string cellF = workSheet.Cell($"F{ith}").Value.ToString().Trim(); ;
|
|
exlIt.CO2 = GetFromExcel(cellF);
|
|
//CH4值
|
|
string cellG = workSheet.Cell($"G{ith}").Value.ToString().Trim();
|
|
exlIt.CH4 = GetFromExcel(cellG);
|
|
//N2O值
|
|
string cellH = workSheet.Cell($"H{ith}").Value.ToString().Trim();
|
|
exlIt.N2O = GetFromExcel(cellH);
|
|
//HFCs值
|
|
string cellI = workSheet.Cell($"I{ith}").Value.ToString().Trim();
|
|
exlIt.HFCs = GetFromExcel(cellI);
|
|
//PFCs值
|
|
string cellJ = workSheet.Cell($"J{ith}").Value.ToString().Trim();
|
|
exlIt.SF6 = GetFromExcel(cellJ);
|
|
//SF6值
|
|
string cellK = workSheet.Cell($"K{ith}").Value.ToString().Trim();
|
|
exlIt.SF6 = GetFromExcel(cellK);
|
|
//NF3值
|
|
string cellL = workSheet.Cell($"L{ith}").Value.ToString().Trim();
|
|
exlIt.NF3 = GetFromExcel(cellL);
|
|
re.Add(exlIt);
|
|
}
|
|
}
|
|
}
|
|
return re;
|
|
}
|
|
|
|
private bool CkExcelData(List<ParameterExcelViewModel> sour, out string errMsg)
|
|
{
|
|
errMsg = "";
|
|
var errList = new List<string>();
|
|
if (sour == null && sour.Count() == 0)
|
|
{
|
|
errMsg = "資料不可為空";
|
|
return false;
|
|
}
|
|
|
|
if (errList != null && errList.Count() > 0)
|
|
{
|
|
errMsg = String.Join(",", errList);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void SaveExelData(List<ParameterExcelViewModel> sour)
|
|
{
|
|
string err = "";
|
|
var transac = db.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (sour != null && sour.Count() > 0)
|
|
{
|
|
var _category = sour.FirstOrDefault().category;
|
|
var categoryList = db.NonYearlyParameterCategories.Where(x => x.Category == _category).ToList();
|
|
if (categoryList == null || categoryList.Count == 0)
|
|
throw new Exception($@"Category: {sour.FirstOrDefault().category.ToString()}不存在");
|
|
else
|
|
{
|
|
var YearlyParaList = new List<YearlyParameter>();
|
|
var category = categoryList.FirstOrDefault();
|
|
foreach (var it in sour)
|
|
{
|
|
var types = db.NonYearlyParameterTypes.Where(x => x.CategoryID == category.ID && x.DisplayNameTW == it.TWNm).ToList();
|
|
int typeId = 0;
|
|
if (types == null || types.Count() == 0)
|
|
{
|
|
var newIt = new NonYearlyParameterType()
|
|
{
|
|
CategoryID = category.ID,
|
|
DisplayNameTW = it.TWNm,
|
|
DisplayNameCN = it.CNNm,
|
|
DisplayNameEN = it.ENNm
|
|
};
|
|
db.NonYearlyParameterTypes.Add(newIt);
|
|
db.SaveChanges();
|
|
typeId = newIt.ID;
|
|
}
|
|
else
|
|
typeId = types.FirstOrDefault().ID;
|
|
|
|
var years = db.NonYearlyParameters.Where(x => !x.IsHistory && x.TypeID == typeId).ToList();
|
|
if (years == null || years.Count() == 0)
|
|
{
|
|
if (it.Value != 0)
|
|
{
|
|
var newIt = new NonYearlyParameter()
|
|
{
|
|
Value = it.Value,
|
|
CO2Value = it.CO2,
|
|
CH4Value = it.CH4,
|
|
N2OValue = it.N2O,
|
|
HFCsValue = it.HFCs,
|
|
PFCsValue = it.PFCs,
|
|
SF6Value = it.SF6,
|
|
NF3Value = it.NF3,
|
|
CreateTime = DateTime.Now,
|
|
IsHistory = false,
|
|
TypeID = typeId
|
|
};
|
|
db.NonYearlyParameters.Add(newIt);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int yearId = years.FirstOrDefault().ID;
|
|
var paras = db.Parameters.Where(x => x.ID == yearId).ToList();
|
|
if (paras != null && paras.Count() > 0)
|
|
{
|
|
var para = paras.FirstOrDefault();
|
|
para.Value = it.Value;
|
|
para.CO2Value = it.CO2;
|
|
para.CH4Value = it.CH4;
|
|
para.N2OValue = it.N2O;
|
|
para.HFCsValue = it.HFCs;
|
|
para.PFCsValue = it.PFCs;
|
|
para.SF6Value = it.SF6;
|
|
para.NF3Value = it.NF3;
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
transac.Commit();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex = CScommon.Exceptions.inner(ex);
|
|
err = ex.Message;
|
|
transac.Rollback();
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public decimal GetFromExcel(string sour)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(sour))
|
|
return 0;
|
|
if (!Decimal.TryParse(sour, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out decimal Value))
|
|
throw new Exception(sour + " not number");
|
|
return Value;
|
|
}
|
|
|
|
public ActionResult Create(Categories? c)
|
|
{
|
|
if (c == null) return RedirectToAction("index", "home");
|
|
var viewModel = new ParameterCreateViewModel();
|
|
viewModel.Category = c.Value;
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Create(ParameterCreateViewModel viewModel)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
viewModel.ToBeCreated.Parameters.Add(viewModel.FirstVersion);
|
|
if (db.NonYearlyParameterCategories.Where(x => x.Category == viewModel.Category).Count() == 0)
|
|
{
|
|
db.NonYearlyParameterCategories.Add(new NonYearlyParameterCategory(viewModel.Category));
|
|
db.SaveChanges();
|
|
}
|
|
|
|
db.NonYearlyParameterCategories.Single(x => x.Category == viewModel.Category).Types.Add(
|
|
viewModel.ToBeCreated);
|
|
db.SaveChanges();
|
|
return RedirectToAction("Index", new { c = viewModel.Category });
|
|
}
|
|
return View(viewModel);
|
|
}
|
|
|
|
public ActionResult Edit(int? typeId)
|
|
{
|
|
if (typeId == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
var type = db.NonYearlyParameterTypes.Include(x => x.Category).Single(x => x.ID == typeId);
|
|
if (type == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
|
|
var ViewModel = new ParameterEditViewModel();
|
|
ViewModel.Category = type.Category.Category;
|
|
ViewModel.History = type.Parameters.OrderByDescending(x => x.CreateTime).ToList();
|
|
ViewModel.NewVersion = ViewModel.History.First();
|
|
ViewModel.ToBeEdited.DisplayNameTW = type.DisplayNameTW;
|
|
ViewModel.ToBeEdited.DisplayNameCN = type.DisplayNameCN;
|
|
ViewModel.ToBeEdited.DisplayNameEN = type.DisplayNameEN;
|
|
ViewModel.ToBeEdited.activityDataType = type.activityDataType;
|
|
ViewModel.ToBeEdited.emitParaType = type.emitParaType;
|
|
ViewModel.ToBeEdited.paraSource = type.paraSource;
|
|
|
|
return View(ViewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Edit(int? typeId, ParameterEditViewModel viewModel)
|
|
{
|
|
if (ModelState.IsValid && typeId != null)
|
|
{
|
|
var ToBeEdit = db.NonYearlyParameterTypes.Find(typeId.Value);
|
|
|
|
// 所有之前的紀錄皆變為歷史紀錄,並新增一筆最新紀錄
|
|
|
|
var nowVersion = ToBeEdit.Parameters.Where(x => !x.IsHistory).FirstOrDefault();
|
|
if (nowVersion != null)
|
|
{
|
|
var newHistVersion = new NonYearlyParameter();
|
|
newHistVersion.Type = nowVersion.Type;
|
|
newHistVersion.TypeID = nowVersion.TypeID;
|
|
newHistVersion.Value = nowVersion.Value;
|
|
newHistVersion.CO2Value = nowVersion.CO2Value;
|
|
newHistVersion.CH4Value = nowVersion.CH4Value;
|
|
newHistVersion.N2OValue = nowVersion.N2OValue;
|
|
newHistVersion.HFCsValue = nowVersion.HFCsValue;
|
|
newHistVersion.PFCsValue = nowVersion.PFCsValue;
|
|
newHistVersion.SF6Value = nowVersion.SF6Value;
|
|
newHistVersion.NF3Value = nowVersion.NF3Value;
|
|
newHistVersion.Description = nowVersion.Description;
|
|
newHistVersion.CreateTime = nowVersion.CreateTime;
|
|
newHistVersion.IsHistory = true;
|
|
ToBeEdit.Parameters.Add(newHistVersion);
|
|
}
|
|
nowVersion.Value = viewModel.NewVersion.Value;
|
|
nowVersion.CO2Value = viewModel.NewVersion.CO2Value;
|
|
nowVersion.CH4Value = viewModel.NewVersion.CH4Value;
|
|
nowVersion.N2OValue = viewModel.NewVersion.N2OValue;
|
|
nowVersion.HFCsValue = viewModel.NewVersion.HFCsValue;
|
|
nowVersion.PFCsValue = viewModel.NewVersion.PFCsValue;
|
|
nowVersion.SF6Value = viewModel.NewVersion.SF6Value;
|
|
nowVersion.NF3Value = viewModel.NewVersion.NF3Value;
|
|
nowVersion.Description = viewModel.NewVersion.Description;
|
|
nowVersion.CreateTime = DateTime.Now;
|
|
|
|
ToBeEdit.DisplayNameCN = viewModel.ToBeEdited.DisplayNameCN;
|
|
ToBeEdit.DisplayNameTW = viewModel.ToBeEdited.DisplayNameTW;
|
|
ToBeEdit.DisplayNameEN = viewModel.ToBeEdited.DisplayNameEN;
|
|
ToBeEdit.activityDataType = viewModel.ToBeEdited.activityDataType; ;
|
|
ToBeEdit.emitParaType = viewModel.ToBeEdited.emitParaType;
|
|
ToBeEdit.paraSource = viewModel.ToBeEdited.paraSource;
|
|
//ToBeEdit.activityDataType = viewModel.ac
|
|
db.SaveChanges();
|
|
return RedirectToAction("Index", new { c = viewModel.Category });
|
|
}
|
|
|
|
var type = db.NonYearlyParameterTypes.Include(x => x.Category).Single(x => x.ID == typeId);
|
|
viewModel.History = type.Parameters.OrderByDescending(x => x.CreateTime).ToList();
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
public ActionResult Delete(int typeId, Categories category)
|
|
{
|
|
var parameter = db.NonYearlyParameterTypes.Include(x => x.Parameters).Include(x=>x.Category).Where(X => X.ID == typeId).FirstOrDefault();
|
|
if (parameter != null)
|
|
{
|
|
//begin DL-20
|
|
var nonYearlyParameters = db.NonYearlyParameters.Where(x => x.TypeID == typeId).ToList();
|
|
if (nonYearlyParameters.Count > 0)
|
|
{
|
|
foreach (var item in nonYearlyParameters)
|
|
{
|
|
//冷媒
|
|
//var itm = db.LCACommonSurveyForm_Refrigerant.Where(x => x.ParameterID == item.ID || x.ParameterID2 == item.ID).FirstOrDefault();//雪种
|
|
//if (itm != null)
|
|
// return Content("<script>alert('" + Resource.DeleteParameterTypeError + "');history.go(-1);</script>");
|
|
//消防設備 廢棄物
|
|
//var fireEquipment = db.LCACommonSurveyForm_FireEquipment.FirstOrDefault(x => x.ParameterID == item.ID);//消防
|
|
var wasteInfo = db.ProductLCAFabSurveyForm_Wastes.FirstOrDefault(x => x.ParameterID == item.ID);//废水
|
|
if ( wasteInfo != null)
|
|
return Content("<script>alert('" + Resource.DeleteParameterTypeError + "');history.go(-1);</script>");
|
|
//其他關注物質
|
|
//var organizationOtherCompound = db.OrganizationOtherCompound.FirstOrDefault(x => x.ParameterID == item.ID);
|
|
var productOtherCompound = db.ProductLCAFabSurveyForm_OtherCompound.FirstOrDefault(x => x.ParameterID == item.ID);
|
|
if (/*organizationOtherCompound != null ||*/ productOtherCompound != null)
|
|
return Content("<script>alert('" + Resource.DeleteParameterTypeError + "');history.go(-1);</script>");
|
|
//運輸 廢棄物運輸
|
|
var transport = db.ProductLCAFabSurveyForm_Transport.FirstOrDefault(x => x.ParameterID == item.ID);
|
|
var wasteTransport = db.ProductLCAFabSurveyForm_WasteTransport.FirstOrDefault(x => x.ParameterID == item.ID);
|
|
if (transport != null || wasteTransport != null)
|
|
return Content("<script>alert('" + Resource.DeleteParameterTypeError + "');history.go(-1);</script>");
|
|
}
|
|
}
|
|
//end
|
|
db.NonYearlyParameterTypes.Remove(parameter);
|
|
db.SaveChanges();
|
|
}
|
|
|
|
return RedirectToAction("Index", new { c = category });
|
|
}
|
|
|
|
public ActionResult DeletePCategory(string PCategory)
|
|
{
|
|
try
|
|
{
|
|
string errMsg = DelPCategory(PCategory);
|
|
if (!string.IsNullOrWhiteSpace(errMsg))
|
|
TempData["ErrMsg"] = errMsg;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TempData["ErrMsg"] = ex.Message;
|
|
}
|
|
return RedirectToAction("Index", "Parameter", new { c = PCategory });
|
|
}
|
|
|
|
private string DelPCategory(string YPCategory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(YPCategory))
|
|
return "分類不可為空";
|
|
else
|
|
{
|
|
Enum.TryParse(YPCategory, out Categories category);
|
|
var it = db.NonYearlyParameterCategories.FirstOrDefault(x => x.Category == category);
|
|
if (it != null)
|
|
{
|
|
var typeList = db.NonYearlyParameterTypes.Where(x => x.CategoryID == it.ID).ToList();
|
|
List<int> typeIds = typeList.Select(x => x.ID).ToList();
|
|
var yearlyParaList = db.NonYearlyParameters.Where(x => typeIds.Any(t => t == x.TypeID)).ToList();
|
|
List<int> yearlyParaIds = yearlyParaList.Select(x => x.ID).ToList();
|
|
var ParaList = db.Parameters.Where(x => yearlyParaIds.Any(yp => yp == x.ID)).ToList();
|
|
List<int> ParaIds = ParaList.Select(x => x.ID).ToList();
|
|
var Wastes = db.ProductLCAFabSurveyForm_Wastes.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
var Transports = db.ProductLCAFabSurveyForm_Transport.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
var WasteTransports = db.ProductLCAFabSurveyForm_WasteTransport.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
//var Refrigerants = db.LCACommonSurveyForm_Refrigerant.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
var PrdOtherCompounds = db.ProductLCAFabSurveyForm_OtherCompound.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
//var OrgOtherCompounds = db.OrganizationOtherCompound.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
//var FireEquipments = db.LCACommonSurveyForm_FireEquipment.Where(x => yearlyParaIds.Any(y => y == x.ParameterID)).ToList();
|
|
var Refrigerants2 = db.LCACommonSurveyForm_Refrigerant.Where(x => yearlyParaIds.Any(y => y == x.ParameterID2)).ToList();
|
|
var Water = db.ProductLCAFabSurveyForm_WaterUsage.Where(x=>yearlyParaIds.Any(y=>y==x.ParameterID)).ToList();
|
|
if (Wastes != null && Wastes.Count() > 0)
|
|
db.ProductLCAFabSurveyForm_Wastes.RemoveRange(Wastes);
|
|
if (Transports != null && Transports.Count() > 0)
|
|
db.ProductLCAFabSurveyForm_Transport.RemoveRange(Transports);
|
|
if (WasteTransports != null && WasteTransports.Count() > 0)
|
|
db.ProductLCAFabSurveyForm_WasteTransport.RemoveRange(WasteTransports);
|
|
//if (Refrigerants != null && Refrigerants.Count() > 0)
|
|
// db.LCACommonSurveyForm_Refrigerant.RemoveRange(Refrigerants);
|
|
if (PrdOtherCompounds != null && PrdOtherCompounds.Count() > 0)
|
|
db.ProductLCAFabSurveyForm_OtherCompound.RemoveRange(PrdOtherCompounds);
|
|
//if (OrgOtherCompounds != null && OrgOtherCompounds.Count() > 0)
|
|
// db.OrganizationOtherCompound.RemoveRange(OrgOtherCompounds);
|
|
//if (FireEquipments != null && FireEquipments.Count() > 0)
|
|
// db.LCACommonSurveyForm_FireEquipment.RemoveRange(FireEquipments);
|
|
if (Refrigerants2 != null && Refrigerants2.Count() > 0)
|
|
db.LCACommonSurveyForm_Refrigerant.RemoveRange(Refrigerants2);
|
|
if (Water!=null&& Water.Count() > 0)
|
|
db.ProductLCAFabSurveyForm_WaterUsage.RemoveRange(Water);
|
|
if (yearlyParaList != null && yearlyParaList.Count() > 0)
|
|
db.NonYearlyParameters.RemoveRange(yearlyParaList);
|
|
if (ParaList != null && ParaList.Count() > 0)
|
|
db.Parameters.RemoveRange(ParaList);
|
|
if (typeList != null && typeList.Count() > 0)
|
|
db.NonYearlyParameterTypes.RemoveRange(typeList);
|
|
db.SaveChanges();
|
|
}
|
|
else
|
|
return "分類不存在";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
db.Dispose();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|