本文介紹如何實(shí)現(xiàn)進(jìn)銷存管理系統(tǒng)的基礎(chǔ)數(shù)據(jù)模塊,基礎(chǔ)數(shù)據(jù)模塊包括商品信息、供應(yīng)商管理和客戶管理3個(gè)菜單頁(yè)面。供應(yīng)商和客戶字段相同,因此可共用一個(gè)頁(yè)面組件類。
- 項(xiàng)目代碼:JxcLite
- 開源地址: https://gitee.com/known/JxcLite
1. 配置模塊
運(yùn)行項(xiàng)目,在【系統(tǒng)管理-模塊管理】中添加商品信息、供應(yīng)商管理、客戶管理3個(gè)模塊菜單,模塊基本信息、模型、頁(yè)面、表單設(shè)置之前有視頻教程,這里不再細(xì)說(shuō)了。
2. 實(shí)體類
在JxcLite項(xiàng)目Entities文件夾下面添加JxGoods.cs和JxPartner.cs兩個(gè)實(shí)體類文件,實(shí)體類代碼可以直接復(fù)制模塊管理中由模型設(shè)置生成的代碼。文章中只簡(jiǎn)單描述一下實(shí)體類的定義,具體代碼參見開源,代碼定義如下:
namespace JxcLite.Entities;
/// <summary>
/// 商品信息類。
/// </summary>
public class JxGoods : EntityBase { }
/// <summary>
/// 商業(yè)伙伴信息類。
/// </summary>
public class JxPartner : EntityBase { }
3. 建表腳本
最理想的情況是:在系統(tǒng)安裝時(shí),通過(guò)實(shí)體類和數(shù)據(jù)庫(kù)類型自動(dòng)生成建表腳本創(chuàng)建實(shí)體數(shù)據(jù)庫(kù)表。這里還是用傳統(tǒng)手動(dòng)方式執(zhí)行建表腳本,在JxcLite.Web項(xiàng)目Resources文件夾下添加Tables.sql資源文件,復(fù)制粘貼由【模塊管理-模型設(shè)置】中生成的建表腳本。文章中只簡(jiǎn)單描述一下建表腳本,具體腳本參見開源,內(nèi)容如下:
CREATE TABLE [JxGoods] (
[Id] varchar(50) NOT NULL PRIMARY KEY,
...
[Files] nvarchar(500) NULL
);
CREATE TABLE [JxPartner] (
[Id] varchar(50) NOT NULL PRIMARY KEY,
...
[Note] ntext NULL,
[Files] nvarchar(500) NULL
);
4. 服務(wù)接口
在JxcLite項(xiàng)目Services文件夾下面添加基礎(chǔ)數(shù)據(jù)模塊服務(wù)接口類,文件名定義為IBaseDataService.cs,該接口定義前后端交互的Api訪問(wèn)方法,包括分頁(yè)查詢、批量刪除實(shí)體、保存實(shí)體。具體方法定義如下:
namespace JxcLite.Services;
public interface IBaseDataService : IService
{
//分頁(yè)查詢商品信息
Task<PagingResult<JxGoods>> QueryGoodsesAsync(PagingCriteria criteria);
//批量刪除商品信息
Task<Result> DeleteGoodsesAsync(List<JxGoods> models);
//保存商品信息
Task<Result> SaveGoodsAsync(UploadInfo<JxGoods> info);
//分頁(yè)查詢供應(yīng)商和客戶信息
Task<PagingResult<JxPartner>> QueryPartnersAsync(PagingCriteria criteria);
//批量刪除供應(yīng)商和客戶信息
Task<Result> DeletePartnersAsync(List<JxPartner> models);
//保存供應(yīng)商和客戶信息
Task<Result> SavePartnerAsync(UploadInfo<JxPartner> info);
}
5. 服務(wù)實(shí)現(xiàn)
在JxcLite.Web項(xiàng)目Services文件夾下面添加基礎(chǔ)數(shù)據(jù)模塊服務(wù)接口的實(shí)現(xiàn)類,文件名定義為BaseDataService.cs,文章中只簡(jiǎn)單描述一下實(shí)現(xiàn)類的定義和繼承,具體實(shí)現(xiàn)參見開源,定義如下:
namespace JxcLite.Web.Services;
class BaseDataService(Context context) : ServiceBase(context), IBaseDataService
{
public Task<PagingResult<JxGoods>> QueryGoodsesAsync(PagingCriteria criteria) { }
public Task<Result> DeleteGoodsesAsync(List<JxGoods> models) { }
public Task<Result> SaveGoodsAsync(UploadInfo<JxGoods> info) { }
public Task<PagingResult<JxPartner>> QueryPartnersAsync(PagingCriteria criteria) { }
public Task<Result> DeletePartnersAsync(List<JxPartner> models) { }
public Task<Result> SavePartnerAsync(UploadInfo<JxPartner> info) { }
}
雙擊打開JxcLite.Web項(xiàng)目中的AppWeb.cs文件,在AddJxcLiteCore方法中注冊(cè)服務(wù)類,前端組件可以通過(guò)依賴注入工廠創(chuàng)建服務(wù)的實(shí)例。代碼如下:
public static class AppWeb
{
public static void AddJxcLiteCore(this IServiceCollection services)
{
services.AddScoped<IBaseDataService, BaseDataService>();
}
}
6. 數(shù)據(jù)依賴
在JxcLite.Web項(xiàng)目Repositories文件夾下面添加基礎(chǔ)數(shù)據(jù)模塊數(shù)據(jù)依賴類,文件名定義為BaseDataRepository.cs,文章中只簡(jiǎn)單描述一下依賴類的定義,具體實(shí)現(xiàn)參見開源,定義如下:
namespace JxcLite.Web.Repositories;
class BaseDataRepository
{
internal static Task<PagingResult<JxGoods>> QueryGoodsesAsync(Database db, PagingCriteria criteria) { }
internal static async Task<bool> ExistsGoodsCodeAsync(Database db, JxGoods model) { }
internal static Task<PagingResult<JxPartner>> QueryPartnersAsync(Database db, PagingCriteria criteria) { }
internal static async Task<bool> ExistsPartnerNameAsync(Database db, JxPartner model) { }
}
7. 數(shù)據(jù)導(dǎo)入類
在JxcLite.Web項(xiàng)目Imports文件夾下面添加商品信息、供應(yīng)商和客戶的導(dǎo)入類,文件名定義為JxGoodsImport.cs和JxPartnerImport.cs,導(dǎo)入類名稱命名規(guī)范是:實(shí)體類名+Import,導(dǎo)入框架自動(dòng)根據(jù)名稱識(shí)別,文章中只簡(jiǎn)單描述一下導(dǎo)入類的定義,具體實(shí)現(xiàn)參見開源,定義如下:
namespace JxcLite.Web.Imports;
class JxGoodsImport(ImportContext context) : ImportBase<JxGoods>(context)
{
//初始化導(dǎo)入字段,自動(dòng)生成導(dǎo)入規(guī)范Excel文件
public override void InitColumns() { }
//執(zhí)行導(dǎo)入文件
public override async Task<Result> ExecuteAsync(SysFile file) { }
}
class JxPartnerImport(ImportContext context) : ImportBase<JxPartner>(context)
{
public override void InitColumns() { }
public override async Task<Result> ExecuteAsync(SysFile file) { }
}
8. 前端頁(yè)面
在JxcLite.Client項(xiàng)目Pages\BaseData文件夾下面添加商品信息和商業(yè)伙伴頁(yè)面類,文件名定義為GoodsList.cs和PartnerList.cs,這3個(gè)模塊的功能和頁(yè)面非常簡(jiǎn)單,只有單表的增刪改查導(dǎo)功能,表單頁(yè)面直接通過(guò)在線表單進(jìn)行配置。列表頁(yè)面繼承BaseTablePage類,由于該框架類封裝了列表頁(yè)面常用的增刪改查導(dǎo)功能,因此具體的功能列表頁(yè)面代碼顯得格外簡(jiǎn)單,只需要定義各操作的服務(wù)調(diào)用方法即可,具體的完整代碼如下:
- 商品信息頁(yè)面
namespace JxcLite.Client.Pages.BaseData;
[StreamRendering]
[Route("/bds/goods")]
public class GoodsList : BaseTablePage<JxGoods>
{
private IBaseDataService Service;
protected override async Task OnPageInitAsync()
{
await base.OnPageInitAsync();
Service = await CreateServiceAsync<IBaseDataService>();
Table.OnQuery = Service.QueryGoodsesAsync;
}
public void New() => Table.NewForm(Service.SaveGoodsAsync, new JxGoods());
public void DeleteM() => Table.DeleteM(Service.DeleteGoodsesAsync);
public void Edit(JxGoods row) => Table.EditForm(Service.SaveGoodsAsync, row);
public void Delete(JxGoods row) => Table.Delete(Service.DeleteGoodsesAsync, row);
public void Import() => ShowImportForm();
public async void Export() => await ExportDataAsync();
}
- 供應(yīng)商和客戶頁(yè)面
[StreamRendering]
[Route("/bds/suppliers")]
public class SupplierList : PartnerList
{
protected override string Type => PartnerType.Supplier;
}
[StreamRendering]
[Route("/bds/customers")]
public class CustomerList : PartnerList
{
protected override string Type => PartnerType.Customer;
}
public class PartnerList : BaseTablePage<JxPartner>
{
private IBaseDataService Service;
//商業(yè)伙伴類型虛擬屬性,供應(yīng)商和客戶頁(yè)面覆寫。
protected virtual string Type { get; }
protected override async Task OnPageInitAsync()
{
await base.OnPageInitAsync();
Service = await CreateServiceAsync<IBaseDataService>();
Table.OnQuery = QueryPartnersAsync;
}
public void New() => Table.NewForm(Service.SavePartnerAsync, new JxPartner { Type = Type });
public void DeleteM() => Table.DeleteM(Service.DeletePartnersAsync);
public void Edit(JxPartner row) => Table.EditForm(Service.SavePartnerAsync, row);
public void Delete(JxPartner row) => Table.Delete(Service.DeletePartnersAsync, row);
public void Import() => ShowImportForm();
public async void Export() => await ExportDataAsync();
private Task<PagingResult<JxPartner>> QueryPartnersAsync(PagingCriteria criteria)
{
criteria.SetQuery(nameof(JxPartner.Type), QueryType.Equal, Type);
return Service.QueryPartnersAsync(criteria);
}
}