using DS.Module.Core; using DS.Module.Core.Data; using DS.Module.Core.Extensions; using DS.WMS.Core.Fee.Dtos; using DS.WMS.Core.Flow.Dtos; using DS.WMS.Core.Flow.Entity; using DS.WMS.Core.Flow.Interface; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Info.Interface; using DS.WMS.Core.Op.Dtos.TaskInteraction; using DS.WMS.Core.Op.Interface.TaskInteraction; using Masuit.Tools.Systems; using Microsoft.Extensions.DependencyInjection; namespace DS.WMS.Core.Info.Method { /// /// 往来单位干系人 /// public class ClientStakeholderService : ServiceBase, IClientStakeholderService { const TaskBaseTypeEnum CLIENT_STAKEHOLDER_TASK = TaskBaseTypeEnum.CLIENT_STAKEHOLDER_AUDIT; Lazy flowService; Lazy taskService; /// /// 初始化 /// /// public ClientStakeholderService(IServiceProvider serviceProvider) : base(serviceProvider) { flowService = new Lazy(serviceProvider.GetRequiredService()); taskService = new Lazy(serviceProvider.GetRequiredService()); } #region 审核 /// /// 提交审核 /// /// /// public async Task SubmitAuditAsync(IdModel idModel) { var list = await TenantDb.Queryable() .InnerJoin((x, y) => x.ClientId == y.Id) .Where((x, y) => idModel.Ids.Contains(x.Id)).Select((x, y) => new { x.Id, x.Status, x.CreateBy, x.CreateByName, CustShortName = y.ShortName }).ToListAsync(); if (list.Exists(x => x.Status == StakeholderStatus.Pending)) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.ItemsAreAuditing)); if (await taskService.Value.HasAuthorizedAsync()) { var requests = list.Select(x => new TaskCreationRequest { BusinessId = x.Id, TaskTypeName = CLIENT_STAKEHOLDER_TASK.ToString(), TaskTitle = $"【{CLIENT_STAKEHOLDER_TASK.GetDescription()}】{x.CustShortName} {x.CreateByName}" }); await TenantDb.Ado.BeginTranAsync(); try { DataResult result; foreach (var request in requests) { result = await taskService.Value.CreateTaskAsync(request, false); if (!result.Succeeded) return result; } await TenantDb.Updateable().Where(x => idModel.Ids.Contains(x.Id)) .SetColumns(x => x.Status == StakeholderStatus.Pending).ExecuteCommandAsync(); await TenantDb.Ado.CommitTranAsync(); return DataResult.Success; } catch (Exception ex) { await TenantDb.Ado.RollbackTranAsync(); await ex.LogAsync(Db); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } var template = await FindTemplateAsync(CLIENT_STAKEHOLDER_TASK); if (template == null) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TemplateNotFound)); for (int i = 0; i < idModel.Ids.Length; i++) { var id = idModel.Ids[i]; var result = flowService.Value.CreateFlowInstance(new CreateFlowInstanceReq { BusinessId = id, TemplateId = template.Id }); if (!result.Succeeded || result.Data is not FlowInstance instance) return result; result = flowService.Value.StartFlowInstance(instance.Id.ToString()); if (!result.Succeeded) return result; } var rows = await TenantDb.Updateable().Where(x => idModel.Ids.Contains(x.Id)) .SetColumns(x => x.Status == StakeholderStatus.Pending).ExecuteCommandAsync(); return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } /// /// 执行审核 /// /// /// public async Task AuditAsync(AuditRequest request) { if (await taskService.Value.HasAuthorizedAsync()) { return await taskService.Value.AuditAsync(new TaskAuditRequest { Ids = request.Ids, Remark = request.Remark, Result = request.Result, TaskTypeName = CLIENT_STAKEHOLDER_TASK.ToString() }); } var list = await flowService.Value.GetInstanceByBSIdAsync(CLIENT_STAKEHOLDER_TASK, ids: request.Ids); if (list.Count != request.Ids.Length) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.NotInAudit)); foreach (var item in list) { var result = flowService.Value.AuditFlowInstance(new FlowAuditInfo { AuditNote = request.Remark, Status = request.Result, Instance = item }); if (!result.Succeeded) return result; } return DataResult.Success; } /// /// 撤销审核 /// /// /// public async Task WithdrawAsync(IdModel idModel) { DataResult result; if (await taskService.Value.HasAuthorizedAsync()) { await TenantDb.Ado.BeginTranAsync(); try { for (int i = 0; i < idModel.Ids.Length; i++) { result = await taskService.Value.WithdrawAsync(new TaskRequest { BusinessId = idModel.Ids[i], TaskTypeName = CLIENT_STAKEHOLDER_TASK.ToString() }, false); if (!result.Succeeded) { return result; } } await TenantDb.Updateable().Where(x => idModel.Ids.Contains(x.Id)) .SetColumns(x => x.Status == StakeholderStatus.Uncommitted) .SetColumns(x => x.Remark == null) .ExecuteCommandAsync(); await TenantDb.Ado.CommitTranAsync(); return DataResult.Success; } catch { await TenantDb.Ado.RollbackTranAsync(); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } var list = await flowService.Value.GetInstanceByBSIdAsync(CLIENT_STAKEHOLDER_TASK, ids: idModel.Ids); if (list.Count != idModel.Ids.Length) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.NotInAudit)); result = await flowService.Value.WithdrawAsync(idModel.Ids); if (!result.Succeeded) return result; int rows = await TenantDb.Updateable().Where(x => idModel.Ids.Contains(x.Id)) .SetColumns(x => x.Status == StakeholderStatus.Uncommitted).ExecuteCommandAsync(); return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } /// /// 审核完成回调 /// /// 回调信息 /// public async Task AuditCallbackAsync(FlowCallback callback) { if (callback.AuditType != CLIENT_STAKEHOLDER_TASK) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.NoAuditItems)); InfoClientStakeholder entity = new() { Id = callback.BusinessId, Remark = callback.RejectReason, Status = callback.FlowStatus == FlowStatusEnum.Approve ? StakeholderStatus.Approved : StakeholderStatus.Rejected }; int rows = await TenantDb.Updateable(entity).UpdateColumns(x => new { x.Remark, x.Status }).ExecuteCommandAsync(); return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } #endregion /// /// 列表 /// /// /// public async Task>> GetListByPageAsync(PageRequest request) { //序列化查询条件 var whereList = request.GetConditionalModels(Db); var result = await TenantDb.Queryable() .Where(whereList) .ToQueryPageAsync(request.PageCondition); if (result.Data?.Count > 0) { var ids = result.Data.Select(x => x.ClientId).Distinct(); var clients = await TenantDb.Queryable().Where(x => ids.Contains(x.Id)) .Select(x => new { x.Id, x.ShortName }).ToListAsync(); foreach (var item in result.Data) { item.ClientShortName = clients.Find(x => x.Id == item.ClientId)?.ShortName; } } return result; } /// /// 获取客户干系人 /// /// /// public async Task> GetAsync(long id) { var model = await TenantDb.Queryable().FirstAsync(x => x.Id == id); return DataResult.Success(model); } /// /// 创建/更新客户干系人 /// /// 干系人 /// public async Task EditAsync(InfoClientStakeholder stakeholder) { await TenantDb.Storageable(stakeholder).DefaultAddElseUpdate().ExecuteCommandAsync(); var result = DataResult.Success; result.Data = stakeholder; return result; } /// /// 创建客户干系人并提交审核 /// /// 干系人 /// public async Task AddSubmitAsync(InfoClientStakeholder stakeholder) { await TenantDb.Insertable(stakeholder).ExecuteCommandAsync(); return await SubmitAuditAsync(new IdModel { Id = stakeholder.Id.ToString(), Ids = [stakeholder.Id] }); } } }