You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
9.6 KiB
C#

using System.Linq.Expressions;
7 months ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
7 months ago
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Flow.Interface;
using DS.WMS.Core.Op.Entity;
7 months ago
using DS.WMS.Core.Sys.Entity;
using Mapster;
using Masuit.Tools.Systems;
7 months ago
namespace DS.WMS.Core.Flow.Method;
/// <summary>
/// <20><EFBFBD>˹<EFBFBD><CBB9><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public class ClientFlowInstanceService : FlowInstanceService, IClientFlowInstanceService
7 months ago
{
/// <summary>
/// <20><>ʼ<EFBFBD><CABC>
/// </summary>
/// <param name="serviceProvider"></param>
public ClientFlowInstanceService(IServiceProvider serviceProvider) : base(serviceProvider)
7 months ago
{
}
/// <summary>
/// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="businessType">ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="ids">ҵ<><D2B5>ID</param>
/// <returns></returns>
public async Task<bool> IsRunning(TaskBaseTypeEnum type, BusinessType? businessType = null, params long[] ids)
{
return await Exists(type, businessType,
x => x.FlowStatus == FlowStatusEnum.Ready || x.FlowStatus == FlowStatusEnum.Running, ids);
}
/// <summary>
/// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="businessType">ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="expression"><3E>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
/// <param name="ids">ҵ<><D2B5>ID</param>
/// <returns></returns>
public async Task<bool> Exists(TaskBaseTypeEnum? type = null,
BusinessType? businessType = null,
Expression<Func<FlowInstance, bool>>? expression = null,
params long[] ids)
{
if (ids == null || ids.Length == 0)
return false;
return await Db.Queryable<FlowInstance>().Where(x => ids.Contains(x.BusinessId))
.WhereIF(type.HasValue, x => x.AuditType == type)
.WhereIF(businessType.HasValue, x => x.BusinessType == businessType)
.WhereIF(expression != null, expression)
.AnyAsync();
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>ID<49><44>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// /// <param name="businessType">ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="ids">ҵ<><D2B5>ID</param>
/// <returns></returns>
public async Task<List<FlowInstance>> GetInstanceByBSIdAsync(TaskBaseTypeEnum type, BusinessType? businessType = null, params long[] ids)
{
if (ids == null || ids.Length == 0)
return [];
return await Db.Queryable<FlowInstance>().Where(x => x.AuditType == type && ids.Contains(x.BusinessId) &&
(x.FlowStatus == FlowStatusEnum.Ready || x.FlowStatus == FlowStatusEnum.Running))
.WhereIF(businessType.HasValue, x => x.BusinessType == businessType)
.OrderByDescending(x => x.CreateTime).Take(1).ToListAsync();
}
protected override FlowInstance? BuildInstance(CreateFlowInstanceReq req)
7 months ago
{
if (string.IsNullOrEmpty(req.TemplateId.ToString()))
return null;
7 months ago
FlowTemplateTenant template = Db.Queryable<FlowTemplateTenant>().First(x => x.Id == req.TemplateId);
7 months ago
return template == null ? null : new FlowInstance
7 months ago
{
CustomName = template.Name,
TemplateId = template.Id,
BusinessId = req.BusinessId,
BusinessType = req.BusinessType,
7 months ago
PermissionId = template.PermissionId,
ColumnView = template.ColumnView,
Content = template.Content,
MarkerNotifyURL = template.MarkerNotifyURL,
CallbackURL = template.CallbackURL,
AuditType = template.AuditType
7 months ago
};
}
protected override string GetForkNodeMakers(FlowRuntime wfruntime, string forkNodeId)
7 months ago
{
string makerList = "";
var nextNode = wfruntime.NextNode;
var nextConditionNodeId = wfruntime.GetNextConditionNodeId(nextNode);
var nextConditionNode = wfruntime.ChildNodes.Find(x => x.Id == nextConditionNodeId);
if (nextConditionNode == null)
return makerList;
7 months ago
if (nextConditionNode.AssigneeType == "role")
{
var users = Db.Queryable<SysRoleUser>().Where(x =>
7 months ago
nextConditionNode.Roles.Contains(x.RoleId.ToString()))
.Select(x => x.UserId).Distinct().ToList();
makerList = string.Join(",", users);
7 months ago
}
else if (nextConditionNode.AssigneeType == "user")
{
makerList = string.Join(",", nextConditionNode.Users);
}
return makerList;
}
protected override FlowRuntime CreateRuntimeService(FlowInstance instance)
{
return new FlowRuntime(instance, Db, TenantDb, User);
}
6 months ago
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>Ϣ
6 months ago
/// </summary>
/// <param name="businessId">ҵ<><D2B5>ID</param>
/// <param name="businessType">ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="types"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
6 months ago
/// <returns></returns>
public DataResult<List<FlowInstanceRes>> GetFlowInstances(long businessId, BusinessType? businessType, params TaskBaseTypeEnum[]? types)
7 months ago
{
var query = Db.Queryable<FlowInstance>().Where(x => x.BusinessId == businessId && x.FlowStatus != FlowStatusEnum.Ready)
.WhereIF(businessType.HasValue, x => x.BusinessType == businessType.Value)
.WhereIF(types != null && types.Length > 0, x => types.Contains(x.AuditType.Value));
var list = query.Select(x => new FlowInstance
{
Id = x.Id,
Content = x.Content,
AuditType = x.AuditType,
FlowStatus = x.FlowStatus
}).ToList();
var list2 = list.Select(x => x.Adapt<FlowInstanceRes>()).ToList();
foreach (var item in list2)
{
item.AuditTypeName = item.AuditType?.GetDescription();
}
return DataResult<List<FlowInstanceRes>>.Success(list2);
7 months ago
}
6 months ago
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="info"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5></param>
/// <returns></returns>
public DataResult AuditFlowInstance(FlowAuditInfo info)
{
ArgumentNullException.ThrowIfNull(info, nameof(info));
6 months ago
if (info.Instance == null)
return DataResult.Failed(<>ܻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", MultiLanguageConst.Operation_Failed);
if (info.Instance.FlowStatus == FlowStatusEnum.Approve)
6 months ago
return DataResult.Failed("<22>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", MultiLanguageConst.FlowInstanceFinished);
if (info.Instance.CallbackURL.IsNullOrEmpty())
{
var template = Db.Queryable<FlowTemplateTenant>().Where(x => x.Id == info.Instance.TemplateId)
.Select(x => new { x.MarkerNotifyURL, x.CallbackURL }).First();
info.Instance.CallbackURL = template.CallbackURL;
info.Instance.MarkerNotifyURL = template.MarkerNotifyURL;
}
6 months ago
return AuditFlowCore(info.Status, info.AuditNote, info.Instance);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="taskType"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="bsIds">ҵ<><D2B5>ID</param>
/// <param name="businessType">ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="note"><3E><>ע</param>
/// <returns></returns>
public async Task<DataResult> WithdrawAsync(TaskBaseTypeEnum taskType, long[] bsIds,
BusinessType? businessType = null, string? note = null)
{
var instances = await GetInstanceByBSIdAsync(taskType, businessType, bsIds);
return await WithdrawCoreAsync(instances, note);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="ids">ID</param>
/// <param name="note"><3E><>ע</param>
/// <returns></returns>
public async Task<DataResult> WithdrawAsync(long[] ids, string? note = null)
{
var instances = ids.Select(x => new FlowInstance
{
Id = x,
ActivityId = FlowChild.END,
MakerList = string.Empty,
FlowStatus = FlowStatusEnum.Draft,
Note = note,
//Deleted = true,
//DeleteBy = userId,
//DeleteTime = dt,
//DeleteUserName = userInfo.UserName
}).ToList();
return await WithdrawCoreAsync(instances, note);
}
internal async Task<DataResult> WithdrawCoreAsync(List<FlowInstance> instances, string? note = null)
{
ArgumentNullException.ThrowIfNull(instances, nameof(instances));
long userId = long.Parse(User.UserId);
var userInfo = await Db.Queryable<SysUser>().Where(x => x.Id == userId).Select(x => new { x.Id, x.UserName }).FirstAsync();
//DateTime dt = DateTime.Now;
await Db.Ado.BeginTranAsync();
try
{
await Db.Updateable(instances).UpdateColumns(x => new
{
x.ActivityId,
x.MakerList,
x.FlowStatus,
x.Note,
//x.Deleted,
//x.DeleteBy,
//x.DeleteTime,
//x.DeleteUserName
}).ExecuteCommandAsync();
var historys = instances.Select(x => new FlowInstanceHistory
{
InstanceId = x.Id,
Content = $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{userInfo.UserName}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>{note}",
Result = -1,
UserName = userInfo.UserName
}).ToList();
await Db.Insertable(historys).ExecuteCommandAsync();
await Db.Ado.CommitTranAsync();
return DataResult.Successed("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>!", MultiLanguageConst.FlowInstanceCancelSuccess);
}
catch (Exception ex)
{
await ex.LogAsync(Db);
await Db.Ado.RollbackTranAsync();
return DataResult.FailedWithDesc(MultiLanguageConst.Operation_Failed);
}
}
7 months ago
}