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 System.Web; using System.IO; namespace Weee.Controllers { public class GasolineEquipmentController : ApiControllerBase { private readonly WeeeParameterDataService parameterservice; private readonly WeeeSheetDataService service; public GasolineEquipmentController(WeeeSheetDataService d, WeeeParameterDataService s) : base(d) { service = d; parameterservice = s; } [Route("api/GasolineEquipment/GetByLcaId/{LCAID}")] //[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not public object GetByLcaId(int LCAID) { LCA lca = service.GetLCA(LCAID); var query = //service.GetSheet(LCAID); service.getGasolineEquipmentWithFactor(LCAID); object ret= new { GasolineEquipments = query.ToList(), Options = parameterservice.GetYearlyParameterOptions((lca.LCAType.Name == "ProductLCA") ? Categories.GasolineEquipmentProduct : Categories.GasolineEquipment), SheetHeader = service.GetSheetHeader(LCAID, (lca.LCAType.Name == "ProductLCA") ? Categories.GasolineEquipmentProduct : Categories.GasolineEquipment), ActivityOptions = activityItems, EmitParaOptions = emitParaItems }; return ret; } [Route("api/GasolineEquipment/SaveAll")] [HttpPost] [ApiEditableAttribute("toBeSave", true)] public object SaveAll(IEnumerable toBeSave) { if (!ModelState.IsValid) { throw new Exception("error"); } var dic = service.getN2OandCH4Factors(toBeSave.First().LCAID); LCA lca = service.GetLCA(toBeSave.First().LCAID); foreach (var rec in toBeSave) { if (rec.ParameterID > 0) { if (rec.Parameter == null) rec.Parameter = parameterservice.GetYearlyParameter((int)rec.ParameterID); rec.ParameterValue = (lca.LCAType.Name == "ProductLCA") ? rec.Parameter.Value : (rec.Parameter.CO2Value + rec.Parameter.CH4Value * dic["CH4"] + rec.Parameter.N2OValue * dic["N2O"]); //rec.ParameterValue = rec.Parameter.CO2Value + rec.Parameter.CH4Value * dic["CH4"] + rec.Parameter.N2OValue * dic["N2O"]; rec.KgCO2e = rec.ParameterValue * rec.Scalar; 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.getGasolineEquipmentWithFactor(result.First().LCAID).Where(x => resultIDs.Contains(x.ID)).ToList(); return new { result = query }; } [Route("api/GasolineEquipment/SaveSheetHeader")] [HttpPost] public HttpResponseMessage SaveHeader(SheetHeader header) { LCA lca = service.GetLCA(header.LCAID); //return this.SaveSheetHeader(service, Categories.GasolineEquipment, header); return this.SaveSheetHeader(service, (lca.LCAType.Name == "ProductLCA") ? Weee.Models.Paramemter.Categories.GasolineEquipmentProduct : Weee.Models.Paramemter.Categories.GasolineEquipment, header); } [Route("api/GasolineEquipment/Save/{LCAID}/{id}")] [HttpPost] [ApiEditableAttribute("toBeSave", false)] public object Save(int LCAID, int id, LCACommonSurveyForm_GasolineEquipments toBeSave) { //toBeSave.ReferenceFileUrl=getBaseUrl()+toBeSave.ReferenceFileUrl; //toBeSave.ReferencePhotoUrl = getBaseUrl() + toBeSave.ReferencePhotoUrl; 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) { if (toBeSave.Parameter == null) toBeSave.Parameter = parameterservice.GetYearlyParameter((int)toBeSave.ParameterID); var dic = service.getN2OandCH4Factors(LCAID); LCA lca = service.GetLCA(LCAID); //toBeSave.ParameterValue = toBeSave.Parameter.CO2Value + toBeSave.Parameter.CH4Value * dic["CH4"] + toBeSave.Parameter.N2OValue * dic["N2O"]; toBeSave.ParameterValue = (lca.LCAType.Name == "ProductLCA") ? toBeSave.Parameter.Value : (toBeSave.Parameter.CO2Value + toBeSave.Parameter.CH4Value * dic["CH4"] + toBeSave.Parameter.N2OValue * dic["N2O"]); toBeSave.KgCO2e = toBeSave.ParameterValue * toBeSave.Scalar; if (toBeSave.activityDataType == 0) // 帶出年度預設值 { toBeSave.activityDataType = toBeSave.Parameter.Type.activityDataType; } if (toBeSave.emitParaType == 0) // 帶出年度預設值 { toBeSave.emitParaType = toBeSave.Parameter.Type.emitParaType; } } if (toBeSave.Scalar==0) toBeSave.KgCO2e = 0; service.SaveSheetItem(toBeSave); var storage = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(Storage.AzureStorage)) as Storage.AzureStorage; if (!string.IsNullOrWhiteSpace(toBeSave.ReferenceFileUrl) && toBeSave.ReferenceFileUrl.Contains("gf_")) { var uri = toBeSave.ReferenceFileUrl; uri = uri.Replace("gf_", "gf" + toBeSave.ID + "_"); storage.MoveToAzure(toBeSave.ReferenceFileUrl, uri); toBeSave.ReferenceFileUrl = uri; } if (!string.IsNullOrWhiteSpace(toBeSave.ReferencePhotoUrl) && toBeSave.ReferencePhotoUrl.Contains("gp_")) { var uri = toBeSave.ReferencePhotoUrl; uri = uri.Replace("gp_", "gp" + toBeSave.ID + "_"); storage.MoveToAzure(toBeSave.ReferencePhotoUrl, uri); toBeSave.ReferencePhotoUrl = uri; } service.SaveSheetItem(toBeSave); var query = service.getGasolineEquipmentWithFactor(LCAID).Where(x => x.ID == toBeSave.ID); return query.ToList().First(); } [Route("api/GasolineEquipment/DeleteAll/{LCAID}")] [HttpDelete] public IHttpActionResult DeleteAll(int LCAID) { service.DeleteSheet(LCAID); return Ok(); } [Route("api/GasolineEquipment/Delete/{ID}")] [HttpDelete] public IHttpActionResult Delete(int ID) { service.DeleteSheetItem(ID); return Ok(); } } }