161 lines
7.2 KiB
C#
161 lines
7.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using JNPF.Common.Contracts;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.FriendlyException;
|
|
using SqlSugar;
|
|
using Tnb.BasicData.Entities;
|
|
using Tnb.Common.Utils;
|
|
using Tnb.QcMgr.Entities;
|
|
using Tnb.QcMgr.Entities.Enums;
|
|
using Tnb.QcMgr.Interfaces;
|
|
using Tnb.WarehouseMgr.Entities;
|
|
using Tnb.WarehouseMgr.Entities.Consts;
|
|
using Tnb.WarehouseMgr.Entities.Dto;
|
|
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
|
|
using Tnb.WarehouseMgr.Entities.Entity.Constraints;
|
|
using Tnb.WarehouseMgr.Entities.Enums;
|
|
using Tnb.WarehouseMgr.Interfaces;
|
|
|
|
namespace Tnb.WarehouseMgr
|
|
{
|
|
/// <summary>
|
|
/// 采购收货、销售发货,通用业务类
|
|
/// </summary>
|
|
/// <typeparam name="TSubEntity"></typeparam>
|
|
public class WmsPurchaseAndSaleCommonService<TSubEntity> : BaseWareHouseService where TSubEntity : BaseEntity<string>, IPurchaseAndSaleEntity, IPurchaseAndSaleQueryEntity, new()
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IUserManager _userManager;
|
|
private static Dictionary<string, object> s_materialMap = new();
|
|
private readonly IQcCheckPlanService _qcCheckPlanService;
|
|
|
|
public WmsPurchaseAndSaleCommonService(ISqlSugarRepository<WmsPurchaseH> repo, IUserManager userManager, IQcCheckPlanService qcCheckPlanService)
|
|
{
|
|
_db = repo.AsSugarClient();
|
|
_userManager = userManager;
|
|
_qcCheckPlanService = qcCheckPlanService;
|
|
}
|
|
|
|
protected async Task<List<TSubEntity>> PurchaseAndSaleUpdate(PurchaseAndReceiveUpInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException("input");
|
|
if (input.details == null) throw new ArgumentNullException(nameof(input.details));
|
|
|
|
var ids = input.details.Select(x => x.id).ToList();
|
|
var purchaseDs = await _db.Queryable<TSubEntity>().Where(it => ids.Contains(it.id)).ToListAsync();
|
|
|
|
if (purchaseDs?.Count > 0)
|
|
{
|
|
var errMaterialTipList = new List<(string materialName, decimal purchaseArriveQty, decimal topPurchaseQty)>();
|
|
|
|
foreach (var pd in purchaseDs)
|
|
{
|
|
var pdInput = input.details.Find(x => x.id == pd.id);
|
|
if (pdInput != null)
|
|
{
|
|
pd.purchase_prqty += pdInput.purchase_arriveqty;
|
|
|
|
var topPurchaseQty = pd.purchase_qty - pd.purchase_prqty;
|
|
if (pdInput.purchase_arriveqty > topPurchaseQty && s_materialMap.ContainsKey(pdInput.material_id) && s_materialMap[pdInput.material_id] != null)
|
|
{
|
|
errMaterialTipList.Add((s_materialMap[pdInput.material_id].ToString()!, pdInput.purchase_arriveqty, topPurchaseQty));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
pd.line_status = pd.purchase_prqty >= pd.purchase_qty ? WmsWareHouseConst.BILLSTATUS_COMPLETE_ID : WmsWareHouseConst.BILLSTATUS_ON_ID;
|
|
}
|
|
|
|
if (errMaterialTipList.Count > 0)
|
|
{
|
|
var itemType = typeof(TSubEntity) == typeof(WmsPurchaseD) ? "收货数" : "发货数";
|
|
var errMsgList = errMaterialTipList.Select(x => $"物料:{x.materialName},实际{itemType}:{x.purchaseArriveQty}已超出可{itemType}:{x.topPurchaseQty}").ToList();
|
|
var errMsg = string.Join("\r\n", errMsgList);
|
|
throw new AppFriendlyException(errMsg, 500);
|
|
}
|
|
|
|
purchaseDs.ForEach(x =>
|
|
{
|
|
x.modify_id = _userManager.UserId;
|
|
x.modify_time = DateTime.Now;
|
|
x.purchase_arriveqty = 0;
|
|
});
|
|
|
|
await _db.Updateable(purchaseDs).UpdateColumns(it => new { it.purchase_prqty, it.line_status }).ExecuteCommandAsync();
|
|
|
|
if (purchaseDs.All(x => x.line_status == WmsWareHouseConst.BILLSTATUS_COMPLETE_ID))
|
|
{
|
|
var purchase = await _db.Queryable<WmsPurchaseH>().FirstAsync(it => it.id == purchaseDs.First().bill_id);
|
|
|
|
if (purchase != null)
|
|
{
|
|
purchase.status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID;
|
|
purchase.modify_id = _userManager.UserId;
|
|
purchase.modify_time = DateTime.Now;
|
|
|
|
await _db.Updateable(purchase).UpdateColumns(it => it.status).ExecuteCommandAsync();
|
|
}
|
|
}
|
|
}
|
|
return purchaseDs ?? Enumerable.Empty<TSubEntity>().ToList();
|
|
}
|
|
|
|
|
|
protected async Task<List<TSubEntity>> GetDetailsByPkId(string pkId)
|
|
{
|
|
if (s_materialMap.Count == 0)
|
|
{
|
|
//物料+规格型号+批次+箱号
|
|
////s_materialMap = await _db.Queryable<BasMaterial>().ToDictionaryAsync(x => x.id, x => x.name);
|
|
}
|
|
var list = await _db.Queryable<TSubEntity>().Where(it => it.bill_id == pkId)
|
|
.Mapper(it => it.material_name = s_materialMap.ContainsKey(it.material_id) ? s_materialMap[it.material_id]?.ToString() ?? "" : "")
|
|
.ToListAsync();
|
|
return list;
|
|
|
|
}
|
|
|
|
protected async Task<bool> Audit<TEntity>(PurchaseAndSaleAuditUpInput input) where TEntity : BaseEntity<string>, IPurchaseAndSaleAuitEntity, new()
|
|
{
|
|
return await _db.Updateable<TEntity>().SetColumns(it => it.audit_status == (int)input.auditType).Where(it => input.ids.Contains(it.id)).ExecuteCommandHasChangeAsync();
|
|
}
|
|
|
|
protected Task SyncMesData(string maintableId, List<string> materialIds, EnumTriggerEvent triggerEvent)
|
|
{
|
|
CreateTaskEntity ctEntity = new();
|
|
ctEntity.maintableid = maintableId;
|
|
ctEntity.materialids = materialIds;
|
|
ctEntity.triggerevent = triggerEvent;
|
|
return _qcCheckPlanService.CreateWmsTask(ctEntity);
|
|
|
|
}
|
|
|
|
protected async Task<bool> UpdateChackStatus<TEntity>(MesCheckdCallbackUpinput input) where TEntity : BaseEntity<string>, InOutCheckStatusUpdateEntity, new()
|
|
{
|
|
var stock = await _db.Queryable<TEntity>().SingleAsync(it => it.id == input.maintableid);
|
|
stock.check_conclusion = input.check_conclusion;
|
|
stock.is_check = 1;
|
|
var isOk = await _db.Updateable(stock).UpdateColumns(it => new { it.check_conclusion, it.is_check }).ExecuteCommandHasChangeAsync();
|
|
var preTask = await _db.Queryable<WmsPretaskH>().FirstAsync(it => it.require_id == input.maintableid);
|
|
if (preTask != null)
|
|
{
|
|
var carry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == preTask.carry_id);
|
|
if (carry != null)
|
|
{
|
|
carry.check_conclusion = input.check_conclusion;
|
|
carry.is_check = 1;
|
|
isOk = await _db.Updateable(carry).UpdateColumns(it => new { it.check_conclusion, it.is_check }).ExecuteCommandHasChangeAsync();
|
|
}
|
|
}
|
|
return isOk;
|
|
}
|
|
|
|
}
|
|
}
|