159 lines
6.6 KiB
C#
159 lines
6.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data;
|
|||
|
using System.Data.Entity;
|
|||
|
using System.Data.Entity.Infrastructure;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Web.Http;
|
|||
|
using System.Web.Http.Description;
|
|||
|
using Weee.DAL;
|
|||
|
using Weee.Models;
|
|||
|
using Weee.Service;
|
|||
|
using Weee.Models.ExtensionMethods;
|
|||
|
using Weee.Filter;
|
|||
|
using Microsoft.AspNet.Identity;
|
|||
|
using System.Web.UI.WebControls;
|
|||
|
using NLog;
|
|||
|
using static Weee.Models.LCACommonSurveyForm_PowerUsages;
|
|||
|
|
|||
|
namespace Weee.Controllers
|
|||
|
{
|
|||
|
public class PowerUsageController : ApiControllerBase
|
|||
|
{
|
|||
|
private readonly WeeeMonthlyDataService service;
|
|||
|
private readonly WeeeParameterDataService parameterservice;
|
|||
|
private readonly WeeeDataContext _db;
|
|||
|
private WeeeLCADataService serviceLCA;
|
|||
|
|
|||
|
public PowerUsageController(WeeeMonthlyDataService d, WeeeParameterDataService s
|
|||
|
, WeeeDataContext db, WeeeLCADataService serviceLCA)
|
|||
|
: base(d)
|
|||
|
{
|
|||
|
service = d;
|
|||
|
parameterservice = s;
|
|||
|
_db = db;
|
|||
|
this.serviceLCA = serviceLCA;
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/PowerUsage/GetPowerUsages/{LCAID}")]
|
|||
|
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
|
|||
|
public object Get(int LCAID)
|
|||
|
{
|
|||
|
LCA lca = service.GetLCA(LCAID);
|
|||
|
object ret =
|
|||
|
new
|
|||
|
{
|
|||
|
PowerUsages = service.GetSheet<LCACommonSurveyForm_PowerUsages>(LCAID),
|
|||
|
// 檢查目前的盤查是否為產品型,用於讀取不同的係數
|
|||
|
options = parameterservice.GetYearlyParameterOptions((lca.LCAType.Name == "ProductLCA") ? Models.Paramemter.Categories.ElectricProduct : Models.Paramemter.Categories.Electric),
|
|||
|
SheetHeader = service.GetSheetHeader(LCAID, (lca.LCAType.Name == "ProductLCA") ? Models.Paramemter.Categories.ElectricProduct : Models.Paramemter.Categories.Electric),
|
|||
|
ActivityOptions = activityItems,
|
|||
|
EmitParaOptions = emitParaItems,
|
|||
|
factor = parameterservice.LCAID2powerPara(LCAID)
|
|||
|
};
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/PowerUsage/SaveSheetHeader")]
|
|||
|
[HttpPost]
|
|||
|
public HttpResponseMessage SaveHeader(SheetHeader header)
|
|||
|
{
|
|||
|
return this.SaveSheetHeader(service, Weee.Models.Paramemter.Categories.Electric, header);
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/PowerUsage/SavePowerUsages/{LCAID}/{activity}/{emitPara}")]
|
|||
|
[HttpPost]
|
|||
|
[ApiEditableAttribute("ToBeSave", true)]
|
|||
|
public object SaveAll(int LCAID, int activity, int emitPara,
|
|||
|
IEnumerable<LCACommonSurveyForm_PowerUsages> ToBeSave)
|
|||
|
{
|
|||
|
if (!ModelState.IsValid)
|
|||
|
{
|
|||
|
throw new Exception("error");
|
|||
|
}
|
|||
|
if (ToBeSave == null || ToBeSave.Count() == 0)
|
|||
|
return null;
|
|||
|
serviceLCA.Initialize(User.Identity.GetUserId());
|
|||
|
LCA lca = serviceLCA.GetLCA(LCAID);
|
|||
|
lca.activityDataTypePowerUsage = (ActivityDataType)activity;
|
|||
|
lca.emitParaTypePowerUsage = (EmitParaType)emitPara;
|
|||
|
_db.SaveChanges();
|
|||
|
//OrganizationLCA orgLCA = serviceLCA.GetOrganizationLCA(LCAID);
|
|||
|
//orgLCA.activityDataTypePowerUsage =(ActivityDataType) activity;
|
|||
|
//orgLCA.emitParaTypePowerUsage =(EmitParaType) emitPara;
|
|||
|
//serviceLCA.SaveOrganizationLCA(orgLCA);
|
|||
|
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 });
|
|||
|
Weee.Models.Paramemter.Parameter curPara = new Weee.Models.Paramemter.SimaproParameter();
|
|||
|
curPara.CO2Value = 0;
|
|||
|
string curArea = "";
|
|||
|
string curYear="";
|
|||
|
decimal scalarTotal = 0;
|
|||
|
foreach (var item in ToBeSave)
|
|||
|
{
|
|||
|
curArea= item.Area;
|
|||
|
curYear = item.Year;//.Parameter.Year;
|
|||
|
item.ID = 0;
|
|||
|
item.Peak =(decimal) Math.Round(item.Peak==null?0:(double)item.Peak, 4);
|
|||
|
item.HalfPeak = (decimal)Math.Round(item.HalfPeak == null ? 0 : (double)item.HalfPeak, 4);
|
|||
|
item.SaturdayHalfPeak = (decimal)Math.Round(item.SaturdayHalfPeak == null ? 0 : (double)item.SaturdayHalfPeak, 4);
|
|||
|
item.OffPeak = (decimal)Math.Round(item.OffPeak == null ? 0 : (double)item.OffPeak, 4);
|
|||
|
if (item.ParameterID != null)
|
|||
|
{
|
|||
|
Weee.Models.Paramemter.Parameter para = parameters.First(x => x.ID == item.ParameterID);
|
|||
|
curPara = para;
|
|||
|
if (item.Type == PowerType.GreenNoCarbon)
|
|||
|
item.KgCO2e = 0;
|
|||
|
else
|
|||
|
item.UpdateKgCO2e(para.Value);
|
|||
|
scalarTotal += item.Scalar;
|
|||
|
}
|
|||
|
else
|
|||
|
item.KgCO2e = 0;
|
|||
|
}
|
|||
|
service.DeleteSheet<LCACommonSurveyForm_PowerUsages>(ToBeSave.First().LCAID);
|
|||
|
var result = service.SaveSheet(ToBeSave);
|
|||
|
LifecycleAssmtDataService sds =new LifecycleAssmtDataService(_db);
|
|||
|
string userID = User.Identity.GetUserId();
|
|||
|
int yearI = 0;
|
|||
|
if (curYear != null)
|
|||
|
{
|
|||
|
int.TryParse(curYear, out yearI);
|
|||
|
}
|
|||
|
string err=sds.power2lifeCycle(userID, LCAID, scalarTotal, curArea, yearI, curPara);
|
|||
|
if (!string.IsNullOrWhiteSpace(err))
|
|||
|
{
|
|||
|
Logger log = NLog.LogManager.GetCurrentClassLogger();
|
|||
|
log.Error($"error in api api/PowerUsage/SavePowerUsages sync to life cycle item: {err}");
|
|||
|
}
|
|||
|
return new { result = result };
|
|||
|
}
|
|||
|
|
|||
|
//[Route("api/PowerUsage/SavePowerUsage/{LCAID}/{id}")]
|
|||
|
//[HttpPost]
|
|||
|
//public int Save(int LCAID, int id, PowerUsage powerusage)
|
|||
|
//{
|
|||
|
// if (!ModelState.IsValid) { return -1; }
|
|||
|
// if (id != powerusage.ID || LCAID != powerusage.LCAID) { return -1; }
|
|||
|
// service.SaveMonthlyData(powerusage);
|
|||
|
|
|||
|
// return powerusage.ID;
|
|||
|
//}
|
|||
|
|
|||
|
//[Route("api/PowerUsage/DeletePowerUsages/{LCAID}")]
|
|||
|
//[HttpDelete]
|
|||
|
//public IHttpActionResult DeleteAll(int LCAID)
|
|||
|
//{
|
|||
|
// service.DeleteSheet<PowerUsage>(LCAID);
|
|||
|
// return Ok();
|
|||
|
//}
|
|||
|
}
|
|||
|
}
|