using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Uow; using WmsWebApi.EntityFrameworkCore; using WmsWebApi.OtherZll; using WmsWebApi.Wms; namespace WmsWebApi.Boms; [Route("/api/bom")] public class BomService : ApplicationService, IBomService { private readonly ITmPgPartgroupRepository _tmPgPartgroupRepository; private readonly ITaBomRepository _taBomRepository; private readonly ITaPartRepository _taPartRepository; private readonly IBomManager _bomDtoRepository; private readonly TmPgWmsUpdate _tmPgWmsUpdate; private readonly Volo.Abp.Uow.IUnitOfWorkManager _unitOfWorkManager; public BomService(ITmPgPartgroupRepository tmPgPartgroupRepository, ITaBomRepository taBomRepository, ITaPartRepository taPartRepository, IBomManager bomDtoRepository, TmPgWmsUpdate tmPgWmsUpdate , Volo.Abp.Uow.IUnitOfWorkManager unitOfWorkManager) { _tmPgPartgroupRepository = tmPgPartgroupRepository; _taBomRepository = taBomRepository; _taPartRepository = taPartRepository; _bomDtoRepository = bomDtoRepository; _tmPgWmsUpdate = tmPgWmsUpdate; _unitOfWorkManager = unitOfWorkManager; } [HttpPost("add")] //[UnitOfWork(false)] public async Task AddAsync([FromBody] object content) { var result = new ReturnResult(); BomDto _bom; bool bUpdate = false,bOtherWork = false, bDel = false,bNotFind = false, bPartGroup = false; try { _bom = JsonConvert.DeserializeObject(content.ToString()); } catch (Exception ex) { result.TYPE = 'E'; result.MESSAGE = "Json格式不正确,详细信息:" + ex.Message; return result; } if (string.IsNullOrEmpty(_bom.MATNR)) { result.TYPE = 'E'; result.MESSAGE = "物料号不能为空"; return result; } WmsWebApiBOMDTO bomdto = new WmsWebApiBOMDTO() { MATNR = _bom.MATNR, MAKTX = _bom.MAKTX, WERKS = _bom.WERKS, STLAN = _bom.STLAN, STLAL = _bom.STLAL, DATUV = _bom.DATUV, BMENG = _bom.BMENG, STLST = _bom.STLST, LOEKZ = _bom.LOEKZ, JSON = content.ToString() }; bomdto.SetId(GuidGenerator); try { var _remark = DateTime.Now.ToString("yyyy-MM-dd") + "!" + Guid.NewGuid(); if (_bom.WERKS == "1000") { var partgrouplist = _tmPgPartgroupRepository.Where(p => p.IsBom == true); var part = _taPartRepository.FirstOrDefault(p => p.PartCode == _bom.MATNR); if (part == null) { bNotFind = true; result.MESSAGE = $"没有找到物料号!{_bom.MATNR}"; } else { WmsWebApiBom b = new WmsWebApiBom() { MATNR = _bom.MATNR, MAKTX = _bom.MAKTX, WERKS = _bom.WERKS, STLAN = _bom.STLAN, STLAL = _bom.STLAL, DATUV = _bom.DATUV, BMENG = _bom.BMENG, STLST = _bom.STLST, LOEKZ = _bom.LOEKZ, IsFisRead = false, IsDelete = false, }; b.SetId(GuidGenerator); //判断是删除还是增加 if (_bom.LOEKZ == "1" || _bom.items.Count == 0) { bDel = true; bPartGroup = false; //var bom = new TA_BOM(); //bom.PartCode = _bom.MATNR; //await _tmPgWmsUpdate.DelTaBomAsync(bom); b.IsDelete = true; await _bomDtoRepository.AddBomAsync(b); } else { #region BOM信息更新 if (partgrouplist.Select(p => p.PARTGROUP).Contains(part.PartGroup)) { List bDetail = new List(); foreach (var item in _bom.items) { /*var bom = new TA_BOM(); bom.PartCode = _bom.MATNR; bom.BeginDate = DateTime.ParseExact(_bom.DATUV, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);//DateTime.Parse(_bom.DATUV); bom.Remark = _remark; bom.SubPartCode = item.IDNRK; bom.Qty = item.MENGE; var tmp = await _taBomRepository.FirstOrDefaultAsync(p => p.PartCode == _bom.MATNR && p.SubPartCode == item.IDNRK && p.BeginDate == bom.BeginDate); if (tmp == null) { await _taBomRepository.UpsertAsync(bom); } else { bUpdate = true; await _tmPgWmsUpdate.UpdateTaBomAsync(bom); }*/ WmsWebApiBomDetail tmp = new WmsWebApiBomDetail() { PId = b.Id, POSNR = item.POSNR, POSTP = item.POSTP, IDNRK = item.IDNRK, MAKTX_1 = item.MAKTX_1, DUMPS = item.DUMPS, MENGE = item.MENGE, MEINS = item.MEINS, SORTF = item.SORTF, ALPGR = item.ALPGR, ALPRF = item.ALPRF, ALPST = item.ALPST, EWAHR = item.EWAHR, ITSOB = item.ITSOB, LGORT = item.LGORT, SANKA = item.SANKA, SANFE = item.SANFE, }; tmp.SetId(GuidGenerator); bDetail.Add(tmp); } await _bomDtoRepository.AddBomAsync(b); await _bomDtoRepository.AddBomDetailAsync(bDetail); } else { bPartGroup = true; result.MESSAGE = "没有对应物料组数据"; } #endregion } } } else { bOtherWork = true; result.MESSAGE = "非长春工厂数据!"; } } catch (Exception ex) { bomdto.ITYPE = ex.GetBaseException().Message; bomdto.EnumRetryStatus = Enums.EnumRetryStatus.WaitRetry; result.TYPE = 'E'; result.MESSAGE = ex.GetBaseException().Message; throw new Exception($"接口异常,请稍后重试:{ex.GetBaseException().Message}", ex); } finally { if (bNotFind) { bomdto.ITYPE = result.MESSAGE; } if (bUpdate) { bomdto.ITYPE = "更新!"; } if (bOtherWork) { bomdto.ITYPE = "非长春工厂数据!"; } if (bPartGroup) { bomdto.ITYPE = "没有对应物料组数据!"; } if (bDel) { bomdto.ITYPE += "删除!"; } await AddWmsWebApiBOMDtoNowUnitOfWorkAsync(bomdto); } return result; } private async Task AddWmsWebApiBOMDtoNowUnitOfWorkAsync(WmsWebApiBOMDTO wmsWebApiBOMDTO) { if (wmsWebApiBOMDTO == null) { return; } using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) { await _bomDtoRepository.AddAsync(wmsWebApiBOMDTO); await uow.SaveChangesAsync(); } } }