Skip to content

Commit

Permalink
Merge pull request liningit#4 from liningit/liningit
Browse files Browse the repository at this point in the history
V2.0.0
  • Loading branch information
liningit authored Jul 9, 2019
2 parents 781c1c2 + ee8fe5f commit fda7565
Show file tree
Hide file tree
Showing 31 changed files with 1,542 additions and 610 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
* 增删改查操作
* [不分库分表](/noshuffled)
* [分库分表](/shuffled)
* [连表查询](/join)
* [高级操作](/advanced)
24 changes: 24 additions & 0 deletions docs/join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 连表查询

v2.0版本支持多表查询了
步骤如下
1. 调用方法是通过`IQuery.OuterJoin`或者`IQuery.InnerJoin`进行连表查询,返回IJoinQuery对象.
1. 可以调用`IJoinQuery.And,Or`进行条件过滤.调用`Select`返回`ISelectResult`.
1. 通过仓储的`GetList``GetPaging`进行返回结果.
```csharp
var repository = GetRepository();
var query = QueryFactory.Create<ProductSaleByDayNSEntity>(m => DBFunction.Function<DateTime>("ISNULL", m.UpdateDate, DateTime.Now) > new DateTime(2019, 6, 26));
var jq = query.InnerJoin(QueryFactory.Create<ShopEntity>(), m => m.ShopID, m => m.SysNo, (x, y) => new { Sale = x, Shop = y });
jq.And(m => m.Shop.ShopName.Contains("店铺"));
jq.OrderByDescing(m => m.Sale.Sales + 1);
jq.OrderBy(m => m.Sale.ProductName + m.Sale.OutProductID);
jq.StarSize = 10;
jq.Rows = 5;
var res = jq.Select(m => m.Sale);
var paging = repository.GetPaging(res);
//也可以下面这样返回dto.第二个参数表示第一个表是否要查询所有列.
var res2 = jq.Select(m => new PSDto { ShopName = m.Shop.ShopName }, true);
var paging2 = repository.GetPaging(res2);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items
```
31 changes: 23 additions & 8 deletions src/LnskyDB.Demo/Controllers/InitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using LnskyDB;
using LnskyDB.Demo.Entity.Data;
using LnskyDB.Demo.Entity.Purify;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -15,15 +16,12 @@ public class InitController : ControllerBase

