Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/WmsSterilizationInstockHService.cs
2024-10-08 10:57:30 +08:00

218 lines
11 KiB
C#

using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.EventBus;
using JNPF.FriendlyException;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Entities.Entity;
using Tnb.WarehouseMgr.Entities.Enums;
using Tnb.WarehouseMgr.Interfaces;
using Tnb.ProductionMgr.Entities.Entity;
using Tnb.BasicData;
using Tnb.BasicData.Interfaces;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 库内转移
/// </summary>
[OverideVisualDev(ModuleConsts.MODULE_WMSSTERILIZATIONINSTOCKCHID)]
[ServiceModule(BizTypeId)]
public class WmsSterilizationInstockHService : BaseWareHouseService
{
private const string BizTypeId = WmsWareHouseConst.BIZTYPE_WMSSTERILIZATIONINSTOCKCH_ID;
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IWareHouseService _wareHouseService;
private readonly IBillRullService _billRullService;
private readonly IUserManager _userManager;
private readonly IThirdApiRecordService _thirdApiRecordService;
public WmsSterilizationInstockHService(
ISqlSugarRepository<WmsCarryH> repository,
IRunService runService,
IVisualDevService visualDevService,
IWareHouseService wareHouseService,
IUserManager userManager,
IBillRullService billRullService,
IThirdApiRecordService thirdApiRecordService,
IEventPublisher publisher)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
_wareHouseService = wareHouseService;
_userManager = userManager;
_billRullService = billRullService;
_thirdApiRecordService = thirdApiRecordService;
}
public override async Task ModifyAsync(WareHouseUpInput input)
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
BasLocation endLocation = await _db.Queryable<BasLocation>().Where(r => r.id == input.wmsDistaskH.endlocation_id).FirstAsync();
string outWhid = "";
string inWhid = "";
string outDeptid = "";
string outDeptvid = "";
string inDeptid = "";
string inDeptvid = "";
string tranTypeid = "";
string tranTypecode = "";
// 生产入库(灭菌完成) 仓库传 1001A1100000001MA8KP
if (endLocation.wh_id == WmsWareHouseConst.WAREHOUSE_MJC_ID)
{
outWhid = WmsWareHouseConst.bipwarehouseid_bz;
inWhid = WmsWareHouseConst.bipwarehouseid_mj;
outDeptid = WmsWareHouseConst.sichangqushengchanbu_id;
outDeptvid = WmsWareHouseConst.sichangqushengchanbu_vid;
inDeptid = WmsWareHouseConst.sichangqucangku_id;
inDeptvid = WmsWareHouseConst.sichangqucangku_vid;
tranTypeid = "1001A110000000077KMR";
tranTypecode = "4K-Cxx-17";
}
// 生产入库(成品) 仓库传 1001A1100000001MA8ME
else if (endLocation.wh_id == WmsWareHouseConst.WAREHOUSE_CP_ID)
{
outWhid = WmsWareHouseConst.bipwarehouseid_mj;
inWhid = WmsWareHouseConst.bipwarehouseid_cp;
outDeptid = WmsWareHouseConst.sichangqucangku_id;
outDeptvid = WmsWareHouseConst.sichangqucangku_vid;
inDeptid = WmsWareHouseConst.sichangqucangku_id;
inDeptvid = WmsWareHouseConst.sichangqucangku_vid;
tranTypeid = "1001A11000000006YKHG";
tranTypecode = "4K-Cxx-18";
bool isOk = await _db.Updateable<WmsSterilizationInstockH>().SetColumns(it => new WmsSterilizationInstockH { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID })
.Where(it => it.id == input.requireId).ExecuteCommandHasChangeAsync();
if (!isOk)
{
throw Oops.Oh(ErrorCode.COM1001);
}
}
WmsSterilizationInstockH wmsRawmatTransferoutstockH = await _db.Queryable<WmsSterilizationInstockH>().SingleAsync(x=>x.id==input.requireId);
List<WmsCarryCode> wmsCarryCodes = await _db.Queryable<WmsCarryCode>().Where(r => r.carry_id == input.wmsDistaskH.carry_id).OrderBy(x=>x.id).ToListAsync();
decimal qty = wmsCarryCodes.Sum(r => r.codeqty);
List<String> unitCodes = wmsCarryCodes.Select(x => x.unit_id).Distinct().ToList();
List<string> materialIds = wmsCarryCodes.Select(x => x.material_id).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) || unitCodes.Contains(y.Id)))
.Select((x, y) => y)
.ToListAsync();
string supplierId = WmsWareHouseConst.TIANYIGONGYINGSHANG_ID;
List<string> tableIds = new List<string>();
tableIds.Add(WmsWareHouseConst.AdministratorOrgId);
// tableIds.Add(WmsWareHouseConst.AdministratorUserId);
tableIds.AddRange(materialIds);
tableIds.Add(supplierId);
tableIds.AddRange(unitDatas.Select(x => x.Id).ToList());
string userId = wmsRawmatTransferoutstockH.create_id ?? WmsWareHouseConst.AdministratorUserId;
tableIds.Add(userId);
List<ErpExtendField> erpExtendFields = await _db.Queryable<ErpExtendField>().Where(x => tableIds.Contains(x.table_id)).ToListAsync();
// string erpCreateId = erpExtendFields.Find(x => x.table_id == userId)?.user_id ?? WmsWareHouseConst.ERPUSERID;
string erpCreateId = WmsWareHouseConst.ERPUSERID;
ErpExtendField erpOrg = erpExtendFields.Find(x => x.table_id == (WmsWareHouseConst.AdministratorOrgId));
string nowStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
List<WmsErpWarehouserelaH> wmsErpWarehouserelaHs = await _db.Queryable<WmsErpWarehouserelaH>().Where(x=>!SqlFunc.IsNullOrEmpty(x.id)).ToListAsync();
//List<Dictionary<string, object>> requestData = new List<Dictionary<string, object>>();
Dictionary<string, object> erpRequestData = new Dictionary<string, object>();
erpRequestData.Add("billmaker", erpCreateId);
erpRequestData.Add("corpoid", erpOrg.corpoid);
erpRequestData.Add("corpvid", erpOrg.corpvid);
erpRequestData.Add("cdptid",outDeptid);//部门先写死
erpRequestData.Add("cdptvid",outDeptvid);//部门先写死
erpRequestData.Add("cotherdptid",inDeptid);//部门先写死
erpRequestData.Add("cotherdptvid",inDeptvid);//部门先写死
erpRequestData.Add("creationtime", nowStr);
erpRequestData.Add("creator", erpCreateId);
erpRequestData.Add("cwarehouseid", outWhid);//先写死
erpRequestData.Add("cotherwhid", inWhid);//先写死
erpRequestData.Add("dbilldate", wmsRawmatTransferoutstockH.create_time.Value.ToString("yyyy-MM-dd HH:mm:ss"));
erpRequestData.Add("dmakedate", nowStr);
erpRequestData.Add("dshldarrivedate", nowStr);
erpRequestData.Add("dshlddiliverdate", nowStr);
erpRequestData.Add("fbillflag", 1);
erpRequestData.Add("isbackdeliver", false);
erpRequestData.Add("ntotalnum", 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("vbillcode", wmsRawmatTransferoutstockH.bill_code);
erpRequestData.Add("ctrantypeid", tranTypeid);//先写死
erpRequestData.Add("vtrantypecode", tranTypecode);//先写死
erpRequestData.Add("distask_code", input.wmsDistaskH.bill_code);//先写死
List<Dictionary<string, object>> erpRequestDataDetails = new List<Dictionary<string, object>>();
foreach(var item in wmsCarryCodes)
{
// 出库数量wmsMaterialSignD.sign_qty
erpRequestDataDetails.Add(new Dictionary<string, object>()
{
["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"] = (wmsCarryCodes.FindIndex(x => x.id == item.id) + 1) * 10,
["cunitid"] = erpExtendFields.Find(x => x.table_id == (unitDatas.Find(x => x.EnCode == item.unit_id || x.Id==item.unit_id)?.Id ?? ""))?.cunitid ?? "",
["nnum"] = item.codeqty,
["nassistnum"] = item.codeqty,
["pk_group"] = erpOrg.pk_group,
["pk_org"] = erpOrg.pk_org,
["pk_org_v"] = erpOrg.pk_org_v,
["vbatchcode"] = item.code_batch,
["vdef19"] = 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/whstrans/save";
thirdWebapiRecord.url = WmsWareHouseConst.BIP_DOMAIN + "uapws/rest/whstrans/save";
thirdWebapiRecord.request_data = JsonConvert.SerializeObject(erpRequestData);
thirdWebapiRecord.create_time = DateTime.Now;
thirdWebapiRecord.remark = $"【WmsSterilizationInstockHService ModifyAsync】任务号wms_distask_h:{input.wmsDistaskH.bill_code},灭菌入库记录wms_sterilization_instock_h:{wmsRawmatTransferoutstockH.bill_code}";
await _db.Insertable(thirdWebapiRecord).ExecuteCommandAsync();
BasFactoryConfig callErp = await _db.Queryable<BasFactoryConfig>().FirstAsync(x => x.enabled == 1 && x.key == FactoryConfigConst.CALLERP);
if(callErp.value=="1"){
await _thirdApiRecordService.Send(new List<ThirdWebapiRecord> { thirdWebapiRecord }, "自动", _db);
}
}
}
}