using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.ClearScript.Util.Web;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.ProductionMgr.Interfaces;
using Tnb.ProductionMgr.Entities.Consts;
namespace Tnb.ProductionMgr
{
///
/// 业务实现:物料签收
///
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class PrdFeedingService : IPrdFeedingService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRullService;
public PrdFeedingService(
ISqlSugarRepository repository,
IBillRullService billRullService,
IUserManager userManager
)
{
_repository = repository;
_userManager = userManager;
_billRullService = billRullService;
}
[HttpPost]
public async Task SaveData(MaterialReceiptInput input)
{
var db = _repository.AsSugarClient();
DbResult result = await db.Ado.UseTranAsync(async () =>
{
var moTask = await db.Queryable().FirstAsync(x => x.id == input.mo_task_id);
var inputMaterials = await db.Queryable()
.Where(x => x.mbom_id == moTask.bom_id && x.mbom_process_id == input.mbom_process_id)
.Select(x=>x.material_id)
.ToListAsync();
string code = await _billRullService.GetBillNumber(MoStatus.FEEDING_CODE);
PrdFeedingH prdFeedingH = new PrdFeedingH()
{
code = code,
station_id = input.station_id,
mo_task_id = input.mo_task_id,
process_id = input.process_id,
equip_id = input.equip_id,
workshop_id = input.workshop_id,
carry_id = input.carry_id,
workline_id = input.workline_id,
carry_code = input.carry_code,
remark = input.remark,
mbom_process_id = input.mbom_process_id,
create_id = _userManager.UserId,
create_time = DateTime.Now,
org_id = _userManager.GetUserInfo().Result.organizeId
};
List list = new List();
if (input.details != null && input.details.Count > 0)
{
foreach (var item in input.details)
{
if(!inputMaterials.Contains(item["material_id"]))
throw new Exception("该物料不是生产bom投入物料,不能签收");
var detail = await db.Queryable()
.Where(x => x.carry_id == input.carry_id && x.is_all_feeding == 0).FirstAsync();
decimal num = Convert.ToDecimal(item["num"]);
list.Add(new PrdFeedingD
{
feeding_id = prdFeedingH.id,
material_receipt_detail_id = detail?.id,
material_id = item["material_id"],
num = num,
batch = item["batch"],
unit_id = item["unit_id"],
carry_id = input.carry_id,
});
if (detail != null)
{
if(detail.feeding_num + num > detail.num)
{
throw new Exception("投料数量不能大于签收数量");
}else if (detail.feeding_num + num == detail.num)
{
await db.Updateable()
.SetColumns(x => x.feeding_num == x.feeding_num + num)
.SetColumns(x => x.is_all_feeding == 1)
.Where(x => x.id == detail.id)
.ExecuteCommandAsync();
}
else
{
await db.Updateable()
.SetColumns(x => x.feeding_num == x.feeding_num + num)
.Where(x => x.id == detail.id)
.ExecuteCommandAsync();
}
}
else
{
throw new Exception("没有签收单,无法投料");
}
}
}
else
{
throw new Exception("没有签收物料");
}
await db.Insertable(prdFeedingH).ExecuteCommandAsync();
await db.Insertable(list).ExecuteCommandAsync();
});
if(!result.IsSuccess) throw Oops.Oh(result.ErrorMessage);
return result.IsSuccess ? "签收成功" : result.ErrorMessage;
}
}
}