demo20230512/Controllers/Api/ProductLCA/OtherCompoundController.cs
2023-05-12 10:20:28 +08:00

318 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Weee.Models;
using Weee.Models.Paramemter;
using Weee.Service;
using PagedList;
using Weee.Filter;
using Weee.DAL;
using Weee.ViewModels.Lca;
using System.Web;
using System.IO;
namespace Weee.Controllers
{
public class OtherCompoundController : ApiControllerBase
{
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeSheetDataService service;
private readonly WeeeDataContext db;
public OtherCompoundController(WeeeSheetDataService d
, WeeeParameterDataService s, WeeeDataContext _db)
: base(d)
{
service = d;
parameterservice = s;
db = _db;
}
[Route("api/OtherCompound/GetByLcaId/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaId(int LCAID)
{
object ret;
//Start CFT-50
//判断是组织型还是产品型
var organizationInfo = db.OrganizationLCAs.FirstOrDefault(o => o.ID == LCAID);
if (organizationInfo != null)
{
var result = service.GetSheet<OrganizationLCAFabSurveyForm_OtherCompounds>(LCAID);
ret = new
{
OtherCompounds = result.ToList(),
Options = //parameterservice.GetNonYearlyParameterOptions(Categories.OtherCompound),
parameterservice.GetARnGWPoptions(GWPparameterType.Others, LCAID),
SheetHeader = service.GetSheetHeader(LCAID, Categories.OtherCompound),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems,
ParameterSource = CScommon.ProgramConstants.ipccAR6
};
}
else
{
//End CFT-50
var query = service.GetSheet<ProductLCAFabSurveyForm_OtherCompounds>(LCAID);
ret = new
{
OtherCompounds = query.ToList(),
Options = //parameterservice.GetNonYearlyParameterOptions(Categories.OtherCompound),
parameterservice.GetARnGWPoptions(GWPparameterType.Others, LCAID),
SheetHeader = service.GetSheetHeader(LCAID, Categories.OtherCompound),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems,
ParameterSource = CScommon.ProgramConstants.ipccAR6
};
}
return ret;
}
[Route("api/OtherCompound/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Weee.Models.Paramemter.Categories.OtherCompound, header);
}
[Route("api/OtherCompound/SaveAll")]
[HttpPost]
[ApiEditableAttribute("toBeSave", true)]
public object SaveAll(IEnumerable<OrganizationLCAFabSurveyForm_OtherCompounds> toBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
//Start CFT-50
int LCAID = toBeSave.ToList().Count > 0 ? toBeSave.ToList()[0].LCAID : 0;
var organizationInfo = db.OrganizationLCAs.FirstOrDefault(o => o.ID == LCAID);
if (organizationInfo != null)
{
var list = new List<OrganizationLCAFabSurveyForm_OtherCompounds>();
foreach (var rec in toBeSave.ToList())
{
if (rec.Scalar == 0)
rec.KgCO2e = 0;
if (rec.ParameterID > 0)
{
rec.ARnGWPid = rec.ParameterID;
rec.ParameterID = null;
}
//if (rec.Parameter == null)
// rec.Parameter = parameterservice.GetNonYearlyParameter((int)rec.ParameterID);
//if (rec.activityDataType == 0) // 帶出年度預設值
//{
// rec.activityDataType = rec.Parameter.Type.activityDataType;
//}
//if (rec.emitParaType == 0) // 帶出年度預設值
//{
// rec.emitParaType = rec.Parameter.Type.emitParaType;
//}
//}
var query = saveOrganizationOtherCombunds(rec);
list.Add(query);
}
return new { result = list };
}
else
{
var productlist = new List<ProductLCAFabSurveyForm_OtherCompounds>();
foreach (var rec in toBeSave.ToList())
{
if (rec.ParameterID > 0)
{
if (rec.Parameter == null)
rec.Parameter = parameterservice.GetNonYearlyParameter((int)rec.ParameterID);
if (rec.activityDataType == 0) // 帶出年度預設值
{
rec.activityDataType = rec.Parameter.Type.activityDataType;
}
if (rec.emitParaType == 0) // 帶出年度預設值
{
rec.emitParaType = rec.Parameter.Type.emitParaType;
}
}
productlist.Add(new ProductLCAFabSurveyForm_OtherCompounds(rec));
}
//End CFT-50
var result = service.SaveSheet(productlist);
return new { result = productlist };
}
}
[Route("api/OtherCompound/Save/{LCAID}/{id}")]
[HttpPost]
[ApiEditableAttribute("toBeSave", false)]
public object Save(int LCAID, int id, OrganizationLCAFabSurveyForm_OtherCompounds toBeSave)
{
//toBeSave.ReferenceFileUrl=getBaseUrl()+toBeSave.ReferenceFileUrl;
if (!ModelState.IsValid) {
return BadRequest();
}
if (id != toBeSave.ID || LCAID != toBeSave.LCAID) {
return BadRequest();
}
//Start CFT-50
//判断是组织型还是产品型
var organizationInfo = db.OrganizationLCAs.FirstOrDefault(o => o.ID == LCAID);
if (organizationInfo != null)
{
if (toBeSave.Scalar==0)
toBeSave.KgCO2e = 0;
if (toBeSave.ParameterID > 0)
{
toBeSave.ARnGWPid = toBeSave.ParameterID;
toBeSave.ParameterID = null;
//if (toBeSave.Parameter == null)
// toBeSave.Parameter = parameterservice.GetNonYearlyParameter((int)toBeSave.ParameterID);
//if (toBeSave.activityDataType == 0) // 帶出年度預設值
//{
// toBeSave.activityDataType = toBeSave.Parameter.Type.activityDataType;
//}
//if (toBeSave.emitParaType == 0) // 帶出年度預設值
//{
// toBeSave.emitParaType = toBeSave.Parameter.Type.emitParaType;
//}
}
var item = saveOrganizationOtherCombunds(toBeSave);
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
if (!string.IsNullOrWhiteSpace(item.ReferenceFileUrl) && item.ReferenceFileUrl.Contains("of_"))
{
var uri = item.ReferenceFileUrl;
uri = uri.Replace("of_", "of" + item.ID + "_");
storage.MoveToAzure(item.ReferenceFileUrl, uri);
item.ReferenceFileUrl = uri;
}
service.SaveSheetItem(item);
return item;
}
else
{
if (toBeSave.Scalar == 0)
toBeSave.KgCO2e = 0;
if (toBeSave.ParameterID > 0)
{
toBeSave.ARnGWPid = toBeSave.ParameterID;
toBeSave.ParameterID = null;
//if (toBeSave.Parameter == null)
// toBeSave.Parameter = parameterservice.GetNonYearlyParameter((int)toBeSave.ParameterID);
//if (toBeSave.activityDataType == 0) // 帶出年度預設值
//{
// toBeSave.activityDataType = toBeSave.Parameter.Type.activityDataType;
//}
//if (toBeSave.emitParaType == 0) // 帶出年度預設值
//{
// toBeSave.emitParaType = toBeSave.Parameter.Type.emitParaType;
//}
}
var productSheet = new ProductLCAFabSurveyForm_OtherCompounds(toBeSave);
service.SaveSheetItem(productSheet);
var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage;
if (!string.IsNullOrWhiteSpace(productSheet.ReferenceFileUrl) && productSheet.ReferenceFileUrl.Contains("of_"))
{
var uri = productSheet.ReferenceFileUrl;
uri = uri.Replace("of_", "of" + productSheet.ID + "_");
storage.MoveToAzure(productSheet.ReferenceFileUrl, uri);
productSheet.ReferenceFileUrl = uri;
}
service.SaveSheetItem(productSheet);
return productSheet;
}
return toBeSave;
}
[Route("api/OtherCompound/DeleteAll/{LCAID}")]
[HttpDelete]
public IHttpActionResult DeleteAll(int LCAID)
{
//Start CFT-50
//判断是组织型还是产品型
var organizationInfo = db.OrganizationLCAs.FirstOrDefault(o => o.ID == LCAID);
if (organizationInfo != null)
{
service.DeleteSheet<OrganizationLCAFabSurveyForm_OtherCompounds>(LCAID);
}
else
{
service.DeleteSheet<ProductLCAFabSurveyForm_OtherCompounds>(LCAID);
}
//service.DeleteSheet<OtherCompound>(LCAID);
//End CFT-50
return Ok();
}
[Route("api/OtherCompound/Delete/{ID}/{LCAID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID, int LCAID)
{
//Start CFT-50
//判断是组织型还是产品型
var lcaInfo = db.OrganizationLCAs.FirstOrDefault(o => o.ID == LCAID);
if(lcaInfo != null)
{
service.DeleteSheetItem<OrganizationLCAFabSurveyForm_OtherCompounds>(ID);
}
else
{
service.DeleteSheetItem<ProductLCAFabSurveyForm_OtherCompounds>(ID);
}
//service.DeleteSheetItem<OtherCompound>(ID);
//End CFT-50
return Ok();
}
private OrganizationLCAFabSurveyForm_OtherCompounds saveOrganizationOtherCombunds(
OrganizationLCAFabSurveyForm_OtherCompounds toBeSave)
{
OrganizationLCAFabSurveyForm_OtherCompounds item = null;// new OrganizationLCAFabSurveyForm_OtherCompounds();
bool toAdd = true;
if (toBeSave.ID > 0)
{
item = db.OrganizationOtherCompound.Find(toBeSave.ID);
if (item != null)
toAdd = false;
}
if (toAdd)
item = new OrganizationLCAFabSurveyForm_OtherCompounds();
item.ARnGWPid = toBeSave.ARnGWPid;
item.ParameterID = toBeSave.ParameterID;
item.LCAID = toBeSave.LCAID;
item.KgCO2e = toBeSave.KgCO2e;
item.Description = toBeSave.Description;
item.Scalar =Math.Round( toBeSave.Scalar,4);
item.ProcessName = toBeSave.ProcessName;
item.ResponsibleUnit = toBeSave.ResponsibleUnit;
item.Name = toBeSave.Name;
item.CreatedTime = toBeSave.CreatedTime;
item.activityDataType = toBeSave.activityDataType;
item.emitParaType = toBeSave.emitParaType;
if (toBeSave.GWPFactor == null)
toBeSave.GWPFactor = 1;
if (toBeSave.CO2Factor==null)
toBeSave.CO2Factor = 1;
item.GWPFactor =Math.Round((decimal) toBeSave.GWPFactor,10);
item.CO2Factor =Math.Round((decimal)toBeSave.CO2Factor,10);
item.ReferenceFileUrl= toBeSave.ReferenceFileUrl;
item.ingredientName=toBeSave.ingredientName;
item.warmGasType = toBeSave.warmGasType;
item.paraSource = toBeSave.paraSource;
item.CO2Value = toBeSave.CO2Value;
item.CH4Value = toBeSave.CH4Value;
item.N2OValue = toBeSave.N2OValue;
item.HFCsValue = toBeSave.HFCsValue;
item.PFCsValue = toBeSave.PFCsValue;
item.SF6Value = toBeSave.SF6Value;
item.NF3Value = toBeSave.NF3Value;
if (toAdd)
db.OrganizationOtherCompound.Add(item);
db.SaveChanges();
return item;
}
}
}