demo20230512/Controllers/Api/RiskAssmt/WaterUsageController.cs

81 lines
2.8 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.Http;
using Weee.DAL;
using Weee.Models;
using Weee.Service;
using Weee.Models.ExtensionMethods;
using Weee.Filter;
using System.Net.Http;
using Weee.Models.Paramemter;
namespace Weee.Controllers
{
public class RiskAssmtWaterUsageController : ApiControllerBase
{
private readonly WaterUsageDataService service;
private readonly WeeeParameterDataService parameterservice;
private readonly WeeeDataContext _db;
public RiskAssmtWaterUsageController(WaterUsageDataService d, WeeeParameterDataService s, WeeeDataContext db)
: base(d)
{
service = d;
parameterservice = s;
_db = db;
}
[Route("api/RiskAssmtWaterUsage/GetWaterUsages/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object Get(int LCAID)
{
return new
{
WaterUsages = service.GetSheet<LCARiskAssmtSurveyForm_WaterUsages>(LCAID),
options = parameterservice.GetYearlyParameterOptions(Categories.Water),
SheetHeader = service.GetSheetHeader(LCAID,Categories.Water)
};
}
[Route("api/RiskAssmtWaterUsage/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Categories.Water, header);
}
[Route("api/RiskAssmtWaterUsage/SaveWaterUsages")]
[HttpPost]
[ApiEditableRiskAssmtAttribute("ToBeSave", true)]
public object SaveAll(IEnumerable<LCARiskAssmtSurveyForm_WaterUsages> ToBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
if (ToBeSave == null || ToBeSave.Count() == 0) return null;
var parameterIDs = ToBeSave
.Select(x => x.ParameterID)
.Where(x => x != null)
.Distinct()
.Cast<int>();
var parameters = _db.Parameters
.Where(x => parameterIDs.Contains(x.ID))
.Select(x => new { x.ID, x.Value });
foreach (var item in ToBeSave.Where(x => x.ParameterID != null))
{
item.ID = 0;
if (item.ParameterID != null)
item.UpdateKgCO2e(parameters.First(x => x.ID == item.ParameterID).Value);
else
item.KgCO2e = 0;
}
service.DeleteSheet<LCARiskAssmtSurveyForm_WaterUsages>(ToBeSave.First().LCAID);
var result = service.SaveSheet(ToBeSave);
return new { result = result };
}
}
}