public static object lockObj = new object();
static List<string> lstDataSource = new List<string> { "测试来源1", "测试来源2", "自动生成" };
static Dictionary<Guid, string> dicShop = new Dictionary<Guid, string> {
{ Guid.Parse("2E3E3B71-94B5-4C16-BFAC-2FF136B78FF5"), "测试店铺1" },
{ Guid.Parse("4E37A7E1-8BAC-44C9-B128-58E4049E3CCF"), "测试店铺2" }
};
static List<ShopEntity> lstShop = new List<ShopEntity>();
static Dictionary<Guid, string> dicProduct = new Dictionary<Guid, string>();
static InitController()
{
InitDic(dicProduct, "测试商品", 10);

InitDic(dicProduct, "测试商品", 10);
}
private static void InitDic(Dictionary<Guid, string> dic, string namePre, int count)
{
Expand All @@ -42,9 +40,26 @@ public string Index()
}
lock (lockObj)
{

isRuning = true;
try
{
var shopRepository = RepositoryFactory.Create<ShopEntity>();
shopRepository.Delete(QueryFactory.Create<ShopEntity>());
for (int i = 0; i < 10; i++)
{


var shop = new ShopEntity
{
SysNo = Guid.NewGuid(),
ShopCode = "00" + i,
ShopName = "测试店铺" + i,
ShopType = i % 3
};
shopRepository.Add(shop);
}
lstShop = shopRepository.GetList(QueryFactory.Create<ShopEntity>());
var importGroupId = Guid.NewGuid();
var random = new Random();
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
Expand All @@ -65,8 +80,8 @@ public string Index()
var tempNS = new ProductSaleByDayNSEntity();
tempNS.SysNo = temp.SysNo = Guid.NewGuid();
tempNS.DataSource = temp.DataSource = lstDataSource[random.Next(lstDataSource.Count)];
tempNS.ShopID = temp.ShopID = dicShop.Keys.ToList()[random.Next(dicShop.Count)];
tempNS.ShopName = temp.ShopName = dicShop[temp.ShopID];
tempNS.ShopID = temp.ShopID = lstShop[random.Next(lstShop.Count)].SysNo;

tempNS.ProductID = temp.ProductID = p.Key;
tempNS.OutProductID = temp.OutProductID = p.Value;
tempNS.ProductName = temp.ProductName = p.Value;
Expand Down
7 changes: 5 additions & 2 deletions src/LnskyDB.Demo/Controllers/ProductSaleByDayController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@


using LnskyDB.Demo.Entity.Purify;
using LnskyDB.Demo.Repository.Purify;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using LnskyDB.Model;

namespace LnskyDB.Demo.Controllers
{
Expand All @@ -24,7 +27,7 @@ private static IRepository<ProductSaleByDayEntity> GetRepository()
{
//也可以继承实例化
return new ProductSaleByDayRepository();
}
}
}
// GET http://localhost:53277/ProductSaleByDay/Get

Expand Down
26 changes: 23 additions & 3 deletions src/LnskyDB.Demo/Controllers/ProductSaleByDayNSController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using LnskyDB.Demo.Entity.Data;
using LnskyDB.Demo.Entity.Purify;
using LnskyDB.Demo.Repository.Purify;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -73,6 +74,7 @@ public ActionResult<Paging<ProductSaleByDayNSEntity>> GetPaging()
var query = QueryFactory.Create<ProductSaleByDayNSEntity>(m => DBFunction.Function<DateTime>("ISNULL", m.UpdateDate, DateTime.Now) > new DateTime(2019, 6, 26));

query.OrderByDescing(m => m.StatisticalDate);
query.OrderByDescing(m => m.Sales + 1);
query.StarSize = 0;
query.Rows = 10;
var paging = repository.GetPaging(query);
Expand All @@ -91,7 +93,6 @@ public void Add()
DataSource = "新增测试来源",
ProductID = Guid.NewGuid(),
ShopID = Guid.NewGuid(),
ShopName = "测试店铺",
ProductName = "测试商品",
OutProductID = Guid.NewGuid().ToString(),
ImportGroupId = Guid.NewGuid(),
Expand All @@ -109,7 +110,6 @@ public bool Update()
{
SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
DataSource = "测试来源修改",
ShopName = "店铺修改",
StatisticalDate = new DateTime(2019, 01, 05),
};
var repository = GetRepository();
Expand All @@ -123,7 +123,6 @@ public int UpdateWhere()
var updateEntity = new ProductSaleByDayNSEntity()
{
DataSource = "测试来源修改",
ShopName = "店铺修改Where",

};
var repository = GetRepository();
Expand Down Expand Up @@ -159,6 +158,27 @@ public int DleteWhere(string shopName, DateTime shuffledTempDate, string dataSou
return repository.Delete(where);
}

//GET http://localhost:53277/ProductSaleByDayNS/GetJoinPaging
[HttpGet]
public ActionResult<Paging<ProductSaleByDayNSEntity>> GetJoinPaging()
{
var repository = GetRepository();
var query = QueryFactory.Create<ProductSaleByDayNSEntity>(m => DBFunction.Function<DateTime>("ISNULL", m.UpdateDate, DateTime.Now) > new DateTime(2019, 6, 26));
var jq = query.InnerJoin(QueryFactory.Create<ShopEntity>(), m => m.ShopID, m => m.SysNo, (x, y) => new { Sale = x, Shop = y });
jq.And(m => m.Shop.ShopName.Contains("店铺"));
jq.OrderByDescing(m => m.Sale.Sales + 1);
jq.OrderBy(m => m.Sale.ProductName + m.Sale.OutProductID);
jq.StarSize = 10;
jq.Rows = 5;
var res = jq.Select(m => m.Sale);
var paging = repository.GetPaging(res);
//也可以下面这样返回dto.第二个参数表示第一个表是否要查询所有列.
var res2 = jq.Select(m => new PSDto { ShopName = m.Shop.ShopName }, true);
var paging2 = repository.GetPaging(res2);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items
return paging;
}

[HttpGet]
public void TestThread(string shopName, DateTime shuffledTempDate, string dataSource)
Expand Down
44 changes: 44 additions & 0 deletions src/LnskyDB.Demo/Entity/Data/ShopEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using LnskyDB.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Immutable;
namespace LnskyDB.Demo.Entity.Data
{
public class ShopEntity : BaseDBModel
{
private static ImmutableList<string> _DBModel_PKCols = ImmutableList.Create("SysNo");
public override ImmutableList<string> GetDBModel_PKCols() => _DBModel_PKCols;


public override string GetDBModel_TableName() => "Data_Shop{0}";
public override string GetDBModel_DBName() => "LnskyNS{0}";

#region Model

Guid _SysNo;
/// <summary>
/// 系统编号
/// </summary>
public Guid SysNo { get { return _SysNo; } set { Change("SysNo"); _SysNo = value; } }

string _ShopCode;
/// <summary>
/// 店铺编码
/// </summary>
public string ShopCode { get { return _ShopCode; } set { Change("ShopCode"); _ShopCode = value; } }

string _ShopName;
/// <summary>
/// 店铺名称
/// </summary>
public string ShopName { get { return _ShopName; } set { Change("ShopName"); _ShopName = value; } }

int? _ShopType;
/// <summary>
/// 店铺类型
/// </summary>
public int? ShopType { get { return _ShopType; } set { Change("ShopType"); _ShopType = value; } }

#endregion Model
}
}
6 changes: 0 additions & 6 deletions src/LnskyDB.Demo/Entity/Purify/ProductSaleByDayNSEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ public class ProductSaleByDayNSEntity : BaseDBModel
/// </summary>
public Guid ShopID { get { return _ShopID; } set { Change("ShopID"); _ShopID = value; } }

string _ShopName;
/// <summary>
/// 店铺名称
/// </summary>
public string ShopName { get { return _ShopName; } set { Change("ShopName"); _ShopName = value; } }

DateTime _StatisticalDate;
/// <summary>
/// 统计日期
Expand Down
18 changes: 18 additions & 0 deletions src/LnskyDB.Demo/PSDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LnskyDB.Demo
{
public class PSDto
{
public Guid SysNo { get; set; }
public string DataSource { get; set; }
public string OutProductID { get; set; }
public Guid ShopID { get; set; }
public DateTime StatisticalDate { get; set; }
public decimal Sales { get; set; }
public string ShopName { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/LnskyDB.Demo/Repository/Data/ShopRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using LnskyDB;
using LnskyDB.Demo.Entity.Data;
using LnskyDB.Demo.RepositoryInterface.Data;

namespace LnskyDB.Demo.Repository.Data
{
public class ShopRepository : Repository<ShopEntity>, IShopRepository
{
}
}

9 changes: 9 additions & 0 deletions src/LnskyDB.Demo/RepositoryInterface/Data/IShopRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LnskyDB;
using LnskyDB.Demo.Entity.Data;

namespace LnskyDB.Demo.RepositoryInterface.Data
{
public interface IShopRepository : IRepository<ShopEntity>
{
}
}
Loading

0 comments on commit fda7565

Please sign in to comment.