using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Newtonsoft.Json;
using Volo.Abp.Application.Services;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using WmsWebApi.BackgroundJob;
using WmsWebApi.Domain;
using WmsWebApi.EntityFrameworkCore;
using WmsWebApi.Wms;
namespace WmsWebApi.Parts;
///
/// 零件
///
[Route("/api/part")]
public class PartService : ApplicationService, IPartService
{
private readonly ITmPgPartgroupRepository _tmPgPartgroupRepository;
private readonly ITaPartRepository _taPartRepository;
private readonly ITaCustPartRepository _taCustPartRepository;
private readonly IPartManager _partDtoRepository;
private readonly TmPgWmsUpdate _tmPgWmsUpdate;
private readonly Volo.Abp.Uow.IUnitOfWorkManager _unitOfWorkManager;
private readonly IBackgroundJobManager _backgroundJobManager;
///
/// 是否是请求重试
///
public bool IsRequestRetry { get; set; } = false;
public PartService(ITmPgPartgroupRepository tmPgPartgroupRepository, ITaPartRepository taPartRepository,
ITaCustPartRepository taCustPartRepository,
IPartManager partDtoRepository,
TmPgWmsUpdate tmPgWmsUpdate,
IUnitOfWorkManager unitOfWorkManager,
IBackgroundJobManager backgroundJobManager)
{
_tmPgPartgroupRepository = tmPgPartgroupRepository;
_taPartRepository = taPartRepository;
_taCustPartRepository = taCustPartRepository;
_partDtoRepository = partDtoRepository;
_tmPgWmsUpdate = tmPgWmsUpdate;
_unitOfWorkManager = unitOfWorkManager;
_backgroundJobManager = backgroundJobManager;
}
[HttpPost("add")]
//[UnitOfWork(false)]
public async Task AddAsync([FromBody] object content)
{
var result = new ReturnResult();
PartDto _part;
bool bUpdate = false,bOtherWork = false,bErr = false;
try
{
_part = JsonConvert.DeserializeObject(content.ToString());
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = "Json格式不正确,详细信息:" + ex.Message;
return result;
}
if (string.IsNullOrEmpty(_part.MATNR))
{
result.TYPE = 'E';
result.MESSAGE = "物料号不能为空!";
return result;
}
WmsWebApiPARTDTO partdto = new WmsWebApiPARTDTO()
{
MATNR = _part.MATNR,
MBRSH = _part.MBRSH,
MTART = _part.MTART,
WERKS = _part.WERKS,
VTWEG = _part.VTWEG,
MAKTX = _part.MAKTX,
MAKTX1 = _part.MAKTX1,
MEINS = _part.MEINS,
MATKL = _part.MATKL,
GROES = _part.GROES,
ZTEXT22 = _part.ZTEXT22,
DISGR = _part.DISGR,
JSON = content.ToString()
};
partdto.SetId(GuidGenerator);
try
{
//判断是否是长春工厂
if (_part.WERKS != "1000")
{
bOtherWork = true;
result.MESSAGE = "其他工厂数据";
}
else
{
if (_part.MATNR.Trim().StartsWith("1") || _part.MATNR.Trim().StartsWith("2") || _part.MATNR.Trim().StartsWith("3") || _part.MATNR.Trim().StartsWith("7"))
{
#region part信息更新
var part = new TA_PART();
//更新part信息
part.ErpPartCode = _part.MATNR.Trim();
part.PartCode = _part.MATNR.Trim();
//[DisplayName("物料描述1")]--物料描述
part.PartDesc1 = _part.MAKTX.Trim();
//[DisplayName("物料描述2")]--大小量纲
part.PartDesc2 = _part.GROES.Trim();
//[DisplayName("项目类别组")]--MRP类型
part.ProjectId = _part.DISMM.Trim();
//[DisplayName("单位")]--单位
part.Unit = _part.MEINS.Trim();
//[DisplayName("物料类型")] 直接是成品---物料类型编码
part.PartType = _part.MTART.Trim();
////[DisplayName("物料种类")]--
//part.PartKind = dr["物料种类"].ToString();
part.PartKind = string.Empty;
//[DisplayName("物料组")]--物料组编码
part.PartGroup = _part.MATKL.Trim();
//[DisplayName("状态")]
part.State = "A";
if (_part.MMSTA.Trim().ToUpper() == "Z2")
part.State = "H";
//[DisplayName("管理类型")] --判断物料组属性
//part.ManageType = dr["序列号管理"].ToString() == "1" ? EnumManageType.单包装 : EnumManageType.批量;
part.ManageType = 0;
var partgroup = await _tmPgPartgroupRepository.FirstOrDefaultAsync(p => p.WERKS == "1000" && p.PARTGROUP == part.PartGroup && p.IsManageType);
if (partgroup != null)
{
part.ManageType = 1;
}
//[DisplayName("价格标识")]---价格标识
var detail1 = _part.zzchar.FirstOrDefault(p => p.CHARACT == "ZMM005");
if (detail1 != null)
part.Promo = detail1.VALUE_NEUTRAL.Trim();
//更新时间
part.PalletTemplate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//[DisplayName("ABC类")] 暂时作为位置信息使用---总成控制号标识
detail1 = _part.zzchar.FirstOrDefault(p => p.CHARACT == "ZMM006");
if (detail1 != null)
part.AbcClass = detail1.VALUE_NEUTRAL.Trim();
// ---关键控制号标识
detail1 = _part.zzchar.FirstOrDefault(p => p.CHARACT == "ZMM007");
if (detail1 != null)
part.PalletSize = detail1.VALUE_NEUTRAL.Trim();
//[DisplayName("配置信息")] 暂时作为位置信息使用--位置
detail1 = _part.zzchar.FirstOrDefault(p => p.CHARACT == "ZMM003");
if (detail1 != null)
part.Configuration = detail1.VALUE_NEUTRAL.Trim();
//[DisplayName("存储标包数")] --序列号管理
decimal stockPackQty = 1;
if (part.ManageType == 1)
{
part.StockPackQty = 1;
}
else
{
detail1 = _part.zzchar.FirstOrDefault(p => p.CHARACT == "ZMM008");
if (detail1 == null)
{
part.StockPackQty = 1;
}
else
{
if (decimal.TryParse(detail1.VALUE_NEUTRAL, out stockPackQty))
{
part.StockPackQty = stockPackQty;
}
else
{
part.StockPackQty = 1;
}
}
}
//[DisplayName("收货口")]--交货工厂
part.ReceivePort = _part.DWERK.Trim();
//[DisplayName("备注")]--补充备注
part.Remark = _part.ZTEXT.Trim();
//[DisplayName("图号")] 物料组描述--物料组
part.DrawingNumber = _part.WGBEZ.Trim();
//[DisplayName("旧物料号")]--旧物料号
part.StoreKeeper = _part.BISMT.Trim();
#endregion
var current = await _taPartRepository.FirstOrDefaultAsync(p => p.PartCode == part.PartCode);
if (current == null)
{
await _taPartRepository.UpsertAsync(part);
}
else
{
bUpdate = true;
await _tmPgWmsUpdate.UpdateTaPartAsync(part);
}
//没有客户物料号就不管理 --客户物料号 ZTEXT22
string custPartCode = _part.ZTEXT22.Trim();
// 分销渠道
string custId = _part.VTWEG.Trim();
//判断有没有对应的CustP
//art信息,没有就新建,有就更新
var custpart = new TA_CUST_PART();
#region custpart信息更新
//[DisplayName("客户编号")]
custpart.CustId = custId;
//[DisplayName("客户物料号")]--客户物料号
custpart.CustPartCode = _part.ZTEXT22.Trim();
//[DisplayName("客户物料描述")]--补充备注
custpart.CustPartName = _part.ZTEXT.Trim();
//[DisplayName("物料号")]
custpart.PartCode = part.PartCode.Trim();
//[DisplayName("备注")]--分销渠道
custpart.Remark = _part.VTWEG.Trim();
#endregion
if (!string.IsNullOrEmpty(custPartCode))
{
//var curCustPart = await _taCustPartRepository.FirstOrDefaultAsync(p => p.CustId == custpart.CustId && p.PartCode == custpart.PartCode);
//if (curCustPart == null)
//{
// await _taCustPartRepository.UpsertAsync(custpart);
//}
//else
//{
// await _tmPgWmsUpdate.UpdateTaCustPartAsync(custpart);
//}
await _tmPgWmsUpdate.DeleteTaCustPartAsync(custpart);
await _taCustPartRepository.UpsertAsync(custpart);
}
else //删除对应客户物料
{
await _tmPgWmsUpdate.DeleteTaCustPartAsync(custpart);
}
}
else
{
bErr = true;
result.MESSAGE = $"非WinWMS使用的物料号!{_part.MATNR.Trim()}";
}
}
}
catch (Exception ex)
{
bErr = true;
partdto.EnumRetryStatus = Enums.EnumRetryStatus.WaitRetry;
result.TYPE = 'E';
result.MESSAGE = ex.GetBaseException().Message;
if (IsRequestRetry == false)
{
await AddBackgroundJobAsync(content.ToString());
}
throw new Exception($"接口异常,请稍后重试:{ex.GetBaseException().Message}", ex);
}
finally
{
if (IsRequestRetry == false)
{
if (bUpdate)
{
partdto.ITYPE = "更新";
}
if (bOtherWork)
{
partdto.ITYPE = "非长春工厂数据";
}
if (bErr)
{
partdto.ITYPE = result.MESSAGE;
}
await AddWmsWebApiPARTDTONowUnitOfWorkAsync(partdto);
}
}
return result;
}
private async Task AddWmsWebApiPARTDTONowUnitOfWorkAsync(WmsWebApiPARTDTO wmsWebApiPARTDTO)
{
if (wmsWebApiPARTDTO == null) { return; }
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
await _partDtoRepository.AddAsync(wmsWebApiPARTDTO);
await uow.SaveChangesAsync();
}
}
///
/// 添加后台工作者
///
/// Json参数
///
private async Task AddBackgroundJobAsync(string strJson)
{
if (strJson == null || strJson == "") { return; }
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
var requestRetryArgs = new RequestRetryArgs()
{
ActionName = Enums.EnumActionName.PartAdd,
StrJson = strJson
};
await _backgroundJobManager.EnqueueAsync(requestRetryArgs, priority: BackgroundJobPriority.Normal, delay: TimeSpan.FromSeconds(60));
await uow.SaveChangesAsync();
}
}
}