Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/WmsPDACarryBindService.cs
2023-06-20 17:34:40 +08:00

133 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 载具绑定
/// </summary>
[OverideVisualDev(ModuleConsts.MODULE_WMSCARRYBINDPDA_ID)]
public class WmsPDACarryBindService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRullService;
public WmsPDACarryBindService(
ISqlSugarRepository<WmsCarryH> repository,
IRunService runService,
IVisualDevService visualDevService,
IUserManager userManager,
IBillRullService billRullService)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
_userManager = userManager;
_billRullService = billRullService;
OverideFuncs.CreateAsync = PDACarryBind;
}
private async Task<dynamic> PDACarryBind(VisualDevModelDataCrInput input)
{
var isOk = false;
try
{
await _db.Ado.BeginTranAsync();
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSCARRYMOOUTSTK_ID, true);
await _runService.Create(templateEntity, input);
if (input == null) throw new ArgumentNullException(nameof(input));
var carryId = input.data.ContainsKey("carry_id") ? input.data["carry_id"]?.ToString() : "";
var subCarryId = input.data.ContainsKey("newcarry_id") ? input.data["newcarry_id"]?.ToString() : "";
var carry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == carryId);
var subCarry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == subCarryId);
WmsCarrybindH wmsCarrybindH = carry.Adapt<WmsCarrybindH>();
if (carry != null && subCarry != null)
{
wmsCarrybindH.id = SnowflakeIdHelper.NextId();
wmsCarrybindH.org_id = carry.org_id;
wmsCarrybindH.carry_id = carry.id;
wmsCarrybindH.membercarry_id = subCarry.id;
wmsCarrybindH.membercarry_code = subCarry.carry_code;
wmsCarrybindH.loc = input.data[nameof(WmsCarrybindH.loc)].ParseToInt(1);
wmsCarrybindH.create_id = _userManager.UserId;
wmsCarrybindH.create_time = DateTime.Now;
var row = await _db.Insertable(wmsCarrybindH).ExecuteCommandAsync();
carry.carry_status = "1";
row = await _db.Updateable(carry).ExecuteCommandAsync();
subCarry.carry_status = "1";
row = await _db.Updateable(subCarry).ExecuteCommandAsync();
var items = await _db.Queryable<WmsCarryCode>().Where(it => it.carry_id == subCarryId).ToListAsync();
for (int i = 0; i < items.Count; i++)
{
WmsCarrybindCode wmsCarrybindCode = new();
wmsCarrybindCode.id = SnowflakeIdHelper.NextId();
wmsCarrybindCode.org_id = subCarry.id;
wmsCarrybindCode.carrybind_id = wmsCarrybindH.id;
wmsCarrybindCode.material_id = items[i].material_id;
wmsCarrybindCode.material_code = items[i].material_code;
wmsCarrybindCode.barcode = items[i].barcode;
wmsCarrybindCode.code_batch = items[i].code_batch;
wmsCarrybindCode.codeqty = items[i].codeqty;
wmsCarrybindCode.membercarry_id = subCarry.id;
wmsCarrybindCode.membercarry_code = subCarry.carry_code;
wmsCarrybindCode.unit_id = items[i].unit_id;
wmsCarrybindCode.unit_code = items[i].unit_code;
wmsCarrybindCode.create_id = _userManager.UserId;
wmsCarrybindCode.create_time = DateTime.Now;
row = await _db.Insertable(wmsCarrybindCode).ExecuteCommandAsync();
}
isOk = (row > 0);
if (!isOk) throw Oops.Oh(ErrorCode.COM1001);
}
else
{
if (carry == null || subCarry == null)
{
throw new AppFriendlyException("没有可用的主载具", 500);
}
}
await _db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await _db.Ado.RollbackTranAsync();
throw;
}
return Task.FromResult(true);
}
/* public override async Task ModifyAsync(WareHouseUpInput input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
var isOk = await _db.Updateable<WmsCarryReplaceH>().SetColumns(it => new WmsCarryReplaceH { status = input.bizTypeId }).Where(it => it.id == input.requireId).ExecuteCommandHasChangeAsync();
if (!isOk) throw Oops.Oh(ErrorCode.COM1001);
}*/
}
}