Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/MesForErpService.cs

168 lines
6.9 KiB
C#

using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Message.Service;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.TaskScheduler;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Entity;
using Tnb.WarehouseMgr.Entities.Consts;
namespace Tnb.ProductionMgr
{
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class MesForErpService: IDynamicApiController, ITransient
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRuleService;
public static Dictionary<string, string> moTypeDic = new Dictionary<string, string>()
{
["1"] = "25019163616533", //注塑
["2"] = "25019172714005", //挤出
["3"] = "25019181615125", //组装
["4"] = "25019191681045", //包装
};
public MesForErpService(ISqlSugarRepository<PrdMo> repository,
IBillRullService billRuleService,
IUserManager userManager)
{
_userManager = userManager;
_billRuleService = billRuleService;
_db = repository.AsSugarClient();
}
[HttpPost]
[AllowAnonymous]
public async Task<string> SavePrdMo(List<PrdMo> input)
{
if (input == null || input.IsEmpty())
throw Oops.Bah("参数不能为空");
Log.Information($"生产工单接收参数:{JsonConvert.SerializeObject(input)}");
List<PrdMo> moList = new List<PrdMo>();
List<ErpExtendField> extendFieldList = new List<ErpExtendField>();
foreach (var item in input)
{
if (string.IsNullOrEmpty(item.mo_code))
{
Log.Error("【SavePrdMo】工单代码不能为空");
throw Oops.Bah("工单代码不能为空");
}
if (string.IsNullOrEmpty(item.mo_type))
{
Log.Error("【SavePrdMo】工单类型不能为空");
throw Oops.Bah("工单类型不能为空");
}
if (item.plan_start_date == null)
{
Log.Error("【SavePrdMo】计划开始日期不能为空");
throw Oops.Bah("计划开始日期不能为空");
}
if (item.plan_end_date == null)
{
Log.Error("【SavePrdMo】计划结束日期不能为空");
throw Oops.Bah("计划结束日期不能为空");
}
if (string.IsNullOrEmpty(item.material_code))
{
Log.Error("【SavePrdMo】物料编号不能为空");
throw Oops.Bah("物料编号不能为空");
}
if (string.IsNullOrEmpty(item.unit_id))
{
Log.Error("【SavePrdMo】单位不能为空");
throw Oops.Bah("单位不能为空");
}
if (item.plan_qty == null || item.plan_qty <= 0)
{
Log.Error("【SavePrdMo】计划数量不能为空");
throw Oops.Bah("计划数量不能为空");
}
if (item.ebom_version == null || item.ebom_version.IsEmpty())
{
Log.Error("【SavePrdMo】物料清单版本不能为空");
throw Oops.Bah("物料清单版本不能为空");
}
BasMaterial basMaterial = await _db.Queryable<BasMaterial>().SingleAsync(x => x.code == item.material_code);
if (basMaterial == null)
{
Log.Error($"【SavePrdMo】未找到物料编号为{item.material_code}的物料");
throw Oops.Bah($"未找到物料编号为{item.material_code}的物料");
}
List<BasMaterialUnit> basMaterialUnits = await _db.Queryable<BasMaterialUnit>().Where(x => x.material_id == basMaterial.id).ToListAsync();
List<String> units = basMaterialUnits.Select(x => x.auxiliary_unit_id).Distinct().ToList();
if(units!=null && !string.IsNullOrEmpty(basMaterial.unit_id)) units.Add(basMaterial.unit_id);
if (!units.Contains(item.unit_id))
{
Log.Error($"【SavePrdMo】{basMaterial.name}不存在{item.unit_id}该单位");
throw Oops.Bah($"{basMaterial.name}不存在{item.unit_id}该单位");
}
if (!await _db.Queryable<BasEbomH>().AnyAsync(x => x.material_id == basMaterial.id && x.version == item.ebom_version))
{
Log.Error($"【SavePrdMo】系统中无法找到物料清单{item.ebom_version}版本");
throw Oops.Bah($"系统中无法找到物料清单{item.ebom_version}版本");
}
DictionaryDataEntity unitDic = await _db.Queryable<DictionaryDataEntity>().Where(x=>x.DictionaryTypeId==WmsWareHouseConst.UNITTYPEID && x.EnCode==item.unit_id).FirstAsync();
item.id = SnowflakeIdHelper.NextId();
item.material_id = basMaterial.id;
item.mo_source = "1";
item.mo_type = moTypeDic[item.mo_type];
item.mo_code = item.mo_code;
item.create_id = WmsWareHouseConst.AdministratorUserId;
item.create_time = DateTime.Now;
item.mo_status = DictConst.ToBeScheduledId;
item.erp_mo_pk = item.erp_mo_pk;
item.erp_line_pk = item.erp_line_pk;
item.erp_lineno = item.erp_lineno;
item.unit_id = unitDic?.Id ?? item.unit_id;
moList.Add(item);
}
DbResult<bool> result = await _db.Ado.UseTranAsync(async () =>
{
await _db.Insertable(moList).ExecuteCommandAsync();
});
if (result.IsSuccess)
{
Log.Information($"【SavePrdMo】生产工单保存成功");
}
else
{
Log.Error($"【SavePrdMo】{result.ErrorMessage}");
throw Oops.Bah($"{result.ErrorMessage}");
}
return !result.IsSuccess ? result.ErrorMessage : "保存成功";
}
}
}