@@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Components.Forms;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System.Drawing.Drawing2D;
using System.Text;
namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Services
{
@@ -40,22 +41,15 @@ namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Service
int total = new RefAsync<int>();
var data = SqlSugarDb.Db.Queryable<BPA_Batching, BPA_BatchingType, BPA_BatchingUint>((a, b, c) =>
new JoinQueryInfos(JoinType.Left, a.Batching_Type == b.Id,
JoinType.Left, a.StockUint == c.Id))
.Where((a, b, c) => a.IsDeleted == 0)
.WhereIF(!string.IsNullOrEmpty(inputDto.Name), (a, b, c) => a.Batching_Name.Contains(inputDto.Name))
.Select((a, b, c) => new MaterialDto()
var data = await SqlSugarDb.Db.Queryable<BPA_Batching>()
.WhereIF(!string.IsNullOrEmpty(inputDto.Name), a => a.Name.Contains(inputDto.Name))
.Select(a => new MaterialDto()
{
Id = a.Id,
Code = a.Code,
Name = a.Batching_Name,
//TypeId = b.Id,
TypeName = b.Name,
// UintId = c.Id,
UintName = c.Name,
Name = a.Name,
}).ToPageList(inputDto.Current, inputDto.PageSize, ref total);
}).ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
return new PageUtil<List<MaterialDto>>()
{
@@ -68,112 +62,175 @@ namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Service
/// <summary>
/// 添加物料
/// </summary>
/// <param name="I nputDto"></param>
/// <param name="i nputDto"></param>
/// <returns></returns>
public async Task<string> AddMaterial(BaseRequestDto <MaterialCreateDto> inputDto)
public async Task<List<ResponseMessageDto>> AddMaterial(List <MaterialCreateDto> inputDto)
{
try
{
var list=new List<BPA_Batching>();
SqlSugarDb.Db.Ado.BeginTran();
foreach (var item in inputDto.DataInfo)
var userId = await _checkServices.GetUserId(App.HttpContext.Request.Headers["key"].ToString());
var batchingList = await SqlSugarDb.Db.Queryable<BPA_Batching>().ToListAsync();
var typeList = await SqlSugarDb.Db.Queryable<BPA_BatchingType>().ToListAsync();
var uintList = await SqlSugarDb.Db.Queryable<BPA_BatchingUint>().ToListAsync();
var typeIdList = new List<string>();
var uintIdList = new List<string>();
var repeatList = new List<ResponseMessageDto>();
var successList = new List<ResponseMessageDto>();
var defaultUintData = uintList.FirstOrDefault(t => t.Name == "g");
var defaultUint = new DefaultUintDto();
if (defaultUintData != null)
{
//1.物料单位查询
var typeData = await SqlSugarDb.Db.Queryable<BPA_BatchingType>().FirstAsync(x => x.Name == item.TypeName);
if (typeData == null)
defaultUint.Id = defaultUintData.Id;
defaultUint.Name = defaultUintData.Name;
}
else
{
var dUint = new BPA_BatchingUint
{
typeData = new BPA_BatchingType()
Id = Guid.NewGuid().ToString(),
Name = "默认单位",
CreateBy = userId
};
await SqlSugarDb.Db.Insertable(dUint).ExecuteCommandAsync();
defaultUint.Id = dUint.Id;
defaultUint.Name = dUint.Name;
}
var typeAddList = new List<BPA_BatchingType>();
var typeUpdateList = new List<BPA_BatchingType>();
var uintAddList = new List<BPA_BatchingUint>();
var uintUpdateList = new List<BPA_BatchingUint>();
var batchingAddList = new List<BPA_Batching>();
var batchingUpdateList = new List<BPA_Batching>();
foreach (var item in inputDto)
{
var batchingData = batchingList.FirstOrDefault(t => t.Name == item.Name && t.Id != item.Id);
if (batchingData != null)
repeatList.Add(new ResponseMessageDto { Code = 30001, Message = "物料上传失败:重复物料数据", Id = item.Id, Name = item.Name });
var typeData = typeList.FirstOrDefault(t => t.Name == item.TypeName && t.Id != item.TypeId);
if (typeData != null)
repeatList.Add(new ResponseMessageDto { Code = 30001, Message = "物料类型上传失败:重复物料类型数据", Id = item.TypeId, Name = item.TypeName });
if (string.IsNullOrWhiteSpace(item.UintId))
{
item.UintId = defaultUint.Id;
item.UintName = defaultUint.Name;
}
else
{
var uintData = uintList.FirstOrDefault(t => t.Name == item.UintName && t.Id != item.UintId);
if (uintData != null)
repeatList.Add(new ResponseMessageDto { Code = 30001, Message = "物料单位上传失败:重复物料单位数据", Id = item.UintId, Name = item.UintName });
}
var typeInfo = typeList.FirstOrDefault(x => x.Id == item.TypeId);
if (typeInfo == null)
{
if (!typeIdList.Contains(item.TypeId))
{
GroupId = CurrentUser.GroupId,
Id = Guid.NewGuid().ToString(),
Name = string.IsNullOrEmpty(item.TypeName) ? "默认分类" : item.TypeName,
};
SqlSugarDb.Db.Insertable(typeData).ExecuteCommand();
typeInfo = new BPA_BatchingType()
{
Id = item.TypeId,
Name = item.TypeName,
CreateBy = userId
};
typeAddList.Add(typeInfo);
typeIdList.Add(item.TypeId);
}
}
//2.单位查询
var uintData = await SqlSugarDb.Db.Queryable<BPA_BatchingUint>()
.FirstAsync(x => x.Name == item.UintName);
if (uintData == null)
else
{
uintData = new BPA_BatchingUint()
typeInfo.Name = item.TypeName;
typeUpdateList.Add(typeInfo);
}
successList.Add(new ResponseMessageDto { Code = 30000, Message = "物料类型上传成功", Id = item.TypeId, Name = item.TypeName });
var uintInfo = uintList.FirstOrDefault(x => x.Id == item.UintId);
if (uintInfo == null)
{
if (!uintIdList.Contains(item.UintId))
{
GroupId = CurrentUser.GroupId,
Id = Guid.NewGuid().ToString(),
Name = string.IsNullOrEmpty(item.UintName) ? "默认分类" : item.UintName,
};
SqlSugarDb.Db.Insertable(uintData).ExecuteCommand();
uintInfo = new BPA_BatchingUint()
{
Id = item.UintId,
Name = item.UintName,
CreateBy = userId
};
uintAddList.Add(uintInfo);
uintIdList.Add(item.UintId);
}
}
//3.物料查询
var materialData = await SqlSugarDb.Db.Queryable<BPA_Batching>().FirstAsync(x => x.Batching_Name == item.Name || x.Code == item.Code);
if (materialData == null)
else
{
uintInfo.Name = item.UintName;
uintUpdateList.Add(uintInfo);
}
successList.Add(new ResponseMessageDto { Code = 30000, Message = "物料单位上传成功", Id = item.UintId, Name = item.UintName });
var code = GenerateRandomString();
while (batchingList.Any(t => t.Code == code))
{
materialData = new BPA_Batching()
code = GenerateRandomString();
}
var batchingInfo = batchingList.FirstOrDefault(t => t.Id == item.Id);
if (batchingInfo == null)
{
batchingInfo = new BPA_Batching()
{
Id = Guid.NewGuid().ToString(),
GroupId = CurrentUser.GroupId,
Aittribute = 0,
Batching_Name = item.Name,
Batching_Type = typeData.Id,
Code = item.Code,
Price = 0,
Specs = "",
outstockUint = uintData.Id,
Status = CommonStatus.ENABLE,
StockUint = uintData.Id,
TypeID = typeData.Id,
IsDeleted = 0,
Id = item.Id,
Name = item.Name,
Code = code,
//StockUint = item.UintId,
CreateBy = userId
};
SqlSugarDb.Db.Insertable(materialData).ExecuteCommand();
batchingAddList.Add(batchingInfo);
}
else
{
throw Oops.Oh(ErrorCodeEnum.Code1002);
batchingInfo.Name = item.Name;
//batchingInfo.StockUint = item.UintId;
batchingUpdateList.Add(batchingInfo);
}
list.Add(materialData);
successList.Add(new ResponseMessageDto { Code = 30000, Message = "物料上传成功", Id = item.Id, Name = item.Name } );
}
if (repeatList.Count > 0)
return repeatList;
await SqlSugarDb.Db.Insertable(typeAddList).ExecuteCommandAsync();
await SqlSugarDb.Db.Updateable(typeUpdateList).ExecuteCommandAsync();
await SqlSugarDb.Db.Insertable(uintAddList).ExecuteCommandAsync();
await SqlSugarDb.Db.Updateable(uintUpdateList).ExecuteCommandAsync();
await SqlSugarDb.Db.Insertable(batchingAddList).ExecuteCommandAsync();
await SqlSugarDb.Db.Updateable(batchingUpdateList).ExecuteCommandAsync();
SqlSugarDb.Db.Ado.CommitTran();
#region 下发数据到设备
var data = new List<PushDataBatchingDto>();
foreach (var item in list)
{
data.Add(new PushDataBatchingDto()
{
Aittribute = item.Aittribute,
AutoKey = item.AutoKey,
Code = item.Code,
ForeignKeyRe = item.ForeignKeyRe,
Id = item.Id,
Name = item.Batching_Name,
netrecovery = item.netrecovery,
outstockUint = item.outstockUint,
Price = item.Price,
proportion = item.proportion,
Specs = item.Specs,
StockUint = item.StockUint,
StockUintName = item.StockUint,
TypeID = item.TypeID,
});
}
if (inputDto.IsPush)
{
await _thirdpartyPushService.AddPushRecordAndPushDevice<MaterialCreateDto>(inputDto, 2, JsonConvert.SerializeObject(list.Select(x => x.Id).ToList()),
JsonConvert.SerializeObject(data));
}
return successList;
#region 下发数据到设备(后厨弃用)
// var data = new List<PushDataBatchingDto>();
// foreach (var item in list)
// {
// data.Add(new PushDataBatchingDto()
// {
// Aittribute = item.Aittribute,
// AutoKey = item.AutoKey,
// Code = item.Code,
// ForeignKeyRe = item.ForeignKeyRe,
// Id = item.Id,
// Name = item.Batching_Name,
// netrecovery = item.netrecovery,
// outstockUint = item.outstockUint,
// Price = item.Price,
// proportion = item.proportion,
// Specs = item.Specs,
// StockUint = item.StockUint,
// StockUintName = item.StockUint,
// TypeID = item.TypeID,
// });
// }
// if (inputDto.IsPush)
// {
// await _thirdpartyPushService.AddPushRecordAndPushDevice<MaterialCreateDto>(inputDto, 2, JsonConvert.SerializeObject(list.Select(x => x.Id).ToList()),
// JsonConvert.SerializeObject(data));
// }
#endregion
return JsonConvert.SerializeObject(list.Select(x => x.Id).ToList());
}
catch (Exception e)
{
@@ -185,17 +242,15 @@ namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Service
/// <summary>
/// 删除物料
/// </summary>
/// <param name="MaterialId "></param>
/// <param name="inputDto "></param>
/// <returns></returns>
public async Task<bool> DelMaterial(DelMaterialDto inputDto)
{
try
{
var materialData = await SqlSugarDb.Db.Queryable<BPA_Batching>().FirstAsync(x => x.Id == inputDto.MaterialId );
var materialData = await SqlSugarDb.Db.Queryable<BPA_Batching>().Where(x => inputDto.Ids.Contains(x.Id)).ToListAsync( );
if (materialData == null)
{
throw Oops.Oh(ErrorCodeEnum.Code1003);
}
throw Oops.Oh("物料不存在");
var res = await SqlSugarDb.Db.Deleteable(materialData).ExecuteCommandAsync();
return res > 0;
}
@@ -203,118 +258,102 @@ namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Service
{
throw Oops.Oh(e.Message);
}
}
/// <summary>
/// 修改物料
/// </summary>
/// <param name="I nputDto"></param>
/// <param name="i nputDto"></param>
/// <returns></returns>
public async Task<bool> UpdateMateria(BaseRequestDto <MaterialUpdateDto> inputDto)
public async Task<bool> UpdateMateria(List <MaterialUpdateDto> inputDto)
{
try
{
SqlSugarDb.Db.Ado.BeginTran();
if (inputDto.DataInfo.Count>1)
{
throw Oops.Oh("不支持多物料修改");
}
var materia = inputDto.DataInfo.FirstOrDefault();
//1.物料单位查询
var typeData = await SqlSugarDb.Db.Queryable<BPA_BatchingType>().FirstAsync(x => x.Name == materia.TypeName);
if (typeData == null)
var batchingList = await SqlSugarDb.Db.Queryable<BPA_Batching>().ToListAsync();
var typeList = await SqlSugarDb.Db.Queryable<BPA_BatchingType>().ToListAsync();
var uintList = await SqlSugarDb.Db.Queryable<BPA_BatchingUint>().ToListAsync();
var typeIdList = new Dictionary<string, string>();
var uintIdList = new Dictionary<string, string>();
foreach (var item in inputDto)
{
typeData = new BPA_BatchingType()
{
GroupId = CurrentUser.GroupId,
Id = Guid.NewGuid().ToString(),
Name = string.IsNullOrEmpty(materia.TypeName) ? "默认分类" : materia.TypeName,
};
SqlSugarDb.Db.Insertable(typeData).ExecuteCommand();
}
//2.单位查询
var uintData = await SqlSugarDb.Db.Queryable<BPA_BatchingUint>()
.FirstAsync(x => x.Name == materia.UintName);
if (uintData == null)
{
uintData = new BPA_BatchingUint()
{
GroupId = CurrentUser.GroupId,
Id = Guid.NewGuid().ToString(),
Name = string.IsNullOrEmpty(materia.UintName) ? "默认分类" : materia.UintName,
};
SqlSugarDb.Db.Insertable(uintData).ExecuteCommand();
}
//3.物料查询
var material = await SqlSugarDb.Db.Queryable<BPA_Batching>().FirstAsync(x => x.Code == materia.Code || x.Batching_Name == materia.Name);
// if(material==null) throw Oops.Oh(ErrorCodeEnum.Code1003);
var materialData = await SqlSugarDb.Db.Queryable<BPA_Batching>().FirstAsync(x => x.Id == materia.Id);
if (materialData == null)
{
throw Oops.Oh(ErrorCodeEnum.Code1003);
}
else
{
if (material != null && material.Id == materialData.Id)
//1.物料单位查询
var typeId = string.Empty;
var typeData = typeList.FirstOrDefault(x => x.Name == item.TypeName);
if (typeData == null)
{
throw Oops.Oh(ErrorCodeEnum.Code10017);
if (!typeIdList.ContainsKey(item.TypeName))
{
typeData = new BPA_BatchingType()
{
Id = Guid.NewGuid().ToString(),
Name = item.TypeName
};
SqlSugarDb.Db.Insertable(typeData).ExecuteCommand();
typeIdList.Add(item.TypeName, typeData.Id);
typeId = typeData.Id;
}
else
typeId = typeIdList[item.TypeName];
}
materialData = new BPA_Batching()
else
typeId = typeData.Id;
//2.单位查询
var uintId = string.Empty;
var uintData = uintList.FirstOrDefault(x => x.Name == item.UintName);
if (uintData == null)
{
Id = materia.Id,
GroupId = CurrentUser.GroupId,
Aittribute = 0,
Batching_Name = materia.Name,
Batching_Type = typeData.Id,
Code = materia.Code,
Price = 0,
Specs = "",
outstockUint = uintData.Id,
Status = CommonStatus.ENABLE,
StockUint = uintData.Id,
TypeID = typeData.Id,
IsDeleted = 0,
};
SqlSugarDb.Db.Updateable(materialData).ExecuteCommand();
if (!uintIdList.ContainsKey(item.UintName))
{
uintData = new BPA_BatchingUint()
{
Id = Guid.NewGuid().ToString(),
Name = item.UintName
};
SqlSugarDb.Db.Insertable(uintData).ExecuteCommand();
uintIdList.Add(item.UintName, uintData.Id);
uintId = uintData.Id;
}
else
uintId = uintIdList[item.UintName];
}
else
uintId = uintData.Id;
var bathingData = batchingList.FirstOrDefault(t => t.Id == item.Id);
if (bathingData == null)
throw Oops.Oh($"物料“{item.Name}”不存在");
bathingData.Name = item.Name;
//bathingData.StockUint = uintId;
SqlSugarDb.Db.Updateable(bathingData).ExecuteCommand();
#region 下发数据到设备(后厨弃用)
//var data = new PushDataBatchingDto()
//{
// Aittribute = materialData.Aittribute,
// AutoKey = materialData.AutoKey,
// Code = materialData.Code,
// ForeignKeyRe = materialData.ForeignKeyRe,
// Id = materialData.Id,
// Name = materialData.Batching_Name,
// netrecovery = materialData.netrecovery,
// outstockUint = materialData.outstockUint,
// Price = materialData.Price,
// proportion = materialData.proportion,
// Specs = materialData.Specs,
// StockUint = materialData.StockUint,
// StockUintName = materialData.StockUint,
// TypeID = materialData.TypeID,
//};
//if (inputDto.IsPush)
//{
// await _thirdpartyPushService.AddPushRecordAndPushDevice<MaterialUpdateDto>(inputDto, 2, materialData.Id,
// JsonConvert.SerializeObject(data));
//}
#endregion
}
SqlSugarDb.Db.Ado.CommitTran();
#region 下发数据到设备
var data = new PushDataBatchingDto()
{
Aittribute = materialData.Aittribute,
AutoKey = materialData.AutoKey,
Code = materialData.Code,
ForeignKeyRe = materialData.ForeignKeyRe,
Id = materialData.Id,
Name = materialData.Batching_Name,
netrecovery = materialData.netrecovery,
outstockUint = materialData.outstockUint,
Price = materialData.Price,
proportion = materialData.proportion,
Specs = materialData.Specs,
StockUint = materialData.StockUint,
StockUintName = materialData.StockUint,
TypeID = materialData.TypeID,
};
if (inputDto.IsPush)
{
await _thirdpartyPushService.AddPushRecordAndPushDevice<MaterialUpdateDto>(inputDto, 2, materialData.Id,
JsonConvert.SerializeObject(data));
}
#endregion
return true;
}
catch (Exception e)
@@ -323,5 +362,44 @@ namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Service
throw Oops.Oh(e.Message);
}
}
private static Random random = new();
public static string GenerateRandomString()
{
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string digits = "0123456789";
StringBuilder result = new();
for (int i = 0; i < 4; i++)
{
result.Append(letters[random.Next(letters.Length)]);
}
for (int i = 0; i < 4; i++)
{
result.Append(digits[random.Next(digits.Length)]);
}
return result.ToString();
}
/// <summary>
/// 分页查询物料分类
/// </summary>
/// <param name="inputDto"></param>
/// <returns></returns>
public async Task<PageUtil<List<MaterialTypeDto>>> GetMaterialTypePageList(MaterialPageInputDto inputDto)
{
int total = new RefAsync<int>();
var data = await SqlSugarDb.Db.Queryable<BPA_BatchingType>()
.WhereIF(!string.IsNullOrEmpty(inputDto.Name), a => a.Name.Contains(inputDto.Name))
.Select(a => new MaterialTypeDto()
{
Id = a.Id,
Name = a.Name
}).ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
return new PageUtil<List<MaterialTypeDto>>()
{
Total = total,
Data = data
};
}
}
}