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

140 lines
5.6 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 DocumentFormat.OpenXml.Wordprocessing;
namespace Weee.Controllers
{
public class FireEquipmentController : ApiControllerBase
{
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeSheetDataService service;
public FireEquipmentController(WeeeSheetDataService d, WeeeParameterDataService s) : base(d)
{
service = d;
parameterservice = s;
}
[Route("api/FireEquipment/GetByLcaId/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetByLcaId(int LCAID)
{
var query = //service.GetSheet<LCACommonSurveyForm_FireEquipments>(LCAID);
service.getFireEquipmentWithFactor(LCAID);
// 取得排放源,包含消防與同時是消防&冷媒的項目
var option1 = parameterservice.GetARnGWPoptions(GWPparameterType.FireEquipment, LCAID);
var option2 = parameterservice.GetARnGWPoptions(GWPparameterType.FireEquipmentAndRefrigerant, LCAID);
object ret = new
{
FireEquipments = query.ToList(),
Options = option1.Concat(option2.Where(kvp => !option1.ContainsKey(kvp.Key))).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
//parameterservice.GetNonYearlyParameterOptions(Categories.FireEquipment),
SheetHeader = service.GetSheetHeader(LCAID, Categories.FireEquipment),
ActivityOptions = activityItems,
EmitParaOptions = emitParaItems
};
return ret;
}
[Route("api/FireEquipment/SaveAll")]
[HttpPost]
[ApiEditableAttribute("toBeSave", true)]
public object SaveAll(IEnumerable<LCACommonSurveyForm_FireEquipments> toBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
foreach (var rec in toBeSave)
{
if (rec.ParameterID > 0)
{
rec.ARnGWPid = rec.ParameterID;
rec.ParameterID = null;
}
decimal paraValue = parameterservice.getARnGWPvalue(rec.LCAID, (int)rec.ARnGWPid);
rec.KgCO2e = paraValue * rec.Scalar * rec.Quantity;
//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 result = service.SaveSheet(toBeSave);
var resultIDs = result.Select(y => y.ID).ToList();
var query = service.getFireEquipmentWithFactor(result.First().LCAID).Where(x => resultIDs.Contains(x.ID)).ToList();
return new { result = query };
}
[Route("api/FireEquipment/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.FireEquipment, header);
}
[Route("api/FireEquipment/Save/{LCAID}/{id}")]
[HttpPost]
[ApiEditableAttribute("toBeSave", false)]
public object Save(int LCAID, int id, LCACommonSurveyForm_FireEquipments toBeSave)
{
if (!ModelState.IsValid) {
return BadRequest();
}
if (id != toBeSave.ID || LCAID != toBeSave.LCAID) {
return BadRequest();
}
toBeSave.Scalar = Math.Round(toBeSave.Scalar, 4);
if (toBeSave.ParameterID > 0)
{
toBeSave.ARnGWPid = toBeSave.ParameterID;
toBeSave.ParameterID = null;
}
decimal paraValue = parameterservice.getARnGWPvalue(LCAID, (int) toBeSave.ARnGWPid);
toBeSave.KgCO2e = paraValue * toBeSave.Scalar * toBeSave.Quantity;
//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;
//}
service.SaveSheetItem(toBeSave);
var query = service.getFireEquipmentWithFactor(LCAID).Where(x => x.ID == toBeSave.ID);
return query.ToList().First();
}
[Route("api/FireEquipment/DeleteAll/{LCAID}")]
[HttpDelete]
public IHttpActionResult DeleteAll(int LCAID)
{
service.DeleteSheet<LCACommonSurveyForm_FireEquipments>(LCAID);
return Ok();
}
[Route("api/FireEquipment/Delete/{ID}")]
[HttpDelete]
public IHttpActionResult Delete(int ID)
{
service.DeleteSheetItem<LCACommonSurveyForm_FireEquipments>(ID);
return Ok();
}
}
}