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 Bowin.Common.Linq;
namespace EMIS.CommonLogic.EvaluationManage
{
public class EvaluationTableServices : BaseServices, IEvaluationTableServices
{
public EvaluationTableDAL evaluationTableDAL { get; set; }
///
/// 查询评价表信息
///
/// 查询条件实体
/// 评价类型
/// 参评类型
/// 页码
/// 显示页数
///
public IGridResultSet GetEvaluationTableViewGrid(ConfiguretView configuretView, Guid? evaluationType, Guid? evaluationIntType, int? openStatus, int pageIndex, int pageSize)
{
var query = evaluationTableDAL.GetEvaluationTableQueryable(x => true);
if (evaluationIntType.HasValue && evaluationIntType != Guid.Empty)
{
query = query.Where(x => x.EvaluationIntTypeID == evaluationIntType);
}
if (evaluationType.HasValue && evaluationType != Guid.Empty)
{
query = query.Where(x => x.TypeID == evaluationType);
}
if (openStatus >= 0)
{
query = query.Where(x => x.IsEnabled == openStatus);
}
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.EvaluationType).OrderBy(x => x.Code).ToGridResultSet(pageIndex, pageSize);
return query.OrderBy(x => x.EvaluationType).OrderBy(x => x.Code).ToGridResultSet(pageIndex, pageSize);
}
///
/// 查询评价表信息
///
/// 查询条件实体
/// 评价类型
/// 参评类型
///
public List GetEvaluationTableViewList(ConfiguretView configuretView, Guid? evaluationType, Guid? evaluationIntType, int? openStatus)
{
var query = evaluationTableDAL.GetEvaluationTableQueryable(x => true);
if (evaluationIntType.HasValue && evaluationIntType != Guid.Empty)
{
query = query.Where(x => x.EvaluationIntTypeID == evaluationIntType);
}
if (evaluationType.HasValue && evaluationType != Guid.Empty)
{
query = query.Where(x => x.TypeID == evaluationType);
}
if (openStatus >= 0)
{
query = query.Where(x => x.IsEnabled == openStatus);
}
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.CreateTime).ToList();
return query.OrderByDescending(x => x.CreateTime).ToList();
}
///
/// 获取评价表信息
///
/// 主键ID
///
public EM_EvaluationTable GetEvaluationTable(Guid? tableID)
{
//查询条件
Expression> expression = (x => x.EvaluationTableID == tableID.Value);
return evaluationTableDAL.evaluationTableRepository.GetSingle(expression);
}
///
/// 获取评价表信息
///
/// 评价表名
///
public EM_EvaluationTable GetEvaluationTable(string Name)
{
//查询条件
Expression> expression = (x => x.Name == Name);
return evaluationTableDAL.evaluationTableRepository.GetSingle(expression);
}
///
/// 获取评价表信息
///
/// 主键ID
///
public EvaluationTableView GetEvaluationTableView(Guid? tableID)
{
EvaluationTableView evaluationTableView = new EvaluationTableView();
if (tableID.HasValue && tableID != Guid.Empty)
evaluationTableView = evaluationTableDAL.GetEvaluationTableQueryable(x => true).Where(x => x.TableID == tableID).FirstOrDefault();
return evaluationTableView;
}
///
/// 添加
///
/// 实体
///
public bool EvaluationTableAdd(EM_EvaluationTable evaluationTable)
{
try
{
UnitOfWork.Add(evaluationTable);
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
///
/// 修改
///
/// 实体
///
public bool EvaluationTableUpdate(EM_EvaluationTable evaluationTable)
{
try
{
UnitOfWork.Update(evaluationTable);
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
///
/// 删除
///
///
///
public bool EvaluationTableDelete(List tableIDs)
{
try
{
UnitOfWork.Delete(x => tableIDs.Contains(x.EvaluationTableID));
UnitOfWork.Commit();
return true;
}
catch (Exception)
{
throw;
}
}
public bool ValidateEvaluationTable(Guid? tableID, string name, string code)
{
if (string.IsNullOrWhiteSpace(code))
{
throw new Exception("评价表编号不能为空!");
}
if (string.IsNullOrWhiteSpace(name))
{
throw new Exception("评价表名不能为空!");
}
code = code.Trim();
name = name.Trim();
Expression> codeFilter = w => w.Code == code;
Expression> nameFilter = w => w.Name == name;
if (tableID.HasValue && tableID != Guid.Empty) //编辑状态
{
codeFilter = codeFilter.And(w => w.EvaluationTableID != tableID);
nameFilter = nameFilter.And(w => w.EvaluationTableID != tableID);
}
var hasCode = evaluationTableDAL.evaluationTableRepository.Entities.Any(codeFilter);
if (hasCode)
{
throw new Exception("评价表编号已存在!");
}
var hasName = evaluationTableDAL.evaluationTableRepository.Entities.Any(nameFilter);
if (hasName)
{
throw new Exception("评价表名已存在!");
}
return true;
}
public void SaveOrUpdateEvaluationTable(EvaluationTableView model)
{
ValidateEvaluationTable(model.TableID, model.Name, model.Code);
var user = EMIS.Utility.FormValidate.CustomPrincipal.Current;
EM_EvaluationTable entity;
var entityList = evaluationTableDAL.evaluationTableRepository.Entities.Where(x => x.EvaluationIntTypeID == model.EvaluationIntTypeID && x.EvaluationTypeID == model.TypeID).ToList();
if (model.TableID.HasValue && model.TableID != Guid.Empty)
{
//entity = entityList.FirstOrDefault(x => x.TableID == model.TableID);
entity = GetEvaluationTable(model.TableID);
entityList.Remove(entity);//移除
entity.Code = model.Code;
entity.Name = model.Name;
entity.EvaluationIntTypeID = model.EvaluationIntTypeID;
entity.EvaluationTypeID = model.TypeID;
entity.Weight = model.Weight;
entity.Remark = model.Remark;
entity.ModifyUserID = user.UserID;
entity.ModifyTime = DateTime.Now;
entity.IsEnabled = model.IsEnabled == (int)CF_YesOrNoStatus.Yes ? true : false;
UnitOfWork.Update(entity);
}
else
{
entity = new EM_EvaluationTable();
entity.EvaluationTableID = Guid.NewGuid();
entity.Code = model.Code;
entity.Name = model.Name;
entity.EvaluationIntTypeID = model.EvaluationIntTypeID;
entity.EvaluationTypeID = model.TypeID;
entity.Weight = model.Weight;
entity.Remark = model.Remark;
entity.ModifyUserID = user.UserID;
entity.ModifyTime = DateTime.Now;
entity.CreateTime = DateTime.Now;
entity.CreateUserID = user.UserID;
entity.IsEnabled = model.IsEnabled == (int)CF_YesOrNoStatus.Yes ? true : false;
UnitOfWork.Add(entity);
}
if (entity.IsEnabled == true)
{
entityList.ForEach(item => item.IsEnabled = false);
}
UnitOfWork.Commit();
}
}
}