625 lines
30 KiB
C#
625 lines
30 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using Weee.Models.Paramemter;
|
|
using Weee.Models;
|
|
using Weee.Areas.Admin.ViewModels;
|
|
using Weee.DAL;
|
|
using System.Threading;
|
|
using System.IO;
|
|
using ClosedXML.Excel;
|
|
using System.Net.Mime;
|
|
using Resources.Helper;
|
|
using Qcarbon.Database.Shared;
|
|
using Resources;
|
|
using Weee.ViewModels;
|
|
using CScommon;
|
|
using System.Globalization;
|
|
|
|
namespace Weee.Areas.Admin.Controllers
|
|
{
|
|
public class YearlyParameterController : AdminControllerBase
|
|
{
|
|
protected yearlyParameterCategoryService _yearlyParameterCategoryService = null;
|
|
protected yearlyParameterTypeService _yearlyParameterTypeService = null;
|
|
protected yearlyParameterAreaService _yearlyParameterAreaService = null;
|
|
protected yearlyParameterService _yearlyParameterService = null;
|
|
|
|
public YearlyParameterController(WeeeDataContext db) : base(db)
|
|
{
|
|
_yearlyParameterCategoryService = new yearlyParameterCategoryService(db);
|
|
_yearlyParameterTypeService = new yearlyParameterTypeService(db);
|
|
_yearlyParameterAreaService = new yearlyParameterAreaService(db);
|
|
_yearlyParameterService = new yearlyParameterService(db);
|
|
}
|
|
|
|
public ActionResult Index(Categories? c) // 這神了非c不可不能改名字
|
|
{
|
|
if (c == null)
|
|
return RedirectToAction("index", "home");
|
|
var ViewModel = loadViewModel(c);
|
|
ViewModel.category = c;
|
|
|
|
if (TempData["ErrMsg"] != null && !string.IsNullOrWhiteSpace(TempData["ErrMsg"].ToString()))
|
|
ViewModel.errMsg = TempData["ErrMsg"].ToString();
|
|
|
|
return View(ViewModel);
|
|
}
|
|
private YearlyParameterIndexViewModel loadViewModel(Categories? c)
|
|
{
|
|
var ViewModel = new YearlyParameterIndexViewModel()
|
|
{
|
|
Parameters = db.YearlyParameters
|
|
.Include(y => y.Area)
|
|
.Include(y => y.Type)
|
|
.Where(z => z.Area.Category.Category == c && !z.IsHistory)
|
|
.OrderBy(z=>z.TypeID)
|
|
.ThenBy(z=>z.AreaID)
|
|
.ThenBy(z=>z.Year)
|
|
.ToList(),
|
|
Categories = db.YearlyParameterCategories
|
|
.Include(x => x.Areas)
|
|
.Include(x => x.Types)
|
|
.Where(x => x.Category == c).ToList()
|
|
};
|
|
ViewModel.Years = ViewModel.Parameters.OrderBy(x=>x.Year).Select(x => x.Year).Distinct().ToList();
|
|
//db.YearlyParameters.OrderBy(x => x.Year).Select(x => x.Year).Distinct().ToList();
|
|
ViewModel.category = c;
|
|
return ViewModel;
|
|
}
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Index(YearlyParameterIndexViewModel oriViewModel)
|
|
{
|
|
YearlyParameterIndexViewModel viewModel=null;
|
|
switch (oriViewModel.cmd)
|
|
{
|
|
case "upload":
|
|
viewModel = loadViewModel(oriViewModel.category);
|
|
viewModel.uploadExcelFile = oriViewModel.uploadExcelFile;
|
|
viewModel.errMsg = dealWithExcelUpload(viewModel);
|
|
if (!string.IsNullOrWhiteSpace(viewModel.errMsg))
|
|
TempData["ErrMsg"] = viewModel.errMsg;
|
|
return RedirectToAction("Index", "YearlyParameter", new { c = oriViewModel.category });
|
|
break;
|
|
case "download":
|
|
viewModel = loadViewModel(oriViewModel.category);
|
|
XLWorkbook excelWorkbook = new XLWorkbook(); // 寫資料進excelWorkbook
|
|
string sheetName = viewModel.Categories.First().DisplayName;
|
|
var worksheet = excelWorkbook.Worksheets.Add("Sheet1");
|
|
int rowTh;
|
|
HashSet<YearlyParameterType> types = (HashSet<YearlyParameterType>)viewModel.Categories[0].Types;
|
|
HashSet<YearlyParameterArea> areas =(HashSet<YearlyParameterArea>) viewModel.Categories[0].Areas;
|
|
worksheet.Cell($"A1").Value = "類別";
|
|
worksheet.Cell($"B1").Value = "區域";
|
|
worksheet.Cell($"C1").Value = "年份";
|
|
worksheet.Cell($"D1").Value = "值";
|
|
worksheet.Cell($"E1").Value = "CO2係數";
|
|
worksheet.Cell($"F1").Value = "CH4係數";
|
|
worksheet.Cell($"G1").Value = "N2O係數";
|
|
worksheet.Cell($"H1").Value = "HFCs係數";
|
|
worksheet.Cell($"I1").Value = "PFCs係數";
|
|
worksheet.Cell($"J1").Value = "SF6係數";
|
|
worksheet.Cell($"K1").Value = "NF3係數";
|
|
|
|
List<int> years = viewModel.Years;
|
|
//string cell;
|
|
rowTh = 2;
|
|
foreach (YearlyParameterType typeRec in types)
|
|
{
|
|
foreach (YearlyParameterArea areaRec in areas)
|
|
{
|
|
for (int i = 0; i < years.Count; i++)
|
|
{
|
|
worksheet.Cell($"A{rowTh}").Value = $"{typeRec.DisplayNameTW}({typeRec.ID})";
|
|
worksheet.Cell($"B{rowTh}").Value = $"{areaRec.DisplayNameTW}({areaRec.ID})";
|
|
worksheet.Cell($"C{rowTh}").Value = years[i];
|
|
foreach (var para in viewModel.Parameters)
|
|
{
|
|
if (para.Year == years[i] && para.Type.ID == typeRec.ID && para.Area.ID == areaRec.ID)
|
|
{
|
|
worksheet.Cell($"D{rowTh}").Value = para.Value;
|
|
worksheet.Cell($"E{rowTh}").Value = para.CO2Value;
|
|
worksheet.Cell($"F{rowTh}").Value = para.CH4Value;
|
|
worksheet.Cell($"G{rowTh}").Value = para.N2OValue;
|
|
worksheet.Cell($"H{rowTh}").Value = para.HFCsValue;
|
|
worksheet.Cell($"I{rowTh}").Value = para.PFCsValue;
|
|
worksheet.Cell($"J{rowTh}").Value = para.SF6Value;
|
|
worksheet.Cell($"K{rowTh}").Value = para.NF3Value;
|
|
}
|
|
}
|
|
rowTh++;
|
|
}
|
|
}
|
|
}
|
|
string myName = Server.UrlEncode($"年參數維護_{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);
|
|
}
|
|
return View(viewModel);
|
|
}
|
|
|
|
private string dealWithExcelUpload(YearlyParameterIndexViewModel 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<YearlyParameterExcelViewModel> GetExcelData(HttpPostedFileBase uploadExcelFile)
|
|
{
|
|
var re = new List<YearlyParameterExcelViewModel>();
|
|
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 YearlyParameterExcelViewModel();
|
|
//類別
|
|
string cellA = workSheet.Cell($"A{ith}").Value.ToString().Trim();
|
|
exlIt.Type = cellA;
|
|
//區域
|
|
string cellB = workSheet.Cell($"B{ith}").Value.ToString().Trim();
|
|
exlIt.Area = cellB;
|
|
// 年份
|
|
string cellC = workSheet.Cell($"C{ith}").Value.ToString().Trim();
|
|
int.TryParse(cellC, out int YearValue);
|
|
exlIt.Year = YearValue;
|
|
// 值
|
|
string cellD = workSheet.Cell($"D{ith}").Value.ToString().Trim();
|
|
exlIt.Value = GetFromExcel(cellD);
|
|
// CO2係數
|
|
string cellE = workSheet.Cell($"E{ith}").Value.ToString().Trim();
|
|
exlIt.CO2 = GetFromExcel(cellE);
|
|
// CH4係數
|
|
string cellF = workSheet.Cell($"F{ith}").Value.ToString().Trim();
|
|
exlIt.CH4 = GetFromExcel(cellF);
|
|
// N2O係數
|
|
string cellG = workSheet.Cell($"G{ith}").Value.ToString().Trim();
|
|
exlIt.N2O = GetFromExcel(cellG);
|
|
// HFCs係數
|
|
string cellH = workSheet.Cell($"H{ith}").Value.ToString().Trim();
|
|
exlIt.HFCs = GetFromExcel(cellH);
|
|
// PFCs係數
|
|
string cellI = workSheet.Cell($"I{ith}").Value.ToString().Trim();
|
|
exlIt.PFCs = GetFromExcel(cellI);
|
|
// SF6係數
|
|
string cellJ = workSheet.Cell($"J{ith}").Value.ToString().Trim();
|
|
exlIt.SF6 = GetFromExcel(cellJ);
|
|
// NF3係數
|
|
string cellK = workSheet.Cell($"K{ith}").Value.ToString().Trim();
|
|
exlIt.NF3 = GetFromExcel(cellK);
|
|
re.Add(exlIt);
|
|
}
|
|
}
|
|
}
|
|
return re;
|
|
}
|
|
|
|
private bool CkExcelData(List<YearlyParameterExcelViewModel> sour, out string errMsg)
|
|
{
|
|
errMsg = "";
|
|
var errList = new List<string>();
|
|
if (sour == null && sour.Count() == 0)
|
|
{
|
|
errMsg = "資料不可為空";
|
|
return false;
|
|
}
|
|
foreach (var it in sour)
|
|
{
|
|
string errType = excelCellIdDispname(it.Type, out int? typeId, out string typeDispname);
|
|
if (string.IsNullOrWhiteSpace(errType))
|
|
{
|
|
it.Type = typeDispname;
|
|
it.TypeOldId = typeId;
|
|
}
|
|
else
|
|
{
|
|
string colErr = $@"類別(Type): {it.Type}錯誤, 錯誤訊息:{errType}";
|
|
errList.Add(colErr);
|
|
}
|
|
|
|
string errArea = excelCellIdDispname(it.Area, out int? areaId, out string areaDispname);
|
|
if (string.IsNullOrWhiteSpace(errArea))
|
|
{
|
|
it.Area = areaDispname;
|
|
it.AreaOldId = areaId;
|
|
}
|
|
else
|
|
{
|
|
string colErr = $@"區域(Area): {it.Area}錯誤, 錯誤訊息:{errArea}";
|
|
errList.Add(colErr);
|
|
}
|
|
}
|
|
if (errList != null && errList.Count() > 0)
|
|
{
|
|
errMsg = String.Join(",", errList);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void SaveExelData(List<YearlyParameterExcelViewModel> sour)
|
|
{
|
|
string err = "";
|
|
var transac = db.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (sour != null && sour.Count() > 0)
|
|
{
|
|
var _category = sour.FirstOrDefault().category;
|
|
var categoryList = db.YearlyParameterCategories.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.YearlyParameterTypes.Where(x => x.CategoryID == category.ID && x.DisplayNameTW == it.Type).ToList();
|
|
int typeId = 0;
|
|
if (types == null || types.Count() == 0)
|
|
{
|
|
var newIt = new YearlyParameterType()
|
|
{
|
|
CategoryID = category.ID,
|
|
DisplayNameTW = it.Type,
|
|
DisplayNameCN = it.Type,
|
|
DisplayNameEN = it.Type
|
|
};
|
|
db.YearlyParameterTypes.Add(newIt);
|
|
db.SaveChanges();
|
|
typeId = newIt.ID;
|
|
}
|
|
else
|
|
typeId = types.FirstOrDefault().ID;
|
|
var areas = db.YearlyParameterAreas.Where(x => x.CategoryID == category.ID && x.DisplayNameTW == it.Area).ToList();
|
|
int areaId = 0;
|
|
if (areas == null || areas.Count() == 0)
|
|
{
|
|
var newIt = new YearlyParameterArea()
|
|
{
|
|
CategoryID = category.ID,
|
|
DisplayNameTW = it.Area,
|
|
DisplayNameCN = it.Area,
|
|
DisplayNameEN = it.Area
|
|
};
|
|
db.YearlyParameterAreas.Add(newIt);
|
|
db.SaveChanges();
|
|
areaId = newIt.ID;
|
|
}
|
|
else
|
|
areaId = areas.FirstOrDefault().ID;
|
|
var years = db.YearlyParameters.Where(x => !x.IsHistory && x.Year == it.Year && x.AreaID == areaId && x.TypeID == typeId).ToList();
|
|
if (years == null || years.Count() == 0)
|
|
{
|
|
if(it.Value != 0)
|
|
{
|
|
var newIt = new YearlyParameter()
|
|
{
|
|
Value = it.Value,
|
|
CO2Value = it.CO2,
|
|
CH4Value = it.CH4,
|
|
N2OValue = it.N2O,
|
|
HFCsValue = it.HFCs,
|
|
PFCsValue = it.PFCs,
|
|
SF6Value = it.SF6,
|
|
NF3Value = it.NF3,
|
|
Year = it.Year,
|
|
CreateTime = DateTime.Now,
|
|
IsHistory = false,
|
|
AreaID = areaId,
|
|
TypeID = typeId
|
|
};
|
|
db.YearlyParameters.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 Details(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
var yearlyParameter = db.YearlyParameters.Find(id);
|
|
if (yearlyParameter == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
return View(yearlyParameter);
|
|
}
|
|
|
|
public ActionResult Create(int? areaId, int? typeId, int? year)
|
|
{
|
|
if (areaId != null && typeId != null && year != null)
|
|
{
|
|
var ViewModel = new YearlyParameterCreateViewModel()
|
|
{
|
|
ToBeCreated = new YearlyParameter()
|
|
{
|
|
Year = year.Value
|
|
},
|
|
History = db.YearlyParameters
|
|
.Where(x => x.AreaID == areaId.Value && x.TypeID == typeId.Value
|
|
&& x.Year == year.Value)
|
|
.OrderByDescending(x => x.CreateTime)
|
|
.ToList(),
|
|
AreaDisplayName = db.YearlyParameterAreas.Find(areaId.Value).DisplayName,
|
|
TypeDisplayName = db.YearlyParameterTypes.Find(typeId.Value).DisplayName,
|
|
categories = db.YearlyParameterAreas.Find(areaId.Value).Category.Category
|
|
};
|
|
|
|
if (ViewModel.History.FirstOrDefault() != null)
|
|
{
|
|
ViewModel.ToBeCreated.Value = ViewModel.History.First().Value;
|
|
ViewModel.ToBeCreated.CO2Value = ViewModel.History.First().CO2Value;
|
|
ViewModel.ToBeCreated.CH4Value = ViewModel.History.First().CH4Value;
|
|
ViewModel.ToBeCreated.N2OValue = ViewModel.History.First().N2OValue;
|
|
ViewModel.ToBeCreated.HFCsValue = ViewModel.History.First().HFCsValue;
|
|
ViewModel.ToBeCreated.PFCsValue = ViewModel.History.First().PFCsValue;
|
|
ViewModel.ToBeCreated.SF6Value = ViewModel.History.First().SF6Value;
|
|
ViewModel.ToBeCreated.NF3Value = ViewModel.History.First().NF3Value;
|
|
ViewModel.ToBeCreated.Description = ViewModel.History.First().Description;
|
|
}
|
|
|
|
return View(ViewModel);
|
|
}
|
|
return RedirectToAction("index","home");
|
|
}
|
|
|
|
// POST: /admin/YearlyParamter/Create
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Create(int? areaid, int? typeid, YearlyParameterCreateViewModel ViewModel)
|
|
{
|
|
ViewModel.History = new List<YearlyParameter>();
|
|
if (areaid != null && typeid != null)
|
|
{
|
|
ViewModel.History = db.YearlyParameters
|
|
.Where(x => x.AreaID == areaid.Value && x.TypeID == typeid.Value && x.Year == ViewModel.ToBeCreated.Year)
|
|
.OrderByDescending(x => x.CreateTime)
|
|
.ToList();
|
|
if (ModelState.IsValid)
|
|
{
|
|
var latest = ViewModel.History.Where(x=>!x.IsHistory).FirstOrDefault();
|
|
ViewModel.ToBeCreated.AreaID = areaid.Value;
|
|
ViewModel.ToBeCreated.TypeID = typeid.Value;
|
|
ViewModel.History.ToList().ForEach(x => x.IsHistory = true);
|
|
// 新增一筆歷史紀錄儲存當下這筆參數的data
|
|
if (latest != null)
|
|
{
|
|
var newHistory = new YearlyParameter();
|
|
newHistory.Value = latest.Value;
|
|
newHistory.CO2Value = latest.CO2Value;
|
|
newHistory.CH4Value = latest.CH4Value;
|
|
newHistory.N2OValue = latest.N2OValue;
|
|
newHistory.HFCsValue = latest.HFCsValue;
|
|
newHistory.PFCsValue = latest.PFCsValue;
|
|
newHistory.SF6Value = latest.SF6Value;
|
|
newHistory.NF3Value = latest.NF3Value;
|
|
newHistory.Area = latest.Area;
|
|
newHistory.AreaID = latest.AreaID;
|
|
newHistory.Type = latest.Type;
|
|
newHistory.TypeID = latest.TypeID;
|
|
newHistory.Year = latest.Year;
|
|
newHistory.Description = latest.Description;
|
|
newHistory.CreateTime = latest.CreateTime;
|
|
newHistory.IsHistory = true;
|
|
db.YearlyParameters.Add(newHistory);
|
|
// 將編輯過後的值押到原先的這個資料上
|
|
latest.Value = ViewModel.ToBeCreated.Value;
|
|
latest.CO2Value = ViewModel.ToBeCreated.CO2Value;
|
|
latest.CH4Value = ViewModel.ToBeCreated.CH4Value;
|
|
latest.N2OValue = ViewModel.ToBeCreated.N2OValue;
|
|
latest.HFCsValue = ViewModel.ToBeCreated.HFCsValue;
|
|
latest.PFCsValue = ViewModel.ToBeCreated.PFCsValue;
|
|
latest.SF6Value = ViewModel.ToBeCreated.SF6Value;
|
|
latest.NF3Value = ViewModel.ToBeCreated.NF3Value;
|
|
latest.Description = ViewModel.ToBeCreated.Description;
|
|
latest.IsHistory = false;
|
|
latest.CreateTime = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
db.YearlyParameters.Add(ViewModel.ToBeCreated);
|
|
}
|
|
|
|
db.SaveChanges();
|
|
return RedirectToAction("Index", new { c = ViewModel.categories });
|
|
}
|
|
}
|
|
return View(ViewModel);
|
|
}
|
|
|
|
// GET: /admin/YearlyParamter/Delete/5
|
|
public ActionResult Delete(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
YearlyParameter yearlyparameter = db.YearlyParameters.Find(id);
|
|
if (yearlyparameter == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
return View(yearlyparameter);
|
|
}
|
|
|
|
// POST: /admin/YearlyParamter/Delete/5
|
|
[HttpPost, ActionName("Delete")]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult DeleteConfirmed(int id)
|
|
{
|
|
YearlyParameter yearlyparameter = db.YearlyParameters.Find(id);
|
|
db.YearlyParameters.Remove(yearlyparameter);
|
|
db.SaveChanges();
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
public ActionResult DeleteYPCategory(string YPCategory)
|
|
{
|
|
try
|
|
{
|
|
string errMsg = DelYPCategory(YPCategory);
|
|
if (!string.IsNullOrWhiteSpace(errMsg))
|
|
TempData["ErrMsg"] = errMsg;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
TempData["ErrMsg"] = ex.Message;
|
|
}
|
|
return RedirectToAction("Index", "YearlyParameter", new { c = YPCategory });
|
|
}
|
|
|
|
private string DelYPCategory(string YPCategory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(YPCategory))
|
|
return "分類不可為空";
|
|
else
|
|
{
|
|
Enum.TryParse(YPCategory, out Categories category);
|
|
var it = db.YearlyParameterCategories.FirstOrDefault(x => x.Category == category);
|
|
if (it != null)
|
|
{
|
|
var areaList = db.YearlyParameterAreas.Where(x=>x.CategoryID == it.ID).ToList();
|
|
var typeList = db.YearlyParameterTypes.Where(x => x.CategoryID == it.ID).ToList();
|
|
List<int> areaIds = areaList.Select(x => x.ID).ToList();
|
|
List<int> typeIds = typeList.Select(x => x.ID).ToList();
|
|
var yearlyParaList = db.YearlyParameters.Where(x => areaIds.Any(a => a == x.AreaID) || 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 SteamUsageList = db.LCACommonSurveyForm_SteamUsage.Where(x => ParaIds.Any(p => p == x.ParameterID)).ToList();
|
|
var PowerUsageList = db.LCACommonSurveyForm_PowerUsage.Where(x => ParaIds.Any(p => p == x.ParameterID)).ToList();
|
|
var VehicleList = db.LCACommonSurveyForm_Vehicle.Where(x => ParaIds.Any(p => p == x.ParameterID)).ToList();
|
|
var GasolineList = db.LCACommonSurveyForm_GasolineEquipment.Where(x => ParaIds.Any(p => p == x.ParameterID)).ToList();
|
|
var KitchenList = db.LCACommonSurveyForm_Kitchen.Where(x => ParaIds.Any(p => p == x.ParameterID)).ToList();
|
|
if (SteamUsageList != null && SteamUsageList.Count() > 0)
|
|
db.LCACommonSurveyForm_SteamUsage.RemoveRange(SteamUsageList);
|
|
if (PowerUsageList != null && PowerUsageList.Count() > 0)
|
|
db.LCACommonSurveyForm_PowerUsage.RemoveRange(PowerUsageList);
|
|
if (VehicleList != null && VehicleList.Count() > 0)
|
|
db.LCACommonSurveyForm_Vehicle.RemoveRange(VehicleList);
|
|
if (GasolineList != null && GasolineList.Count() > 0)
|
|
db.LCACommonSurveyForm_GasolineEquipment.RemoveRange(GasolineList);
|
|
if (KitchenList != null && KitchenList.Count() > 0)
|
|
db.LCACommonSurveyForm_Kitchen.RemoveRange(KitchenList);
|
|
if (yearlyParaList != null && yearlyParaList.Count() > 0)
|
|
db.YearlyParameters.RemoveRange(yearlyParaList);
|
|
if (ParaList != null && ParaList.Count() > 0)
|
|
db.Parameters.RemoveRange(ParaList);
|
|
if (typeList != null && typeList.Count() > 0)
|
|
db.YearlyParameterTypes.RemoveRange(typeList);
|
|
if (areaList != null && areaList.Count() > 0)
|
|
db.YearlyParameterAreas.RemoveRange(areaList);
|
|
db.SaveChanges();
|
|
}
|
|
else
|
|
return "分類不存在";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
db.Dispose();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|