- using RayPI.Model;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- namespace RayPI.Service
- {/// <summary>
- /// 服务层基类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public abstract class BaseService<T> where T:class,new()
- {
- public BaseService()
- {
- db = GetClient();
- sdb = db.GetSimpleClient();
- }
- public SqlSugarClient db;
- public SimpleClient sdb;
- /// <summary>
- /// 获取客户端
- /// </summary>
- /// <returns></returns>
- private SqlSugarClient GetClient()
- {
- SqlSugarClient db = new SqlSugarClient(
- new ConnectionConfig()
- {
- ConnectionString = BaseDBConfig.ConnectionString,
- DbType = DbType.SqlServer,
- IsAutoCloseConnection = true
- }
- );
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
- Console.WriteLine();
- };
- return db;
- }
- #region CRUD
- public TableModel<T> GetPageList(int pageIndex, int pageSize)
- {
- PageModel p = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
- Expression<Func<T, bool>> ex = (it => 1 == 1);
- List<T> data = sdb.GetPageList(ex, p);
- var t = new TableModel<T>
- {
- Code = 0,
- Count = p.PageCount,
- Data = data,
- Msg = "成功"
- };
- return t;
- }
- public T Get(long id)
- {
- return sdb.GetById<T>(id);
- }
- public bool Add(T entity)
- {
- return sdb.Insert(entity);
- }
- public bool Update(T entity)
- {
- return sdb.Update(entity);
- }
- public bool Dels(dynamic[] ids)
- {
- return sdb.DeleteByIds<T>(ids);
- }
- #endregion
- }
- }
来源: https://www.cnblogs.com/RayWang/p/9498591.html