Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/PrdOutstockService.cs
2023-11-24 11:43:29 +08:00

383 lines
19 KiB
C#

using JNPF;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.CollectiveOAuth.Models;
using JNPF.Extras.CollectiveOAuth.Utils;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.Permission;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.ProductionMgr.Interfaces;
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
namespace Tnb.ProductionMgr
{
/// <summary>
/// 生产领料
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
[OverideVisualDev(ModuleId)]
public class PrdOutstockService : IOverideVisualDevService, IPrdOutstockService, IDynamicApiController, ITransient
{
private const string ModuleId = "27542506559765";
private readonly ISqlSugarRepository<PrdOutstockH> _repository;
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IDictionaryDataService _dictionaryDataService;
private readonly IOrganizeService _organizeService;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRullService;
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
public PrdOutstockService(ISqlSugarRepository<PrdOutstockH> repository, IOrganizeService organizeService, IBillRullService billRullService,IRunService runService, IUserManager userManager, IVisualDevService visualDevService, IDictionaryDataService dictionaryDataService)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
_billRullService = billRullService;
_dictionaryDataService = dictionaryDataService;
_repository = repository;
_userManager = userManager;
_organizeService = organizeService;
OverideFuncs.CreateAsync = Create;
}
private async Task<dynamic> Create(VisualDevModelDataCrInput visualDevModelDataCrInput)
{
try
{
ISqlSugarClient db = _repository.AsSugarClient();
MESCreateOutstockInput input = new()
{
outstock = new MESWmsOutstockHInput(),
outstockDs = new List<MESWmsOutstockDInput>()
};
string? locationId = visualDevModelDataCrInput.data.ContainsKey("location_id") ? visualDevModelDataCrInput.data["location_id"].ToString() : "";
BasLocation location = await db.Queryable<BasLocation>().SingleAsync(x => x.id == locationId);
input.outstock.bill_type = visualDevModelDataCrInput.data.ContainsKey("bill_type") ? visualDevModelDataCrInput.data["bill_type"].ToString() : "";
// input.outstock.bill_date = visualDevModelDataCrInput.data.ContainsKey("bill_date") ? Convert.ToDateTime(visualDevModelDataCrInput.data["bill_date"].ToString()) : DateTime.Now;
input.outstock.bill_date = DateTime.Now;
input.outstock.org_id = _userManager.GetUserInfo().Result.organizeId;
input.outstock.warehouse_id = visualDevModelDataCrInput.data.ContainsKey("warehouse_id") ? visualDevModelDataCrInput.data["warehouse_id"].ToString() : "";
input.outstock.create_id = _userManager.UserId;
input.outstock.location_code = location?.location_code ?? "";
// foreach (var item in (JArray)visualDevModelDataCrInput.data["tablefield107"])
// {
// input.outstockDs.Add(new MESWmsOutstockDInput()
// {
// material_id = item.Value<string>("material_id"),
// material_code = item.Value<string>("material_code"),
// unit_id = item.Value<string>("unit_id"),
// pr_qty = item.Value<decimal>("pr_qty"),
// code_batch = item.Value<string>("code_batch"),
// source_id = item.Value<string>("source_id"),
// });
// }
input.outstockDs = JsonConvert.DeserializeObject<List<MESWmsOutstockDInput>>(JsonConvert.SerializeObject(visualDevModelDataCrInput.data["tablefield107"]));
string domain = _userManager.Domain;
Dictionary<string, object> header = new()
{
["Authorization"] = App.HttpContext!=null ? App.HttpContext.Request.Headers["Authorization"] : ""
};
string sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CREATE_OUTSTOCK, JsonConvert.SerializeObject(input), header);
Log.Information(sendResult);
AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
if (authResponse.code != 200)
{
throw Oops.Bah(authResponse.msg);
}
else
{
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleId, true);
await _runService.Create(templateEntity, visualDevModelDataCrInput);
}
return await Task.FromResult(true);
}
catch (Exception e)
{
Console.WriteLine(e);
Log.Error(e.Message);
throw Oops.Bah(e.Message);
}
}
[HttpPost]
public async Task<dynamic> GeneralOutstock(GeneralOutstockInput generalOutstockInput)
{
try
{
ISqlSugarClient db = _repository.AsSugarClient();
string warehouse_id = "26103348825381";//二楼缓存仓
MESCreateOutstockInput input = new()
{
outstock = new MESWmsOutstockHInput(),
outstockDs = new List<MESWmsOutstockDInput>()
};
PrdMoTask prdMoTask = await db.Queryable<PrdMoTask>().SingleAsync(x => x.id == generalOutstockInput.mo_task_id);
BasLocation location = await db.Queryable<BasLocation>().Where(x => x.location_code == generalOutstockInput.location_code).FirstAsync();
if (location == null)
{
throw Oops.Bah("未找到库位");
}
string locationId = location.id;
PrdOutstockH prdOutstockH = new()
{
bill_code = await _billRullService.GetBillNumber(CodeTemplateConst.PRDOUTSTOCK_CODE),
};
input.outstock.bill_type = DictConst.SHENGCHANLINGLIAO;
// input.outstock.bill_date = visualDevModelDataCrInput.data.ContainsKey("bill_date") ? Convert.ToDateTime(visualDevModelDataCrInput.data["bill_date"].ToString()) : DateTime.Now;
input.outstock.bill_date = DateTime.Now;
input.outstock.org_id = _userManager.GetUserInfo().Result.organizeId;
input.outstock.warehouse_id = warehouse_id;
input.outstock.create_id = _userManager.UserId;
input.outstock.location_code = location?.location_code ?? "";
input.outstock.source_code = prdOutstockH.bill_code;
input.outstock.source_id = prdOutstockH.id;
OrganizeEntity workline = await _organizeService.GetAnyParentByWorkstationId(generalOutstockInput.workstation_id, DictConst.RegionCategoryWorklineCode);
List<string> materialIds = generalOutstockInput.details.Select(x => x.material_id).ToList();
List<BasMaterial> basMaterials = await db.Queryable<BasMaterial>().Where(x => materialIds.Contains(x.id)).ToListAsync();
Dictionary<string, object> unitIdDic = await db.Queryable<BasMaterial>()
.LeftJoin<DictionaryTypeEntity>((a, b) => b.EnCode == DictConst.MeasurementUnit)
.LeftJoin<DictionaryDataEntity>((a, b, c) => b.Id == c.DictionaryTypeId && a.unit_id == c.EnCode)
.Where((a, b, c) => materialIds.Contains(a.id))
.Select((a, b, c) => new
{
key = a.id,
value = c.Id
})
.ToDictionaryAsync(x => x.key, x => x.value);
foreach (GeneralOutstockDInput item in generalOutstockInput.details)
{
input.outstockDs.Add(new MESWmsOutstockDInput()
{
material_id = item.material_id,
material_code = basMaterials.First(x => x.id == item.material_id).code,
pr_qty = item.num,
unit_id = unitIdDic[item.material_id].ToString()
});
}
string domain = _userManager.Domain;
Dictionary<string, object> header = new()
{
["Authorization"] = App.HttpContext!=null ? App.HttpContext.Request.Headers["Authorization"] : ""
};
string sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CREATE_OUTSTOCK, JsonConvert.SerializeObject(input), header);
Log.Information(sendResult);
AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
if (authResponse.code != 200)
{
throw Oops.Bah(authResponse.msg);
}
else
{
prdOutstockH.bill_type = DictConst.SHENGCHANLINGLIAO;
prdOutstockH.type = "1";
prdOutstockH.warehouse_id = warehouse_id;
prdOutstockH.location_code = generalOutstockInput.location_code;
prdOutstockH.create_id = _userManager.UserId;
prdOutstockH.org_id = _userManager.GetUserInfo().Result.organizeId;
prdOutstockH.bill_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
prdOutstockH.create_time = DateTime.Now;
prdOutstockH.workstation_id = generalOutstockInput.workstation_id;
prdOutstockH.workline = workline?.Id ?? "";
prdOutstockH.status = DictConst.OUTSTOCKSTATUSADD;
prdOutstockH.mo_task_id = prdMoTask.id;
List<PrdOutstockD> prdOutstockDs = new();
foreach (GeneralOutstockDInput item in generalOutstockInput.details)
{
prdOutstockDs.Add(new PrdOutstockD()
{
material_id = item.material_id,
material_code = basMaterials.First(x => x.id == item.material_id).code,
material_name = basMaterials.First(x => x.id == item.material_id).name,
pr_qty = item.num,
unit_id = unitIdDic[item.material_id].ToString(),
outstock_id = prdOutstockH.id,
source_id = prdMoTask.mo_task_code
});
}
DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
{
_ = await _repository.InsertAsync(prdOutstockH);
if (prdOutstockDs.Count > 0)
{
_ = await db.Insertable<PrdOutstockD>(prdOutstockDs).ExecuteCommandAsync();
}
});
if (!result.IsSuccess)
{
throw Oops.Bah(result.ErrorMessage);
}
}
return await Task.FromResult(true);
}
catch (Exception e)
{
Console.WriteLine(e);
Log.Error(e.Message);
throw Oops.Bah(e.Message);
}
}
[HttpPost]
public async Task<dynamic> MaterialOutstock(MaterialOutstockInput materialOutstockInput)
{
try
{
var db = _repository.AsSugarClient();
string warehouse_id = "27209786980373";//长件挤出缓存仓
MESCreateOutstockInput input = new MESCreateOutstockInput();
input.outstock = new MESWmsOutstockHInput();
input.outstockDs = new List<MESWmsOutstockDInput>();
PrdMoTask prdMoTask = await db.Queryable<PrdMoTask>().SingleAsync(x=>x.id==materialOutstockInput.mo_task_id);
BasLocation location = await db.Queryable<BasLocation>().Where(x=>x.location_code==materialOutstockInput.location_code).FirstAsync();
if (location == null) throw Oops.Bah("未找到库位");
string locationId = location.id;
PrdOutstockH prdOutstockH = new()
{
bill_code = await _billRullService.GetBillNumber(CodeTemplateConst.PRDOUTSTOCK_CODE),
};
input.outstock.bill_type = DictConst.SHENGCHANLINGLIAO;
// input.outstock.bill_date = visualDevModelDataCrInput.data.ContainsKey("bill_date") ? Convert.ToDateTime(visualDevModelDataCrInput.data["bill_date"].ToString()) : DateTime.Now;
input.outstock.bill_date = DateTime.Now;
input.outstock.org_id = _userManager.GetUserInfo().Result.organizeId;
input.outstock.warehouse_id = warehouse_id;
input.outstock.create_id = _userManager.UserId;
input.outstock.location_code = location?.location_code ?? "";
input.outstock.source_code = prdOutstockH.bill_code;
input.outstock.source_id = prdOutstockH.id;
OrganizeEntity workline = await _organizeService.GetAnyParentByWorkstationId(materialOutstockInput.workstation_id, DictConst.RegionCategoryWorklineCode);
List<string> materialIds = materialOutstockInput.details.Select(x => x.material_id).ToList();
List<BasMaterial> basMaterials = await db.Queryable<BasMaterial>().Where(x=>materialIds.Contains(x.id)).ToListAsync();
Dictionary<string,object> unitIdDic = await db.Queryable<BasMaterial>()
.LeftJoin<DictionaryTypeEntity>((a, b) => b.EnCode == DictConst.MeasurementUnit)
.LeftJoin<DictionaryDataEntity>((a, b, c) => b.Id == c.DictionaryTypeId && a.unit_id == c.EnCode)
.Where((a,b,c)=>materialIds.Contains(a.id))
.Select((a, b, c) => new
{
key = a.id,
value = c.Id
})
.ToDictionaryAsync(x => x.key, x => x.value);
foreach (var item in materialOutstockInput.details)
{
input.outstockDs.Add(new MESWmsOutstockDInput()
{
material_id = item.material_id,
material_code = basMaterials.First(x=>x.id==item.material_id).code,
pr_qty = item.num,
unit_id = unitIdDic[item.material_id].ToString()
});
}
string domain = _userManager.Domain;
Dictionary<string, object> header = new Dictionary<string, object>()
{
["Authorization"] = App.HttpContext!=null ? App.HttpContext.Request.Headers["Authorization"] : ""
};
var sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CREATE_OUTSTOCK,JsonConvert.SerializeObject(input),header);
Log.Information(sendResult);
AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
if (authResponse.code != 200)
{
throw Oops.Bah(authResponse.msg);
}
else
{
prdOutstockH.bill_type = DictConst.SHENGCHANLINGLIAO;
prdOutstockH.type = "2";
prdOutstockH.warehouse_id = warehouse_id;
prdOutstockH.location_code = materialOutstockInput.location_code;
prdOutstockH.create_id = _userManager.UserId;
prdOutstockH.org_id = _userManager.GetUserInfo().Result.organizeId;
prdOutstockH.bill_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
prdOutstockH.create_time = DateTime.Now;
prdOutstockH.workstation_id = materialOutstockInput.workstation_id;
prdOutstockH.workline = workline?.Id ?? "";
prdOutstockH.status = DictConst.OUTSTOCKSTATUSADD;
prdOutstockH.mo_task_id = prdMoTask.id;
List<PrdOutstockD> prdOutstockDs = new List<PrdOutstockD>();
foreach (var item in materialOutstockInput.details)
{
prdOutstockDs.Add(new PrdOutstockD()
{
material_id = item.material_id,
material_code = basMaterials.First(x=>x.id==item.material_id).code,
material_name = basMaterials.First(x=>x.id==item.material_id).name,
pr_qty = item.num,
unit_id = unitIdDic[item.material_id].ToString(),
outstock_id = prdOutstockH.id,
source_id = prdMoTask.mo_task_code
});
}
DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
{
await _repository.InsertAsync(prdOutstockH);
if (prdOutstockDs.Count > 0)
{
await db.Insertable<PrdOutstockD>(prdOutstockDs).ExecuteCommandAsync();
}
});
if (!result.IsSuccess)
{
throw Oops.Bah(result.ErrorMessage);
}
}
return await Task.FromResult(true);
}
catch (Exception e)
{
Console.WriteLine(e);
Log.Error(e.Message);
throw Oops.Bah(e.Message);
}
}
}
}