212 lines
9.3 KiB
C#
212 lines
9.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data.Entity;
|
|||
|
using System.Data.SqlClient;
|
|||
|
using System.Linq;
|
|||
|
using System.Web;
|
|||
|
using Weee.DAL;
|
|||
|
using Weee.Models;
|
|||
|
using Weee.ViewModels.DataTransferObject;
|
|||
|
|
|||
|
namespace Weee.Service
|
|||
|
{
|
|||
|
public class AssmtFactorDataService : WeeeLCADataService
|
|||
|
{
|
|||
|
public AssmtFactorDataService(WeeeDataContext db)
|
|||
|
: base(db)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public IQueryable<LCARiskAssmtSurveyForm_AssmtFactor> GetList(int LCAID)
|
|||
|
{
|
|||
|
if (!AuthorizedLCAs.Contains(LCAID)) throw new Exception("not authorized");
|
|||
|
return _db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Include(x => x.FactorScores).Where(x => x.LCAID == LCAID)
|
|||
|
.OrderByDescending(x => x.ID);
|
|||
|
}
|
|||
|
|
|||
|
public LCARiskAssmtSurveyForm_AssmtFactor Save(AssmtFactorDTO toBeSave)
|
|||
|
{
|
|||
|
CheckScore(toBeSave);
|
|||
|
|
|||
|
var user = GetUserContext();
|
|||
|
|
|||
|
if (!AuthorizedLCAs.Contains(toBeSave.LCAID)) throw new Exception("not authorized");
|
|||
|
var entry = _db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().FirstOrDefault(x => x.ID == toBeSave.ID);
|
|||
|
var isEdited = false;
|
|||
|
var assmtFactorIDList = new List<int>();
|
|||
|
if (entry == null)
|
|||
|
{
|
|||
|
if (_db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Any(x => x.LCAID == toBeSave.LCAID && x.FactorName == toBeSave.FactorName))
|
|||
|
{
|
|||
|
throw new Exception($"评估因子名称不能重复");
|
|||
|
}
|
|||
|
|
|||
|
entry = new LCARiskAssmtSurveyForm_AssmtFactor()
|
|||
|
{
|
|||
|
LCAID = toBeSave.LCAID,
|
|||
|
FactorName = toBeSave.FactorName,
|
|||
|
Comment = toBeSave.Comment
|
|||
|
};
|
|||
|
|
|||
|
foreach (var factorScore in toBeSave.FactorScores)
|
|||
|
{
|
|||
|
entry.FactorScores.Add(new LCARiskAssmtSurveyForm_AssmtFactorScore()
|
|||
|
{
|
|||
|
OptionDescription = factorScore.OptionDescription,
|
|||
|
OptionScore = factorScore.OptionScore,
|
|||
|
Comment = factorScore.Comment,
|
|||
|
CreatedBy = user.Id,
|
|||
|
CreatedDate = DateTime.Now
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
_db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Add(entry);
|
|||
|
_db.Entry(entry).State = EntityState.Added;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (_db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Any(x => x.ID != entry.ID && x.LCAID == toBeSave.LCAID && x.FactorName == toBeSave.FactorName))
|
|||
|
{
|
|||
|
throw new Exception($"评估因子名称不能重复");
|
|||
|
}
|
|||
|
|
|||
|
entry.FactorName = toBeSave.FactorName;
|
|||
|
entry.Comment = toBeSave.Comment;
|
|||
|
entry.ModifiedBy = user.Id;
|
|||
|
entry.ModifiedDate = DateTime.Now;
|
|||
|
|
|||
|
var factorScores = _db.Set<LCARiskAssmtSurveyForm_AssmtFactorScore>().Where(x => x.FactorId == entry.ID).ToList();
|
|||
|
var count = factorScores.Count;
|
|||
|
|
|||
|
for (int i = count - 1; i >= 0; i--)
|
|||
|
{
|
|||
|
var factorScore = factorScores.ElementAt(i);
|
|||
|
|
|||
|
if (!toBeSave.FactorScores.Any(x => x.ID == factorScore.ID))
|
|||
|
{
|
|||
|
assmtFactorIDList.Add(factorScore.FactorId);
|
|||
|
isEdited = true;
|
|||
|
factorScores.Remove(factorScore);
|
|||
|
|
|||
|
_db.Set<LCARiskAssmtSurveyForm_AssmtFactorScore>().Attach(factorScore);
|
|||
|
_db.Entry(factorScore).State = EntityState.Deleted;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
foreach (var factorScore in toBeSave.FactorScores)
|
|||
|
{
|
|||
|
if (factorScore.ID > 0)
|
|||
|
{
|
|||
|
assmtFactorIDList.Add(factorScore.FactorId);
|
|||
|
isEdited = true;
|
|||
|
var updatingFactorScore = factorScores.First(fs => fs.ID == factorScore.ID);
|
|||
|
|
|||
|
updatingFactorScore.OptionDescription = factorScore.OptionDescription;
|
|||
|
updatingFactorScore.OptionScore = factorScore.OptionScore;
|
|||
|
updatingFactorScore.Comment = factorScore.Comment;
|
|||
|
updatingFactorScore.ModifiedBy = user.Id;
|
|||
|
updatingFactorScore.ModifiedDate = DateTime.Now;
|
|||
|
|
|||
|
_db.Set<LCARiskAssmtSurveyForm_AssmtFactorScore>().Attach(updatingFactorScore);
|
|||
|
_db.Entry(updatingFactorScore).State = EntityState.Modified;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var addingFactorScore = new LCARiskAssmtSurveyForm_AssmtFactorScore()
|
|||
|
{
|
|||
|
FactorId = entry.ID,
|
|||
|
OptionDescription = factorScore.OptionDescription,
|
|||
|
OptionScore = factorScore.OptionScore,
|
|||
|
Comment = factorScore.Comment,
|
|||
|
CreatedBy = user.Id,
|
|||
|
CreatedDate = DateTime.Now
|
|||
|
};
|
|||
|
|
|||
|
_db.Set<LCARiskAssmtSurveyForm_AssmtFactorScore>().Attach(addingFactorScore);
|
|||
|
_db.Entry(addingFactorScore).State = EntityState.Added;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
_db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().Attach(entry);
|
|||
|
_db.Entry(entry).State = EntityState.Modified;
|
|||
|
_db.Entry(entry).Property(x => x.LCAID).IsModified = false;
|
|||
|
}
|
|||
|
|
|||
|
_db.SaveChanges();
|
|||
|
// 將新增的評估因子更新到顯著性評估項目中
|
|||
|
string sql = $@"
|
|||
|
insert into LCARiskAssmtSurveyForm_SignificanceAssmtFactor (SignificanceAssmtID, AssmtFactorID, Score)
|
|||
|
select a.SignificanceAssmtID, a.AssmtFactorID, 0 Score
|
|||
|
from
|
|||
|
(select a.ID AssmtFactorID, s.id SignificanceAssmtID
|
|||
|
from LCARiskAssmtSurveyForm_AssmtFactor a, LCARiskAssmtSurveyForm_SignificanceAssmt s
|
|||
|
where a.LCAID=@LCAID and s.LCAID=@LCAID) a
|
|||
|
left join (
|
|||
|
select SignificanceAssmtID, AssmtFactorID
|
|||
|
from LCARiskAssmtSurveyForm_SignificanceAssmtFactor
|
|||
|
group by SignificanceAssmtID, AssmtFactorID
|
|||
|
) c on a.AssmtFactorID=c.AssmtFactorID and a.SignificanceAssmtID=c.SignificanceAssmtID
|
|||
|
where c.AssmtFactorID is null or c.SignificanceAssmtID is null
|
|||
|
";
|
|||
|
int rows = _db.Database.ExecuteSqlCommand(sql, new SqlParameter("@LCAID", toBeSave.LCAID));
|
|||
|
UpdateLCA_SignificanceAssmts(toBeSave.LCAID, assmtFactorIDList);
|
|||
|
return entry;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckScore(AssmtFactorDTO toBeSave)
|
|||
|
{
|
|||
|
if (toBeSave.FactorScores.Any(x => x.OptionScore < 1 || x.OptionScore > 10))
|
|||
|
{
|
|||
|
throw new Exception($"评分范围1~10:{nameof(toBeSave.FactorScores)}");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Delete(int ID)
|
|||
|
{
|
|||
|
var entry = _db.Set<LCARiskAssmtSurveyForm_AssmtFactor>().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();
|
|||
|
|
|||
|
|
|||
|
// 將刪除的評估因子更新到顯著性評估項目中
|
|||
|
string sql = $@"
|
|||
|
delete from LCARiskAssmtSurveyForm_SignificanceAssmtFactor
|
|||
|
where LCARiskAssmtSurveyForm_SignificanceAssmtFactor.AssmtFactorID = @ID
|
|||
|
and LCARiskAssmtSurveyForm_SignificanceAssmtFactor.SignificanceAssmtID in (select id from LCARiskAssmtSurveyForm_SignificanceAssmt where LCAID = @LCAID)
|
|||
|
";
|
|||
|
object[] parameters = { new SqlParameter("@ID", ID), new SqlParameter("@LCAID", entry.LCAID) };
|
|||
|
int rows = _db.Database.ExecuteSqlCommand(sql, parameters);
|
|||
|
UpdateLCA_SignificanceAssmts(entry.LCAID,new List<int>());
|
|||
|
}
|
|||
|
|
|||
|
public void UpdateLCA_SignificanceAssmts(int LCAID,List<int> assmtFacotrIDs)
|
|||
|
{
|
|||
|
var significanceAssmts = _db.LCARiskAssmtSurveyForm_SignificanceAssmts.Where(x => x.LCAID == LCAID).ToList();
|
|||
|
var factors = (from saf in _db.LCARiskAssmtSurveyForm_SignificanceAssmtFactors
|
|||
|
join sa in _db.LCARiskAssmtSurveyForm_SignificanceAssmts on saf.SignificanceAssmtID equals sa.ID
|
|||
|
where sa.LCAID == LCAID
|
|||
|
select saf).ToList();
|
|||
|
var gradingScore = _db.LCAs.Where(x => x.ID == LCAID).First().GradingScore;
|
|||
|
foreach (var factor in factors)
|
|||
|
{
|
|||
|
if (assmtFacotrIDs.Contains(factor.AssmtFactorID))
|
|||
|
factor.Score = 0;
|
|||
|
}
|
|||
|
foreach (var significanceAssmt in significanceAssmts)
|
|||
|
{
|
|||
|
var thisFactors = factors.Where(x => x.SignificanceAssmtID == significanceAssmt.ID).Select(x => x.Score);
|
|||
|
if (thisFactors.Count() == 0)
|
|||
|
significanceAssmt.Score = 0;
|
|||
|
else
|
|||
|
significanceAssmt.Score = thisFactors.Aggregate((m, n) => m * n);
|
|||
|
significanceAssmt.Significance = significanceAssmt.Score >= gradingScore ? 1 : 0;
|
|||
|
}
|
|||
|
_db.SaveChanges();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|