using System.Text;
using AngleSharp.Dom;
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Fee.Interface;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Flow.Interface;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using LanguageExt;
using Mapster;
using Masuit.Tools.Systems;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
///
/// 费用记录实现
///
public class FeeRecordService : IFeeRecordService
{
const long PERMISSION_ID = 1793490768447541248;
readonly string DefaultCurrency = "CNY";
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
readonly IClientFlowInstanceService flowService;
public FeeRecordService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
flowService = _serviceProvider.GetRequiredService();
}
///
/// 列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable()
.Where(whereList)
.Select()
.ToQueryPage(request.PageCondition);
//关联用户名称
var userIds = data.Data.Where(x => x.UpdateBy.HasValue).Select(x => x.UpdateBy.Value)
//.Union(data.Data.Where(x => x.SubmitBy.HasValue).Select(x => x.SubmitBy.Value))
.Union(data.Data.Select(x => x.CreateBy)).Distinct();
var users = db.Queryable().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToList();
foreach (var item in data.Data)
{
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
if (item.UpdateBy.HasValue)
{
item.UpdateByName = users.Find(x => x.Id == item.UpdateBy.Value)?.UserName;
}
//if (item.SubmitBy.HasValue)
//{
// item.SubmitByName = users.Find(x => x.Id == item.SubmitBy.Value)?.UserName;
//}
}
return data;
}
///
/// 列表
///
///
///
public DataResult> GetList(string query)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var src = tenantDb.Queryable();
if (!query.IsNullOrEmpty())
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(query);
src = src.Where(whereList);
}
var data = src.ToList();
return new DataResult>(ResultCode.Success) { Data = data };
}
///
/// 检查业务是否已费用锁定
///
/// 业务ID
/// 锁定范围
///
bool IsFeeLocked(long bid, FeeType type = FeeType.All)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
bool? isFeeLocking = null;
switch (type)
{
case FeeType.Receivable:
break;
case FeeType.Payable:
break;
case FeeType.All:
isFeeLocking = tenantDb.Queryable().Where(x => x.BusinessId == bid).Select(x => x.IsFeeLocking).First();
break;
}
return isFeeLocking.GetValueOrDefault();
}
///
/// 提交
///
/// 业务ID
/// 要提交的费用记录
///
public DataResult InsertOrUpdate(long bid, IEnumerable items)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (IsFeeLocked(bid))
return DataResult.Failed("当前业务已费用锁定,禁止提交", MultiLanguageConst.Operation_Failed);
try
{
tenantDb.Ado.BeginTran();
DateTime dtNow = DateTime.Now;
var feeIds = items.Where(x => x.Id > 0).Select(x => x.Id).ToArray();
//包含修改的项,需要检测费用状态再修改
if (feeIds.Length > 0)
{
var fees = tenantDb.Queryable().Where(x => feeIds.Contains(x.Id)).Select(x => new FeeRecord
{
Id = x.Id,
FeeName = x.FeeName,
FeeStatus = x.FeeStatus
}).ToList();
StringBuilder sb = new StringBuilder();
foreach (var fe in fees)
{
if (fe.FeeStatus != FeeStatus.Entering && fe.FeeStatus != FeeStatus.RejectSubmission)
{
sb.Append($"费用【{fe.FeeName}】状态不正确,无法修改;");
continue;
}
}
if (sb.Length > 0)
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
}
//若计价货币单位不等于默认货币则尝试获取最新汇率
FetchExchangeRate(tenantDb, items);
List list = new List(items.Count());
foreach (var item in items)
{
item.FeeStatus = FeeStatus.Entering;
if (item.Id == 0)
{
item.BusinessId = bid;
tenantDb.Insertable(item).ExecuteCommand();
}
else
{
tenantDb.Updateable(item).IgnoreColumns(x => new
{
x.FeeStatus,
x.CreateBy,
x.CreateTime,
x.BusinessId,
x.DeleteBy,
x.Deleted,
x.DeleteTime,
x.SubmitDate,
x.SubmitBy
}).ExecuteCommand();
}
list.Add(item);
}
tenantDb.Ado.CommitTran();
var list2 = list.Select(x => x.Adapt()).ToList();
return DataResult.Successed("保存成功", list2, MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
ex.Log(db);
return DataResult.Failed("保存失败", MultiLanguageConst.DataUpdateFailed);
}
}
//获取汇率
void FetchExchangeRate(SqlSugarScopeProvider tenantDb, IEnumerable records)
{
var exRecords = records.Where(x => !x.ExchangeRate.HasValue && x.Currency != DefaultCurrency).ToList();
if (exRecords.Count > 0)
{
var codes = exRecords.Select(x => x.Currency).Distinct().ToList();
var currencies = tenantDb.Queryable().Where(x => codes.Contains(x.CodeName)).Includes(x => x.Exchanges).ToList();
DateTime dtNow = DateTime.Now;
foreach (var item in exRecords)
{
var currency = currencies.Find(x => x.CodeName == item.Currency);
if (currency != null)
{
item.ExchangeRate = currency.DefaultRate;
if (currency.Exchanges != null)
{
//取当前时间范围内的最新一条
var exchange = currency.Exchanges.FindAll(x => x.Status == StatusEnum.Enable &&
x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.UpdateTime).FirstOrDefault();
if (exchange != null)
item.ExchangeRate = item.FeeType == FeeType.Receivable ? exchange.DRValue : exchange.CRValue;
}
}
}
}
}
///
/// 根据模板ID创建
///
/// 业务ID
/// 模板ID
///
public DataResult CreateByTemplate(long bid, params long[] tidArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
bool hasExists = tenantDb.Queryable().LeftJoin((x, y) =>
x.FeeId == y.FeeId && x.FeeType == y.FeeType).Any((x, y) =>
x.BusinessId == bid && tidArray.Contains(y.TemplateId) && !y.Deleted);
if (hasExists)
return DataResult.Failed("费用记录已存在", MultiLanguageConst.FeeRecordExist);
var details = tenantDb.Queryable().Where(x => tidArray.Contains(x.TemplateId) && !x.Deleted).Select(x => new
{
x.FeeType,
x.FeeId,
x.FeeCode,
x.FeeName,
x.FeeFrt,
x.FeeGroup,
x.CustomerName,
x.CustomerType,
x.CustomerId,
x.Unit,
x.UnitPrice,
x.Currency,
x.ExchangeRate,
x.Tax,
x.TaxRate,
x.AccTaxRate,
x.IsAdvancedPay,
x.IsInvoice,
x.SaleOrgId,
x.Note
}).ToList();
List records = new List(details.Count);
foreach (var item in details)
{
var record = item.Adapt();
record.BusinessId = bid;
record.SubmitDate = DateTime.Now;
records.Add(record);
}
//若计价货币单位不等于默认货币则尝试获取最新汇率
FetchExchangeRate(tenantDb, records);
int result = tenantDb.Insertable(records).ExecuteCommand();
return result > 0 ? DataResult.Successed("保存成功", records, MultiLanguageConst.DataCreateSuccess) : DataResult.Successed("操作失败!", MultiLanguageConst.Operation_Failed);
}
///
/// 根据费用明细转换为模板明细
///
/// 费用明细ID
///
public DataResult> ReadAsTemplate(params long[] idArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var list = tenantDb.Queryable().Where(x => idArray.Contains(x.Id)).Select().ToList();
return new DataResult>(ResultCode.Success) { Data = list };
}
///
/// 删除
///
/// 费用记录ID
///
public DataResult Delete(params long[] ids)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var bid = tenantDb.Queryable().Where(x => ids.Contains(x.Id)).Select(x => x.BusinessId).First();
if (IsFeeLocked(bid))
return DataResult.Failed("当前业务已费用锁定,禁止修改", MultiLanguageConst.Operation_Failed);
if (tenantDb.Queryable().Any(x => ids.Contains(x.Id) && (x.FeeStatus != FeeStatus.Entering && x.FeeStatus != FeeStatus.RejectSubmission)))
return DataResult.Failed("只能删除状态为‘录入’或‘驳回提交’的费用", MultiLanguageConst.FeeRecordDelete);
int result = tenantDb.Deleteable(x => ids.Contains(x.Id)).ExecuteCommand();
return result > 0 ? DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess) : DataResult.Failed("删除失败!", MultiLanguageConst.Operation_Failed);
}
///
/// 发起审批/申请删除工作流
///
/// 审批类型
/// 费用记录ID
///
public DataResult SubmitForApproval(FeeAuditType auditType, params long[] idArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var fees = tenantDb.Queryable().Where(x => idArray.Contains(x.Id)).Select(x => new FeeRecord
{
Id = x.Id,
FeeName = x.FeeName,
FeeStatus = x.FeeStatus,
FlowId = x.FlowId,
BusinessId = x.BusinessId
}).ToList();
if (fees.IsNullOrEmpty())
return DataResult.Failed($"未能获取费用信息,提交失败", MultiLanguageConst.Operation_Failed);
//业务状态检测
if (IsFeeLocked(fees[0].BusinessId))
return DataResult.Failed("当前业务已费用锁定,禁止修改", MultiLanguageConst.Operation_Failed);
if (fees.Any(x => x.FlowId.HasValue))
return DataResult.Failed($"当前费用包含正在审批中的费用,无法提交", MultiLanguageConst.Operation_Failed);
if (fees.Any(x => x.FeeStatus == FeeStatus.PartialSettlement || x.FeeStatus == FeeStatus.SettlementCompleted))
return DataResult.Failed($"当前审批费用包含已结算/部分结算的费用,无法提交", MultiLanguageConst.Operation_Failed);
DataResult result = DataResult.Failed(string.Empty, MultiLanguageConst.Operation_Failed);
tenantDb.Ado.BeginTran();
try
{
if (auditType == FeeAuditType.ApplyAudit)
{
result = ApplyAudit(tenantDb, fees);
}
else if (auditType == FeeAuditType.ApplyDeletion)
{
result = ApplyDelete(tenantDb, fees);
}
if (!result.Succeeded)
{
tenantDb.Ado.RollbackTran();
return result;
}
tenantDb.Updateable(fees).UpdateColumns(x => new
{
x.Id,
x.FeeStatus,
x.SubmitBy,
x.SubmitDate,
x.FlowId
}).ExecuteCommand();
tenantDb.Ado.CommitTran();
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
ex.Log(db);
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
}
}
//录入->提交审核
DataResult ApplyAudit(SqlSugarScopeProvider tenantDb, List fees)
{
StringBuilder sb = new StringBuilder();
foreach (var fe in fees)
{
if (fe.FeeStatus != FeeStatus.Entering && fe.FeeStatus != FeeStatus.RejectSubmission)
{
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
continue;
}
}
if (sb.Length > 0)
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
var template = FindTemplate(FeeAuditType.ApplyAudit.ToString());
if (template == null)
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
DateTime dtNow = DateTime.Now;
foreach (var item in fees)
{
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq { BusinessId = item.Id, TemplateId = template.Id });
if (result.Succeeded)
{
var instance = result.Data as FlowInstance;
flowService.StartFlowInstance(instance.Id.ToString());
//变更状态为提交审核
item.FeeStatus = FeeStatus.AuditSubmitted;
item.SubmitBy = long.Parse(user.UserId);
item.SubmitDate = dtNow;
item.FlowId = instance.Id;
}
}
return DataResult.Success;
}
//审核通过->申请删除
DataResult ApplyDelete(SqlSugarScopeProvider tenantDb, List fees)
{
StringBuilder sb = new StringBuilder();
foreach (var fe in fees)
{
if (fe.FeeStatus != FeeStatus.AuditPassed && fe.FeeStatus != FeeStatus.RejectApplication)
{
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
continue;
}
}
if (sb.Length > 0)
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
var template = FindTemplate(FeeAuditType.ApplyDeletion.ToString());
if (template == null)
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
DateTime dtNow = DateTime.Now;
foreach (var item in fees)
{
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq { BusinessId = item.Id, TemplateId = template.Id });
if (result.Succeeded)
{
var instance = result.Data as FlowInstance;
flowService.StartFlowInstance(instance.Id.ToString());
//变更状态为申请删除
item.FeeStatus = FeeStatus.ApplyDeletion;
item.SubmitBy = long.Parse(user.UserId);
item.SubmitDate = dtNow;
item.FlowId = instance.Id;
}
}
return DataResult.Success;
}
///
/// 发起费用修改申请
///
/// 费用修改信息
///
public DataResult SubmitForModification(IEnumerable items)
{
var idList = items.Select(x => x.FeeRecordId).Distinct().ToList();
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var fees = tenantDb.Queryable().Select(x => new FeeRecord
{
Id = x.Id,
FeeName = x.FeeName,
FeeStatus = x.FeeStatus
}).ToList();
StringBuilder sb = new StringBuilder();
foreach (var fe in fees)
{
if (fe.FeeStatus != FeeStatus.AuditPassed && fe.FeeStatus != FeeStatus.RejectApplication)
{
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
continue;
}
}
if (sb.Length > 0)
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
var template = FindTemplate(FeeAuditType.ApplyModification.ToString());
if (template == null)
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
DateTime dtNow = DateTime.Now;
tenantDb.Ado.BeginTran();
try
{
foreach (var fee in fees)
{
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq { BusinessId = fee.Id, TemplateId = template.Id });
if (result.Succeeded)
{
var instance = result.Data as FlowInstance;
flowService.StartFlowInstance(instance.Id.ToString());
//变更状态为申请修改
fee.FeeStatus = FeeStatus.ApplyModification;
fee.SubmitBy = long.Parse(user.UserId);
fee.SubmitDate = dtNow;
fee.FlowId = instance.Id;
}
}
var list = items.ToList();
tenantDb.Insertable(list).ExecuteCommand();
tenantDb.Updateable(fees).UpdateColumns(x => new { x.FeeStatus, x.FlowId }).ExecuteCommand();
tenantDb.Ado.CommitTran();
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
ex.Log(db);
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
}
}
///
/// 根据审批结果更新审批状态
///
/// 回调信息
///
public DataResult UpdateAuditStatus(FlowCallback callback)
{
var auditType = callback.AuditType.ToEnum();
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
FeeRecord fee = null;
BusinessFeeStatus businessFee = null;
if (auditType == FeeAuditType.Business)
{
businessFee = tenantDb.Queryable().Where(x => x.Id == callback.BusinessId).Select(
x => new BusinessFeeStatus { Id = x.Id, BusinessId = x.BusinessId, BillAuditStatus = x.BillAuditStatus }).First();
if (businessFee == null)
return DataResult.Failed("未能找到业务信息,更新状态失败", MultiLanguageConst.Operation_Failed);
}
else
{
fee = tenantDb.Queryable().Where(x => x.Id == callback.BusinessId).Select(
x => new FeeRecord { Id = x.Id, FeeStatus = x.FeeStatus }).First();
if (fee == null)
return DataResult.Failed("未能找到费用记录,更新状态失败", MultiLanguageConst.Operation_Failed);
fee.Reason = callback.RejectReason;
}
tenantDb.Ado.BeginTran();
try
{
switch (auditType)
{
case FeeAuditType.ApplyAudit:
fee.AuditBy = long.Parse(user.UserId);
fee.AuditDate = DateTime.Now;
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
fee.FeeStatus = FeeStatus.AuditPassed;
fee.Reason = string.Empty;
}
else if (callback.FlowStatus == FlowStatusEnum.Reject)
fee.FeeStatus = FeeStatus.RejectSubmission;
tenantDb.Updateable(fee).UpdateColumns(x => new
{
x.Id,
x.FeeStatus,
x.AuditBy,
x.AuditDate,
x.Reason,
x.FlowId
}).ExecuteCommand();
break;
case FeeAuditType.ApplyModification:
//申请修改审核成功需要回填费用信息
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
fee.FeeStatus = FeeStatus.AuditPassed;
fee.Reason = string.Empty;
var fm = tenantDb.Queryable().Where(x => x.FeeRecordId == fee.Id).OrderByDescending(x => x.CreateTime).First();
if (fm == null)
return DataResult.Failed("未找到费用修改信息,更新失败", MultiLanguageConst.Operation_Failed);
var entity = fm.Adapt();
entity.FeeStatus = FeeStatus.AuditPassed;
entity.Reason = callback.RejectReason;
fee = entity;
fm.Deleted = true;
fm.DeleteTime = DateTime.Now;
fm.DeleteBy = long.Parse(user.UserId);
tenantDb.Updateable(fm).UpdateColumns(x => new
{
x.Id,
x.DeleteBy,
x.Deleted,
x.DeleteTime
}).ExecuteCommand();
}
else if (callback.FlowStatus == FlowStatusEnum.Reject)
fee.FeeStatus = FeeStatus.RejectApplication;
tenantDb.Updateable(fee).UpdateColumns(x => new
{
x.Id,
x.FeeStatus,
x.Reason,
x.FlowId
}).ExecuteCommand();
break;
case FeeAuditType.ApplyDeletion:
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
tenantDb.Deleteable(fee).ExecuteCommand();
}
else
{
fee.FeeStatus = FeeStatus.RejectApplication;
tenantDb.Updateable(fee).UpdateColumns(x => new
{
x.Id,
x.FeeStatus,
x.Reason,
x.FlowId
}).ExecuteCommand();
}
break;
case FeeAuditType.Business:
FeeStatus status = FeeStatus.RejectSubmission;
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
businessFee.BillAuditStatus = BillAuditStatus.AuditPassed;
status = FeeStatus.AuditPassed;
}
else if (callback.FlowStatus == FlowStatusEnum.Reject)
{
businessFee.BillAuditStatus = BillAuditStatus.Rejected;
}
tenantDb.Updateable(businessFee).UpdateColumns(x => new
{
x.BillAuditStatus,
x.FlowId
}).ExecuteCommand();
tenantDb.Updateable().
SetColumns(x => x.FeeStatus == status).
SetColumns(x=>x.FlowId == null).
Where(x => x.BusinessId == businessFee.BusinessId &&
(x.FeeStatus == FeeStatus.Entering || x.FeeStatus == FeeStatus.RejectSubmission)).ExecuteCommand();
break;
default:
return DataResult.Failed("费用未处于审批状态,更新状态失败", MultiLanguageConst.Operation_Failed);
}
//驳回申请则逻辑删除关联工作流
if (callback.FlowStatus == FlowStatusEnum.Reject)
{
db.Updateable(new FlowInstance
{
Id = callback.InstanceId,
Deleted = true,
DeleteBy = long.Parse(user.UserId),
DeleteTime = DateTime.Now
}).UpdateColumns(x => new { x.Deleted, x.DeleteBy, x.DeleteTime }).ExecuteCommand();
}
tenantDb.Ado.CommitTran();
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
ex.Log(db);
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
}
}
///
/// 撤销审批
///
/// 费用记录ID
///
public DataResult Withdraw(params long[] idArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var fees = tenantDb.Queryable().Where(x => idArray.Contains(x.Id)).Select(x => new FeeRecord
{
Id = x.Id,
FeeName = x.FeeName,
FeeStatus = x.FeeStatus,
FlowId = x.FlowId
}).ToList();
if (fees.IsNullOrEmpty())
return DataResult.Failed("未能找到费用记录", MultiLanguageConst.Operation_Failed);
//未在审批状态中
var fees2 = fees.FindAll(x => x.FlowId == null).ToList();
if (fees2.Count > 0)
{
string msg = string.Join("; ", fees2.Select(x => $"{x.FeeName}"));
return DataResult.Failed($"以下费用项:{msg} 未在审批状态中,无需撤销", MultiLanguageConst.Operation_Failed);
}
var flows = fees.Select(x => new FlowInstance { Id = x.FlowId.Value, FlowStatus = (int)FlowStatusEnum.Draft, MakerList = string.Empty }).ToList();
DateTime dtNow = DateTime.Now;
try
{
tenantDb.Ado.BeginTran();
db.Updateable(flows).UpdateColumns(x => new { x.FlowStatus, x.MakerList }).ExecuteCommand();
foreach (var item in fees)
{
switch (item.FeeStatus)
{
case FeeStatus.AuditSubmitted:
item.FeeStatus = FeeStatus.Entering;
break;
case FeeStatus.ApplyModification:
item.FeeStatus = FeeStatus.AuditPassed;
break;
case FeeStatus.ApplyDeletion:
item.FeeStatus = FeeStatus.AuditPassed;
break;
}
item.SubmitBy = long.Parse(user.UserId);
item.SubmitDate = dtNow;
item.FlowId = null;
}
tenantDb.Updateable(fees).UpdateColumns(x => new { x.Id, x.FeeStatus, x.SubmitBy, x.SubmitDate, x.FlowId }).ExecuteCommand();
tenantDb.Ado.CommitTran();
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
tenantDb.Ado.RollbackTran();
ex.Log(db);
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
}
}
///
/// 整单审核
///
/// 业务类型
/// 业务ID
///
public DataResult AuditBusiness(BusinessType type, long bid)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var entity = tenantDb.Queryable().Where(x => x.BusinessId == bid &&
x.BusinessType == BusinessType.OceanShippingExport).Select(x => new BusinessFeeStatus
{
Id = x.Id,
IsFeeLocking = x.IsFeeLocking,
BillAuditStatus = x.BillAuditStatus,
FlowId = x.FlowId
}).First();
if (entity == null)
return DataResult.Failed("未能找到业务信息", MultiLanguageConst.Operation_Failed);
if (entity.IsFeeLocking.GetValueOrDefault())
return DataResult.Failed("当前业务已费用锁定,禁止提交", MultiLanguageConst.Operation_Failed);
if (entity.FlowId.HasValue)
return DataResult.Failed($"当前业务正在审批中,无法提交", MultiLanguageConst.Operation_Failed);
if (entity.BillAuditStatus == BillAuditStatus.AuditPassed)
return DataResult.Failed($"当前业务的费用状态为:{entity.BillAuditStatus.GetDescription()},无法提交", MultiLanguageConst.Operation_Failed);
var template = FindTemplate(FeeAuditType.Business.ToString());
if (template == null)
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq { BusinessId = entity.Id, TemplateId = template.Id });
if (result.Succeeded)
{
var instance = result.Data as FlowInstance;
flowService.StartFlowInstance(instance.Id.ToString());
//变更状态为提交审核
entity.BillAuditStatus = BillAuditStatus.AuditSubmitted;
entity.BillFeeStatusTime = DateTime.Now;
entity.FlowId = instance.Id;
tenantDb.Updateable(entity).UpdateColumns(x => new
{
x.BillAuditStatus,
x.BillFeeStatusTime,
x.FlowId
}).ExecuteCommand();
tenantDb.Ado.CommitTran();
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
}
return DataResult.Failed("创建工作流失败", MultiLanguageConst.Operation_Failed);
}
//回写业务主表的费用状态
void WriteBackStatus(SqlSugarScopeProvider tenantDb, List fees)
{
}
///
/// 查找模板
///
/// 审批类型
///
FlowTemplateTenant FindTemplate(string auditType)
{
return db.Queryable().Where(x =>
x.Status == StatusEnum.Enable &&
x.PermissionId == PERMISSION_ID &&
x.AuditType == auditType).First();
}
}
}