demo20230512/Controllers/Api/ProductLCA/WorkHourController.cs

457 lines
19 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Weee.DAL;
using Weee.Filter;
using Weee.Models;
using Weee.Models.ExtensionMethods;
using Weee.Service;
namespace Weee.Controllers
{
public class WorkHourController : ApiControllerBase
{
private readonly WeeeMonthlyDataService service;
private readonly WeeeDataContext _db;
public WorkHourController(WeeeMonthlyDataService d, WeeeDataContext db)
: base(d)
{
service = d;
_db = db;
}
[Route("api/WorkHour/GetWorkHours")]
[HttpPost]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object Get(PeopleTypesParameters parameters)
{
if (HttpContext.Current.Session!=null)
HttpContext.Current.Session["LCAid"] = parameters.LCAID;
Dictionary<string, decimal> dictFactors = service.getWorkhourFactors();
List<string> Factors = new List<string>();
foreach(var rec in dictFactors)
{
Factors.Add(rec.Key);
Factors.Add(rec.Value.ToString("0.0000000000"));
}
var GWP_CH4 = (from a in _db.AssessmentReportGWP
where a.chemicalFormula == "CH4"
select a).FirstOrDefault();
var arVersion = (from a in _db.LCAs // uow.GetDBcontext().LCAs
where a.ID == parameters.LCAID
select a.ARversion).FirstOrDefault();
if (GWP_CH4 != null && arVersion != null)
{
Factors.Add("GWP");
if (arVersion == "AR4")
Factors.Add(GWP_CH4.AR4GWP100.ToString("0.0000000000"));
else if (arVersion == "AR5")
Factors.Add(GWP_CH4.AR5GWP100.ToString("0.0000000000"));
else if (arVersion == "AR6")
Factors.Add(GWP_CH4.AR6GWP100.ToString("0.0000000000"));
}
if (Factors.Count < 12)
{
for (int i = Factors.Count; i < 12; i++)
Factors.Add("");
}
if(parameters.peopleType == null)
{
parameters.peopleType = "直接人員";
}
object ret = null;
if (parameters.peopleType == "直接人員" || parameters.peopleType == "間接人員")
{
var WorkHours = service.GetSheet<LCACommonSurveyForm_WorkHours>(parameters.LCAID);
if (parameters.peopleType == "直接人員")
{
WorkHours = WorkHours.Where(x => x.Type == LCACommonSurveyForm_WorkHours.WorkHourType.Direct);
if (!WorkHours.Any())
{
var workHoursList = new List<LCACommonSurveyForm_WorkHours>();
for (int i = 1; i <= 12; i++)
{
workHoursList.Add(new LCACommonSurveyForm_WorkHours
{
Type = LCACommonSurveyForm_WorkHours.WorkHourType.Direct,
Index = i,
LCAID = parameters.LCAID
});
}
WorkHours.Concat(workHoursList);
_db.LCACommonSurveyForm_WorkHour.AddRange(workHoursList);
_db.SaveChanges();
}
}
else if (parameters.peopleType == "間接人員")
{
WorkHours = WorkHours.Where(x => x.Type == LCACommonSurveyForm_WorkHours.WorkHourType.Indirect);
if (!WorkHours.Any())
{
var workHoursList = new List<LCACommonSurveyForm_WorkHours>();
for (int i = 1; i <= 12; i++)
{
workHoursList.Add(new LCACommonSurveyForm_WorkHours
{
Type = LCACommonSurveyForm_WorkHours.WorkHourType.Indirect,
Index = i,
LCAID = parameters.LCAID
});
}
WorkHours.Concat(workHoursList);
_db.LCACommonSurveyForm_WorkHour.AddRange(workHoursList);
_db.SaveChanges();
}
}
ret = new
{
WorkHours,
SheetHeader = service.GetSheetHeader(parameters.LCAID, Weee.Models.Paramemter.Categories.Septic),
Factors
};
}
else
{
ret = new
{
WorkHours = service.GetSheet<LCACommonSurveyForm_WorkHours>(parameters.LCAID).Where(x => x.Type == LCACommonSurveyForm_WorkHours.WorkHourType.WorkHourPeopleType && x.WorkHourPeopleType == parameters.peopleType),
SheetHeader = service.GetSheetHeader(parameters.LCAID, Weee.Models.Paramemter.Categories.Septic),
Factors
};
}
return ret;
}
[Route("api/WorkHour/GetAllWorkHours/{LCAID}")]
//[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not
public object GetAll(int LCAID)
{
if (HttpContext.Current.Session != null)
HttpContext.Current.Session["LCAid"] = LCAID;
Dictionary<string, decimal> dictFactors = service.getWorkhourFactors();
List<string> Factors = new List<string>();
foreach (var rec in dictFactors)
{
Factors.Add(rec.Key);
Factors.Add(rec.Value.ToString("0.0000000000"));
}
var GWP_CH4 = (from a in _db.AssessmentReportGWP
where a.chemicalFormula == "CH4"
select a).FirstOrDefault();
var arVersion = (from a in _db.LCAs // uow.GetDBcontext().LCAs
where a.ID == LCAID
select a.ARversion).FirstOrDefault();
if (GWP_CH4 != null && arVersion != null)
{
Factors.Add("GWP");
if (arVersion == "AR4")
Factors.Add(GWP_CH4.AR4GWP100.ToString("0.0000000000"));
else if (arVersion == "AR5")
Factors.Add(GWP_CH4.AR5GWP100.ToString("0.0000000000"));
else if (arVersion == "AR6")
Factors.Add(GWP_CH4.AR6GWP100.ToString("0.0000000000"));
}
if (Factors.Count < 12)
{
for (int i = Factors.Count; i < 12; i++)
Factors.Add("");
}
var ret = new
{
WorkHours = service.GetSheet<LCACommonSurveyForm_WorkHours>(LCAID).OrderBy(x=>x.Type).ThenBy(x=>x.WorkHourPeopleType).ThenBy(x=>x.Index),
SheetHeader = service.GetSheetHeader(LCAID, Weee.Models.Paramemter.Categories.Septic),
Factors
};
return ret;
}
[Route("api/WorkHour/GetPeopleTypes/{LCAID}")]
public object GetPeopleTypes(int LCAID)
{
if (HttpContext.Current.Session != null)
HttpContext.Current.Session["LCAid"] = LCAID;
var PeopleTypes = _db.LCACommonSurveyForm_WorkHour.Where(x => x.LCAID == LCAID && x.WorkHourPeopleType != null).Select(x => x.WorkHourPeopleType).Distinct().ToList();
if (!PeopleTypes.Contains("間接人員"))
{
PeopleTypes.Insert(0, "間接人員");
}
if (!PeopleTypes.Contains("直接人員"))
{
PeopleTypes.Insert(0, "直接人員");
}
object ret = new
{
PeopleTypes
};
return ret;
}
public class PeopleTypesParameters
{
public int LCAID { get; set; }
public String peopleType { get; set; }
}
[Route("api/WorkHour/CreatePeopleTypes")]
[HttpPost]
public object CreatePeopleTypes(PeopleTypesParameters parameters)
{
if (HttpContext.Current.Session != null)
HttpContext.Current.Session["LCAid"] = parameters.LCAID;
if(_db.LCACommonSurveyForm_WorkHour.Where(x => x.LCAID == parameters.LCAID && x.WorkHourPeopleType == parameters.peopleType).Count() > 0)
{
//return null;
}
else
{
for (int i = 1; i <= 12; i++)
{
var workHour = new LCACommonSurveyForm_WorkHours();
workHour.Index = i;
workHour.WorkHourPeopleType = parameters.peopleType;
workHour.Type = LCACommonSurveyForm_WorkHours.WorkHourType.WorkHourPeopleType;
workHour.LCAID = parameters.LCAID;
_db.LCACommonSurveyForm_WorkHour.Add(workHour);
}
_db.SaveChanges();
}
var PeopleTypes = service.GetSheet<LCACommonSurveyForm_WorkHours>(parameters.LCAID).Where(x => x.WorkHourPeopleType != null).Select(x => x.WorkHourPeopleType).Distinct().ToList();
if (!PeopleTypes.Contains("間接人員"))
{
PeopleTypes.Insert(0, "間接人員");
}
if (!PeopleTypes.Contains("直接人員"))
{
PeopleTypes.Insert(0, "直接人員");
}
object ret = new
{
PeopleTypes
};
return ret;
}
[Route("api/WorkHour/DeletePeopleTypes")]
[HttpPost]
public object DeletePeopleTypes(PeopleTypesParameters parameters)
{
if (HttpContext.Current.Session != null)
HttpContext.Current.Session["LCAid"] = parameters.LCAID;
if (parameters.peopleType != "直接人員" && parameters.peopleType != "間接人員")
{
var workHourList = _db.LCACommonSurveyForm_WorkHour.Where(x => x.LCAID == parameters.LCAID && x.WorkHourPeopleType == parameters.peopleType);
if (workHourList.Count() > 0)
{
_db.LCACommonSurveyForm_WorkHour.RemoveRange(workHourList);
_db.SaveChanges();
}
}
var PeopleTypes = service.GetSheet<LCACommonSurveyForm_WorkHours>(parameters.LCAID).Where(x => x.WorkHourPeopleType != null).Select(x => x.WorkHourPeopleType).Distinct().ToList();
if (!PeopleTypes.Contains("間接人員"))
{
PeopleTypes.Insert(0, "間接人員");
}
if (!PeopleTypes.Contains("直接人員"))
{
PeopleTypes.Insert(0, "直接人員");
}
object ret = new
{
PeopleTypes
};
return ret;
}
[Route("api/WorkHour/SaveSheetHeader")]
[HttpPost]
public HttpResponseMessage SaveHeader(SheetHeader header)
{
return this.SaveSheetHeader(service, Weee.Models.Paramemter.Categories.Septic, header);
}
[Route("api/WorkHour/SaveWorkHours")]
[HttpPost]
[ApiEditableAttribute("ToBeSave", true)]
public object SaveAll(IEnumerable<LCACommonSurveyForm_WorkHours> ToBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
if (ToBeSave == null || ToBeSave.Count() == 0)
return null;
int LCAID = ToBeSave.First().LCAID;
var spvalue = _db.NonYearlyParameters
.Where(y => y.Type.Category.Category == Weee.Models.Paramemter.Categories.Septic)
.Where(y => !y.IsHistory)
.Where(y => y.Type.DisplayNameTW != "GWP")
.AsNoTracking()
.Select(y => y.Value)
.AsEnumerable()
.Multiply();
var GWP_CH4 = (from a in _db.AssessmentReportGWP
where a.chemicalFormula == "CH4"
select a).FirstOrDefault();
var arVersion = (from a in _db.LCAs // uow.GetDBcontext().LCAs
where a.ID == LCAID
select a.ARversion).FirstOrDefault();
if (GWP_CH4 != null && arVersion != null)
{
if (arVersion == "AR4")
spvalue *= GWP_CH4.AR4GWP100;
else if (arVersion == "AR5")
spvalue *= GWP_CH4.AR5GWP100;
else if (arVersion == "AR6")
spvalue *= GWP_CH4.AR6GWP100;
}
// service.SaveMonthlyData(workhour);
foreach (var item in ToBeSave)
{
var entity = _db.LCACommonSurveyForm_WorkHour.Where(x => x.ID == item.ID).FirstOrDefault();
if(entity != null)
{
_db.LCACommonSurveyForm_WorkHour.Remove(entity);
}
item.ID = 0;
item.WorkerNumber = Math.Round(item.WorkerNumber, 4);
item.WorkDay = Math.Round(item.WorkDay, 4);
item.AverageHourPerDay = Math.Round(item.AverageHourPerDay, 4);
item.UpdateKgCO2e(spvalue);
//the kgco2e should devide by 8 , 因為係數中有一個單位為 每人每日(8小時)汙水量 , 而非每人每小時汙水量
//而工時的Scalar 單位為 "小時"
item.KgCO2e *= 0.125m;
}
_db.SaveChanges();
//service.DeleteSheet<LCACommonSurveyForm_WorkHours>(LCAID);
var result = service.SaveSheet(ToBeSave);
var qry = (from a in _db.LCAs
where a.ID == LCAID
select a).FirstOrDefault();
if (qry != null)
{
qry.activityDataTypeWorkHour = ActivityDataType.;
qry.emitParaTypeWorkHour = EmitParaType.;
_db.SaveChanges();
}
return new { result = result };
}
[Route("api/WorkHour/SaveAllTypesWorkHours")]
[HttpPost]
[ApiEditableAttribute("ToBeSave", true)]
public object SaveAllTypes(IEnumerable<LCACommonSurveyForm_WorkHours> ToBeSave)
{
if (!ModelState.IsValid)
{
throw new Exception("error");
}
if (ToBeSave == null || ToBeSave.Count() == 0)
return null;
int LCAID = ToBeSave.First().LCAID;
var spvalue = _db.NonYearlyParameters
.Where(y => y.Type.Category.Category == Weee.Models.Paramemter.Categories.Septic)
.Where(y => !y.IsHistory)
.Where(y => y.Type.DisplayNameTW != "GWP")
.AsNoTracking()
.Select(y => y.Value)
.AsEnumerable()
.Multiply();
var GWP_CH4 = (from a in _db.AssessmentReportGWP
where a.chemicalFormula == "CH4"
select a).FirstOrDefault();
var arVersion = (from a in _db.LCAs // uow.GetDBcontext().LCAs
where a.ID == LCAID
select a.ARversion).FirstOrDefault();
if (GWP_CH4 != null && arVersion != null)
{
if (arVersion == "AR4")
spvalue *= GWP_CH4.AR4GWP100;
else if (arVersion == "AR5")
spvalue *= GWP_CH4.AR5GWP100;
else if (arVersion == "AR6")
spvalue *= GWP_CH4.AR6GWP100;
}
// 全部清空
_db.LCACommonSurveyForm_WorkHour.RemoveRange(_db.LCACommonSurveyForm_WorkHour.Where(x => x.LCAID == LCAID).ToList());
// 再逐個類別匯入
foreach (var group in ToBeSave.GroupBy(x=>new { x.Type, x.WorkHourPeopleType }).ToList())
{
var items = group.ToList();
var type = group.Key.Type;
var workHourPeopleType = group.Key.WorkHourPeopleType;
var cnt = items.Count();
for (var i = 0; i < items.Count() && i < 12; i++)
{
items[i].ID = 0;
items[i].Index = i + 1;
items[i].WorkerNumber = Math.Round(items[i].WorkerNumber, 4);
items[i].WorkDay = Math.Round(items[i].WorkDay, 4);
items[i].AverageHourPerDay = Math.Round(items[i].AverageHourPerDay, 4);
items[i].UpdateKgCO2e(spvalue);
//the kgco2e should devide by 8 , 因為係數中有一個單位為 每人每日(8小時)汙水量 , 而非每人每小時汙水量
//而工時的Scalar 單位為 "小時"
items[i].KgCO2e *= 0.125m;
}
for(var i = cnt; i < 12; i++)
{
items.Add(new LCACommonSurveyForm_WorkHours()
{
ID = 0,
LCAID = LCAID,
Index = i + 1,
Type = type,
WorkHourPeopleType = workHourPeopleType,
});
}
_db.LCACommonSurveyForm_WorkHour.AddRange(items);
}
_db.SaveChanges();
var PeopleTypes = _db.LCACommonSurveyForm_WorkHour.Where(x => x.LCAID == LCAID && x.WorkHourPeopleType != null).Select(x => x.WorkHourPeopleType).Distinct().ToList();
if (!PeopleTypes.Contains("間接人員"))
{
PeopleTypes.Insert(0, "間接人員");
}
if (!PeopleTypes.Contains("直接人員"))
{
PeopleTypes.Insert(0, "直接人員");
}
object ret = new
{
PeopleTypes
};
return ret;
}
//[Route("api/WorkHour/SaveWorkHour/{LCAID}/{id}")]
//[HttpPost]
//public int Save(int LCAID, int id, WorkHour workhour)
//{
// if (!ModelState.IsValid) { return -1; }
// if (id != workhour.ID || LCAID != workhour.LCAID) { return -1; }
// return workhour.ID;
//}
//[Route("api/WorkHour/DeleteWorkHours/{LCAID}")]
//[HttpDelete]
//public IHttpActionResult DeleteAll(int LCAID)
//{
// service.DeleteSheet<WorkHour>(LCAID);
// return Ok();
//}
}
}