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.
557 lines
14 KiB
557 lines
14 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.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace EC.Service.Base
|
|
{
|
|
[Obsolete("已过期 建议使用 MvcService2<T>")]
|
|
public class MvcService<T> where T : class
|
|
{
|
|
/// <summary>
|
|
/// 字符串修改为空 设置字符串,修正没有Dto 不能设置为空的Bug
|
|
/// </summary>
|
|
public readonly string DefaultNull = "Null";
|
|
|
|
protected Orm<T> db = new Orm<T>();
|
|
|
|
#region Insert
|
|
|
|
/// <summary>
|
|
/// 保存返回newid
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
public int Insert(T model)
|
|
{
|
|
return db.Insert(model);
|
|
}
|
|
|
|
#endregion Insert
|
|
|
|
#region Update
|
|
|
|
/// <summary>
|
|
/// Update
|
|
/// </summary>
|
|
/// <param name="model"></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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按照主键删除
|
|
/// </summary>
|
|
/// <param name="keyValue"></param>
|
|
/// <returns></returns>
|
|
public int Delete(string keyValue)
|
|
{
|
|
return db.Delete(keyValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件批量删除
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <returns></returns>
|
|
public int DeleteAll(string where)
|
|
{
|
|
return db.DeleteAll(where);
|
|
}
|
|
|
|
#endregion Delete
|
|
|
|
#region FindEntity 查询对象
|
|
|
|
/// <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);
|
|
}
|
|
|
|
#endregion FindEntity 查询对象
|
|
|
|
#region FindEntityList
|
|
|
|
/// <summary>
|
|
/// 查询
|
|
/// </summary>
|
|
/// <param name="where">条件语句</param>
|
|
/// <param name="orderBy">排序字段 可为空 默认为主键排序</param>
|
|
/// <returns>list</returns>
|
|
public List<T> FindEntityListByWhere(string where, string orderBy = "id")
|
|
{
|
|
return db.SelectEntityList(where, orderBy);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询
|
|
/// </summary>
|
|
/// <param name="queryJson"></param>
|
|
/// <param name="orderBy"></param>
|
|
/// <returns></returns>
|
|
public List<T> FindEntityList(string queryJson, string orderBy)
|
|
{
|
|
string where = GetCondition(queryJson);
|
|
return FindEntityListByWhere(where, orderBy);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <param name="orderBy"></param>
|
|
/// <returns></returns>
|
|
public List<T> FindEntityLists(string where, string orderBy)
|
|
{
|
|
return FindEntityListByWhere(where, orderBy);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <param name="pageIndex"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="count"></param>
|
|
/// <returns></returns>
|
|
public List<T> FindEntityList(string where, int pageIndex, int pageSize, out int count)
|
|
{
|
|
return db.SelectEntityList(where, pageIndex, pageSize, out count);
|
|
}
|
|
|
|
#endregion FindEntityList
|
|
|
|
#region SelectEntity
|
|
|
|
public T SelectEntityByWhere(string sql)
|
|
{
|
|
var dt = db.SelectSql($"SELECT * FROM {db.TableName()} WHERE {sql} limit 1");
|
|
if (dt.IsEmpty())
|
|
return null;
|
|
|
|
var dr = dt.Rows[0];
|
|
var obj = Activator.CreateInstance<T>();// 实例化
|
|
var propertyList = typeof(T).GetProperties();//获取泛型类的类型,并得到泛型类所有属性的集合
|
|
foreach (var pi in propertyList)//遍历属性集合
|
|
{
|
|
var type = pi.PropertyType;//属性类型
|
|
var value = dr[pi.Name];//属性值
|
|
if (!Convert.IsDBNull(value))//不空则赋值
|
|
{
|
|
pi.SetValue(obj, Convert.ChangeType(value, type));//给泛型的属性赋值
|
|
}
|
|
}
|
|
dt.Clear();
|
|
dt.Dispose();
|
|
return obj;
|
|
}
|
|
|
|
public List<T> SelectEntityListByWhere(string sql)
|
|
{
|
|
var dt = db.SelectSql($"SELECT * FROM {db.TableName()} WHERE {sql}");
|
|
var list = new List<T>();
|
|
if (dt.IsEmpty())
|
|
return list;
|
|
|
|
var propertyList = typeof(T).GetProperties();//获取泛型类的类型,并得到泛型类所有属性的集合
|
|
foreach (DataRow dr in dt.Rows)//遍历数据列
|
|
{
|
|
var obj = Activator.CreateInstance<T>();//实例化
|
|
foreach (var pi in propertyList)//遍历属性集合
|
|
{
|
|
var type = pi.PropertyType;//属性类型
|
|
var value = dr[pi.Name];//属性值
|
|
if (!Convert.IsDBNull(value))//不空则赋值
|
|
{
|
|
pi.SetValue(obj, Convert.ChangeType(value, type));//给泛型的属性赋值
|
|
}
|
|
}
|
|
list.Add(obj);
|
|
}
|
|
dt.Clear();
|
|
dt.Dispose();
|
|
return list;
|
|
}
|
|
|
|
#endregion SelectEntity
|
|
|
|
#region Select DataTable
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="where"></param>
|
|
/// <param name="orderBy"></param>
|
|
/// <returns></returns>
|
|
public DataTable Select(string where, string orderBy)
|
|
{
|
|
return db.SelectFromView(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.SelectFromView(where, pageIndex, pageSize, out count);
|
|
}
|
|
|
|
public DataTable Select(string where, string orderBy, int pageIndex, int pageSize, out int count)
|
|
{
|
|
return db.SelectFromView(where, orderBy, pageIndex, pageSize, out count);
|
|
}
|
|
|
|
#endregion Select DataTable
|
|
|
|
#region 获取数据 与jqgrid 配配套
|
|
|
|
/// <summary>
|
|
/// 根据条件查询
|
|
/// </summary>
|
|
/// <param name="queryJson"></param>
|
|
/// <param name="orderBy"></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 = "";
|
|
if (!string.IsNullOrEmpty(pagination.sidx))
|
|
{
|
|
orderby += pagination.sidx;
|
|
if (!string.IsNullOrEmpty(pagination.sord))
|
|
{
|
|
orderby += (pagination.sord.Trim().ToLower() == "asc") ? " asc " : " desc";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
orderby = db.TableKey();
|
|
}
|
|
return GetPageList(pagination, queryJson, orderby);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到分页
|
|
/// </summary>
|
|
/// <param name="pagination">分页</param>
|
|
/// <param name="queryJson">查询参数</param>
|
|
/// <param name="orderBy"></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;
|
|
DataTable dt = Select(where, orderBy, pageIndex, pageSize, out var count);
|
|
pagination.records = count;
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到分页
|
|
/// </summary>
|
|
/// <param name="pagination">分页</param>
|
|
/// <param name="fEntity">查询参数</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;
|
|
DataTable dt = Select(where, orderby, pageIndex, pageSize, out var count);
|
|
pagination.records = count;
|
|
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 获取数据 与jqgrid 配配套
|
|
|
|
#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="newEntity">实体对象</param>
|
|
///// <returns></returns>
|
|
//public virtual int SaveForm(string keyValue, T newEntity)
|
|
//{
|
|
// if (!string.IsNullOrEmpty(keyValue))
|
|
// {
|
|
// var entity = FindEntity(keyValue);
|
|
// entity = EntityNewToOld(entity, newEntity);
|
|
// return db.Update(entity);
|
|
// }
|
|
// else
|
|
// {
|
|
// return db.Insert(newEntity);
|
|
// }
|
|
//}
|
|
|
|
///// <summary>
|
|
///// 保存表单(新增、修改)
|
|
///// </summary>
|
|
///// <param name="newEntity">实体对象</param>
|
|
///// <param name="keyValue">主键值</param>
|
|
///// <returns></returns>
|
|
//public virtual int SaveForm(T newEntity, string keyValue = "")
|
|
//{
|
|
// if (!string.IsNullOrEmpty(keyValue))
|
|
// {
|
|
// var entity = FindEntity(keyValue);
|
|
// entity = EntityNewToOld(entity, newEntity);
|
|
// return db.Update(entity);
|
|
// }
|
|
// else
|
|
// {
|
|
// return db.Insert(newEntity);
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 保存主从表
|
|
/// </summary>
|
|
/// <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 : class
|
|
{
|
|
Hashtable sqllist = new Hashtable();
|
|
List<MyParameter> myParameterList = new List<MyParameter>();
|
|
if (!string.IsNullOrEmpty(keyValue))
|
|
{
|
|
string sql = db.UpdateSql(entity, myParameterList);
|
|
sqllist.Add(sql, myParameterList);
|
|
}
|
|
else
|
|
{
|
|
string sql = db.InsertSql(entity, 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取新的Id
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int GetMaxId()
|
|
{
|
|
string sql = "select max(id)+1 from " + db.TableName();
|
|
return FormatCom.ToInt(DBAccess.ExecuteScalar(sql));
|
|
}
|
|
|
|
#endregion 提交数据
|
|
|
|
/// <summary>
|
|
/// 用于程序 修改功能用
|
|
/// </summary>
|
|
/// <param name="oldEntity"></param>
|
|
/// <param name="newEntity"></param>
|
|
/// <returns></returns>
|
|
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() + ",", StringComparison.Ordinal) > -1)
|
|
{
|
|
continue;
|
|
}
|
|
object value = GetPropertyValue(newEntity, propertyname);
|
|
//排除 null "" INT_MIN(数字)
|
|
if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
|
|
{
|
|
if (value.Equals(DefaultNull))
|
|
{
|
|
field.SetValue(oldEntity, null, null);
|
|
}
|
|
else
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
///copy model 值 由Model1 ->Model2
|
|
/// </summary>
|
|
/// <typeparam name="T1"></typeparam>
|
|
/// <typeparam name="T2"></typeparam>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public 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;
|
|
}
|
|
}
|
|
}
|