demo20230512/Areas/Certification/Controllers/LCAController.cs

341 lines
17 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weee.Areas.Certification.ViewModels;
using Weee.DAL;
using Weee.Models;
using Weee.Service;
using Weee.Areas.Certification.Models;
using Weee.Models.ExtensionMethods;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Weee.Models.Paramemter;
using Weee.DataTransferObject;
using System.IO;
using EmailService.others;
using System.Net;
using System.Web.Configuration;
using System.Web.Http;
using Weee.Controllers;
namespace Weee.Areas.Certification.Controllers
{
// to do: Add parameter id in workhour, power usage, water usage
public class LCAController : CertificationControllerBase
{
protected WeeeDataContext _db;
protected WeeeLCADataService lcaDataService;
protected WeeeSheetDataService sheetDataService;
private readonly WeeeParameterDataService parameterservice;
private readonly SignificanceAssmtDataService significanceAssmtservice;
private readonly LifecycleAssmtDataService lifecycleservice;
private readonly EmailService.Service service = DependencyResolver.Current.GetService<EmailService.Service>();//CFT-89
private string fileSavedPath = WebConfigurationManager.AppSettings["UploadPath"];//CFT-89
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
this.lcaDataService.Initialize(User.Identity.GetUserId());
this.sheetDataService.Initialize(User.Identity.GetUserId());
this.significanceAssmtservice.Initialize(User.Identity.GetUserId());
this.lifecycleservice.Initialize(User.Identity.GetUserId());
}
public LCAController(WeeeDataContext db, WeeeLCADataService lcaDataService
, WeeeSheetDataService sheetDataService
, WeeeCompanyDataService companyDataService
, WeeeParameterDataService parameterservice
, SignificanceAssmtDataService significanceAssmtservice
, LifecycleAssmtDataService lifecycleservice)
: base(db)
{
this.lcaDataService = lcaDataService;
this.sheetDataService = sheetDataService;
this.parameterservice = parameterservice;
this.significanceAssmtservice = significanceAssmtservice;
this.lifecycleservice = lifecycleservice;
this._db = new WeeeDataContext();
}
public ActionResult ProductLcaDetail(int id)
{
//var qry = (from a in uow.productRepository.Get()
// select a).AsQueryable();
var viewModel = new ProductLcaViewModel();
var productLca = lcaDataService.GetProductLCA(id);
viewModel.ProductLca = productLca;
var inventoryStage = productLca.TargetInventoryStageData;
var usageStage = new ProductUsageStageDTO();
if (inventoryStage != null)
{
usageStage.WattInOperation = inventoryStage.WattInOperation;
usageStage.WattInLowOperation = inventoryStage.WattInLowOperation;
usageStage.TurnOnHourPerDayInWorkDay = inventoryStage.TurnOnHourPerDayInWorkDay;
usageStage.TurnOnHourPerDayInOffDay = inventoryStage.TurnOnHourPerDayInOffDay;
usageStage.WorkDayPerYear = inventoryStage.WorkDayPerYear;
usageStage.PowerFactor = inventoryStage.ProductUsageStagePowerFactor;
usageStage.Description = inventoryStage.ProductUsageStageDescription;
usageStage.CalculateResult = inventoryStage.GetUsageStageCarbonFootprint();
}
viewModel.UsageStage = usageStage;
var abandonedStage = _db.ProductLCAAbandonedStages.Where(x => x.OwnerLCA.ID == id).Count() > 0 ? _db.ProductLCAAbandonedStages.Where(x => x.OwnerLCA.ID == id).First() : null;
var abandonedStageDTO = new AbandonedStageDTO();
if (inventoryStage != null && abandonedStage != null)
{
//abandonedStageDTO.WasteWeight = abandonedStage.WasteWeight;
//abandonedStageDTO.RecyclePercentage = abandonedStage.RecyclePercentage;
//abandonedStageDTO.ReusePercentage = abandonedStage.ReusePercentage;
//abandonedStageDTO.RecoverPercentage = abandonedStage.RecoverPercentage;
//abandonedStageDTO.IncinerationParameter = abandonedStage.IncinerationParameter;
//abandonedStageDTO.AbandonedParameter = abandonedStage.AbandonedParameter;
//abandonedStageDTO.Description = abandonedStage.Description;
//abandonedStageDTO.CalculateResult = inventoryStage.ProductAbandonedStageCarbonFootprint;
}
viewModel.AbandonedStage = abandonedStageDTO;
/*
if (productLca.PCRID == null)
{
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(id, Categories.BOM);
}
else
{
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(id, Categories.DirectMaterial);
}
*/
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(id, Categories.DirectMaterial);
viewModel.IndirectMaterial = this.sheetDataService.GetSheetHeader(id, Categories.IndirectMaterial);
viewModel.WrapMaterial = this.sheetDataService.GetSheetHeader(id, Categories.WrapMaterial);
viewModel.WorkHourHeader = this.sheetDataService.GetSheetHeader(id, Categories.Septic);
viewModel.PowerHeader = this.sheetDataService.GetSheetHeader(id, Categories.Electric);
viewModel.WaterHeader = this.sheetDataService.GetSheetHeader(id, Categories.Water);
viewModel.WasteWaterHeader = this.sheetDataService.GetSheetHeader(id, Categories.WasteWater);
viewModel.TransportHeader = this.sheetDataService.GetSheetHeader(id, Categories.Transport);
viewModel.WasteTransportHeader = this.sheetDataService.GetSheetHeader(id, Categories.SheetHeaderWasteTransport);
viewModel.VehicleHeader = this.sheetDataService.GetSheetHeader(id, Categories.Vehicle);
viewModel.GasolineHeader = this.sheetDataService.GetSheetHeader(id, Categories.GasolineEquipment);
viewModel.KitchenHeader = this.sheetDataService.GetSheetHeader(id, Categories.Kitchen);
viewModel.RefrigerantHeader = this.sheetDataService.GetSheetHeader(id, Categories.Refrigerant);
viewModel.FireEquipmentHeader = this.sheetDataService.GetSheetHeader(id, Categories.FireEquipment);
viewModel.SteamHeader = this.sheetDataService.GetSheetHeader(id, Categories.Steam);
viewModel.OtherHeader = this.sheetDataService.GetSheetHeader(id, Categories.OtherCompound);
ViewData["DownloadZipReport"] =baseUrl+ viewModel.ProductLca.UploadZipReportUrl;
return View(viewModel);
}
public ActionResult OrganizationLcaDetail(int id)
{
var organizationLca = lcaDataService.GetOrganizationLCA(id);
//return View(organizationLca);
//根據LCAID新加folder
// Thomas Mao 3/21 found error, the uploaded part use Lca folder but template folder
//used here, can never work at all
ViewData["DownloadZipReport"] =baseUrl+ organizationLca.UploadZipReportUrl;
ViewData["sources"] = parameterservice.GetNonYearlyParameterOptions(Categories.OtherCompound);
return View(organizationLca);
}
public ActionResult OrganizationLcaDetailLifecycleAssmt(int id)
{
var organizationLca = lcaDataService.GetOrganizationLCA(id);
//return View(organizationLca);
//根據LCAID新加folder
// Thomas Mao 3/21 found error, the uploaded part use Lca folder but template folder
//used here, can never work at all
ViewData["StageOptions"] = CScommon.ProgramConstants.lifeCycleStages;
ViewData["DownloadZipReport"] =baseUrl+ organizationLca.UploadZipReportUrl;
ViewData["sources"] = parameterservice.GetNonYearlyParameterOptions(Categories.OtherCompound);
return View(organizationLca);
}
public ActionResult OrganizationLcaDetailSignificance(int id)
{
var organizationLca = lcaDataService.GetOrganizationLCA(id);
var query = significanceAssmtservice.GetList(id);
query = query.OrderBy(x => x.LifeCycleStage).ThenBy(x => x.ID);
var assmtFactors = significanceAssmtservice.GetAssmtFactors(id).ToList().Select(x =>
new { x.ID, x.FactorName, FactorScores = x.FactorScores.Select(s => new { s.OptionScore, OptionDescription = $"{s.OptionScore}{s.OptionDescription}" }) });
var lca = significanceAssmtservice.GetLCA(id);
ViewData["StageOptions"] = CScommon.ProgramConstants.lifeCycleStages;
ViewData["significanceAssmt"] = query.ToList();
ViewData["AssmtFactors"] = assmtFactors;
ViewData["GradingScore"] = lca.GradingScore;
return View(organizationLca);
}
public ActionResult SupplierLcaDetail(int supplierLcaId, int productLcaId)
{
ProductLCA supplierLca = lcaDataService.GetProductLCA(supplierLcaId);
var viewModel = new ProductLcaViewModel();
viewModel.ProductLca = supplierLca;
/*
if (supplierLca.PCRID == null)
{
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.BOM);
}
else
{
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.DirectMaterial);
}
*/
viewModel.DirectMaterial = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.DirectMaterial);
viewModel.IndirectMaterial = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.IndirectMaterial);
viewModel.WrapMaterial = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.WrapMaterial);
viewModel.WorkHourHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Septic);
viewModel.PowerHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Electric);
viewModel.WaterHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Water);
viewModel.WasteWaterHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.WasteWater);
viewModel.TransportHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Transport);
viewModel.WasteTransportHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.SheetHeaderWasteTransport);
viewModel.VehicleHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Vehicle);
viewModel.GasolineHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.GasolineEquipment);
viewModel.KitchenHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Kitchen);
viewModel.RefrigerantHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Refrigerant);
viewModel.FireEquipmentHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.FireEquipment);
viewModel.SteamHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.Steam);
viewModel.OtherHeader = this.sheetDataService.GetSheetHeader(supplierLcaId, Categories.OtherCompound);
ViewBag.ProductLcaId = productLcaId;
return View("ProductLcaDetail" ,viewModel);
}
[System.Web.Mvc.HttpPost]
public ActionResult ProductLcaConfirm(int lcaId)
{
lcaDataService.ProductLCAWaitToConfirmed(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult ProductLcaReject(int lcaId)
{
lcaDataService.ProductLCAWaitToRejected(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult ProductLcaRefuse(int lcaId)
{
lcaDataService.LCARefuse(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult OrganizationLcaConfirm(int lcaId)
{
lcaDataService.OrganizationLCAWaitToConfirmed(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult OrganizationLcaReject(int lcaId)
{
lcaDataService.OrganizationLCAWaitToRejected(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult OrganizationLcaRefuse(int lcaId)
{
lcaDataService.LCARefuse(lcaId);
return RedirectToAction("index", "Home");
}
[System.Web.Mvc.HttpPost]
public ActionResult ProductLcaChangeScheduledVerificationDate(int lcaId, ProductLcaViewModel model)
{
lcaDataService.LcaChangeScheduledVerificationDate(lcaId, model.ProductLca.ScheduledVerificationDate);
return RedirectToAction("ProductLcaDetail", "LCA", new { id = lcaId });
}
[System.Web.Mvc.HttpPost]
public ActionResult OrganizationLcaChangeScheduledVerificationDate(int lcaId, OrganizationLCA model)
{
lcaDataService.LcaChangeScheduledVerificationDate(lcaId, model.ScheduledVerificationDate);
return RedirectToAction("OrganizationLcaDetail", "LCA", new { id = lcaId });
}
[System.Web.Mvc.HttpPost]
public ActionResult OrganizationLcaUploadFile(int lcaId)
{
if (Request == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
HttpPostedFileBase file = Request.Files["UploadedFile"];
try
{
if(file != null)
{
//string fileName = file.FileName.ToString();
string fileName = "";
string extesion = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
var uri = storage.SaveToAzure(file.InputStream, file.FileName, baseUrl, "lca" + lcaId, false
, $"{CScommon.ProgramConstants.OrganizationDownloadReply}.zip");
if (uri.ToString().Contains("Browser_Local"))
{
fileName = uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1);
}
//var fullFilePath = this.FileUploadHandler(file);
lcaDataService.SaveCertificationZipFile(lcaId, fileName);
TempData["successMsg"] = "File Import Success!";
}
}
catch (Exception exception)
{
TempData["errorMsg"] = exception.Message;
return RedirectToAction("OrganizationLcaDetail","LCA", new { id = lcaId });
}
return RedirectToAction("OrganizationLcaDetail", "LCA", new { id = lcaId });
}
[System.Web.Mvc.HttpPost]
public ActionResult ProductLcaUploadFile(int lcaId)
{
if (Request == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
HttpPostedFileBase file = Request.Files["UploadedFile"];
try
{
if (file != null)
{
//string fileName = file.FileName.ToString();
string fileName = "";
string extesion = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
var uri = storage.SaveToAzure(file.InputStream, file.FileName, baseUrl, "lca" + lcaId, false
, $"{CScommon.ProgramConstants.ProductDownloadReply}.zip");
if (uri.ToString().Contains("Browser_Local"))
{
fileName = uri.ToString().Substring(uri.ToString().IndexOf("Browser_Local") - 1);
}
//var fullFilePath = this.FileUploadHandler(file);
lcaDataService.SaveCertificationZipFile(lcaId, fileName);
TempData["successMsg"] = "File Import Success!";
}
}
catch (Exception exception)
{
TempData["errorMsg"] = exception.Message;
return RedirectToAction("ProductLcaDetail", "LCA", new { id = lcaId });
}
return RedirectToAction("ProductLcaDetail", "LCA", new { id = lcaId });
}
}
}