- A+
所属分类:.NET技术
abp(net core)+easyui+efcore实现仓储管理系统目录
abp(net core)+easyui+efcore实现仓储管理系统——EasyUI前端页面框架 (十八)
五、使用存储过程创建单号
1. 在Visual Studio 2017的“解决方案资源管理器”中,右键单击“ABP.TPLMS.Core”项目的“IRepositories”文件夹,在弹出菜单中选择“添加” > “类”。如下图。
2.在弹出对话框中选择“接口”, 将接口命名为 IOutStockOrderRepository,然后选择“添加”。如下图。
3.在IOutStockOrderRepository接口定义我们需要用到的方法,代码如下。
using Abp.Dependency; using Abp.Domain.Repositories; using ABP.TPLMS.Entitys; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Text; namespace ABP.TPLMS.IRepositories { public interface IOutStockOrderRepository: IRepository<OutStockOrder, int>,
ITransientDependency { DbCommand CreateCommand(string commandText, CommandType commandType
, params object[] parameters); /// <summary> /// 创建单号 /// </summary> /// <param name="name">单证名称代码</param> /// <returns></returns> string GetNo(string name); /// <summary> /// 导入入库货物信息 /// </summary> /// <param name="ids">导入库单的ID集合</param> /// <param name="no">单号</param> void ImportInStockOrder(string ids, string no); } }
4. 在Visual Studio 2017的“解决方案资源管理器”中,右键单击“ABP.TPLMS.EntityFrameworkCore”项目的“Repositories”文件夹,在弹出菜单中选择“添加” > “类”, 将类命名为 OutStockOrderRepository,并继承IOutStockOrderRepository接口。实现接口中的方法。代码如下。
using Abp.Data; using Abp.Dependency; using Abp.EntityFrameworkCore; using ABP.TPLMS.Entitys; using ABP.TPLMS.IRepositories; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Text; namespace ABP.TPLMS.EntityFrameworkCore.Repositories { public class OutStockOrderRepository: TPLMSRepositoryBase<OutStockOrder, int>
, IOutStockOrderRepository, ITransientDependency { private readonly IActiveTransactionProvider _transactionProvider; public OutStockOrderRepository(IDbContextProvider<TPLMSDbContext>
dbContextProvider):base(dbContextProvider) { } protected OutStockOrderRepository(IDbContextProvider<TPLMSDbContext>
dbContextProvider,IActiveTransactionProvider transactionProvider) : base(dbContextProvider) { _transactionProvider = transactionProvider; } public DbCommand CreateCommand(string commandText, CommandType commandType
, params SqlParameter[] parameters) { EnsureConnectionOpen(); var dbFacade = Context.Database; var connection = Microsoft.EntityFrameworkCore
.RelationalDatabaseFacadeExtensions
.GetDbConnection(dbFacade); var command = connection.CreateCommand(); command.CommandText = commandText; command.CommandType = commandType; command.Transaction = GetActiveTransaction(); foreach (var parameter in parameters) { command.Parameters.Add(parameter); } return command; } DbCommand IOutStockOrderRepository.CreateCommand(string commandText
, CommandType commandType, params object[] parameters) { EnsureConnectionOpen(); var dbFacade = Context.Database; var connection = Microsoft.EntityFrameworkCore
.RelationalDatabaseFacadeExtensions
.GetDbConnection(dbFacade); var command = connection.CreateCommand(); command.CommandText = commandText; command.CommandType = commandType; command.Transaction = GetActiveTransaction(); foreach (var parameter in parameters) { command.Parameters.Add(parameter); } return command; } private void EnsureConnectionOpen() { var dbFacade = Context.Database; var connection = Microsoft.EntityFrameworkCore
.RelationalDatabaseFacadeExtensions
.GetDbConnection(dbFacade); if (connection.State != ConnectionState.Open) { connection.Open(); } } private DbTransaction GetActiveTransaction() { return (DbTransaction)_transactionProvider.GetActiveTransaction(
new ActiveTransactionProviderArgs { {"ContextType", typeof(TPLMSDbContext) }, {"MultiTenancySide", MultiTenancySide } }); } string IOutStockOrderRepository.GetNo(string name) { SqlParameter[] parameters = { new SqlParameter("Name",System.Data.SqlDbType.NVarChar,10), new SqlParameter("BH", System.Data.SqlDbType.NVarChar,20) }; parameters[0].Value = name; parameters[1].Direction = System.Data.ParameterDirection.Output; int cnt = Context.Database.ExecuteSqlCommand( "EXEC p_NextBH @Name, @BH output", parameters); string no = parameters[1].Value.ToString(); if (cnt < 0) { no = string.Empty; } return no; } void IOutStockOrderRepository.ImportInStockOrder(string ids, string no) { SqlParameter[] parameters = { new SqlParameter("id",System.Data.SqlDbType.VarChar,500), new SqlParameter("No", System.Data.SqlDbType.NVarChar,20) }; parameters[0].Value = ids + ","; parameters[1].Value = no; int cnt = Context.Database.ExecuteSqlCommand( "EXEC SP_ImportInStockOrder2ODO @id, @No", parameters); } } }
5. 在这里我一共使用了两个存储过程,p_NextBH 与SP_ImportInStockOrder2ODO。其中p_NextBH在入库单管理中已经介绍过了。
6. 在这里介绍一下SP_ImportInStockOrder2ODO这个存储过程的实现,代码如下:
Create Proc [dbo].[SP_ImportInStockOrder2ODO] @id varchar(500), --id组合 @No nvarchar(20) --编号 as CREATE TABLE #IdTable(Id int NULL) DECLARE @str VARCHAR(1000) DECLARE @x xml select @str=@id insert #IdTable SELECT convert(int,id) id FROM (SELECT [value] = CONVERT(XML,'<v>' +REPLACE(@str,',' ,'</v><v>')+ '</v>')) A OUTER APPLY ( SELECT id = N.v.value('.' , 'varchar(100)') FROM A.[value].nodes('/v') N (v) ) B DECLARE @BH nvarchar(20),@batch varchar(20),@maxseqno int select @BH=@No select @maxseqno=isnull(MAX(seqno),0) from [InStockOrderDetail] where InStockNo= @BH INSERT INTO [dbo].[OutStockOrderDetail] ([OutStockNo],[SupplierId],[CargoCode],[HSCode],[CargoName],[Spcf] ,[Unit],[Country],[Brand] ,[Curr],[Package],[Length],[Width],[Height],[Qty] ,[Vol],[LawfQty],[SecdLawfQty],[Price],[TotalAmt],[GrossWt],[NetWt] ,[LawfUnit],[SecdLawfUnit],[Batch],[InStockOrderDetailLocId],[Loc]
,[CreationTime]) SELECT @BH,supplierid,[CargoCode],[HSCode],[CargoName],[Spcf] ,[Unit],[Country],[Brand],[Curr] ,[Package],[Length],[Width],[Height],0 [Qty] ,[Vol] ,0 [LawfQty], 0 [SecdLawfQty] ,[Price],0 [TotalAmt],[GrossWt],[NetWt] ,'' [LawfUnit],'' [SecdLawfUnit],batch,id,loc,getdate() FROM (select row_number() OVER ( order by b.id) seqno,a.supplierid,[CargoCode],[HSCode]
,[CargoName],[Spcf] ,[Unit],[Country],[Brand],[Curr] ,[Package],[Length],[Width],[Height],0 [Qty] ,[Vol] ,0 [LawfQty], 0 [SecdLawfQty] ,[Price],0 [TotalAmt],[GrossWt],[NetWt] ,'' [LawfUnit],'' [SecdLawfUnit],b.id,b.loc,Batch from [dbo].InStockOrderDetail a inner join InStockOrderDetailLoc b on a.Id=b.InStockOrderDetailId left join (select InStockOrderDetailLocId,SUM(isnull(qty,0)) as qty from OutStockOrderDetail group by InStockOrderDetailLocId )c on b.Id=c.InStockOrderDetailLocId where b.Qty-ISNULL(c.qty,0)>0 and b.Id in (select Id from #IdTable) ) iso drop table #IdTable