demo20230512/Controllers/Api/RiskAssmt/LifecycleAssmtController.cs

226 lines
9.4 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Weee.Filter;
using Weee.Models;
using Weee.Service;
using Weee.ViewModels.DataTransferObject;
namespace Weee.Controllers.Api.RiskAssmt
{
public class LifecycleAssmtController : ApiControllerBase
{
private readonly LifecycleAssmtDataService service;
public LifecycleAssmtController(LifecycleAssmtDataService d) : base(d)
{
service = d;
}
[Route("api/LifecycleAssmt/GetByLcaId/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaId(int LCAID)
{
//var type = service.GetLCAType(LCAID);
var query = service.GetList(LCAID);
query = query.OrderBy(x => x.LifeCycleStage).ThenBy(x=>x.MaterialNo).ThenBy(x => x.ID);
//var items = service.GetItems();
return new
{
Analysis = query.ToList(),
StageOptions = new Dictionary<int, string>(){
{ 1, Resource.LifecycleAssmt_materialFromUpstream},//原料取得
{ 2, Resource.LifecycleAssmt_upstreamTrasnport},//上游運輸
{ 3, Resource.LifecycleAssmt_operationOrServiceUse},//運作支援及服務
{ 4, Resource.LifecycleAssmt_downstreamTransport},//下游運輸
{ 5, Resource.LifecycleAssmt_productUsageAndDisposal},//產品使用及廢棄
{ 6, Resource.LifecycleAssmt_wasteTreatment},//廢棄物處理
{ 7, Resource.LifecycleAssmt_others},//其他
},
//SheetHeader = service.GetSheetHeader(LCAID, Categories.Refrigerant)
};
}
/// <summary>
/// life cycle stage (with or without category) to key value dictionary
/// </summary>
/// <param name="LCS"></param>
/// <returns></returns>
[Route("api/LifecycleAssmt/LCScat2keyValues/{LCScat}")]
[HttpGet]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object LCScat2keyValues(int LCScat)
{
Dictionary<string, string> retKeyValues = new Dictionary<string, string>();
switch (LCScat+"")
{
case "1":
retKeyValues.Add("14", Resource.LifecycleAssmt_Category4);
break;
case "2":
retKeyValues.Add("23", Resource.LifecycleAssmt_Category3);
break;
case "3":
retKeyValues.Add("32", Resource.LifecycleAssmt_Category2);
retKeyValues.Add("34", Resource.LifecycleAssmt_Category4);
break;
case "4":
retKeyValues.Add("43", Resource.LifecycleAssmt_Category3);
break;
case "5":
retKeyValues.Add("55", Resource.LifecycleAssmt_Category5);
break;
case "6":
retKeyValues.Add("64", Resource.LifecycleAssmt_Category4);
break;
case "7":
retKeyValues.Add("76", Resource.LifecycleAssmt_others);
break;
case "14":
retKeyValues.Add("4.1a", Resource.LifecycleAssmt_goodsPurchasedByTheOrganization);
break;
//case "16":
// retKeyValues.Add("6", "其他");
// break;
case "23":
retKeyValues.Add("3.1", Resource.LifecycleAssmt_goodsUpstreamTransportAndDistribution);
retKeyValues.Add("3.3", Resource.LifecycleAssmt_employeeCommuting);
retKeyValues.Add("3.4", Resource.LifecycleAssmt_clientOrVisitorsTransport);
retKeyValues.Add("3.5", Resource.LifecycleAssmt_businessTravel);
break;
//case "26":
// retKeyValues.Add("6", "其他");
// break;
case "32":
retKeyValues.Add("2.1", Resource.LifecycleAssmt_importedElectricity);
retKeyValues.Add("2.2", Resource.LifecycleAssmt_importedEnergy);
break;
case "34":
retKeyValues.Add("4.1b", Resource.LifecycleAssmt_upstreamImportedElectricity);
retKeyValues.Add("4.1b2", Resource.LifecycleAssmt_upstreamImportedEnergy);
retKeyValues.Add("4.2", Resource.LifecycleAssmt_emissionsFromCapitalGoods);
retKeyValues.Add("4.4", Resource.LifecycleAssmt_equipmentLeasedByTheOrganization);
retKeyValues.Add("4.5", Resource.LifecycleAssmt_servicesUsedByTheOrganization);
retKeyValues.Add("4.6", Resource.LifecycleAssmt_disposalOfWaste);
retKeyValues.Add("4.7", Resource.LifecycleAssmt_usageOfWater);
break;
//case "36":
// retKeyValues.Add("6", "其他");
// break;
case "43":
retKeyValues.Add("3.2", Resource.LifecycleAssmt_goodsDownstreamTransportAndDistribution);
retKeyValues.Add("3.3", Resource.LifecycleAssmt_wasteTransportation);
break;
//case "46":
// retKeyValues.Add("6", "其他");
// break;
case "55":
retKeyValues.Add("5.1", Resource.LifecycleAssmt_useStageOfTheProduct);
retKeyValues.Add("5.2", Resource.LifecycleAssmt_downstreamLeasedAssets);
retKeyValues.Add("5.3", Resource.LifecycleAssmt_endOfLifeStageOfTheProduct);
retKeyValues.Add("5.4", Resource.LifecycleAssmt_investment);
break;
//case "56":
// retKeyValues.Add("6", "其他");
// break;
case "64":
retKeyValues.Add("4.3", Resource.LifecycleAssmt_disposalOfSolidAndLiquidWaste);
break;
//case "66":
// retKeyValues.Add("6", "其他");
// break;
case "76":
retKeyValues.Add("6", Resource.LifecycleAssmt_others);
break;
default:
break;
}
return new
{
keyValues = retKeyValues,
//SheetHeader = service.GetSheetHeader(LCAID, Categories.Refrigerant)
};
}
[Route("api/LifecycleAssmt/Sync/{LCAID}")]
[HttpPost]
public object Sync(int LCAID, UpdateLIfecycleAssmtDTO input)
{
return new
{
Analysis = service.Sync(LCAID, input)
};
}
[Route("api/LifecycleAssmt/Save/{LCAID}/{id}")]
[HttpPost]
public object Save(int LCAID, int id, LCARiskAssmtSurveyForm_LifecycleAssmt ToBeSave)
{
//ToBeSave.ReferenceFileUrl =getBaseUrl()+ ToBeSave.ReferenceFileUrl;
if (!ModelState.IsValid)
{
List<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors).ToList();
return BadRequest(allErrors[0].Exception.Message);
}
if (id != ToBeSave.ID || LCAID != ToBeSave.LCAID) { return BadRequest(); }
service.Save(ToBeSave);
return ToBeSave;
}
[Route("api/LifecycleAssmt/SaveAll")]
[HttpPost]
public object SaveAll(List<LCARiskAssmtSurveyForm_LifecycleAssmt> toBeSave)
{
//foreach(var item in toBeSave)
//{
// item.EmissionKg = item.ActivityIntensity * item.KgCO2e;
// item.EmissionT = item.EmissionKg / 1000;
//}
//foreach(var item in ModelState.Values)
//{
// item.Value.re
//}
//ModelState.Remove("EmissionKg");
//ModelState.Remove("EmissionT");
if (!ModelState.IsValid)
{
List<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors).ToList();
return BadRequest(allErrors[0].Exception.Message);
}
var errMsg = "";
for(var i = toBeSave.Count - 1; i >= 0; i--)
{
try
{
service.Save(toBeSave[i]);
}
catch (Exception ex)
{
if (!string.IsNullOrWhiteSpace(errMsg))
errMsg += "、";
errMsg += $"[{toBeSave[i].GHGEvaluateItem}]";
toBeSave.RemoveAt(i);
}
}
if (!string.IsNullOrWhiteSpace(errMsg))
errMsg = $"同一生命周期,評估項目 {errMsg} 不能重複,故不進行引用。";
return new { result = toBeSave, errMsg = errMsg };
}
[Route("api/LifecycleAssmt/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
service.Delete(ID);
return Ok();
}
}
}