Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/PrdMaterialReceiptService.cs
2023-11-07 14:58:43 +08:00

373 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using JNPF;
using JNPF.Common.Core.Manager;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.CollectiveOAuth.Models;
using JNPF.Extras.CollectiveOAuth.Utils;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Interfaces.Permission;
using JNPF.Systems.Interfaces.System;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData;
using Tnb.BasicData.Entities;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.ProductionMgr.Interfaces;
using Tnb.WarehouseMgr;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Dto;
namespace Tnb.ProductionMgr
{
/// <summary>
/// 业务实现:物料签收
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class PrdMaterialReceiptService : IPrdMaterialReceiptService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<PrdMaterialReceiptH> _repository;
private readonly IUserManager _userManager;
private readonly IOrganizeService _organizeService;
private readonly IBillRullService _billRullService;
private readonly IPrdMoTaskService _prdMoTaskService;
public PrdMaterialReceiptService(
ISqlSugarRepository<PrdMaterialReceiptH> repository,
IBillRullService billRullService,
IOrganizeService organizeService,
IPrdMoTaskService prdMoTaskService,
IUserManager userManager
)
{
_repository = repository;
_userManager = userManager;
_organizeService = organizeService;
_billRullService = billRullService;
_prdMoTaskService = prdMoTaskService;
}
[HttpPost]
public async Task<dynamic> GetInfoByQrCode(string qrCode)
{
ISqlSugarClient db = _repository.AsSugarClient();
// var result = await db.Queryable<WmsCarryH>()
// .Where((a) => a.carry_code == qrCode)
// .Select(a => new
// {
// carry_id = a.id,
// carry_name = a.carry_name,
// children = SqlFunc.Subqueryable<WmsCarryCode>()
// .LeftJoin<BasMaterial>((b,c)=>b.material_id==c.id)
// .Where((b,c)=>a.id==b.carry_id).ToList((b,c)=>new CarryCodeDetailOutput()
// {
// unit_id = c.unit_id,
// barcode = b.barcode,
// code_batch = b.code_batch,
// codeqty = b.codeqty,
// material_id = b.material_id,
// material_code = c.code,
// material_name = c.name
// })
// }).FirstAsync();
if (await db.Queryable<PrdMaterialReceiptD>()
.AnyAsync(x => x.member_carry_code == qrCode && x.is_all_feeding == 0))
{
FeedingDetailOutput result = await db.Queryable<PrdMaterialReceiptD>()
.LeftJoin<WmsCarryH>((a, b) => a.carry_id == b.id)
.Where((a) => a.member_carry_code == qrCode && a.is_all_feeding == 0)
.Select((a, b) => new FeedingDetailOutput
{
carry_id = b.id,
carry_name = b.carry_name,
children = SqlFunc.Subqueryable<PrdMaterialReceiptD>()
.LeftJoin<BasMaterial>((c, d) => c.material_id == d.id)
.Where((c, d) => a.member_carry_code == c.member_carry_code && c.is_all_feeding == 0)
.ToList((c, d) => new CarryCodeDetailOutput()
{
unit_id = d.unit_id,
// barcode = c.barcode,
// code_batch = c.code_batch,
code_batch = c.batch,
codeqty = c.num,
material_id = c.material_id,
material_code = d.code,
material_name = d.name
})
}).FirstAsync();
return result;
}
else
{
FeedingDetailOutput result = await db.Queryable<PrdMaterialReceiptH>()
.LeftJoin<WmsCarryH>((a, b) => a.carry_id == b.id)
.Where((a) => a.carry_code == qrCode)
.Select((a, b) => new FeedingDetailOutput
{
carry_id = a.carry_id,
carry_name = b.carry_name,
children = SqlFunc.Subqueryable<PrdMaterialReceiptD>()
.LeftJoin<BasMaterial>((c, d) => c.material_id == d.id)
.Where((c, d) => a.id == c.material_receipt_id && c.is_all_feeding == 0).ToList((c, d) => new CarryCodeDetailOutput()
{
unit_id = d.unit_id,
// barcode = c.barcode,
// code_batch = c.code_batch,
code_batch = c.batch,
codeqty = c.num,
material_id = c.material_id,
material_code = d.code,
material_name = d.name
})
}).FirstAsync();
return result;
}
}
[HttpPost]
public async Task<dynamic> SaveData(MaterialReceiptInput input)
{
ISqlSugarClient db = _repository.AsSugarClient();
DbResult<bool> result2 = new();
PrdMaterialReceiptH? prdMaterialReceiptH = null;
List<PrdMaterialReceiptD> list = new();
DbResult<bool> result = await db.Ado.UseTranAsync(async () =>
{
PrdMoTask moTask = await db.Queryable<PrdMoTask>().FirstAsync(x => x.id == input.mo_task_id);
List<string> inputMaterials = await db.Queryable<BasMbomInput>()
.Where(x => x.mbom_id == moTask.bom_id)
.WhereIF(!string.IsNullOrEmpty(input.mbom_process_id), x => x.mbom_process_id == input.mbom_process_id)
.Select(x => x.material_id)
.ToListAsync();
string code = await _billRullService.GetBillNumber(Tnb.BasicData.CodeTemplateConst.MATERIAL_RECEIPT_CODE);
OrganizeEntity workline = await _organizeService.GetAnyParentByWorkstationId(input.station_id, DictConst.RegionCategoryWorklineCode);
OrganizeEntity workshop = await _organizeService.GetAnyParentByWorkstationId(input.station_id, DictConst.RegionCategoryWorkshopCode);
prdMaterialReceiptH = new PrdMaterialReceiptH()
{
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 = workshop?.Id ?? "",
carry_id = input.carry_id,
workline_id = 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
};
if (input.details != null && input.details.Count > 0)
{
foreach (Dictionary<string, object> item in input.details)
{
if (!inputMaterials.Contains(item["material_id"]))
{
throw new Exception("该物料不是生产bom投入物料不能签收");
}
list.Add(new PrdMaterialReceiptD
{
material_receipt_id = prdMaterialReceiptH.id,
material_id = item["material_id"]?.ToString(),
num = Convert.ToDecimal(item["num"]),
batch = item["batch"]?.ToString(),
unit_id = item["unit_id"]?.ToString(),
carry_id = input.carry_id,
barcode = item["barcode"]?.ToString(),
is_all_feeding = 0,
member_carry_id = item["member_carry_id"]?.ToString(),
member_carry_code = item["member_carry_code"]?.ToString(),
feeding_num = 0,
supplier_id = item.ContainsKey("supplier_id") ? item["supplier_id"]?.ToString() : "",
instock_time = item.ContainsKey("instock_time") ? (DateTime)item["instock_time"] : DateTime.Now,
check_conclusion = item.ContainsKey("check_conclusion") ? item["check_conclusion"]?.ToString() : "",
});
}
}
else
{
throw new Exception("没有签收物料");
}
// await db.Insertable<PrdMaterialReceiptH>(prdMaterialReceiptH).ExecuteCommandAsync();
// await db.Insertable<PrdMaterialReceiptD>(list).ExecuteCommandAsync();
});
if (result.IsSuccess)
{
//签收后调用载具签收接口
// await _wmsSignForDeliveryService.MESCarrySign(new MESCarrySignInput()
// {
// org_id = _userManager.GetUserInfo().Result.organizeId,
// create_id = _userManager.UserId,
// carry_code = input.carry_code ?? "",
// });
MESCarrySignInput mesCarrySignInput = new()
{
org_id = _userManager.GetUserInfo().Result.organizeId,
create_id = _userManager.UserId,
carry_code = input.carry_code ?? "",
};
string domain = (App.HttpContext.Request.IsHttps ? "https://" : "http://") + App.HttpContext.Request.Host;
Dictionary<string, object> header = new()
{
["Authorization"] = App.HttpContext.Request.Headers["Authorization"]
};
string sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CARRY_SIGN, JsonConvert.SerializeObject(mesCarrySignInput), header);
Log.Information(sendResult);
AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
result2 = authResponse.code != 200
? throw Oops.Bah(authResponse.msg)
: await db.Ado.UseTranAsync(async () =>
{
_ = await db.Insertable<PrdMaterialReceiptH>(prdMaterialReceiptH).ExecuteCommandAsync();
_ = await db.Insertable<PrdMaterialReceiptD>(list).ExecuteCommandAsync();
});
}
return !result.IsSuccess
? throw Oops.Bah(result.ErrorMessage)
: !result2.IsSuccess ? throw Oops.Bah(result2.ErrorMessage) : (dynamic)"签收成功";
}
[HttpPost]
public async Task<dynamic> SaveDataNew(NewMaterialReceiptInput input)
{
ISqlSugarClient db = _repository.AsSugarClient();
PrdMoTask moTask = await _prdMoTaskService.GetPrdMoTaskInfoByStationId(new Dictionary<string, string>(){
{"station_id",input.station_id}
});
DbResult<bool> result2 = new();
PrdMaterialReceiptH? prdMaterialReceiptH = null;
List<PrdMaterialReceiptD> list = new();
DbResult<bool> result = await db.Ado.UseTranAsync(async () =>
{
List<string> inputMaterials = await db.Queryable<BasMbomInput>()
.Where(x => x.mbom_id == moTask.bom_id)
.WhereIF(!string.IsNullOrEmpty(moTask.mbom_process_id), x => x.mbom_process_id == moTask.mbom_process_id)
.Select(x => x.material_id)
.ToListAsync();
string code = await _billRullService.GetBillNumber(Tnb.BasicData.CodeTemplateConst.MATERIAL_RECEIPT_CODE);
OrganizeEntity workline = await _organizeService.GetAnyParentByWorkstationId(input.station_id, DictConst.RegionCategoryWorklineCode);
OrganizeEntity workshop = await _organizeService.GetAnyParentByWorkstationId(input.station_id, DictConst.RegionCategoryWorkshopCode);
WmsCarryH carry = await db.Queryable<WmsCarryH>().Where(x=>x.carry_code==input.carry_code).FirstAsync();
prdMaterialReceiptH = new PrdMaterialReceiptH()
{
code = code,
station_id = input.station_id,
mo_task_id = moTask.id,
process_id = moTask.process_id,
equip_id = moTask.eqp_id,
workshop_id = workshop?.Id ?? "",
carry_id = carry.id,
workline_id = workline?.Id ?? "",
carry_code = input.carry_code,
mbom_process_id = moTask.mbom_process_id,
create_id = _userManager.UserId,
create_time = DateTime.Now,
org_id = _userManager.GetUserInfo().Result.organizeId
};
if (input.details != null && input.details.Count > 0)
{
foreach (var item in input.details)
{
if (!inputMaterials.Contains(item.material_id) && !input.sure)
{
throw new Exception("存在不属于生产bom的投入物料确认签收吗");
}
list.Add(new PrdMaterialReceiptD
{
material_receipt_id = prdMaterialReceiptH.id,
material_id = item.material_id,
num = item.codeqty,
batch = item.code_batch,
unit_id = item.unit_id,
carry_id = carry.id,
barcode = item.barcode,
is_all_feeding = 0,
member_carry_id = item.member_carryid,
member_carry_code = item.member_carrycode,
feeding_num = 0,
supplier_id = item.supplier_id,
instock_time = item.instock_time,
check_conclusion = item.check_conclusion,
});
}
}
else
{
throw new Exception("没有签收物料");
}
// await db.Insertable<PrdMaterialReceiptH>(prdMaterialReceiptH).ExecuteCommandAsync();
// await db.Insertable<PrdMaterialReceiptD>(list).ExecuteCommandAsync();
});
if (result.IsSuccess)
{
//签收后调用载具签收接口
// await _wmsSignForDeliveryService.MESCarrySign(new MESCarrySignInput()
// {
// org_id = _userManager.GetUserInfo().Result.organizeId,
// create_id = _userManager.UserId,
// carry_code = input.carry_code ?? "",
// });
MESCarrySignInput mesCarrySignInput = new()
{
org_id = _userManager.GetUserInfo().Result.organizeId,
create_id = _userManager.UserId,
carry_code = input.carry_code ?? "",
};
string domain = (App.HttpContext.Request.IsHttps ? "https://" : "http://") + App.HttpContext.Request.Host;
Dictionary<string, object> header = new()
{
["Authorization"] = App.HttpContext.Request.Headers["Authorization"]
};
string sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CARRY_SIGN, JsonConvert.SerializeObject(mesCarrySignInput), header);
Log.Information(sendResult);
AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
result2 = authResponse.code != 200
? throw Oops.Bah(authResponse.msg)
: await db.Ado.UseTranAsync(async () =>
{
_ = await db.Insertable<PrdMaterialReceiptH>(prdMaterialReceiptH).ExecuteCommandAsync();
_ = await db.Insertable<PrdMaterialReceiptD>(list).ExecuteCommandAsync();
});
}
if (result.ErrorMessage == "存在不属于生产bom的投入物料确认签收吗")
{
return "存在不属于生产bom的投入物料确认签收吗";
}
return !result.IsSuccess
? throw Oops.Bah(result.ErrorMessage)
: !result2.IsSuccess ? throw Oops.Bah(result2.ErrorMessage) : (dynamic)"签收成功";
}
}
}