demo20230512/Service/RiskAssmt/SignificanceAssmtDataService.cs
2023-05-12 10:20:28 +08:00

231 lines
9.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Web;
using Weee.DAL;
using Weee.Models;
using Weee.ViewModels.Lca.LCARiskAssmtSurveyForm;
namespace Weee.Service
{
public class SignificanceAssmtDataService : WeeeLCADataService
{
public SignificanceAssmtDataService(WeeeDataContext db)
: base(db)
{
}
public IQueryable<LCARiskAssmtSurveyForm_SignificanceAssmtQuery> GetList(int LCAID)
//public IQueryable<LCARiskAssmtSurveyForm_SignificanceAssmt> GetList(int LCAID)
{
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
var qry = _db.Set<LCARiskAssmtSurveyForm_SignificanceAssmt>().Include(x => x.FactorScores).Where(x => x.LCAID == LCAID)
.OrderByDescending(x => x.ID);
//var qry2 = qry.ToList();
//return qry;
var qry2 = (from a in qry
join b in _db.LCARiskAssmtSurveyForm_LifecycleAssmts on
new { LCAID = a.LCAID, LifeCycleStage = a.LifeCycleStage, GHGEvaluateItem = a.GHGEvaluateItem }
equals new { LCAID = b.LCAID, LifeCycleStage = b.LifeCycleStage, GHGEvaluateItem = b.GHGEvaluateItem }
select new LCARiskAssmtSurveyForm_SignificanceAssmtQuery
{
emitPercentage = b.Percentage,
ID = a.ID,
LCAID = a.LCAID,
Comment = a.Comment,
CreatedBy = a.CreatedBy,
CreatedDate = a.CreatedDate,
FactorScores = a.FactorScores,
ModifiedBy = a.ModifiedBy,
ModifiedDate = a.ModifiedDate,
OwnerLCA = a.OwnerLCA,
LifeCycleStage = a.LifeCycleStage,
Category = a.Category,
GHGEvaluateItem = a.GHGEvaluateItem,
Score = a.Score,
Significance = a.Significance
});
return qry2;
}
public IQueryable<LCARiskAssmtSurveyForm_AssmtFactor> GetAssmtFactors(int LCAID)
{
return _db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Include(x => x.FactorScores).Where(x => x.LCAID == LCAID);
}
public void Sync(int LCAID)
{
// CFT-511 由主表同步到顯著性評估,要刪除已不存在的項目
string sql = $@"
delete s
from LCARiskAssmtSurveyForm_SignificanceAssmt s
left join LCARiskAssmtSurveyForm_LifecycleAssmt l on s.LCAID=l.LCAID and s.LifeCycleStage=l.LifeCycleStage and s.GHGEvaluateItem=l.GHGEvaluateItem
where (l.LCAID is null or l.LifeCycleStage is null or l.GHGEvaluateItem is null or l.GHGinventoryItemNo is null or len(GHGinventoryItemNo)=0) and s.LCAID=@LCAID
";
int rows = _db.Database.ExecuteSqlCommand(sql, new SqlParameter("@LCAID", LCAID));
sql = $@"
delete s
from LCARiskAssmtSurveyForm_SignificanceAssmtFactor s
join LCARiskAssmtSurveyForm_SignificanceAssmt a on s.SignificanceAssmtID=a.ID
left join LCARiskAssmtSurveyForm_AssmtFactor f on f.ID=s.AssmtFactorID
where f.id is null and a.LCAID=@LCAID
";
rows = _db.Database.ExecuteSqlCommand(sql, new SqlParameter("@LCAID", LCAID));
var user = GetUserContext();
var list = GetList(LCAID).ToList(); // destination
var analyses = _db.Set<LCARiskAssmtSurveyForm_LifecycleAssmt>()
.Where(x => x.LCAID == LCAID && x.GHGinventoryItemNo!=null && x.GHGinventoryItemNo.Length>0).ToList();
var factors = _db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Where(x => x.LCAID == LCAID).ToList();
if (analyses.Count == 0)
{
throw new Exception("从生命週期流程圖及評估複製資料失败!");
}
else if (factors.Count < 2)
{
throw new Exception("請最少設定兩個以上的評估因子,才可以同步!");
}
else
{
foreach (var lifecycleAssmt in analyses) // source
{
if (!list.Any(s => s.LifeCycleStage == lifecycleAssmt.LifeCycleStage && s.GHGEvaluateItem == lifecycleAssmt.GHGEvaluateItem))
{
int parsedCategory = 0;
int.TryParse(lifecycleAssmt.category, out parsedCategory);
var toBeAdd = new LCARiskAssmtSurveyForm_SignificanceAssmt()
{
LCAID = LCAID,
LifeCycleStage = lifecycleAssmt.LifeCycleStage,
GHGEvaluateItem = lifecycleAssmt.GHGEvaluateItem,
Category= parsedCategory,//lifecycleAssmt.category,
CreatedBy = user.Id,
CreatedDate = DateTime.Now
};
//SetCategory(toBeAdd);
foreach (var factor in factors)
{
toBeAdd.FactorScores.Add(new LCARiskAssmtSurveyForm_SignificanceAssmtFactor()
{
AssmtFactorID = factor.ID
});
}
_db.Set<LCARiskAssmtSurveyForm_SignificanceAssmt>().Add(toBeAdd);
_db.Entry(toBeAdd).State = EntityState.Added;
}
}
_db.SaveChanges();
}
}
//private void SetCategory(LCARiskAssmtSurveyForm_SignificanceAssmt significanceAssmt)
//{
// /*
// * 1. 原物料取得category 4
// * 2. 上游運輸, category 3
// * 3. 運作支援及服務, 僅外購電力為category 2, 其餘為category 4
// * 4. 下游運輸, category 3
// * 5. 產品使用及廢棄, category 5
// * 6. 廢棄物處理, category 4
// */
// if (significanceAssmt.LifeCycleStage == 1)
// {
// significanceAssmt.Category = 4;
// }
// else if (significanceAssmt.LifeCycleStage == 2)
// {
// significanceAssmt.Category = 3;
// }
// else if (significanceAssmt.LifeCycleStage == 3)
// {
// significanceAssmt.Category = IsPurchasedElectricity(significanceAssmt.GHGEvaluateItem) ? 2 : 4;
// }
// else if (significanceAssmt.LifeCycleStage == 4)
// {
// significanceAssmt.Category = 3;
// }
// else if (significanceAssmt.LifeCycleStage == 5)
// {
// significanceAssmt.Category = 5;
// }
// else if (significanceAssmt.LifeCycleStage == 6)
// {
// significanceAssmt.Category = 4;
// }
//}
private bool IsPurchasedElectricity(string ghgEvaluateItem)
{
return !string.IsNullOrEmpty(ghgEvaluateItem) && ghgEvaluateItem.Contains("外購電力") && ghgEvaluateItem.ToUpper().StartsWith("C2");
}
public void UpdateGradingScore(int LCAID, int GradingScore)
{
var lca = GetLCA(LCAID);
var list = GetList(LCAID).ToList();
foreach (var significanceAssmt in list)
{
significanceAssmt.Significance = significanceAssmt.Score >= GradingScore ? 1 : 0;
_db.LCARiskAssmtSurveyForm_SignificanceAssmts.AddOrUpdate(significanceAssmt);
}
lca.GradingScore = GradingScore;
_db.SaveChanges();
}
public void UpdateListDefaultPurpose(int LCAID, string ListDefaultPurpose)
{
var lca = GetLCA(LCAID);
lca.ListDefaultPurpose = ListDefaultPurpose;
int rows = _db.SaveChanges();
Thread.Sleep(0);
}
public LCARiskAssmtSurveyForm_SignificanceAssmt Save(LCARiskAssmtSurveyForm_SignificanceAssmt toBeSave)
{
var user = GetUserContext();
if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized");
var entry = _db.Entry(toBeSave);
if (toBeSave.ID == 0)
{
// 資料來自於生命週期流程圖及評估
toBeSave.CreatedBy = user.Id;
toBeSave.CreatedDate = DateTime.Now;
entry.State = EntityState.Added;
}
else
{
foreach (var factorScore in toBeSave.FactorScores)
{
_db.Entry(factorScore).State = EntityState.Modified;
}
toBeSave.ModifiedBy = user.Id;
toBeSave.ModifiedDate = DateTime.Now;
entry.State = EntityState.Modified;
entry.Property(x => x.LCAID).IsModified = false;
}
_db.SaveChanges();
return toBeSave;
}
public void Delete(int ID)
{
var entry = _db.Set<LCARiskAssmtSurveyForm_SignificanceAssmt>().Where(x => x.ID == ID).Single();
var LCA = GetLCA(entry.LCAID);
if (LCA.Status != LCAStatus.New && LCA.Status != LCAStatus.Processing) throw new Exception("Business logic error , should not delete data in this status: " + LCA.Status.ToString());
var ToBeDelete = _db.Entry(entry);
ToBeDelete.State = EntityState.Deleted;
_db.SaveChanges();
}
}
}