You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
423 lines
10 KiB
423 lines
10 KiB
using EC.Data;
|
|
using EC.Util;
|
|
using EC.Util.Extension;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace EC.Service.Base
|
|
{
|
|
public class MvcService2<T> where T : BaseEntity
|
|
{
|
|
public IEeasyOrm<T> db = OrmFactory.CreateOrm<T>();
|
|
|
|
/// <summary>
|
|
/// 开始事物
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public DbTransaction BeginTrans()
|
|
{
|
|
return db.BeginTrans();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提交事物
|
|
/// </summary>
|
|
public void Commit()
|
|
{
|
|
db.Commit();
|
|
}
|
|
|
|
public void Rollback()
|
|
{
|
|
db.Rollback();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加如 事物
|
|
/// </summary>
|
|
/// <param name="_dbtran"></param>
|
|
public void AddTrans(DbTransaction _dbtran)
|
|
{
|
|
db.AddTrans(_dbtran);
|
|
}
|
|
|
|
#region Insert
|
|
|
|
/// <summary>
|
|
/// 保存返回newid
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns>new id</returns>
|
|
public int Insert(T model)
|
|
{
|
|
return db.Insert(model);
|
|
}
|
|
|
|
#endregion Insert
|
|
|
|
#region Update
|
|
|
|
/// <summary>
|
|
/// Update
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public int Update(T model)
|
|
{
|
|
return db.Update(model);
|
|
}
|
|
|
|
#endregion Update
|
|
|
|
#region Delete
|
|
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public int Delete(int id)
|
|
{
|
|
return db.Delete(id);
|
|
}
|
|
|
|
#endregion Delete
|
|
|
|
#region Select
|
|
|
|
/// <summary>
|
|
///通过id 查询 对象
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public T FindEntity(int id)
|
|
{
|
|
return db.FindEntity(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过主键查询对象 [TableKeyAttribute("key")]
|
|
/// </summary>
|
|
/// <param name="keyValue"></param>
|
|
/// <returns></returns>
|
|
public T FindEntity(string keyValue)
|
|
{
|
|
return db.FindEntity(keyValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过 条件 查询对象
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <returns></returns>
|
|
public T FindEntityByWhere(string where)
|
|
{
|
|
return db.FindEntityByWhere(where);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询
|
|
/// </summary>
|
|
/// <param name="where">条件语句</param>
|
|
/// <param name="orderby">排序字段 可为空 默认为主键排序</param>
|
|
/// <returns>list</returns>
|
|
public IList<T> SelectEntityList(string where, string orderby)
|
|
{
|
|
return db.FindList(where, orderby);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <param name="pageIndex"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="count"></param>
|
|
/// <returns></returns>
|
|
public IList<T> SelectEntityList(string where, int pageIndex, int pageSize, out int count)
|
|
{
|
|
return db.FindList(where, pageIndex, pageSize, out count);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <returns></returns>
|
|
public DataTable Select(string where, string orderby)
|
|
{
|
|
return db.FindDataTable(where, orderby);//
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <param name="pageIndex"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="count"></param>
|
|
/// <returns>返回DataSet</returns>
|
|
public DataTable Select(string where, int pageIndex, int pageSize, out int count)
|
|
{
|
|
return db.FindDataTableFromView(where, pageIndex, pageSize, out count);
|
|
}
|
|
|
|
public DataTable Select(string where, string orderby, int pageIndex, int pageSize, out int count)
|
|
{
|
|
return db.FindDataTableFromView(where, pageIndex, pageSize, out count, orderby);
|
|
}
|
|
|
|
#endregion Select
|
|
|
|
#region 提交数据
|
|
|
|
/// <summary>
|
|
/// 删除数据
|
|
/// </summary>
|
|
/// <param name="keyValue">主键</param>
|
|
public virtual int RemoveForm(string keyValue)
|
|
{
|
|
return db.Delete(keyValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存表单(新增、修改)
|
|
/// </summary>
|
|
/// <param name="keyValue">主键值</param>
|
|
/// <param name="entity">实体对象</param>
|
|
/// <returns></returns>
|
|
public virtual int SaveForm(string keyValue, T newEntity)
|
|
{
|
|
if (!string.IsNullOrEmpty(keyValue))
|
|
{
|
|
T entity = FindEntity(keyValue);
|
|
entity = EntityNewToOld(entity, newEntity);
|
|
return db.Update(entity);
|
|
}
|
|
else
|
|
{
|
|
return db.Insert(newEntity);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="T2"></typeparam>
|
|
/// <param name="keyvalue"></param>
|
|
/// <param name="entity"></param>
|
|
/// <param name="detaillist"></param>
|
|
/// <returns></returns>
|
|
public bool SaveForm<T2>(string keyValue, T entity, List<T2> detaillist) where T2 : BaseEntity
|
|
{
|
|
Hashtable sqllist = new Hashtable();
|
|
List<MyParameter> myParameterList = new List<MyParameter>();
|
|
if (!string.IsNullOrEmpty(keyValue))
|
|
{
|
|
string sql = SqlGenerator.UpdateSql(entity, ref myParameterList);
|
|
sqllist.Add(sql, myParameterList);
|
|
}
|
|
else
|
|
{
|
|
string sql = SqlGenerator.InsertSql(entity, ref myParameterList);
|
|
sqllist.Add(sql, myParameterList);
|
|
}
|
|
|
|
foreach (T2 detail in detaillist)
|
|
{
|
|
List<MyParameter> detailParameterList = new List<MyParameter>();
|
|
string detailsql = new Orm<T2>().InsertSql(detail, detailParameterList);
|
|
sqllist.Add(detailsql, detailParameterList);
|
|
}
|
|
return DBAccess.ExecuteSql(sqllist);
|
|
}
|
|
|
|
#endregion 提交数据
|
|
|
|
protected T EntityNewToOld(T oldEntity, T newEntity)
|
|
{
|
|
string insertkeys = ",createdate,createuserid,createusername,partorg,";
|
|
Type t = oldEntity.GetType();
|
|
PropertyInfo[] properties = t.GetProperties();
|
|
|
|
if (properties.Count<PropertyInfo>() > 1) //至少两个字段,一个主键,一个数据字段。
|
|
{
|
|
foreach (PropertyInfo field in properties)
|
|
{
|
|
bool ispk = EntityAttribute.IsTableKey(field);
|
|
if (ispk)
|
|
{
|
|
continue;
|
|
}
|
|
string propertyname = field.Name;
|
|
if (insertkeys.IndexOf("," + propertyname.ToLower() + ",") > -1)
|
|
{
|
|
continue;
|
|
}
|
|
object value = GetPropertyValue(newEntity, propertyname);
|
|
if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
|
|
{
|
|
field.SetValue(oldEntity, value, null);
|
|
}
|
|
}
|
|
}
|
|
return oldEntity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据属性名称得到 属性值
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <param name="propertyname"></param>
|
|
/// <returns></returns>
|
|
protected object GetPropertyValue(object obj, string propertyname)
|
|
{
|
|
Type tm = obj.GetType();
|
|
PropertyInfo property = tm.GetProperty(propertyname);
|
|
if (property != null)
|
|
{
|
|
return property.GetValue(obj, null);
|
|
}
|
|
else return null;
|
|
}
|
|
|
|
#region 获取数据
|
|
|
|
/// <summary>
|
|
/// 根据条件查询
|
|
/// </summary>
|
|
/// <param name="queryJson"></param>
|
|
/// <returns></returns>
|
|
public virtual DataTable GetList(string queryJson, string orderby)
|
|
{
|
|
string where = GetCondition(queryJson);
|
|
return Select(where, orderby);
|
|
}
|
|
|
|
public virtual DataTable GetPageList(Pagination pagination, string queryJson)
|
|
{
|
|
string orderby = db.TableKey();
|
|
return GetPageList(pagination, queryJson, orderby);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到分页
|
|
/// </summary>
|
|
/// <param name="pagination">分页</param>
|
|
/// <param name="queryJson">查询参数</param>
|
|
/// <returns></returns>
|
|
public virtual DataTable GetPageList(Pagination pagination, string queryJson, string orderby)
|
|
{
|
|
string where = GetCondition(queryJson);
|
|
int pageIndex = pagination.page;
|
|
int pageSize = pagination.rows;
|
|
int icount = 0;
|
|
DataTable dt = Select(where, orderby, pageIndex, pageSize, out icount);
|
|
pagination.records = icount;
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到分页
|
|
/// </summary>
|
|
/// <param name="pagination">分页</param>
|
|
/// <param name="queryJson">查询参数</param>
|
|
/// <returns></returns>
|
|
public virtual DataTable GetPageList(Pagination pagination, Dictionary<string, string> fEntity)
|
|
{
|
|
string orderby = db.TableKey();
|
|
string where = GetCondition(fEntity);
|
|
int pageIndex = pagination.page;
|
|
int pageSize = pagination.rows;
|
|
int icount = 0;
|
|
DataTable dt = Select(where, orderby, pageIndex, pageSize, out icount);
|
|
pagination.records = icount;
|
|
return dt;
|
|
}
|
|
|
|
public virtual string GetCondition(Dictionary<string, string> fEntity)
|
|
{
|
|
string where = "1=1";
|
|
|
|
return where;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拼写查询条件
|
|
/// </summary>
|
|
/// <param name="queryJson"></param>
|
|
/// <returns></returns>
|
|
public virtual string GetCondition(string queryJson)
|
|
{
|
|
string where = "1=1";
|
|
|
|
if (!queryJson.IsEmpty())
|
|
{
|
|
queryJson = queryJson.Trim();
|
|
|
|
if (!queryJson.IsEmpty())
|
|
{
|
|
if (queryJson.StartsWith("["))
|
|
{
|
|
var queryParam = queryJson.ToList<Condition>();
|
|
foreach (Condition condition in queryParam)
|
|
{
|
|
if (!condition.condition.IsEmpty() && !condition.keyword.IsEmpty())
|
|
{
|
|
where += " and " + condition.condition + " like '%" + condition.keyword + "%'";
|
|
}
|
|
}
|
|
}
|
|
else if (queryJson.StartsWith("{"))
|
|
{
|
|
var queryParam = queryJson.ToJObject();
|
|
//查询条件
|
|
if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
|
|
{
|
|
string condition = queryParam["condition"].ToString();
|
|
string keyword = queryParam["keyword"].ToString();
|
|
|
|
where += " and " + condition + " like '%" + keyword + "%'";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return where;
|
|
}
|
|
|
|
#endregion 获取数据
|
|
|
|
/// <summary>
|
|
///copy model 值
|
|
/// </summary>
|
|
/// <typeparam name="T1"></typeparam>
|
|
/// <typeparam name="T2"></typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
protected T2 Model1ToModel2<T1, T2>(T1 model)
|
|
{
|
|
T2 dto = Activator.CreateInstance<T2>();
|
|
Type t = dto.GetType();
|
|
PropertyInfo[] properties = t.GetProperties();
|
|
|
|
if (properties.Count<PropertyInfo>() > 1) //至少两个字段,一个主键,一个数据字段。
|
|
{
|
|
foreach (PropertyInfo field in properties)
|
|
{
|
|
string propertyname = field.Name;
|
|
object value = GetPropertyValue(model, propertyname);
|
|
|
|
field.SetValue(dto, value, null);
|
|
}
|
|
}
|
|
|
|
return dto;
|
|
}
|
|
}
|
|
}
|