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.

179 lines
8.0 KiB
C#

using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.TaskPlat.Dtos;
using DS.WMS.Core.TaskPlat.Entity;
using DS.WMS.Core.TaskPlat.Interface;
using LanguageExt.Pipes;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DS.WMS.Core.TaskPlat.Method
{
/// <summary>
/// 任务分配设置维护服务
/// </summary>
public class TaskAllocationService : ITaskAllocationService
{
private readonly IUser user;
private readonly ILogger<TaskManageBCService> logger;
private readonly ISaasDbService saasDbService;
public TaskAllocationService(IUser user,
ILogger<TaskManageBCService> logger,
ISaasDbService saasDbService)
{
this.user = user;
this.logger = logger;
this.saasDbService = saasDbService;
}
/// <summary>
/// 获取任务分配列表
/// </summary>
/// <param name="carrierId">船公司Id可选</param>
public async Task<DataResult<TaskAllocationtSetQueryListDto>> GetList(long? carrierId)
{
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
var list = await tenantDb.Queryable<TaskAllocationtSet>()
.Where(x => x.CarrierId == carrierId &&
(x.IsAllotCustom
|| x.IsAllotCustomerService
|| x.IsAllotDispatcher
|| x.IsAllotOperator
|| x.IsAllotSale
|| x.IsAllotVouchingClerk
|| x.IsAllotFinancialStaff
|| x.IsAllotDriver))
.ToListAsync();
Func<TaskAllocationtSet, TaskTypeInfoDto> selector = x => new TaskTypeInfoDto() { TaskTypeCode = x.TaskTypeCode, TaskTypeName = x.TaskTypeName };
TaskAllocationtSetQueryListDto result = new()
{
OperatorTaskTypeList = list.Where(x => x.IsAllotOperator).Select(selector).ToList(),
VouchingClerkTaskTypeList = list.Where(x => x.IsAllotVouchingClerk).Select(selector).ToList(),
SaleTaskTypeList = list.Where(x => x.IsAllotSale).Select(selector).ToList(),
CustomTaskTypeList = list.Where(x => x.IsAllotCustom).Select(selector).ToList(),
FinancialStaffTaskTypeList = list.Where(x => x.IsAllotFinancialStaff).Select(selector).ToList(),
CustomerServiceTaskTypeList = list.Where(x => x.IsAllotCustomerService).Select(selector).ToList(),
DriverTaskTypeList = list.Where(x => x.IsAllotDriver).Select(selector).ToList(),
DispatcherTaskTypeList = list.Where(x => x.IsAllotDispatcher).Select(selector).ToList(),
};
//if (carrierId != null && list.Count != 0)
//{
// result.CarrierId = list.First().CarrierId;
// result.Carrier = list.First().Carrier;
// result.CarrierCode = list.First().CarrierCode;
//}
return DataResult<TaskAllocationtSetQueryListDto>.Success(result);
}
/// <summary>
/// 获取任务分配列表中已存在的船公司列表(用于查询)
/// </summary>
public async Task<DataResult<List<CarrierInfoDto>>> GetContainsCarrierList()
{
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
var carrierList = await tenantDb.Queryable<TaskAllocationtSet>().Select(x => new CarrierInfoDto()
{
CarrierId = x.CarrierId,
Carrier = x.Carrier,
CarrierCode = x.CarrierCode
}).Where(x => x.CarrierId != null).Distinct().ToListAsync();
return DataResult<List<CarrierInfoDto>>.Success(carrierList);
}
/// <summary>
/// 保存任务分配设置
/// </summary>
public async Task<DataResult> Save(TaskAllocationtSetSaveDto saveDto)
{
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
if (string.IsNullOrEmpty(saveDto.CarrierCode))
{
saveDto.CarrierCode = null;
}
if (string.IsNullOrEmpty(saveDto.Carrier))
{
saveDto.Carrier = null;
}
if (saveDto.CarrierId == 0)
{
saveDto.CarrierId = null;
}
var fieldName = saveDto.AllocationTargetCode switch
{
"Operator" => nameof(TaskAllocationtSet.IsAllotOperator),
"VouchingClerk" => nameof(TaskAllocationtSet.IsAllotVouchingClerk),
"Sale" => nameof(TaskAllocationtSet.IsAllotSale),
"Custom" => nameof(TaskAllocationtSet.IsAllotCustom),
"FinancialStaff" => nameof(TaskAllocationtSet.IsAllotFinancialStaff),
"CustomerService" => nameof(TaskAllocationtSet.IsAllotCustomerService),
"Driver" => nameof(TaskAllocationtSet.IsAllotDriver),
"Dispatcher" => nameof(TaskAllocationtSet.IsAllotDispatcher),
_ => throw new NotImplementedException(),
};
try
{
// 先查出来已有的、指定船公司的配置
var oldSettingList = await tenantDb.Queryable<TaskAllocationtSet>().Where(x => x.CarrierId == saveDto.CarrierId).Select(x => new { x.Id, x.TaskTypeCode }).ToListAsync();
var oldSettingIdList = oldSettingList.Select(x => x.Id);
var oldSettingTypeCodeList = oldSettingList.Select(x => x.TaskTypeCode);
await tenantDb.Ado.BeginTranAsync();
// 将原有的状态设置为未分配
await tenantDb.Updateable<TaskAllocationtSet>()
.SetColumns(fieldName, false)
.Where(x => oldSettingIdList.Contains(x.Id))
.ExecuteCommandAsync();
// 补充缺少的任务类型
if (saveDto.TaskTypeList?.Any() == true)
{
var waitAddTypeCodeList = saveDto.TaskTypeList.Select(x => x.TaskTypeCode).Except(oldSettingTypeCodeList).ToList();
if (waitAddTypeCodeList.Count > 0)
{
var waitAddSetList = waitAddTypeCodeList.Select(x => new TaskAllocationtSet()
{
CarrierId = saveDto.CarrierId,
Carrier = saveDto.Carrier,
CarrierCode = saveDto.CarrierCode,
TaskTypeCode = x,
TaskTypeName = saveDto.TaskTypeList.First(t => t.TaskTypeCode == x).TaskTypeName,
}).ToList();
await tenantDb.Insertable(waitAddSetList).ExecuteCommandAsync();
}
var setTaskCodeList = saveDto.TaskTypeList.Select(x => x.TaskTypeCode);
// 更新指定船公司的配置
await tenantDb.Updateable<TaskAllocationtSet>()
.SetColumns(fieldName, true)
.Where(x => x.CarrierId == saveDto.CarrierId && setTaskCodeList.Contains(x.TaskTypeCode))
.ExecuteCommandAsync();
}
await tenantDb.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await tenantDb.Ado.RollbackTranAsync();
logger.LogError(ex, "保存任务分配设置异常");
throw;
}
return DataResult.Successed(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.DataUpdateSuccess)));
}
}
}