fix 批量上传
This commit is contained in:
@@ -32,7 +32,7 @@ public class CostUploadJob : BaseJob
|
||||
var endDayOfMonth = e.AddDays(-1);
|
||||
|
||||
//从余额找人
|
||||
var userCodes = await DbContext.Queryable<UserBalanceEntity>()
|
||||
var userCodeAll = await DbContext.Queryable<UserBalanceEntity>()
|
||||
.Where(x => SqlFunc.Subqueryable<UserBalanceUploadRecordEntity>()
|
||||
.Where(p => p.UserCode == x.UserCode)
|
||||
.Where(p => p.Year == s.Year && p.Month == s.Month)
|
||||
@@ -42,51 +42,78 @@ public class CostUploadJob : BaseJob
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var userCode in userCodes)
|
||||
foreach (var userCodes in userCodeAll.Chunk(100))
|
||||
{
|
||||
var balance = await DbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.Where(x => x.CostDate >= s && x.CostDate < e)
|
||||
.Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source))
|
||||
.SumAsync(x => x.Amount);
|
||||
|
||||
if (balance == 0)
|
||||
var dict = new Dictionary<string, decimal>(userCodes.Length);
|
||||
foreach (var userCode in userCodes)
|
||||
{
|
||||
continue;
|
||||
var balance = await DbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.Where(x => x.CostDate >= s && x.CostDate < e)
|
||||
.Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source))
|
||||
.SumAsync(x => x.Amount);
|
||||
|
||||
if (balance == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
dict.Add(userCode, Math.Abs(balance));
|
||||
}
|
||||
|
||||
var response = await jipApi.UploadCostAsync(new UploadCostRequest
|
||||
{
|
||||
Data = new List<UploadCostItemRequest>
|
||||
Data = dict.Select(x => new UploadCostItemRequest
|
||||
{
|
||||
new UploadCostItemRequest
|
||||
{
|
||||
WorkCode = userCode,
|
||||
BeginDate = endDayOfMonth.ToString("yyyy-MM-dd"),
|
||||
Charge = Math.Abs(balance),
|
||||
Code= "2086"
|
||||
}
|
||||
}
|
||||
WorkCode = x.Key,
|
||||
BeginDate = endDayOfMonth.ToString("yyyy-MM-dd"),
|
||||
Charge = x.Value,
|
||||
Code = "2086"
|
||||
|
||||
}).ToList()
|
||||
});
|
||||
|
||||
if (response.Data.FirstOrDefault(x => x.WorkCode == userCode)?.Success == "S")
|
||||
|
||||
var handleData = response.Data.Where(x => x.Success == "S").ToList();
|
||||
var workCodes = handleData.Select(x => x.WorkCode).ToList();
|
||||
|
||||
var records = await DbContext.Queryable<UserBalanceUploadRecordEntity>()
|
||||
.Where(x => workCodes.Contains(x.UserCode))
|
||||
.Where(x => x.Year == s.Year && x.Month == s.Month)
|
||||
.ToListAsync();
|
||||
|
||||
var updatedRecords = new List<UserBalanceUploadRecordEntity>();
|
||||
var newRecords = new List<UserBalanceUploadRecordEntity>();
|
||||
foreach (var item in handleData)
|
||||
{
|
||||
if (!await DbContext.Queryable<UserBalanceUploadRecordEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Where(x => x.Year == s.Year && x.Month == s.Month)
|
||||
.AnyAsync())
|
||||
var record = records.FirstOrDefault(x => x.UserCode == item.WorkCode);
|
||||
if (record != null)
|
||||
{
|
||||
await DbContext.InsertAsync(new UserBalanceUploadRecordEntity
|
||||
record.Cost = dict[item.WorkCode];
|
||||
updatedRecords.Add(record);
|
||||
}
|
||||
else
|
||||
{
|
||||
newRecords.Add(new UserBalanceUploadRecordEntity
|
||||
{
|
||||
UserCode = item.WorkCode,
|
||||
Year = s.Year,
|
||||
Month = s.Month,
|
||||
UserCode = userCode,
|
||||
Cost = balance,
|
||||
Cost = dict[item.WorkCode]
|
||||
});
|
||||
}
|
||||
}
|
||||
if (updatedRecords.Any())
|
||||
{
|
||||
await DbContext.UpdateAsync(updatedRecords);
|
||||
}
|
||||
|
||||
if (newRecords.Any())
|
||||
{
|
||||
await DbContext.InsertAsync(newRecords);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user