demo20230512/Areas/Lca/Controllers/RiskMaterialTransportController.cs

194 lines
8.0 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using CScommon;
using Microsoft.AspNet.Identity;
using Qcarbon.ViewModels.DTO;
using Resources;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Weee.DAL;
using Weee.Models;
using Weee.Models.ExtensionMethods;
using Weee.Service;
namespace Weee.Areas.Lca.Controllers
{
//[Authorize(Roles = ProgramConstants.normalcompany)]
public class RiskMaterialTransportController : LCAcontrollerBase
{
protected MaterialC3EmissionDataService service;
public RiskMaterialTransportController(WeeeDataContext db)
: base(db)
{
service = new MaterialC3EmissionDataService(db);
}
// GET: Lca/RiskMaterialTransport
//[Route("LCA/RiskMaterialTransport/Index/{LCAID?}")]
//[Route("RiskMaterialTransport/Index/{LCAID?}")]
//[Route("LCA/RiskMaterialTransport/{LCAID}")]
public ActionResult Index(int? LCAID)
{
ViewBag.activeRisk = "materialTransport";
//var v = User.Identity.GetUserAccountType(db);
RiskMaterialListViewModel viewModel = new RiskMaterialListViewModel();
if (LCAID == null)
viewModel.errMsg = "LCAID null!";
if (viewModel.errMsg=="")
{
viewModel.LCAID =(int) LCAID;
ViewBag.LCAID = viewModel.LCAID;
loadMaterialList(viewModel);
}
return View(viewModel);
}
private string loadMaterialList(RiskMaterialListViewModel viewModel)
{
string ret = "";
// get view model from LCAID
var qry = (from a in uow.LCARiskAssmtSurveyFormMaterialC3EmissionRepository.Get()
where a.LCAID ==viewModel.LCAID
select a).AsQueryable();
if (qry == null)
viewModel.errMsg = "No record(s)!";
else
{
if (viewModel.modelList == null)
viewModel.modelList = new List<LCARiskAssmtSurveyForm_MaterialC3Emission>();
foreach (var rec in qry.ToList())
{
//string json = CScommon.JsonUtl.fromT(rec);
//LCARiskAssmtSurveyForm_MaterialC3Emission toAdd =
// CScommon.JsonUtl.toT<LCARiskAssmtSurveyForm_MaterialC3Emission>(json);
viewModel.modelList.Add(rec);// toAdd);
}
}
return ret;
}
private string updateSelectedModel(RiskMaterialListViewModel viewModel)
{
string ret = "";
var qry = (from a in uow.LCARiskAssmtSurveyFormMaterialC3EmissionRepository.Get()
where a.LCAID==viewModel.LCAID && a.MaterialNo==viewModel.selectedID
select a).FirstOrDefault();
viewModel.selectedModel = qry;
return ret;
}
[HttpPost]
public ActionResult Index(RiskMaterialListViewModel viewModel)
{
string err = "";
switch(viewModel.cmd)
{
case "選取":
err = updateSelectedModel(viewModel);
TempData["selectedModel"] = viewModel.selectedModel;
break;
case "btnAdd":
return RedirectToAction("AddUpdate", new { LCAID = viewModel.LCAID });
break;
case "btnEdit":
RiskMaterialViewModel editModel = new RiskMaterialViewModel();
editModel.editing = true;
editModel.LCAID = viewModel.LCAID;
editModel.singleRec =(LCARiskAssmtSurveyForm_MaterialC3Emission) TempData["selectedModel"];// viewModel.selectedModel;
TempData["viewModel"] = editModel;
return RedirectToAction("AddUpdate", new { LCAID = viewModel.LCAID });
break;
case "btnDelete":
break;
default:
break;
}
err = loadMaterialList(viewModel);
return View(viewModel);
}
public ActionResult AddUpdate(int? LCAID)
{
RiskMaterialViewModel viewModel;
if (LCAID == null)
{
viewModel = new RiskMaterialViewModel();
viewModel.errMsg = "LCAID parameter missing!";
return View(viewModel);
}
ViewBag.LCAID = LCAID;
var qry = TempData["viewModel"] as RiskMaterialViewModel;
if (qry != null)//editing
viewModel = qry;
else//adding
{
viewModel = new RiskMaterialViewModel();
viewModel.LCAID = (int)LCAID;
}
return View(viewModel);
}
[HttpPost]
//public ActionResult Add(RiskMaterialViewModel viewModel)
public ActionResult AddUpdate(RiskMaterialViewModel viewModel)
{
verifyAdd(viewModel);
if (!ModelState.IsValid)
return View(viewModel);
viewModel.singleRec.ActivityIntensity = viewModel.singleRec.MaterialSpec
* viewModel.singleRec.AnnualPurchaseAmount;
viewModel.singleRec.Emission = viewModel.singleRec.ActivityIntensity
* viewModel.singleRec.KgCO2e;
switch (viewModel.cmd)
{
case "更新":
break;
case "儲存":
//service.Save(viewModel.singleRec);
LCARiskAssmtSurveyForm_MaterialC3Emission toBeSave = viewModel.singleRec;
toBeSave.LCAID = viewModel.LCAID;
var entry = db.Entry(toBeSave);
if (toBeSave.ID == 0)
{
var qry = (from a in db.LCARiskAssmtSurveyForm_MaterialC3Emissions
where a.LCAID == toBeSave.LCAID && a.ID > 0
&& a.MaterialNo == toBeSave.MaterialNo
select a);
if (qry.Any())
{
ModelState.AddModelError("", $"料號 {toBeSave.MaterialNo} 已存在!");
return View(viewModel);
}
//toBeSave.ID = Guid.NewGuid();
toBeSave.CreatedBy =User.Identity.GetUserId();
toBeSave.CreatedDate = DateTime.Now;
entry.State = EntityState.Added;
}
else
{
toBeSave.ModifiedBy = User.Identity.GetUserId();
toBeSave.ModifiedDate = DateTime.Now;
entry.State = EntityState.Modified;
entry.Property(x => x.LCAID).IsModified = false;
}
db.SaveChanges();
viewModel.successMsg = "新增成功!";
break;
default:
break;
}
return View(viewModel);
}
protected string verifyAdd(RiskMaterialViewModel viewModel)
{
string ret = "";
if (string.IsNullOrWhiteSpace(viewModel.singleRec.MaterialNo))
ModelState.AddModelError("",$"{Resource.MaterialNo} {Resource.HeaderMessageRequire}" );
if (string.IsNullOrWhiteSpace(viewModel.singleRec.MaterialName))
ModelState.AddModelError("", $"{Resource.RAMC3EMaterialName} {Resource.HeaderMessageRequire}");
if (string.IsNullOrWhiteSpace(viewModel.singleRec.Unit))
ModelState.AddModelError("", $"{Resource.RAMC3EMaterialActivityUnit} {Resource.HeaderMessageRequire}");
//!!...檢查料號是否重複
return ret;
}
}
}