using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EMIS.DataLogic.Common.EvaluationManage;
using Bowin.Common.Linq.Entity;
using EMIS.Entities;
using EMIS.ViewModel;
using System.Linq.Expressions;
using EMIS.ViewModel.EvaluationManage;
using EMIS.CommonLogic.SystemServices;
using Bowin.Common.Linq;
namespace EMIS.CommonLogic.EvaluationManage
{
public class EvaluationTargetServices : BaseServices, IEvaluationTargetServices
{
public EvaluationTargetDAL evaluationTargetDAL { get; set; }
public ISerialNumberServices SerialNumberServices { get; set; }
///
/// 查询评价指标信息
///
/// 查询条件实体
/// 评价表名ID
/// 评分标准ID
/// 页码
/// 显示页数
///
public IGridResultSet GetEvaluationTargetViewGrid(ConfiguretView configuretView, Guid? tableID, Guid? normID, int pageIndex, int pageSize)
{
var query = evaluationTargetDAL.GetEvaluationTargetQueryable(x => true);
if (tableID.HasValue && tableID != Guid.Empty)
query = query.Where(x => x.TableID == tableID);
if (normID.HasValue && normID != Guid.Empty)
query = query.Where(x => x.NormID == normID);
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.Code).OrderBy(x => x.OrderID).ToGridResultSet(pageIndex, pageSize);
return query.OrderBy(x => x.Code).OrderBy(x => x.OrderID).ToGridResultSet(pageIndex, pageSize);
}
///
/// 查询评价指标信息
///
/// 查询条件实体
/// 评价表名ID
/// 评分标准ID
///
public List GetEvaluationTargetViewList(ConfiguretView configuretView, Guid? tableID, Guid? normID)
{
var query = evaluationTargetDAL.GetEvaluationTargetQueryable(x => true);
if (tableID.HasValue && tableID != Guid.Empty)
query = query.Where(x => x.TableID == tableID);
if (normID.HasValue && normID != Guid.Empty)
query = query.Where(x => x.NormID == normID);
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.OrderID).ToList();
return query.OrderBy(x => x.OrderID).ToList();
}
///
/// 获取评价指标信息
///
/// 主键ID
///
public EM_EvaluationTarget GetEvaluationTarget(Guid? targetID)
{
//查询条件
Expression> expression = (x => x.EvaluationTargetID == targetID.Value);
return evaluationTargetDAL.evaluationTargetRepository.GetSingle(expression);
}
///
/// 获取评分标准信息
///
/// 评价指标名称
///
public EMIS.Entities.EM_EvaluationTarget GetEvaluationTarget(string Name)
{
//查询条件
Expression> expression = (x => x.Name == Name);
return evaluationTargetDAL.evaluationTargetRepository.GetSingle(expression);
}
///
/// 获取评价指标信息
///
/// 主键ID
///
public EvaluationTargetView GetEvaluationTargetView(Guid? targetID)
{
EvaluationTargetView evaluationTargetView = new EvaluationTargetView();
if (targetID.HasValue && targetID != Guid.Empty)
evaluationTargetView = evaluationTargetDAL.GetEvaluationTargetQueryable(x => true).Where(x => x.TargetID == targetID).FirstOrDefault();
return evaluationTargetView;
}
///
/// 添加
///
/// 实体
///
public bool EvaluationTargetAdd(EM_EvaluationTarget evaluationTarget)
{
try
{
UnitOfWork.Add(evaluationTarget);
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
///
/// 修改
///
/// 实体
///
public bool EvaluationTargetUpdate(EM_EvaluationTarget evaluationTarget)
{
try
{
UnitOfWork.Update(evaluationTarget);
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
///
/// 删除
///
///
///
public bool EvaluationTargetDelete(List targetIDs)
{
try
{
UnitOfWork.Delete(x => targetIDs.Contains(x.EvaluationTargetID));
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
///
/// 验证
///
///
///
///
public void Verification(Guid targetID, string code, string name)
{
var sameCode = evaluationTargetDAL.evaluationTargetRepository.GetSingle(x => x.Code == code && x.EvaluationTargetID != targetID);
if (sameCode != null)
{
throw new Exception("评价指标代码已存在,请重新输入!");
}
var sameName = evaluationTargetDAL.evaluationTargetRepository.GetSingle(x => x.Name == name && x.EvaluationTargetID != targetID);
if (sameName != null)
{
throw new Exception("评价指标名称已存在,请重新输入!");
}
}
public bool ValidateEvaluationTarget(EvaluationTargetView model)
{
if (string.IsNullOrWhiteSpace(model.Name))
{
throw new Exception("指标名称不能为空!");
}
if (!model.TableID.HasValue || model.TableID == Guid.Empty)
{
throw new Exception("评价表不能为空!");
}
model.Name = model.Name.Trim();
Expression> nameFilter = w => w.Name == model.Name && w.EvaluationTableID == model.TableID;
if (model.TargetID.HasValue && model.TargetID != Guid.Empty) //编辑状态
{
nameFilter = nameFilter.And(w => w.EvaluationTargetID != model.TargetID);
}
var hasName = evaluationTargetDAL.evaluationTargetRepository.Entities.Any(nameFilter);
if (hasName)
{
throw new Exception("指标名称已存在!");
}
//var table = evaluationTargetDAL.evaluationTableRepository.GetSingle(w => w.TableID == model.TableID);
//if (model.Weight > table.Weight || model.Weight < 0)
//{
// throw new Exception("指标权重 必须在0到" + table.Weight + "之间!");
//}
//var weightSum = evaluationTargetDAL.evaluationTargetRepository.GetList(w => w.TableID == model.TableID).Sum(s => s.Weight ?? 0);
//weightSum += model.Weight.Value;
//if (weightSum > table.Weight)
//{
// throw new Exception("指标权重 之和(" + weightSum + ")必须等于" + table.Weight + "!");
//}
return true;
}
public void SaveOrUpdateEvaluationTarget(EvaluationTargetView model)
{
ValidateEvaluationTarget(model);
var user = EMIS.Utility.FormValidate.CustomPrincipal.Current;
EM_EvaluationTarget entity = new EM_EvaluationTarget();
if (!model.TargetID.HasValue || model.TargetID == Guid.Empty)
{
var table = evaluationTargetDAL.evaluationTableRepository.GetSingle(w => w.EvaluationTableID == model.TableID);
entity.EvaluationTargetID = Guid.NewGuid();
entity.Code = SerialNumberServices.SetSN(table.Code);
entity.CreateTime = DateTime.Now;
entity.CreateUserID = user.UserID;
UnitOfWork.Add(entity);
}
else
{
entity = GetEvaluationTarget(model.TargetID);
Guid? tableID = entity.EvaluationTableID;
if (evaluationTargetDAL.evaluationEnterRepository.Entities.Any(x => x.EM_EvaluationTable.EvaluationTableID == tableID))
{
throw new Exception("该信息已经被使用不能进行修改");
}
}
entity.Name = model.Name;
entity.EvaluationTableID = model.TableID;
entity.EvaluationNormID = model.NormID;
entity.OrderNo = model.OrderID;
entity.Weight = model.Weight;
entity.Remark = model.Remark;
entity.ModifyUserID = user.UserID;
entity.ModifyTime = DateTime.Now;
UnitOfWork.Commit();
}
public IList GetEvaluationTargetList(List targetIDs)
{
return evaluationTargetDAL.evaluationTargetRepository.GetList(x => targetIDs.Contains(x.EvaluationTargetID)).ToList();
}
public IList GetEvaluationProjectList(Guid targetID)
{
return evaluationTargetDAL.evaluationProjectRepository.GetList(x => x.EvaluationTargetID == targetID).ToList();
}
}
}