You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

438 lines
15 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using WmsWebApi.EntityFrameworkCore;
using WmsWebApi.Wms;
using Abp.Web.Models;
using Microsoft.EntityFrameworkCore;
using Abp.Domain.Uow;
namespace WmsWebApi.ZlldcjLogs;
[Route("/api/zlldcj")]
public class ZlldcjLogAppService : ApplicationService, IZlldcjLogAppService
{
private readonly ZlldcjLogManager _zlldcjLogManager;
private readonly ITbBillRepository _tbBillRepository;
private readonly ITmSapMoveTypeRepository _tbSapMoveTypeRepository;
private readonly ITmOtherAskRepository _tbOtherInOutAskRepository;
private readonly TmOtherAskRepositoryUpdate _tmOtherAskRepositoryUpdate;
private readonly Volo.Abp.Uow.IUnitOfWorkManager _unitOfWorkManager;
public ZlldcjLogAppService(ZlldcjLogManager zlldcjLogManager
, ITbBillRepository tbBillRepository
, ITmSapMoveTypeRepository tbSapMoveTypeRepository
, ITmOtherAskRepository tbOtherInOutAskRepository
, TmOtherAskRepositoryUpdate tmOtherAskRepositoryUpdate
, Volo.Abp.Uow.IUnitOfWorkManager unitOfWorkManager)
{
_zlldcjLogManager = zlldcjLogManager;
_tbBillRepository = tbBillRepository;
_tbSapMoveTypeRepository = tbSapMoveTypeRepository;
_tbOtherInOutAskRepository = tbOtherInOutAskRepository;
_tmOtherAskRepositoryUpdate = tmOtherAskRepositoryUpdate;
_unitOfWorkManager = unitOfWorkManager;
}
[HttpPost("add")]
public async Task<ReturnResult> AddAsync(List<ZlldcjLogDto> dto)
{
var result = new ReturnResult();
List<ZlldcjLog> _ZlldcjLogs = new List<ZlldcjLog>();
foreach (var dtoItem in dto)
{
var entity = ObjectMapper.Map<ZlldcjLogDto, ZlldcjLog>(dtoItem);
entity.SetId(GuidGenerator);
_ZlldcjLogs.Add(entity);
}
try
{
var ret = await _zlldcjLogManager.AddAsync(_ZlldcjLogs);
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = ex.Message;
}
return result;
}
[HttpPost("addObj")]
public async Task<ReturnResult> AddAsync([FromBody] object content)
{
var result = new ReturnResult();
List<ZlldcjLog> _ZlldcjLogs = new List<ZlldcjLog>();
List<ZlldcjLogDto> dtos;
bool bErr = false;
try
{
dtos = JsonConvert.DeserializeObject<List<ZlldcjLogDto>>(content.ToString());
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = "Json格式不正确,详细信息:" + ex.GetBaseException().Message;
//return result;
return result;
}
try
{
var dtoList = dtos.Where(p => p.WERKS == "1000").ToList();
if (dtoList == null || dtoList.Count == 0)
{
bErr = true;
result.MESSAGE = "err:没有1000工厂数据";
}
else
{
foreach (var dtoItem in dtos)
{
var entity = ObjectMapper.Map<ZlldcjLogDto, ZlldcjLog>(dtoItem);
entity.SetId(GuidGenerator);
_ZlldcjLogs.Add(entity);
}
var djList = _ZlldcjLogs.Select(p => p.ZLLDJ).Distinct();
foreach (var dj in djList)
{
var _zlldcjList = _ZlldcjLogs.Where(p => p.ZLLDJ == dj).ToList();
//var ret = await _zlldcjLogManager.AddAsync(_zlldcjList);
//检查是否为存在
var tbBill = await _tbBillRepository.FirstOrDefaultAsync(p => p.BillNum == dj);
//tbBill.CheckIsNotExist();
if (tbBill != null)
{
result.TYPE = 'E';
bErr = true;
result.MESSAGE = $"{dj} 单据已存在";
break;
}
//TODO 创建 tbOrder
tbBill = new TB_BILL
{
BillNum = dj,
BillTime = DateTime.Now,
OperName = _zlldcjList[0].ERNAM,
DetailQty = _zlldcjList.Count,
BillType = 309,
State = 0
//...
};
//确定SubBillType
var sapMoveType = await _tbSapMoveTypeRepository.FirstOrDefaultAsync(p => p.SAPBillType == _zlldcjList[0].ZDJLX
&& p.GetOrBack == _zlldcjList[0].ZLTLX
&& p.MoveType == _zlldcjList[0].BWART);
var sapBillDesc = "";
if (sapMoveType != null)
{
tbBill.SubBillType = sapMoveType.SubBillType;
tbBill.Remark = sapMoveType.SAPBillDesc;
sapBillDesc = sapMoveType.SAPBillDesc;
List<TM_PG_OTHER_INOUT_ASK> askList = new List<TM_PG_OTHER_INOUT_ASK>();
foreach (var item in _zlldcjList)
{
var hasdetail = await _tbOtherInOutAskRepository.FirstOrDefaultAsync(p => p.BillNum == item.ZLLDJ && p.BillLineID == item.ZLLITEM.ToString());
if (hasdetail != null)
{
throw new UserFriendlyException($"单号 {item.ZLLDJ},行号 {item.ZLLITEM} 已存在!");
}
TM_PG_OTHER_INOUT_ASK ask = new TM_PG_OTHER_INOUT_ASK
{
BillNum = item.ZLLDJ,
BillLineID = item.ZLLITEM.ToString(),
BillType = item.ZDJLX,
BillTypeDesc = sapBillDesc,
GetOrBack = item.ZLTLX,
PartCode = item.MATNR,
Qty = item.BDMNG,
BUn = item.MEINS,
CostCenter = item.KOSTL,
BaseOrder = item.AUFNR,
MoveType = item.BWART,
OrderReason = item.GRUND.ToString(),
MoveReason = sapBillDesc,
Creator = item.ERNAM,
CreatDate = item.ERDAT + " " + item.ERZET,
Remark = item.ZBZSM,
RejecteQty = item.BKNUM,
PickLocation = item.LGORT1,
RejecteLocation = item.LGORT2,
ClaimLocation = item.LGORT3,
PickPerson = item.ZLLR
};
askList.Add(ask);
item.IType = "新增";
}
//新增本地单据
await _tbBillRepository.InsertAsync(tbBill, true);
await _tbOtherInOutAskRepository.InsertManyAsync(askList);
}
else
{
foreach (var item in _zlldcjList)
{
item.IType = "WMS 不需要的接口";
}
}
//新增日志记录
await _zlldcjLogManager.AddZlldcjAsync(_zlldcjList);
}
}
}
catch (Exception ex)
{
bErr = true;
result.TYPE = 'E';
result.MESSAGE = ex.Message;
throw new UserFriendlyException(ex.Message, null, null, ex);
}
finally
{
}
try
{
WmsWebApiZLLDCJDTO zlldcjdto = new WmsWebApiZLLDCJDTO()
{
ZLLDJ = dtos[0].ZLLDJ,
ZDJLX = dtos[0].ZDJLX,
ZLTLX = dtos[0].ZLTLX,
JSON = content.ToString()
};
zlldcjdto.SetId(GuidGenerator);
if (bErr)
{
zlldcjdto.ITYPE = result.MESSAGE;
}
await _zlldcjLogManager.AddDtoAsync(zlldcjdto);
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = ex.GetBaseException().Message;
}
return result;
}
private async Task AddDtoAsync(WmsWebApiZLLDCJDTO dto)
{
if (dto == null) { return; }
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
await _zlldcjLogManager.AddDtoAsync(dto);
await uow.SaveChangesAsync();
}
}
private async Task AddZlldcjAsync(List<ZlldcjLog> dtos)
{
if (dtos == null || dtos.Count <= 0) { return; }
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
await _zlldcjLogManager.AddZlldcjAsync(dtos);
await uow.SaveChangesAsync();
}
}
[HttpPost("close")]
//[UnitOfWork(false)]
public async Task<ReturnResult> CloseAsync([FromBody] object content)//ZlldcjLogDto dto
{
var result = new ReturnResult();
List<ZlldcjLogDto> dtos;
List<ZlldcjLog> _ZlldcjLogs = new List<ZlldcjLog>();
bool bErr = false;
try
{
dtos = JsonConvert.DeserializeObject<List<ZlldcjLogDto>>(content.ToString());
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = "Json格式不正确,详细信息:" + ex.Message;
return result;
}
foreach (var dtoItem in dtos)
{
var entity = ObjectMapper.Map<ZlldcjLogDto, ZlldcjLog>(dtoItem);
entity.SetId(GuidGenerator);
_ZlldcjLogs.Add(entity);
}
try
{
//var ret = await _zlldcjLogManager.CloseAsync(entity);
var tbBill = await _tbBillRepository.FirstOrDefaultAsync(p => p.BillNum == _ZlldcjLogs[0].ZLLDJ);
var tbOtherAskList = _tbOtherInOutAskRepository.Where(p => p.BillNum == _ZlldcjLogs[0].ZLLDJ);
//检查是否存在
if (tbBill == null)
{
bErr = true;
result.MESSAGE = $"{_ZlldcjLogs[0].ZLLDJ} 单据不存在";
}
else
{
//检查状态是否为开启
//tbBill.CheckStatusIsOpen();
if (tbBill.State == 9 || tbBill.State == 2 || tbBill.State == -1)
{
result.TYPE = 'E';
bErr = true;
result.MESSAGE = $"{_ZlldcjLogs[0].ZLLDJ} 单据已完成或已关闭";
}
else
{
bool bClose = true, bFind = false;
foreach (var item in tbOtherAskList)
{
if (_ZlldcjLogs.Find(p => p.ZLLITEM.ToString() == item.BillLineID.Trim()) != null)
{
bFind = true;
item.IsClosed = true;
await _tmOtherAskRepositoryUpdate.UpdateOtherAsk(item);
}
else
{
if (item.IsClosed == false)
bClose = false;
}
}
if (bClose)
{
tbBill.State = 9;
await _tmOtherAskRepositoryUpdate.UpdateBillState(tbBill);
}
//新增日志记录
var ret = await _zlldcjLogManager.CloseAsync(_ZlldcjLogs);
}
}
WmsWebApiZLLDCJDTO zlldcjdto = new WmsWebApiZLLDCJDTO()
{
ZLLDJ = dtos[0].ZLLDJ,
ZDJLX = dtos[0].ZDJLX,
ZLTLX = dtos[0].ZLTLX,
JSON = content.ToString(),
ITYPE = "关闭!"
};
zlldcjdto.SetId(GuidGenerator);
if (bErr)
{
zlldcjdto.ITYPE += result.MESSAGE;
}
await _zlldcjLogManager.AddDtoAsync(zlldcjdto);
await CurrentUnitOfWork.SaveChangesAsync();
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = ex.Message;
}
return result;
}
[HttpPost("test/shiwu")]
public async Task<ReturnResult> TestShiWu()
{
var result = new ReturnResult();
result.TYPE = 'E';
result.MESSAGE = "测试返回结构";
//throw new UserFriendlyException("经过进一步包装的异常", null, null);
throw new Exception("经过进一步包装的异常");
return result;
var dateTime = DateTime.Now;
var tbBill = new TB_BILL()
{
BillNum = dateTime.ToString("yyyyMMddHHmmss"),
State = 0,
BillType = 0,
SubBillType = 0,
BillTime = dateTime,
OperName = dateTime.ToString("yyyyMMddHHmmss"),
DetailQty = 0,
Guid = GuidGenerator.Create()
};
List<TM_PG_OTHER_INOUT_ASK> askList = new List<TM_PG_OTHER_INOUT_ASK>();
for (int i = 0; i < 2; i++)
{
askList.Add(new TM_PG_OTHER_INOUT_ASK()
{
BillNum = dateTime.ToString("yyyyMMddHHmmss"),
BillLineID = i.ToString(),
});
}
var dbContext = await _tbBillRepository.GetDbContextAsync();
dbContext.Database.SetCommandTimeout(120);
try
{
await _tbBillRepository.InsertAsync(tbBill);
throw new Exception("异常了");
await _tbOtherInOutAskRepository.InsertManyAsync(askList);
}
catch (Exception ex)
{
result.TYPE = 'E';
result.MESSAGE = ex.GetBaseException().Message;
throw new UserFriendlyException("经过进一步包装的异常", null, null, ex);
//throw new Exception("经过进一步包装的异常", ex);
}
finally
{
await SaveLogsAsync();
}
return result;
}
private async Task SaveLogsAsync()
{
var dateTime = DateTime.Now;
var tbBill = new TB_BILL()
{
BillNum = dateTime.ToString("yyyyMMddHHmmss") + "log",
State = 0,
BillType = 0,
SubBillType = 0,
BillTime = dateTime,
OperName = dateTime.ToString("yyyyMMddHHmmss"),
DetailQty = 0,
Guid = GuidGenerator.Create()
};
using (var uow = _unitOfWorkManager.Begin(
requiresNew: true, isTransactional: false
))
{
await _tbBillRepository.InsertAsync(tbBill, true);
//await uow.SaveChangesAsync();
}
}
}