@ -1,23 +1,30 @@
using Common.DJYModel ;
using Common.Extensions ;
using Common.Utilities ;
using djy.IService.Afr ;
using djy.Model ;
using djy.Model.Afr ;
using djy.Model.AFRDto ;
using djy.Model.Ams ;
using djy.Model.AmsDto ;
using djy.Service.DjyService ;
using FreeSql .Internal.Model ;
using FreeSql ;
using System ;
using System.Collections.Generic ;
using System.Data.Common ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace djy.Service.AFR
{
public class AfrService : D bContext, IAfrService
public class AfrService : D jyService. D bContext, IAfrService
{
private IUser User { get ; }
public AfrService ( IUser user )
{
User = user ;
}
#region 下拉接口
public List < CommonCNEN > GetCountry ( string strlink , int page , int limit )
{
@ -182,46 +189,299 @@ namespace djy.Service.AFR
public async Task < PageModel < AFRMaster > > Load ( AFRMasterInputDto input )
{
// 查询草稿箱列表
ISelect < AFRMaster > select = null ;
// 查询草稿箱列表,草稿箱列表采用一主单内含有多个分单的显示模式
if ( input . Type = = 1 )
{
var select = DbBus . Get ( DbList . AMSCenter )
. Select < AFRMaster > ( )
. LeftJoin < AFRHouse > ( ( m , h ) = > m . GID = = h . PID )
. Where ( ( m ) = > m . IsDel = = false )
select = DbAMS . Select < AFRMaster > ( ) ;
}
// 查询已发送列表,已发送列表采用按分单的显示方式
else if ( input . Type = = 2 )
{
select = DbAMS . Select < AFRMaster > ( )
. InnerJoin < AFRHouse > ( ( m , h ) = > m . GID = = h . PID ) ;
}
select . Where ( ( m ) = > m . IsDel = = false )
//下面两个是Controller中传来的条件
. WhereIf ( ! string . IsNullOrEmpty ( input . CompanyId ) , m = > m . CompID = = input . CompanyId )
. WhereIf ( ! string . IsNullOrEmpty ( input . UserId ) , m = > m . UserID = = input . UserId )
// 下面是前端传来的条件
. WhereIf ( ! string . IsNullOrEmpty ( input . MBLNO ) , m = > m . MBLNO . Contains ( input . MBLNO ) )
. WhereIf ( ! string . IsNullOrEmpty ( input . UserName ) , m = > m . UserName . Contains ( input . UserName ) )
. WhereIf ( ! string . IsNullOrEmpty ( input . DischargeHarbour ) , m = > m . DischargeHarbour . Contains ( input . DischargeHarbour ) )
. WhereIf ( ! string . IsNullOrEmpty ( input . ShipCompany ) , m = > m . ShipCompany . Contains ( input . ShipCompany ) )
. WhereIf ( input . CreateTimeStart ! = null , m = > m . CreateTime > = input . CreateTimeStart )
. WhereIf ( input . CreateTimeEnd ! = null , m = > m . CreateTime < = input . CreateTimeEnd ) ;
// 分单上的查询条件在这里, 实现方式: 查出分单中的PID, 作为主单的筛选条件
if ( ! string . IsNullOrEmpty ( input . HouseBillNo ) )
{
select . Where < AFRHouse > ( ( m , h ) = > h . HouseBillNo = = input . HouseBillNo ) ;
var pids = await DbAMS . Select < AFRHouse > ( )
. Where ( h = > h . HouseBillNo . Contains ( input . HouseBillNo ) )
. ToListAsync ( h = > h . PID ) ;
select . Where ( m = > pids . Contains ( m . GID ) ) ;
}
List < AFRMaster > result = await select . Page ( input )
. IncludeMany ( m = > m . Houses )
List < AFRMaster > result = await select . IncludeMany ( m = > m . HouseList , then = > then . Where ( h = > h . IsDel = = false ) )
. Page ( input )
. OrderByDescending ( m = > m . CreateTime )
. ToListAsync ( ) ;
// 查询操作历史(只查询最新的一条人为操作历史)
if ( result . Count > 0 )
{
// FreeSql版本较低, 无法使用嵌套查询, 所以采用拼sql的方式
var pids = result . Select ( m = > m . GID ) ;
var pidStr = string . Join ( "','" , pids ) ;
var histories = await DbAMS . Select < AFRMasterHistory > ( )
. WithSql ( @ $ "SELECT * FROM (
SELECT * , row_number ( ) over ( PARTITION BY Pid ORDER BY { nameof ( AFRMasterHistory . CreateTime ) } DESC ) AS row_num FROM AFR_MasterHistory WHERE { nameof ( AFRMasterHistory . Type ) } = 0 AND pid IN ( ' { pidStr } ' ) ) t WHERE row_num = 1 ")
. ToListAsync ( ) ;
histories . ForEach ( item = >
{
var master = result . FirstOrDefault ( m = > m . GID = = item . PID ) ;
if ( master = = null ) return ;
master . History = item ;
} ) ;
}
return new PageModel < AFRMaster > ( input . PageNumber ,
input . Count ,
input . PageSize ,
result ) ;
}
// 查询已发送列表
public async Task < AFRMaster > Get ( string gid )
{
AFRMaster model = await DbAMS . Select < AFRMaster > ( )
. Where ( ( m ) = > m . IsDel = = false & & m . GID = = gid )
. IncludeMany ( m = > m . HouseList , then = > then . Where ( h = > h . IsDel = = false )
. IncludeMany ( house = > house . CntrList ) )
. FirstAsync ( ) ;
return model ;
}
public async Task SaveInfo ( AFRMaster input )
{
int type ;
AFRMaster oldMaster = null ;
var nowTime = DateTime . Now ;
if ( string . IsNullOrEmpty ( input . GID ) )
{
type = 0 ;
}
else
{
return default ;
oldMaster = await Get ( input . GID ) ;
type = oldMaster = = null ? 0 : 1 ;
}
// 新增
if ( type = = 0 )
{
// 将null换为空对象, 避免后续处理null值麻烦
input . HouseList ? ? = new List < AFRHouse > ( ) ;
input . GID = Guid . NewGuid ( ) . ToString ( ) ;
input . UserID = User . GID ;
input . UserName = User . ShowName ;
input . CompID = User . CompId ;
input . CompName = User . CompName ;
input . CreateTime = nowTime ;
input . HouseList . ForEach ( house = >
{
house . GID = Guid . NewGuid ( ) . ToString ( ) ;
house . PID = input . GID ;
house . CreateTime = nowTime ;
// 将null换为空对象, 避免后续处理null值麻烦
house . CntrList ? ? = new List < AFRCntrno > ( ) ;
house . CntrList . ForEach ( cntr = >
{
cntr . GID = Guid . NewGuid ( ) . ToString ( ) ;
cntr . PID = house . GID ;
cntr . CreateTime = nowTime ;
} ) ;
} ) ;
await using DbTransaction tran = await GetDbAmsTransaction ( ) ;
await DbAMS . Insert ( input ) . ExecuteAffrowsAsync ( ) ;
await DbAMS . Insert ( input . HouseList ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
await DbAMS . Insert ( input . HouseList . SelectMany ( h = > h . CntrList ) ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
//HouseBillNo
AFRMasterHistory history = BuildAFRMasterHistory ( input . GID , "新增" , 0 , "创建了单据" ) ;
await DbAMS . Insert ( history ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
tran . Commit ( ) ;
}
public Task SaveInfo ( AFRMasterDto input )
// 修改
else
{
throw new NotImplementedException ( ) ;
// 下面这段的3个作用:
// 1. 将null换为空对象, 避免后续处理null值麻烦
// 2. 如果主键为null, 说明是新加的数据, 这时候需要手动给GID、PID、CreateTime赋值
// 3. 如果主键不为null, 说明是修改的数据, 这时候需要手动给LastUpdate赋值
input . LastUpdate = nowTime ;
if ( input . HouseList = = null )
{
input . HouseList = new List < AFRHouse > ( ) ;
}
else
{
input . HouseList . ForEach ( h = >
{
if ( string . IsNullOrEmpty ( h . GID ) )
{
h . GID = Guid . NewGuid ( ) . ToString ( ) ;
h . PID = input . GID ;
h . CreateTime = nowTime ;
}
else
{
h . LastUpdate = nowTime ;
}
if ( h . CntrList = = null )
{
h . CntrList = new List < AFRCntrno > ( ) ;
}
else
{
h . CntrList . ForEach ( c = >
{
if ( string . IsNullOrEmpty ( c . GID ) )
{
c . GID = Guid . NewGuid ( ) . ToString ( ) ;
c . PID = h . GID ;
c . CreateTime = nowTime ;
}
else
{
c . LastUpdate = nowTime ;
}
} ) ;
}
} ) ;
}
#region 对分单数据统计出要执行的操作,判断哪些数据是 新增/修改/删除
var inputHouseGids = input . HouseList ? . Select ( h = > h . GID ) ;
var oldHouseGids = oldMaster . HouseList ? . Select ( h = > h . GID ) ;
var waitInsertHouseGids = inputHouseGids . Except ( oldHouseGids ) ; // 要新增的分单主键
var waitDeleteHouseGids = oldHouseGids . Except ( inputHouseGids ) ; // 要删除的分单主键
var waitUpdateHouseGids = oldHouseGids . Intersect ( inputHouseGids ) ; // 要修改的分单主键
List < AFRHouse > waitInsertHouseList = null , waitUpdateHouseList = null ;
if ( waitInsertHouseGids . Any ( ) )
{
waitInsertHouseList = input . HouseList . Where ( h = > waitInsertHouseGids . Contains ( h . GID ) ) . ToList ( ) ;
}
if ( waitUpdateHouseGids . Any ( ) )
{
waitUpdateHouseList = input . HouseList . Where ( h = > waitUpdateHouseGids . Contains ( h . GID ) ) . ToList ( ) ;
}
# endregion
#region 对箱子数据统计出要执行的操作,判断哪些数据是 新增/修改/删除
var inputCtnList = input . HouseList . SelectMany ( h = > h . CntrList ) ;
var oldCtnList = oldMaster . HouseList . SelectMany ( h = > h . CntrList ) ;
var inputCtnGids = inputCtnList . Select ( ctn = > ctn . GID ) ;
var oldCtnGids = oldCtnList . Select ( ctn = > ctn . GID ) ;
var waitInsertCtnGids = inputCtnGids . Except ( oldCtnGids ) ; // 要新增的箱子主键
var waitDeleteCtnGids = oldCtnGids . Except ( inputCtnGids ) ; // 要删除的箱子主键
var waitUpdateCtnGids = oldCtnGids . Intersect ( inputCtnGids ) ; // 要修改的箱子主键
List < AFRCntrno > waitInsertCtnList = null , waitUpdateCtnList = null ;
if ( waitInsertCtnGids . Any ( ) )
{
waitInsertCtnList = inputCtnList . Where ( c = > waitInsertCtnGids . Contains ( c . GID ) ) . ToList ( ) ;
}
if ( waitUpdateCtnGids . Any ( ) )
{
waitUpdateCtnList = inputCtnList . Where ( c = > waitUpdateCtnGids . Contains ( c . GID ) ) . ToList ( ) ;
}
# endregion
#region 开始执行
await using DbTransaction tran = await GetDbAmsTransaction ( ) ;
// 主单执行
await DbAMS . Update < AFRMaster > ( )
. SetSource ( input )
. IgnoreColumns ( m = > new { m . CompID , m . CompName , m . UserID , m . UserName } )
. WithTransaction ( tran )
. ExecuteAffrowsAsync ( ) ;
// 分单执行
if ( waitInsertHouseGids . Any ( ) )
{
await DbAMS . Insert ( waitInsertHouseList ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
}
if ( waitDeleteHouseGids . Any ( ) )
{
await DbAMS . Update < AFRHouse > ( )
. Set ( h = > h . IsDel = = true )
. Set ( h = > h . LastUpdate = = nowTime )
. WithTransaction ( tran )
. Where ( h = > waitDeleteHouseGids . Contains ( h . GID ) )
. ExecuteAffrowsAsync ( ) ;
}
if ( waitUpdateHouseGids . Any ( ) )
{
await DbAMS . Update < AFRHouse > ( )
. SetSource ( waitUpdateHouseList )
. IgnoreColumns ( h = > new { h . StateIsMatched , h . StateIsAccept } )
. WithTransaction ( tran )
. ExecuteAffrowsAsync ( ) ;
}
// 箱子执行
if ( waitInsertCtnGids . Any ( ) )
{
await DbAMS . Insert ( waitInsertCtnList ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
}
if ( waitDeleteCtnGids . Any ( ) )
{
await DbAMS . Delete < AFRCntrno > ( )
. WithTransaction ( tran )
. Where ( h = > waitDeleteCtnGids . Contains ( h . GID ) )
. ExecuteAffrowsAsync ( ) ;
}
if ( waitUpdateCtnGids . Any ( ) )
{
await DbAMS . Update < AFRCntrno > ( )
. SetSource ( waitUpdateCtnList )
. WithTransaction ( tran )
. ExecuteAffrowsAsync ( ) ;
}
# endregion
AFRMasterHistory history = BuildAFRMasterHistory ( input . GID , "修改" , 0 , "修改了单据" ) ;
await DbAMS . Insert ( history ) . WithTransaction ( tran ) . ExecuteAffrowsAsync ( ) ;
tran . Commit ( ) ;
}
}
private AFRMasterHistory BuildAFRMasterHistory ( string pid , string state , int type , string remark )
{
var history = new AFRMasterHistory ( )
{
GID = Guid . NewGuid ( ) . ToString ( ) ,
CreateTime = DateTime . Now ,
Operator = User . ShowName ,
PID = pid ,
State = state ,
Type = Convert . ToByte ( type )
} ;
history . Remark = $"{history.Operator}于{history.CreateTime}{remark}" ;
return history ;
}
public Task Delete ( string ids )
@ -238,5 +498,6 @@ namespace djy.Service.AFR
{
throw new NotImplementedException ( ) ;
}
}
}