using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using Weee.DAL; using Weee.Models.Paramemter; using Weee.Models; using System.Data.Entity.Core.Objects; using Qcarbon.ViewModels.DTO; using CScommon; using Resources; using System.Globalization; using Qcarbon.Database.Lca.Org36; using NLog; namespace Weee.Service { public class WeeeEmployeeCommutingService { private WeeeDataContext _db; private Logger log; public WeeeEmployeeCommutingService(WeeeDataContext db) { _db = db; log = NLog.LogManager.GetCurrentClassLogger(); } public List GetByLCAID(int LCAID) { var re = new List(); var dbList = _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Where(x=>x.LCAID == LCAID).ToList(); if (dbList != null && dbList.Count() > 0) re = JsonUtl.jsonCopy, List>(dbList); return re; } public int SaveEmployeeCommuting(EmployeeCommutingViewModel sour) { int re = 0; var roundTripList = new List() { 1, 2, 3}; if (sour == null || sour.TotalEmployees == 0 || sour.WorkingDays == 0 || sour.AverageMovingDistance == 0 || sour.Coefficient == 0 || sour.RroundTrip == 0) throw new Exception("所有欄位不可為0"); if (!CkDecimalLength(sour.AverageMovingDistance)) throw new Exception("移動距離超過範圍"); if (!CkDecimalLength(sour.Coefficient)) throw new Exception("係數超過範圍"); if(!roundTripList.Any(x=>x == sour.RroundTrip)) throw new Exception("往返只可以為1或2"); decimal _PersonDayKm = GetPersonDayKm(sour); if (!CkDecimalLength(_PersonDayKm)) throw new Exception("人天*Km超過範圍"); decimal _KgCO2e = _PersonDayKm * sour.Coefficient; if (!CkDecimalLength(_KgCO2e)) throw new Exception("KgCO2e超過範圍"); //string logicCk = CkSaveLogic(sour); //if(!string.IsNullOrWhiteSpace(logicCk)) // throw new Exception(logicCk); var trans = _db.Database.BeginTransaction(); try { if (sour.Id == 0) { var dbIt = new LCARiskAssmtSurveyForm_EmployeeCommuting() { Guid = Guid.NewGuid(), LCAID = sour.LCAID, Commuting = sour.Commuting, TotalEmployees = sour.TotalEmployees, WorkingDays = sour.WorkingDays, AverageMovingDistance = sour.AverageMovingDistance, PersonDayKm = _PersonDayKm, Coefficient = sour.Coefficient, CreatedDate = DateTime.Now, CreatedBy = sour.ModifiedBy, ModifiedDate = DateTime.Now, ModifiedBy = sour.ModifiedBy, KgCO2e = _KgCO2e, RroundTrip = sour.RroundTrip, MovingScenario = sour.MovingScenario, CoefficientNote = sour.CoefficientNote, CoefficienUnit = sour.CoefficienUnit }; _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Add(dbIt); _db.SaveChanges(); re = dbIt.Id; } else { var list = _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Where(x => x.Id == sour.Id).ToList(); if (list != null && list.Count() > 0) { re = sour.Id; var dbIt = list.FirstOrDefault(); dbIt.Commuting = sour.Commuting; dbIt.TotalEmployees = sour.TotalEmployees; dbIt.WorkingDays = sour.WorkingDays; dbIt.AverageMovingDistance = sour.AverageMovingDistance; dbIt.PersonDayKm = _PersonDayKm; dbIt.KgCO2e = _KgCO2e; dbIt.Coefficient = sour.Coefficient; dbIt.ModifiedBy = sour.ModifiedBy; dbIt.ModifiedDate = DateTime.Now; dbIt.RroundTrip = sour.RroundTrip; dbIt.MovingScenario = sour.MovingScenario; dbIt.CoefficientNote = sour.CoefficientNote; dbIt.CoefficienUnit = sour.CoefficienUnit; _db.SaveChanges(); } else throw new Exception("查無Id: " + sour.Id.ToString() + "資料"); } Org36RecalculateService o36rs = new Org36RecalculateService(_db); o36rs.lifeCycleSyncEmployeeCommuting(sour.LCAID); trans.Commit(); } catch(Exception ex) { trans.Rollback(); log.Error(ex); log.Error(CScommon.Exceptions.inner(ex)); log.Error(ex.StackTrace); throw; } return re; } public void DelEmployeeCommuting(int Id) { var trans = _db.Database.BeginTransaction(); try { var list = _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Where(x => x.Id == Id).ToList(); if (list != null && list.Count > 0) { var dbIt = list.FirstOrDefault(); int LCAID=dbIt.LCAID; _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Remove(dbIt); _db.SaveChanges(); Org36RecalculateService o36rs = new Org36RecalculateService(_db); o36rs.lifeCycleSyncEmployeeCommuting(LCAID); } trans.Commit(); } catch (Exception ex) { trans.Rollback(); log.Error(ex); log.Error(CScommon.Exceptions.inner(ex)); log.Error(ex.StackTrace); throw; } } public decimal GetPersonDayKm(EmployeeCommutingViewModel sour) { decimal re = 0; re = Convert.ToDecimal(sour.TotalEmployees) * Convert.ToDecimal(sour.WorkingDays) * sour.AverageMovingDistance; return re; } public decimal GetPersonDayKm(int TotalEmployees, int WorkingDays, decimal AverageMovingDistance) { decimal re = 0; re = Convert.ToDecimal(TotalEmployees) * Convert.ToDecimal(WorkingDays) * AverageMovingDistance; return re; } public List GetExcelColumnsNms() { var re = new List() { Resource.EmployeesNumber, Resource.WorkingDays, Resource.RoundTrip, Resource.MovingDistance, Resource.SituationDesc, Resource.Parameter, Resource.StaticLabelGlobal_Unit, Resource.Parameter + Resource.Explanation, Resource.KgCO2e }; return re; } public List GetCommutingKeyValue() { var re = new List() { new OptionKeyValueViewModel(){intValue = 1, itemNm = Resource.Drive_taxi}, new OptionKeyValueViewModel(){intValue = 2, itemNm = Resource.Drive_electric_car}, new OptionKeyValueViewModel(){intValue = 3, itemNm = Resource.Ride_motorcycle}, new OptionKeyValueViewModel(){intValue = 4, itemNm = Resource.Riding_motorbike}, new OptionKeyValueViewModel(){intValue = 5, itemNm = Resource.Take_bus}, new OptionKeyValueViewModel(){intValue = 6, itemNm = Resource.Take_MRT}, new OptionKeyValueViewModel(){intValue = 7, itemNm = Resource.Take_high_speed_rail}, }; return re; } public List GetRroundTripKeyValue() { var re = new List() { new OptionKeyValueViewModel(){intValue = 1, itemNm = Resource.EmployeeCommuting_Past}, new OptionKeyValueViewModel(){intValue = 2, itemNm = Resource.EmployeeCommuting_Back}, new OptionKeyValueViewModel(){intValue = 3, itemNm = Resource.EmployeeCommuting_Past + Resource.EmployeeCommuting_Back} }; return re; } public int GetCommutingValue(string sour) { int re = 0; if (!string.IsNullOrWhiteSpace(sour)) { var list = GetCommutingKeyValue().Where(x => x.itemNm == sour); if (list != null && list.Count() > 0) re = list.FirstOrDefault().intValue; } return re; } public int GetRroundTripValue(string sour) { int re = 0; if (!string.IsNullOrWhiteSpace(sour)) { var list = GetRroundTripKeyValue().Where(x => x.itemNm == sour); if (list != null && list.Count() > 0) re = list.FirstOrDefault().intValue; } return re; } public void SaveToDb(List sour) { if (sour != null && sour.Count() > 0) { var tbList = new List(); foreach (var it in sour) { var dbIt = new LCARiskAssmtSurveyForm_EmployeeCommuting() { Guid = it.Guid, LCAID = it.LCAID, Commuting = it.Commuting, TotalEmployees = it.TotalEmployees, WorkingDays = it.WorkingDays, RroundTrip = it.RroundTrip, AverageMovingDistance = it.AverageMovingDistance, MovingScenario = it.MovingScenario, Coefficient = it.Coefficient, CoefficienUnit = it.CoefficienUnit, CoefficientNote = it.CoefficientNote, PersonDayKm = it.PersonDayKm, KgCO2e = it.KgCO2e, CreatedDate = DateTime.Now, CreatedBy = it.ModifiedBy, ModifiedDate = DateTime.Now, ModifiedBy = it.ModifiedBy }; tbList.Add(dbIt); } _db.LCARiskAssmtSurveyForm_EmployeeCommuting.AddRange(tbList); _db.SaveChanges(); } } public List GetExlData(List sour, int LCAID, string ModifiedBy) { var re = new List(); if (sour != null && sour.Count > 0) { foreach (var it in sour) { var vIt = new EmployeeCommutingViewModel() { Guid = Guid.NewGuid(), LCAID = LCAID, Commuting = 0, TotalEmployees = Convert.ToInt32(it.StrTotalEmployees), WorkingDays = Convert.ToInt32(it.StrWorkingDays), RroundTrip = GetRroundTripValue(it.StrRroundTrip), AverageMovingDistance = Convert.ToDecimal(it.StrAverageMovingDistance), MovingScenario = it.MovingScenario, Coefficient = Convert.ToDecimal(it.StrCoefficient), CoefficienUnit = it.CoefficienUnit, CoefficientNote = it.CoefficientNote, KgCO2e = Convert.ToDecimal(it.StrKgCO2e), PersonDayKm = Convert.ToDecimal(it.StrPersonDayKm), ModifiedBy = ModifiedBy }; re.Add(vIt); } } return re; } public string ExlDataCk(List sour) { string errMsg = ""; if (sour == null || sour.Count == 0) errMsg = "匯入Excel不可為空"; else { var errList = new List(); for (int i = 0; i < sour.Count(); i++) { string rowErrMsg = "Line " + (i + 2).ToString() + " Error"; var it = sour[i]; bool rowCk = true; if (rowCk && string.IsNullOrWhiteSpace(it.StrTotalEmployees)) rowCk = false; if (rowCk && string.IsNullOrWhiteSpace(it.StrWorkingDays)) rowCk = false; if(rowCk && string.IsNullOrWhiteSpace(it.StrRroundTrip)) rowCk = false; if (rowCk && string.IsNullOrWhiteSpace(it.StrAverageMovingDistance)) rowCk = false; if (rowCk && string.IsNullOrWhiteSpace(it.StrCoefficient)) rowCk = false; var ritems = GetRroundTripKeyValue().Where(x => x.itemNm == it.StrRroundTrip).ToList(); if (ritems == null || ritems.Count == 0) { rowCk = false; rowErrMsg += ", RroundTrip"; } if (!Int32.TryParse(it.StrTotalEmployees, out int TotalEmployees) || TotalEmployees <= 0) rowCk = false; if (!Int32.TryParse(it.StrWorkingDays, out int WorkingDays) || WorkingDays <= 0) rowCk = false; var AverageMovingDistance = GetFromExcel(it.StrAverageMovingDistance); if (AverageMovingDistance <= 0) rowCk = false; if (!CkDecimalLength(AverageMovingDistance)) { rowCk = false; rowErrMsg += ", AverageMovingDistance"; } var Coefficient = GetFromExcel(it.StrCoefficient); if (Coefficient <= 0) rowCk = false; if (!CkDecimalLength(Coefficient)) { rowCk = false; rowErrMsg += ", Coefficient"; } decimal _PersonDayKm = GetPersonDayKm(TotalEmployees, WorkingDays, AverageMovingDistance); if (CkDecimalLength(_PersonDayKm)) sour[i].StrPersonDayKm = _PersonDayKm.ToString(); else { rowErrMsg += ", PersonDayKm:" + _PersonDayKm.ToString(); rowCk = false; } decimal _KgCO2e = _PersonDayKm * Coefficient; if (CkDecimalLength(_KgCO2e)) sour[i].StrKgCO2e = _KgCO2e.ToString(); else { rowErrMsg += ", KgCO2e:" + _KgCO2e.ToString(); rowCk = false; } if (!rowCk) errList.Add(rowErrMsg); } if (errList.Count() > 0) errMsg = String.Join(Environment.NewLine, errList); } return errMsg; } private decimal GetFromExcel(string sour) { if (string.IsNullOrWhiteSpace(sour)) return 0; if (!Decimal.TryParse(sour, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out decimal Value)) throw new Exception(sour + " not number"); return Value; } private bool CkDecimalLength(decimal sour) { string strDecimal = sour.ToString(); var StrNums = strDecimal.Split('.'); if (StrNums.Count() > 0) { foreach (var it in StrNums) { if (it.Length > 12) return false; } } return true; } public string CkSaveLogic(EmployeeCommutingViewModel sour) { string re = ""; string logicKeyErr = "已存在通勤方式,往返的組合"; if(sour.Id == 0) { var list = _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Where(x => x.LCAID == sour.LCAID && x.Commuting == sour.Commuting && x.RroundTrip == sour.RroundTrip).ToList(); if (list.Count() > 0) re = logicKeyErr; } else { var list = _db.LCARiskAssmtSurveyForm_EmployeeCommuting.Where(x => x.LCAID == sour.LCAID && x.Id != sour.Id && x.Commuting == sour.Commuting && x.RroundTrip == sour.RroundTrip).ToList(); if (list.Count() > 0) re = logicKeyErr; } return re; } } }