366 lines
22 KiB
C#
366 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Aop.Api.Domain;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
using JNPF.Common.Enums;
|
|
using JNPF.Common.Security;
|
|
using JNPF.FriendlyException;
|
|
using JNPF.Systems.Entitys.Permission;
|
|
using JNPF.Systems.Entitys.System;
|
|
using JNPF.Systems.Interfaces.System;
|
|
using JNPF.VisualDev;
|
|
using JNPF.VisualDev.Interfaces;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using SqlSugar;
|
|
using Tnb.BasicData.Entities;
|
|
using Tnb.WarehouseMgr.Entities;
|
|
using Tnb.WarehouseMgr.Entities.Consts;
|
|
using Tnb.WarehouseMgr.Entities.Dto;
|
|
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
|
|
using Tnb.WarehouseMgr.Entities.Entity;
|
|
using Tnb.WarehouseMgr.Interfaces;
|
|
using Tnb.ProductionMgr.Entities.Entity;
|
|
|
|
namespace Tnb.WarehouseMgr
|
|
{
|
|
/// <summary>
|
|
/// 出库签收
|
|
/// </summary>
|
|
[OverideVisualDev(ModuleConsts.MODULE_WmsMaterialSignH_ID)]
|
|
public class WmsMaterialSignHService : BaseWareHouseService
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IUserManager _userManager;
|
|
private readonly IBillRullService _billRullService;
|
|
private readonly IRunService _runService;
|
|
private readonly IVisualDevService _visualDevService;
|
|
private readonly IWmsPDAScanInStockService _wmsPDAScanInStock;
|
|
private readonly IWmsCarryUnbindService _wmsCarryUnbindService;
|
|
private readonly IWareHouseService _wareHouseService;
|
|
public WmsMaterialSignHService(
|
|
ISqlSugarRepository<WmsCarryH> repository,
|
|
IUserManager userManager,
|
|
IBillRullService billRullService,
|
|
IRunService runService,
|
|
IVisualDevService visualDevService,
|
|
IWmsPDAScanInStockService wmsPDAScanInStock,
|
|
IWmsCarryUnbindService wmsCarryUnbindService,
|
|
IWareHouseService wareHouseService)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
_userManager = userManager;
|
|
_billRullService = billRullService;
|
|
_runService = runService;
|
|
_visualDevService = visualDevService;
|
|
_wmsPDAScanInStock = wmsPDAScanInStock;
|
|
_wmsCarryUnbindService = wmsCarryUnbindService;
|
|
_wareHouseService = wareHouseService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 物料签收
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="AppFriendlyException"></exception>
|
|
[HttpPost]
|
|
public async Task MaterialSign(MaterialSignInput input)
|
|
{
|
|
try
|
|
{
|
|
if (input == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(input));
|
|
}
|
|
|
|
WmsCarryH wmsCarryH = await _db.Queryable<WmsCarryH>().Where(r => r.carry_code == input.carry_code).FirstAsync();
|
|
if (wmsCarryH == null)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}不存在", 500);
|
|
}
|
|
if (wmsCarryH.is_lock == 1)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}已锁定", 500);
|
|
}
|
|
if (wmsCarryH.carry_status != "1")
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}未绑定物料", 500);
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(wmsCarryH.location_id))
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}当前库位为空,无法签收", 500);
|
|
}
|
|
|
|
BasLocation carryLoc = await _db.Queryable<BasLocation>().Where(r => r.id == wmsCarryH.location_id).FirstAsync();
|
|
if (carryLoc != null && carryLoc.is_type == "0")
|
|
{
|
|
throw new Exception($"托盘{wmsCarryH.carry_code}在存储库位中,不能签收!");
|
|
}
|
|
|
|
List<WmsCarryCode> wmsCarryCodes = _db.Queryable<WmsCarryCode>().Where(r => r.carry_id == wmsCarryH.id).ToList();
|
|
|
|
WmsMaterialSignH wmsMaterialSignH = new WmsMaterialSignH();
|
|
wmsMaterialSignH.create_id = input.create_id;
|
|
wmsMaterialSignH.create_time = DateTime.Now;
|
|
wmsMaterialSignH.carry_id = wmsCarryH.id;
|
|
wmsMaterialSignH.carry_code = wmsCarryH.carry_code;
|
|
WmsDistaskH wmsDistaskH = await _db.Queryable<WmsDistaskH>().Where(r => r.carry_id == wmsCarryH.id).OrderByDescending(r => r.id).FirstAsync();
|
|
if (wmsDistaskH == null)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】无法找到载具{input.carry_code}的执行任务", 500);
|
|
}
|
|
if (string.IsNullOrEmpty(wmsDistaskH.biz_type))
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}任务{wmsDistaskH.bill_code}的业务类型异常", 500);
|
|
}
|
|
wmsMaterialSignH.biz_type = wmsDistaskH.biz_type;
|
|
List<WmsMaterialSignD> wmsMaterialSignDs = new List<WmsMaterialSignD>();
|
|
await _db.Ado.BeginTranAsync();
|
|
foreach (var item in input.details)
|
|
{
|
|
if (item.sign_qty < 0)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量不能为空", 500);
|
|
}
|
|
|
|
WmsCarryCode wmsCarryCode = wmsCarryCodes.Where(r => r.id == item.carry_code_id).FirstOrDefault();
|
|
if (wmsCarryCode == null)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code} 载具物料明细id {item.carry_code_id}不存在", 500);
|
|
}
|
|
if (item.sign_qty > wmsCarryCode.codeqty)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量{item.sign_qty}超过了可签收数量{wmsCarryCode.codeqty}", 500);
|
|
}
|
|
|
|
WmsMaterialSignD wmsMaterialSignD = new WmsMaterialSignD();
|
|
wmsMaterialSignD.bill_id = wmsMaterialSignH.id;
|
|
wmsMaterialSignD.create_id = input.create_id;
|
|
wmsMaterialSignD.create_time = DateTime.Now;
|
|
wmsMaterialSignD.barcode = wmsCarryCode.barcode;
|
|
wmsMaterialSignD.material_id = wmsCarryCode.material_id;
|
|
BasMaterial basMaterial = await _db.Queryable<BasMaterial>().Where(r => r.code == wmsCarryCode.material_code).FirstAsync();
|
|
if (basMaterial == null)
|
|
{
|
|
throw new AppFriendlyException($"【MaterialSign】物料{wmsCarryCode.material_code} 基础资料不存在", 500);
|
|
}
|
|
wmsMaterialSignD.material_code = basMaterial.code;
|
|
wmsMaterialSignD.material_name = basMaterial.name;
|
|
wmsMaterialSignD.material_specification = basMaterial.material_specification;
|
|
wmsMaterialSignD.code_batch = wmsCarryCode.code_batch;
|
|
wmsMaterialSignD.qty = wmsCarryCode.codeqty;
|
|
wmsMaterialSignD.sign_qty = item.sign_qty;
|
|
wmsMaterialSignDs.Add(wmsMaterialSignD);
|
|
wmsCarryCode.codeqty = wmsCarryCode.codeqty - item.sign_qty;
|
|
|
|
if (wmsCarryCode.codeqty == 0)
|
|
{
|
|
CarryCodeUnbindCodeInput carryCodeUnbindCodeInput = new CarryCodeUnbindCodeInput();
|
|
await _wmsCarryUnbindService.CarryCodeUnbindCode(carryCodeUnbindCodeInput, _db);
|
|
await _db.Updateable<WmsCarryH>().SetColumns(r => r.carry_status == "0").Where(r => r.id == wmsCarryH.id).ExecuteCommandAsync();
|
|
}
|
|
await _db.Updateable<WmsTempCode>().SetColumns(r => r.codeqty == wmsCarryCode.codeqty)
|
|
.Where(r => r.barcode == wmsCarryCode.barcode).ExecuteCommandAsync();
|
|
}
|
|
|
|
|
|
switch (carryLoc.wh_id)
|
|
{
|
|
case WmsWareHouseConst.WAREHOUSE_YCL_ID:
|
|
{
|
|
|
|
await _db.Updateable(wmsCarryCodes).ExecuteCommandAsync();
|
|
|
|
await _wareHouseService.s_taskExecuteSemaphore_YCLInstock.WaitAsync();
|
|
wmsMaterialSignH.warehouse_sign_id = WmsWareHouseConst.WAREHOUSE_YCL_ID;
|
|
wmsMaterialSignH.warehouse_instock_id = WmsWareHouseConst.WAREHOUSE_YCL_ID;
|
|
try
|
|
{
|
|
InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = "1", Size = 1, AvoidBusyPassage = true, Region_id = WmsWareHouseConst.REGION_YCLCache_ID };
|
|
List<BasLocation> endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
|
|
if (endLocations.Count == 0)
|
|
{
|
|
throw new AppFriendlyException("没有可以回库的库位", 500);
|
|
}
|
|
CommonCreatePretaskInput commonCreatePretaskInput = new CommonCreatePretaskInput();
|
|
commonCreatePretaskInput.startlocation_id = carryLoc.id;
|
|
commonCreatePretaskInput.endlocation_id = endLocations[0].id;
|
|
commonCreatePretaskInput.carry_id = wmsCarryH.id;
|
|
commonCreatePretaskInput.carry_code = wmsCarryH.carry_code;
|
|
commonCreatePretaskInput.task_type = WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID;
|
|
commonCreatePretaskInput.biz_type = WmsWareHouseConst.BIZTYPE_WmsMaterialSign_ID;
|
|
|
|
Logger.LogInformation($"【MaterialSign】 开始生成原材料仓回库任务 起点{carryLoc.location_code} 终点{endLocations[0].location_code} 托盘 {wmsCarryH.carry_code}");
|
|
|
|
Entities.Dto.Outputs.Result res = await _wareHouseService.CommonCreatePretask(commonCreatePretaskInput, _db);
|
|
if (res.code != HttpStatusCode.OK)
|
|
{
|
|
throw new AppFriendlyException(res.msg, 500);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
_wareHouseService.s_taskExecuteSemaphore_YCLInstock.Release();
|
|
}
|
|
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
await _db.Insertable(wmsMaterialSignH).ExecuteCommandAsync();
|
|
await _db.Insertable(wmsMaterialSignDs).ExecuteCommandAsync();
|
|
|
|
#region 上传bip
|
|
|
|
switch (wmsDistaskH.biz_type)
|
|
{
|
|
// 原材料调拨出库
|
|
case WmsWareHouseConst.BIZTYPE_WmsRawmatTransferoutstock_ID:
|
|
{
|
|
//todo 对接调拨出库
|
|
// WmsInstockH instock = await _db.Queryable<WmsInstockH>().SingleAsync(x => x.id == input.requireId);
|
|
// List<WmsInstockD> allInstockDetails = await _db.Queryable<WmsInstockD>().Where(it => it.bill_id == input.requireId).ToListAsync();
|
|
//
|
|
// WmsTempCode wmsTempCode = await _db.Queryable<WmsCarryCode>()
|
|
// .LeftJoin<WmsTempCode>((a, b) => a.barcode == b.barcode)
|
|
// .Where((a, b) => a.carry_id == input.wmsDistaskH.carry_id)
|
|
// .Select((a, b) => b)
|
|
// .FirstAsync();
|
|
//
|
|
// string rawmatTransferinstockDId = wmsTempCode?.require_id ?? "";
|
|
// WmsRawmatTransferinstockD wmsRawmatTransferinstockD = await _db.Queryable<WmsRawmatTransferinstockD>().SingleAsync(x => x.id == rawmatTransferinstockDId);
|
|
// string rawmatTransferinstockHId = wmsRawmatTransferinstockD?.bill_id ?? "";
|
|
// WmsRawmatTransferinstockH wmsRawmatTransferinstockH = await _db.Queryable<WmsRawmatTransferinstockH>().SingleAsync(x => x.id == rawmatTransferinstockHId);
|
|
//
|
|
// List<String> materialIds = allInstockDetails.Select(x => x.material_id).Distinct().ToList();
|
|
// List<String> unitCodes = allInstockDetails.Select(x => x.unit_id).Distinct().ToList();
|
|
// List<DictionaryDataEntity> unitDatas = await _db.Queryable<DictionaryTypeEntity>()
|
|
// .LeftJoin<DictionaryDataEntity>((x, y) => x.Id == y.DictionaryTypeId)
|
|
// .Where((x, y) => x.EnCode == DictConst.MeasurementUnit && unitCodes.Contains(y.EnCode))
|
|
// .Select((x, y) => y)
|
|
// .ToListAsync();
|
|
// List<WmsErpWarehouserelaH> erpWarehouserelaHs = await _db.Queryable<WmsErpWarehouserelaH>().Where(x=>x.id!=null).ToListAsync();
|
|
// BasWarehouse basWarehouse = await _db.Queryable<BasWarehouse>().SingleAsync(x=>x.id==instock.warehouse_id);
|
|
//
|
|
// string supplierId = WmsWareHouseConst.TIANYIGONGYINGSHANG_ID;
|
|
// List<string> tableIds = new List<string>();
|
|
// tableIds.Add(_userManager.UserId);
|
|
// tableIds.Add(WmsWareHouseConst.AdministratorOrgId);
|
|
// tableIds.Add(instock.warehouse_id);
|
|
// tableIds.AddRange(materialIds);
|
|
// tableIds.Add(supplierId);
|
|
// tableIds.AddRange(unitDatas.Select(x => x.Id).ToList());
|
|
//
|
|
// List<ErpExtendField> erpExtendFields = await _db.Queryable<ErpExtendField>().Where(x => tableIds.Contains(x.table_id)).ToListAsync();
|
|
// string userAccount = wmsRawmatTransferinstockH?.biller ?? "";
|
|
// string deptCode = wmsRawmatTransferinstockH?.dept_code ?? "";
|
|
// UserEntity userEntity = await _db.Queryable<UserEntity>().Where(x=>x.Account==userAccount).FirstAsync();
|
|
// string userId = userEntity?.Id ?? "";
|
|
// string erpCreateId = erpExtendFields.Find(x => x.table_id == userId)?.user_id ?? "";
|
|
// ErpExtendField erpOrg = erpExtendFields.Find(x => x.table_id == (WmsWareHouseConst.AdministratorOrgId));
|
|
// string nowStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
//
|
|
// List<Dictionary<string, object>> requestData = new List<Dictionary<string, object>>();
|
|
// Dictionary<string, object> erpRequestData = new Dictionary<string, object>();
|
|
// erpRequestData.Add("billmaker", erpCreateId);
|
|
// erpRequestData.Add("cbiztype", "");
|
|
// erpRequestData.Add("cdptid","1001A1100000000JRLI1");//部门先写死
|
|
// erpRequestData.Add("cdptvid","0001A11000000007GGO8");//部门先写死
|
|
// erpRequestData.Add("corpoid", erpOrg.corpoid);
|
|
// erpRequestData.Add("corpvid", erpOrg.corpvid);
|
|
// erpRequestData.Add("creationtime", nowStr);
|
|
// erpRequestData.Add("creator", erpCreateId);
|
|
// // erpRequestData.Add("cwarehouseid", erpExtendFields.Find(x => x.table_id == instock.warehouse_id)?.cotherwhid ?? "");//类型视图里取
|
|
// erpRequestData.Add("cwarehouseid", erpWarehouserelaHs.Find(x => x.erp_warehousecode == basWarehouse.whcode)?.erp_warehouseid ?? "");//类型视图里取
|
|
// erpRequestData.Add("dbilldate", nowStr);
|
|
// erpRequestData.Add("dmakedate", nowStr);
|
|
// erpRequestData.Add("fbillflag", 1);
|
|
// erpRequestData.Add("ntotalnum", allInstockDetails.Sum(x => x.qty));
|
|
// erpRequestData.Add("pk_org", erpOrg.pk_org);
|
|
// erpRequestData.Add("pk_org_v", erpOrg.pk_org_v);
|
|
// erpRequestData.Add("pk_group", erpOrg.pk_group);
|
|
// erpRequestData.Add("vdef1", null);
|
|
// erpRequestData.Add("vbillcode", instock.bill_code);
|
|
// erpRequestData.Add("ctrantypeid", "0001H11000000000D31E");//先写死
|
|
// erpRequestData.Add("vtrantypecode", "4E-01");//先写死
|
|
// erpRequestData.Add("csourcebillhid", wmsRawmatTransferinstockH?.erp_pk ?? "");
|
|
//
|
|
//
|
|
// List<Dictionary<string, object>> erpRequestDataDetails = new List<Dictionary<string, object>>();
|
|
// foreach (WmsInstockD item in allInstockDetails)
|
|
// {
|
|
// erpRequestDataDetails.Add(new Dictionary<string, object>()
|
|
// {
|
|
// ["cbodytranstypecode"] = "4E-01",
|
|
// // ["cbodywarehouseid"] = erpExtendFields.Find(x => x.table_id == instock.warehouse_id)?.cotherwhid ?? "",
|
|
// ["cbodywarehouseid"] = erpWarehouserelaHs.Find(x => x.erp_warehousecode == basWarehouse.whcode)?.erp_warehouseid ?? "",
|
|
// ["cmaterialoid"] = erpExtendFields.Find(x => x.table_id == item.material_id)?.cmaterialoid ?? "",
|
|
// ["cmaterialvid"] = erpExtendFields.Find(x => x.table_id == item.material_id)?.cmaterialvid ?? "",
|
|
// ["corpoid"] = erpOrg.corpoid,
|
|
// ["corpvid"] = erpOrg.corpvid,
|
|
// ["crowno"] = (allInstockDetails.FindIndex(x => x.id == item.id) + 1) * 10,
|
|
// ["csourcebillhid"] = wmsRawmatTransferinstockH?.erp_pk ?? "",
|
|
// ["csourcebillbid"] = wmsRawmatTransferinstockD?.erp_line_pk ?? "",
|
|
// ["cunitid"] = erpExtendFields.Find(x => x.table_id == (unitDatas.Find(x => x.EnCode == item.unit_id)?.Id ?? ""))?.cunitid ?? "",
|
|
// ["cvendorid"] = erpExtendFields.Find(x => x.table_id == supplierId)?.supplier_id ?? "",
|
|
// ["cvendorvid"] = erpExtendFields.Find(x => x.table_id == supplierId)?.supplier_vid ?? "",
|
|
// ["dbizdate"] = instock.create_time.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
// ["dplanarrivedate"] = instock.create_time.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
// ["dplanoutdate"] = instock.create_time.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
// ["nnum"] = item.qty,
|
|
// ["nshouldnum"] = item.pr_qty,
|
|
// ["pk_group"] = erpOrg.pk_group,
|
|
// ["pk_org"] = erpOrg.pk_org,
|
|
// ["pk_org_v"] = erpOrg.pk_org_v,
|
|
// ["vbatchcode"] = item.code_batch,
|
|
// });
|
|
// }
|
|
// erpRequestData.Add("dtls", erpRequestDataDetails);
|
|
// requestData.Add(erpRequestData);
|
|
// BasFactoryConfig config = await _db.Queryable<BasFactoryConfig>().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.BIPURL);
|
|
// ThirdWebapiRecord thirdWebapiRecord = new ThirdWebapiRecord();
|
|
// thirdWebapiRecord.id = SnowflakeIdHelper.NextId();
|
|
// thirdWebapiRecord.third_name = WmsWareHouseConst.BIP;
|
|
// thirdWebapiRecord.name = "调拨入库";
|
|
// thirdWebapiRecord.method = "POST";
|
|
// thirdWebapiRecord.url = config.value + "uapws/rest/transIn/save";
|
|
// // thirdWebapiRecord.url = WmsWareHouseConst.BIP_DOMAIN + "uapws/rest/transIn/save";
|
|
// thirdWebapiRecord.request_data = JsonConvert.SerializeObject(requestData);
|
|
// thirdWebapiRecord.create_time = DateTime.Now;
|
|
// await _db.Insertable(thirdWebapiRecord).ExecuteCommandAsync();
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError($"【MaterialSign】 {ex.Message}");
|
|
Logger.LogError($"【MaterialSign】 {ex.StackTrace}");
|
|
await _db.Ado.RollbackTranAsync();
|
|
throw new AppFriendlyException($"【MaterialSign】物料签收失败 {ex.Message}", 500);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|