Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/LocationDefinitionService.cs
2024-08-27 22:44:42 +08:00

677 lines
31 KiB
C#

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 Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.Common.Redis;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 库位定义业务类
/// </summary>
[OverideVisualDev(ModuleConsts.MODULE_LOCATIONDEFINITION_ID)]
public class LocationDefinitionService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
private readonly RedisData _redisData;
public LocationDefinitionService(ISqlSugarRepository<BasLocation> repository, IUserManager userManager, RedisData redisData)
{
_db = repository.AsSugarClient();
_userManager = userManager;
OverideFuncs.ImportDataAsync = DataImport;
_redisData=redisData;
}
private async Task<dynamic> DataImport(VisualDevImportDataInput input)
{
int row = 0;
List<Dictionary<string, object>> errorlist = new();
VisualDevImportDataOutput result = new();
try
{
List<Dictionary<string, object>> dics = input.list;
List<BasLocation> locs = new();
BasLocation loc = new();
List<string> cStdCodes = new();
List<string> whCodes = new();
List<string> rgCodes = new();
//遍历字典,找出需要查询数据库拿的相关字段
foreach (Dictionary<string, object> d in dics)
{ /*
if (d.Select(x => x.Value.ToString()).ToList().Find(v => v != "" && v != string.Empty && v != null) == null)
{
continue;
}*/
string LCode = d["location_code"]?.ToString() ?? string.Empty;
if (LCode == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据库位编号不可为空", 500);
}
string isType = d["is_type"]?.ToString() ?? string.Empty;
if (isType == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据库位类型不可为空", 500);
}
string floor = d["floor"]?.ToString() ?? string.Empty;
if (floor == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据楼层不可为空", 500);
}
string layers = d["layers"]?.ToString() ?? string.Empty;
string locLine = d["loc_line"]?.ToString() ?? string.Empty;
string locColumn = d["loc_column"]?.ToString() ?? string.Empty;
if (locLine == string.Empty || locColumn == string.Empty || layers == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据行列层不可为空", 500);
}
string isSign = d["is_sign"]?.ToString() ?? string.Empty;
if (isSign == string.Empty)
{
throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据自动签收不可为空", 500);
}
string cStdCode = d["carrystd_id"]?.ToString() ?? string.Empty;
string whCode = d["wh_id"]?.ToString() ?? string.Empty;
string rgCode = d["region_id"]?.ToString() ?? string.Empty;
cStdCodes.Add(cStdCode);
whCodes.Add(whCode);
rgCodes.Add(rgCode);
d["create_time"] = DateTime.Now;
_ = d.Remove("modify_time");
loc = d.Adapt<BasLocation>();
locs.Add(loc);
}
Dictionary<string, object> carryStdDic = await _db.Queryable<WmsCarrystd>().Where(it => cStdCodes.FindAll(x => x.ToString().IsNotEmptyOrNull() && x.ToString() != "").Contains(it.carrystd_code)).ToDictionaryAsync(x => x.carrystd_code, x => x.id);
Dictionary<string, object> whDic = await _db.Queryable<BasWarehouse>().Where(it => whCodes.FindAll(x => x.ToString().IsNotEmptyOrNull() && x.ToString() != "").Contains(it.whcode)).ToDictionaryAsync(x => x.whcode, x => x.id);
Dictionary<string, object> rgDic = await _db.Queryable<BasRegion>().Where(it => rgCodes.FindAll(x => x.ToString().IsNotEmptyOrNull() && x.ToString() != "").Contains(it.region_code)).ToDictionaryAsync(x => x.region_code, x => x.id);
string orgId = _userManager.User.OrganizeId;
string userId = _userManager.UserId;
foreach (BasLocation l in locs)
{
if (!carryStdDic.ContainsKey(l.carrystd_id))
{
throw new AppFriendlyException($"第{locs.IndexOf(l) + 1}个数据的载具规格有误", 500);
}
if (!whDic.ContainsKey(l.wh_id))
{
throw new AppFriendlyException($"第{locs.IndexOf(l) + 1}个数据的仓库有误", 500);
}
if (!rgDic.ContainsKey(l.region_id))
{
throw new AppFriendlyException($"第{locs.IndexOf(l) + 1}个数据的区域名称有误", 500);
}
l.id = SnowflakeIdHelper.NextId();
l.org_id = orgId;
l.location_name = l.location_code;
l.is_lock = 0;
l.carrystd_id = JsonConvert.SerializeObject(new[] { carryStdDic[l.carrystd_id]?.ToString()! });
l.wh_id = whDic[l.wh_id]?.ToString()!;
l.region_id = rgDic[l.region_id]?.ToString();
l.create_id = userId;
l.modify_id = null;
l.modify_time = null;
l.is_mix = 1;
l.is_use = "0";
l.is_overmatch = "1";
}
if (locs.Count > 1000)
{
row = await _db.Fastest<BasLocation>().BulkCopyAsync(locs);
}
/*
else if (locs.Count > 400)
{
_db.Utilities.PageEach(locs, 100, async pageList =>
{
row = await _db.Insertable(pageList).ExecuteCommandAsync();
});
}*/
else
{
row = await _db.Insertable(locs).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;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetYCLlocation()
{
List<List<LocationOutput>> LocationOutputss=new List<List<LocationOutput>>();
var list =await _db.Queryable<BasLocation>().Where(p=> p.wh_id == "1" && p.is_type == "0" && !p.location_code.Contains("-3"))
.OrderBy(p=>p.location_code).ToListAsync();
var carrys = await _db.Queryable<WmsCarryH>().ToListAsync();
var carrycodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
var mats = await _db.Queryable<BasMaterial>().ToListAsync();
var qcresults = await _db.Queryable<WmsCarryCode>()
.InnerJoin<WmsTempCode>((a, b) => a.barcode == b.barcode)
.InnerJoin<WmsPurchaseD>((a, b, c) => c.id == b.require_id)
.Where((a, b, c) => !string.IsNullOrEmpty(c.qc_res)).Select((a,b,c)=>new
{
barcode = a.barcode,
qc_res = c.qc_res
}).ToListAsync();
var lastdata = string.Empty;
List<LocationOutput> LocationOutputs = new List<LocationOutput>();
foreach (var data in list)
{
if (!string.IsNullOrEmpty(lastdata)&& data.location_code.Substring(4,1)!= lastdata.Substring(4, 1))
{
LocationOutputss.Add(LocationOutputs);
LocationOutputs = new List<LocationOutput>();
}
LocationOutput locationOutput = new LocationOutput();
locationOutput.location_code = data.location_code.Replace("YCL-", "");
locationOutput.layers = data.layers;
locationOutput.type = 0;
int status = 0;
if (carrys.Where(p => p.location_id == data.id).Any())
{
var carry= carrys.Where(p => p.location_id == data.id).First();
locationOutput.carry_code = carry.carry_code;
locationOutput.type = 1;
if (carrycodes.Where(p => p.carry_id == carry.id).Any())
{
var carrycode = carrycodes.Where(p => p.carry_id == carry.id).First();
locationOutput.type = 2;
locationOutput.material_code = carrycode.material_code;
locationOutput.barcode = carrycode.barcode;
if (mats.Where(p => p.id == carrycode.material_id).Any())
{
var mat = mats.Where(p => p.id == carrycode.material_id).First();
locationOutput.material_name = mat.name;
}
locationOutput.code_batch = carrycode.code_batch;
locationOutput.num = carrycode.codeqty;
if (qcresults.Where(p => p.barcode == carrycode.barcode).Any())
{
var qcresult = qcresults.Where(p => p.barcode == carrycode.barcode).First();
string qc_result = qcresult.qc_res;
switch (qc_result)
{
case "await":
{
status = 3;
break;
}
case "vergeOk":
{
status = 4;
break;
}
case "no":
{
status = 5;
break;
}
case "ok":
{
status = 2;
break;
}
default:
{
status = 3;
break;
}
}
}
else
{
status = 3;
}
}
}
else
{
if (data.is_use == "1")
{
status = 1;
}
else
{
status = 0;
}
}
lastdata = data.location_code;
locationOutput.status = status;
LocationOutputs.Add(locationOutput);
// 。。。。。。
if (data.location_code == list[list.Count - 1].location_code)
LocationOutputss.Add(LocationOutputs);
}
return LocationOutputss;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetYCLlocation2()
{
List<List<LocationOutput>> LocationOutputss = new List<List<LocationOutput>>();
var list = await _db.Queryable<BasLocation>().Where(p => p.wh_id == "1" && p.is_type == "0" && p.region_id != WmsWareHouseConst.REGION_YCLWX_ID)
.OrderBy(p => p.location_code).ToListAsync();
var carrys = await _db.Queryable<WmsCarryH>().ToListAsync();
var carrycodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
var mats = await _db.Queryable<BasMaterial>().ToListAsync();
var qcresults = await _db.Queryable<WmsCarryCode>()
.InnerJoin<WmsTempCode>((a, b) => a.barcode == b.barcode)
.InnerJoin<WmsPurchaseD>((a, b, c) => c.id == b.require_id)
.Where((a, b, c) => !string.IsNullOrEmpty(c.qc_res)).Select((a, b, c) => new
{
barcode = a.barcode,
qc_res = c.qc_res
}).ToListAsync();
var lastdata = string.Empty;
List<LocationOutput> LocationOutputs = new List<LocationOutput>();
List<LocationOutput> LocationOutputsSum = new List<LocationOutput>();
foreach (var data in list)
{
if (!string.IsNullOrEmpty(lastdata) && data.location_code.Substring(4, 1) != lastdata.Substring(4, 1))
{
LocationOutputss.Add(LocationOutputs);
LocationOutputs = new List<LocationOutput>();
}
LocationOutput locationOutput = new LocationOutput();
locationOutput.location_code = data.location_code.Replace("YCL-", "");
locationOutput.layers = data.layers;
locationOutput.type = 0;
int status = 0;
if (carrys.Where(p => p.location_id == data.id).Any())
{
var carry = carrys.Where(p => p.location_id == data.id).First();
locationOutput.carry_code = carry.carry_code;
locationOutput.type = 1;
if (carrycodes.Where(p => p.carry_id == carry.id).Any())
{
var carrycode = carrycodes.Where(p => p.carry_id == carry.id).First();
locationOutput.type = 2;
locationOutput.material_code = carrycode.material_code;
locationOutput.barcode = carrycode.barcode;
if (mats.Where(p => p.id == carrycode.material_id).Any())
{
var mat = mats.Where(p => p.id == carrycode.material_id).First();
locationOutput.material_name = mat.name;
locationOutput.material_specification = mat.material_specification;
}
locationOutput.code_batch = carrycode.code_batch;
locationOutput.num = carrycode.codeqty;
if (qcresults.Where(p => p.barcode == carrycode.barcode).Any())
{
var qcresult = qcresults.Where(p => p.barcode == carrycode.barcode).First();
string qc_result = qcresult.qc_res;
switch (qc_result)
{
case "await":
{
status = 3;
break;
}
case "vergeOk":
{
status = 4;
break;
}
case "no":
{
status = 5;
break;
}
case "ok":
{
status = 2;
break;
}
default:
{
status = 3;
break;
}
}
}
else
{
status = 3;
}
}
}
else
{
if (data.is_use == "1")
{
status = 1;
}
else
{
status = 0;
}
}
lastdata = data.location_code;
locationOutput.status = status;
LocationOutputs.Add(locationOutput);
LocationOutputsSum.Add(locationOutput);
// 。。。。。。
if (data.location_code == list[list.Count - 1].location_code)
LocationOutputss.Add(LocationOutputs);
}
YCLBoardOutput YCLBoardOutput = new YCLBoardOutput();
YCLBoardOutput.summary = new JObject {
{ "0", LocationOutputsSum.Where(r => r.status == 0).Count() },
{ "1", LocationOutputsSum.Where(r => r.status == 1).Count() },
{ "2", LocationOutputsSum.Where(r => r.status == 2).Count() },
{ "3", LocationOutputsSum.Where(r => r.status == 3).Count() },
{ "4", LocationOutputsSum.Where(r => r.status == 4).Count() },
{ "5", LocationOutputsSum.Where(r => r.status == 5).Count() }
};
YCLBoardOutput.locationOutputs = LocationOutputss;
return YCLBoardOutput;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetYCLlocationWX()
{
List<List<LocationOutput>> LocationOutputss = new List<List<LocationOutput>>();
var list = await _db.Queryable<BasLocation>().Where(p => p.wh_id == "1" && p.is_type == "0" && p.region_id == WmsWareHouseConst.REGION_YCLWX_ID)
.OrderBy(p => p.location_code).ToListAsync();
var carrys = await _db.Queryable<WmsCarryH>().ToListAsync();
var carrycodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
var mats = await _db.Queryable<BasMaterial>().ToListAsync();
var qcresults = await _db.Queryable<WmsCarryCode>()
.InnerJoin<WmsTempCode>((a, b) => a.barcode == b.barcode)
.InnerJoin<WmsPurchaseD>((a, b, c) => c.id == b.require_id)
.Where((a, b, c) => !string.IsNullOrEmpty(c.qc_res)).Select((a, b, c) => new
{
barcode = a.barcode,
qc_res = c.qc_res
}).ToListAsync();
var lastdata = string.Empty;
List<LocationOutput> LocationOutputs = new List<LocationOutput>();
List<LocationOutput> LocationOutputsSum = new List<LocationOutput>();
foreach (var data in list)
{
if (!string.IsNullOrEmpty(lastdata) && data.location_code.Substring(4, 1) != lastdata.Substring(4, 1))
{
LocationOutputss.Add(LocationOutputs);
LocationOutputs = new List<LocationOutput>();
}
LocationOutput locationOutput = new LocationOutput();
locationOutput.location_code = data.location_code.Replace("YCL-", "");
locationOutput.layers = data.layers;
locationOutput.type = 0;
int status = 0;
if (carrys.Where(p => p.location_id == data.id).Any())
{
var carry = carrys.Where(p => p.location_id == data.id).First();
locationOutput.carry_code = carry.carry_code;
locationOutput.type = 1;
if (carrycodes.Where(p => p.carry_id == carry.id).Any())
{
var carrycode = carrycodes.Where(p => p.carry_id == carry.id).First();
locationOutput.type = 2;
locationOutput.material_code = carrycode.material_code;
locationOutput.barcode = carrycode.barcode;
if (mats.Where(p => p.id == carrycode.material_id).Any())
{
var mat = mats.Where(p => p.id == carrycode.material_id).First();
locationOutput.material_name = mat.name;
}
locationOutput.code_batch = carrycode.code_batch;
locationOutput.num = carrycode.codeqty;
if (qcresults.Where(p => p.barcode == carrycode.barcode).Any())
{
var qcresult = qcresults.Where(p => p.barcode == carrycode.barcode).First();
string qc_result = qcresult.qc_res;
switch (qc_result)
{
case "await":
{
status = 3;
break;
}
case "vergeOk":
{
status = 4;
break;
}
case "no":
{
status = 5;
break;
}
case "ok":
{
status = 2;
break;
}
default:
{
status = 3;
break;
}
}
}
else
{
status = 3;
}
}
}
else
{
if (data.is_use == "1")
{
status = 1;
}
else
{
status = 0;
}
}
lastdata = data.location_code;
locationOutput.status = status;
LocationOutputs.Add(locationOutput);
LocationOutputsSum.Add(locationOutput);
// 。。。。。。
if (data.location_code == list[list.Count - 1].location_code)
LocationOutputss.Add(LocationOutputs);
}
YCLBoardOutput YCLBoardOutput = new YCLBoardOutput();
YCLBoardOutput.summary = new JObject {
{ "0", LocationOutputsSum.Where(r => r.status == 0).Count() },
{ "1", LocationOutputsSum.Where(r => r.status == 1).Count() },
{ "2", LocationOutputsSum.Where(r => r.status == 2).Count() },
{ "3", LocationOutputsSum.Where(r => r.status == 3).Count() },
{ "4", LocationOutputsSum.Where(r => r.status == 4).Count() },
{ "5", LocationOutputsSum.Where(r => r.status == 5).Count() }
};
YCLBoardOutput.locationOutputs = LocationOutputss;
return YCLBoardOutput;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetCTUlocation()
{
List<List<LocationOutput>> LocationOutputss = new List<List<LocationOutput>>();
var list = await _db.Queryable<BasLocation>().Where(p => p.wh_id == "2" && p.region_id == "27010227520021" && p.is_type == "0" && p.location_code.Length == 13).OrderBy(p =>new { p.loc_column, p.location_code } ).ToListAsync();
var carrys = await _db.Queryable<WmsCarryH>().ToListAsync();
var carrycodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
int loc_column = 0;
List<LocationOutput> LocationOutputs = new List<LocationOutput>();
foreach (var data in list)
{
if (loc_column!=0&& data.loc_column!=loc_column)
{
LocationOutputss.Add(LocationOutputs);
LocationOutputs = new List<LocationOutput>();
}
LocationOutput locationOutput = new LocationOutput();
locationOutput.location_code = data.location_code;
locationOutput.layers = data.layers;
locationOutput.type = 0;
if (carrys.Where(p => p.location_id == data.id).Any())
{
var carry = carrys.Where(p => p.location_id == data.id).First();
locationOutput.carry_code = carry.carry_code;
locationOutput.type = 1;
if (carrycodes.Where(p => p.carry_id == carry.id).Any())
{
var carrycode = carrycodes.Where(p => p.carry_id == carry.id).First();
locationOutput.barcode = carrycode.barcode;
locationOutput.type = 2;
locationOutput.material_code = carrycode.material_code;
locationOutput.num = carrycode.codeqty;
}
}
loc_column = data.loc_column;
LocationOutputs.Add(locationOutput);
}
return LocationOutputss;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetCPlocation()
{
List<List<LocationOutput>> LocationOutputss = new List<List<LocationOutput>>();
var list = await _db.Queryable<BasLocation>().Where(p => p.wh_id == "26103372441637" && p.region_id == "28598002137877" && p.is_type == "0" && p.location_code.Length == 9).OrderBy(p => p.location_code).ToListAsync();
var carrys = await _db.Queryable<WmsCarryH>().ToListAsync();
var carrycodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
var lastdata = string.Empty;
List<LocationOutput> LocationOutputs = new List<LocationOutput>();
foreach (var data in list)
{
if (!string.IsNullOrEmpty(lastdata) && data.location_code.Substring(3, 3) != lastdata.Substring(3, 3))
{
LocationOutputss.Add(LocationOutputs);
LocationOutputs = new List<LocationOutput>();
}
LocationOutput locationOutput = new LocationOutput();
locationOutput.location_code = data.location_code;
locationOutput.layers = data.layers;
locationOutput.type = 0;
if (carrys.Where(p => p.location_id == data.id).Any())
{
var carry = carrys.Where(p => p.location_id == data.id).First();
locationOutput.carry_code = carry.carry_code;
locationOutput.type = 1;
if (carrycodes.Where(p => p.carry_id == carry.id).Any())
{
var carrycode = carrycodes.Where(p => p.carry_id == carry.id).First();
locationOutput.type = 2;
locationOutput.material_code = carrycode.material_code;
locationOutput.num = carrycode.codeqty;
}
}
lastdata = data.location_code;
LocationOutputs.Add(locationOutput);
}
return LocationOutputss;
}
[HttpPost]
[AllowAnonymous]
public async Task<dynamic> GetZSJData()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("device_state", "事件");
dic.Add("temp1", "前部[゚C]");
dic.Add("temp3", "后部1[゚C]");
dic.Add("temp5", "落下口[゚C]");
dic.Add("zsspeed", "V1[mm/s]");
dic.Add("sjspeed", "V2[mm/s]");
dic.Add("cxtime", "周期时间[s]");
var keys = _redisData.GetAllKeys().Where(p => p.Contains("TYSC-ZSJ")).ToList();
List<ZsjOutput> datas = new List<ZsjOutput>();
foreach (var key in keys)
{
var data = datas.Where(p => p.device_code == key.Substring(0, 12)).FirstOrDefault();
if (data != null)
{
foreach (var d in dic)
{
bool flag = await _redisData.HashExist(key, d.Value);
if (flag)
data.GetType().GetProperty(d.Key)!.SetValue(data,_redisData.GetHash(key, d.Value).Result);
}
}
else
{
ZsjOutput zsjOutput = new ZsjOutput();
zsjOutput.device_code = key.Substring(0, 12);
foreach (var d in dic)
{
bool flag = await _redisData.HashExist(key, d.Value);
if (flag)
zsjOutput.GetType().GetProperty(d.Key)!.SetValue(zsjOutput, _redisData.GetHash(key, d.Value).Result);
}
datas.Add(zsjOutput);
}
}
return datas;
}
}
}