using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.FriendlyException;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDev;
using Mapster;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
namespace Tnb.WarehouseMgr
{
///
/// 点位管理业务类
///
[OverideVisualDev(ModuleConsts.MODULE_WMSPOINT_ID)]
public class WmsPointService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
public WmsPointService(ISqlSugarRepository repository, IUserManager userManager)
{
_db = repository.AsSugarClient();
_userManager = userManager;
OverideFuncs.ImportDataAsync = DataImport;
}
private async Task DataImport(VisualDevImportDataInput input)
{
int row = 0;
List> errorlist = new();
VisualDevImportDataOutput result = new();
try
{
List> dics = input.list;
List points = new();
WmsPointH pt = new();
List aCodes = new();
List lCodes = new();
//遍历字典,找出需要查询数据库拿的相关字段
foreach (Dictionary d in dics)
{
if (d.Select(x => x.Value.ToString()).ToList().Find(v => v != "" && v != string.Empty && v != null) == null)
{
continue;
}
_ = d.ContainsKey("location_code");
string pCode = d["point_code"]?.ToString() ?? string.Empty;
string pName = d["point_name"]?.ToString() ?? string.Empty;
string aCode = d["area_code"]?.ToString() ?? string.Empty;
string lCode = d["location_code"]?.ToString() ?? string.Empty;
string floor = d["floor"]?.ToString() ?? string.Empty;
if (pCode == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据点位编号不可为空", 500);
}
if (pName == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据点位名称不可为空", 500);
}
if (floor == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据楼层不可为空", 500);
}
aCodes.Add(aCode);
lCodes.Add(lCode);
d["create_time"] = DateTime.Now;
_ = d.Remove("modify_time");
pt = d.Adapt();
points.Add(pt);
}
Dictionary aDic = await _db.Queryable().Where(it => aCodes.FindAll(x => x.ToString().IsNotEmptyOrNull() && x.ToString() != "").Contains(it.code)).ToDictionaryAsync(x => x.code, x => x.id);
Dictionary lDic = await _db.Queryable().Where(it => lCodes.FindAll(x => x.ToString().IsNotEmptyOrNull() && x.ToString() != "").Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id);
string orgId = _userManager.User.OrganizeId;
string userId = _userManager.UserId;
foreach (WmsPointH p in points)
{
if (!aDic.ContainsKey(p.area_code))
{
throw new AppFriendlyException($"第{points.IndexOf(p) + 1}个数据的管理区编号有误", 500);
}
p.id = SnowflakeIdHelper.NextId();
p.org_id = orgId;
p.is_lock = 0;
p.status = 1;
p.point_x = 0;
p.point_y = 0;
p.point_z = 0;
p.location_id = p.location_code != null && p.location_code != string.Empty ? lDic[p.location_code]?.ToString() : null;
p.area_id = aDic[p.area_code]?.ToString() ?? throw new AppFriendlyException($"第{points.IndexOf(p) + 1}个数据的管理区编号编号有误", 500);
p.create_id = userId;
p.modify_id = null;
p.modify_time = null;
}
if (points.Count > 1000)
{
row = await _db.Fastest().BulkCopyAsync(points);
}
else if (points.Count > 400)
{
_db.Utilities.PageEach(points, 100, async pageList =>
{
row = await _db.Insertable(pageList).ExecuteCommandAsync();
});
}
else
{
row = await _db.Insertable(points).ExecuteCommandAsync();
}
}
catch (Exception ex)
{
throw Oops.Bah(ex.Message);
}
result = new VisualDevImportDataOutput()
{
snum = row,
fnum = 0,
failResult = errorlist,
resultType = errorlist.Count < 1 ? 0 : 1
};
return result;
}
}
}