500 lines
24 KiB
C#
500 lines
24 KiB
C#
using JNPF.Common.Core.Manager;
|
||
using JNPF.Common.Security;
|
||
using JNPF.DependencyInjection;
|
||
using JNPF.DynamicApiController;
|
||
using JNPF.FriendlyException;
|
||
using JNPF.Systems.Entitys.Permission;
|
||
using JNPF.Systems.Interfaces.System;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using SqlSugar;
|
||
using Tnb.BasicData.Entities;
|
||
using Tnb.EquipMgr.Entities;
|
||
using Tnb.ProductionMgr.Entities;
|
||
using Tnb.ProductionMgr.Entities.Dto;
|
||
using Tnb.ProductionMgr.Interfaces;
|
||
using Tnb.WarehouseMgr.Entities;
|
||
|
||
namespace Tnb.ProductionMgr
|
||
{
|
||
/// <summary>
|
||
/// 业务实现:物料投料
|
||
/// </summary>
|
||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
|
||
[Route("api/[area]/[controller]/[action]")]
|
||
public class PrdFeedingService : IPrdFeedingService, IDynamicApiController, ITransient
|
||
{
|
||
private readonly ISqlSugarRepository<PrdFeedingH> _repository;
|
||
private readonly IUserManager _userManager;
|
||
private readonly IPrdMoTaskService _prdMoTaskService;
|
||
private readonly IBillRullService _billRullService;
|
||
// private readonly WmsSignForDeliveryService _wmsSignForDeliveryService;
|
||
|
||
|
||
|
||
public PrdFeedingService(
|
||
ISqlSugarRepository<PrdFeedingH> repository,
|
||
IBillRullService billRullService,
|
||
IPrdMoTaskService prdMoTaskService,
|
||
// WmsSignForDeliveryService wmsSignForDeliveryService,
|
||
IUserManager userManager
|
||
)
|
||
{
|
||
_repository = repository;
|
||
_userManager = userManager;
|
||
_prdMoTaskService = prdMoTaskService;
|
||
// _wmsSignForDeliveryService = _wmsSignForDeliveryService;
|
||
_billRullService = billRullService;
|
||
}
|
||
|
||
|
||
[HttpPost]
|
||
public async Task<dynamic> SaveData(MaterialReceiptInput input)
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
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 && x.mbom_process_id == input.mbom_process_id)
|
||
.Select(x => x.material_id)
|
||
.ToListAsync();
|
||
|
||
string code = await _billRullService.GetBillNumber(Tnb.BasicData.CodeTemplateConst.FEEDING_CODE);
|
||
PrdFeedingH prdFeedingH = new()
|
||
{
|
||
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<PrdFeedingD> list = new();
|
||
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投入物料,不能签收");
|
||
}
|
||
|
||
PrdMaterialReceiptD? detail = await db.Queryable<PrdMaterialReceiptD>()
|
||
.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"]?.ToString(),
|
||
num = num,
|
||
batch = item["batch"]?.ToString(),
|
||
unit_id = item["unit_id"]?.ToString(),
|
||
carry_id = input.carry_id,
|
||
status = "0",
|
||
use_num = 0,
|
||
});
|
||
|
||
if (detail != null)
|
||
{
|
||
if (detail.feeding_num + num > detail.num)
|
||
{
|
||
throw new Exception("投料数量不能大于签收数量");
|
||
}
|
||
else
|
||
{
|
||
_ = detail.feeding_num + num == detail.num
|
||
? await db.Updateable<PrdMaterialReceiptD>()
|
||
.SetColumns(x => x.feeding_num == x.feeding_num + num)
|
||
.SetColumns(x => x.is_all_feeding == 1)
|
||
.Where(x => x.id == detail.id)
|
||
.ExecuteCommandAsync()
|
||
: await db.Updateable<PrdMaterialReceiptD>()
|
||
.SetColumns(x => x.feeding_num == x.feeding_num + num)
|
||
.Where(x => x.id == detail.id)
|
||
.ExecuteCommandAsync();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("没有签收单,无法投料");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("没有签收物料");
|
||
}
|
||
|
||
PrdMo mo = await db.Queryable<PrdMo>().SingleAsync(x => x.id == moTask.mo_id);
|
||
BasMaterial? material = await db.Queryable<BasMaterial>().SingleAsync(x => x.id == moTask.material_id);
|
||
OrganizeEntity station = await db.Queryable<OrganizeEntity>().SingleAsync(x => x.Id == input.station_id);
|
||
BasMaterial process = await db.Queryable<BasMaterial>().SingleAsync(x => x.id == input.process_id);
|
||
PrdTaskLog taskLog = new()
|
||
{
|
||
id = SnowflakeIdHelper.NextId(),
|
||
mo_code = mo?.mo_code!,
|
||
eqp_code = (await db.Queryable<EqpEquipment>().FirstAsync(it => it.id == input.equip_id))?.code!,
|
||
mold_code = "",
|
||
item_code = material?.code!,
|
||
item_standard = material?.material_standard!,
|
||
status = "生产投料",
|
||
operator_name = _userManager.RealName,
|
||
create_id = _userManager.UserId,
|
||
create_time = DateTime.Now,
|
||
mo_task_id = moTask.id,
|
||
mo_task_code = moTask.mo_task_code,
|
||
station_code = station?.EnCode,
|
||
process_code = process.code
|
||
};
|
||
|
||
_ = await db.Insertable<PrdFeedingH>(prdFeedingH).ExecuteCommandAsync();
|
||
_ = await db.Insertable<PrdFeedingD>(list).ExecuteCommandAsync();
|
||
_ = await db.Insertable<PrdTaskLog>(taskLog).ExecuteCommandAsync();
|
||
|
||
});
|
||
|
||
if (result.IsSuccess)
|
||
{
|
||
//签收后调用载具签收接口
|
||
// await _wmsSignForDeliveryService.MESCarrySign(new MESCarrySignInput()
|
||
// {
|
||
// org_id = _userManager.GetUserInfo().Result.organizeId,
|
||
// create_id = _userManager.UserId,
|
||
// carry_code = input.carry_code ?? "",
|
||
// });
|
||
}
|
||
|
||
return !result.IsSuccess ? throw Oops.Oh(result.ErrorMessage) : (dynamic)(result.IsSuccess ? "投料成功" : result.ErrorMessage);
|
||
}
|
||
|
||
[HttpPost]
|
||
public async Task<dynamic> SaveDataNew(MaterialReceiptNewInput input)
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
PrdFeedingH? prdFeedingH = null;
|
||
List<PrdFeedingD> list = new();
|
||
DbResult<bool> result2 = new();
|
||
DbResult<bool> result = await db.Ado.UseTranAsync(async () =>
|
||
{
|
||
PrdMoTask moTask = await db.Queryable<PrdMoTask>().FirstAsync(x => x.id == input.mo_task_id);
|
||
PrdMoTask parentMoTask = await db.Queryable<PrdMoTask>().FirstAsync(x => x.id == moTask.parent_id);
|
||
string worklineId = moTask.workline_id;
|
||
if (parentMoTask != null && !string.IsNullOrEmpty(parentMoTask.workline_id))
|
||
{
|
||
worklineId = parentMoTask.workline_id;
|
||
}
|
||
WmsCarryH carry = await db.Queryable<WmsCarryH>().SingleAsync(x => x.carry_code == input.carry_code);
|
||
OrganizeEntity workline = await db.Queryable<OrganizeEntity>().SingleAsync(x => x.Id == worklineId);
|
||
OrganizeEntity workshop = await db.Queryable<OrganizeEntity>().SingleAsync(x => x.Id == workline.ParentId);
|
||
List<string> inputMaterials = await db.Queryable<BasMbomInput>()
|
||
.Where(x => x.mbom_id == moTask.bom_id && x.mbom_process_id == moTask.mbom_process_id)
|
||
.Select(x => x.material_id)
|
||
.ToListAsync();
|
||
|
||
|
||
string code = await _billRullService.GetBillNumber(Tnb.BasicData.CodeTemplateConst.FEEDING_CODE);
|
||
prdFeedingH = new PrdFeedingH()
|
||
{
|
||
code = code,
|
||
station_id = input.station_id,
|
||
mo_task_id = input.mo_task_id,
|
||
process_id = moTask.process_id,
|
||
equip_id = input.equip_id,
|
||
workshop_id = workshop?.Id,
|
||
carry_id = carry.id,
|
||
workline_id = moTask.workline_id,
|
||
carry_code = input.carry_code,
|
||
// remark = input.remark,
|
||
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 (Dictionary<string, string> item in input.details)
|
||
{
|
||
if (!inputMaterials.Contains(item["material_id"]))
|
||
{
|
||
throw new Exception("该物料不是生产bom投入物料,不能签收");
|
||
}
|
||
|
||
PrdMaterialReceiptD? detail = await db.Queryable<PrdMaterialReceiptD>()
|
||
.Where(x => x.member_carry_code == input.carry_code && 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 = carry.id,
|
||
status = "0",
|
||
use_num = 0,
|
||
});
|
||
|
||
if (detail != null)
|
||
{
|
||
if (detail.feeding_num + num > detail.num)
|
||
{
|
||
throw new Exception("投料数量不能大于签收数量");
|
||
}
|
||
else
|
||
{
|
||
_ = detail.feeding_num + num == detail.num
|
||
? await db.Updateable<PrdMaterialReceiptD>()
|
||
.SetColumns(x => x.feeding_num == x.feeding_num + num)
|
||
.SetColumns(x => x.is_all_feeding == 1)
|
||
.Where(x => x.id == detail.id)
|
||
.ExecuteCommandAsync()
|
||
: await db.Updateable<PrdMaterialReceiptD>()
|
||
.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>(prdFeedingH).ExecuteCommandAsync();
|
||
_ = await db.Insertable<PrdFeedingD>(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 ?? "",
|
||
// });
|
||
|
||
// var mesCarrySignInput = new MESCarrySignInput()
|
||
// {
|
||
// 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 Dictionary<string, object>()
|
||
// {
|
||
// ["Authorization"] = App.HttpContext.Request.Headers["Authorization"]
|
||
// };
|
||
// var sendResult = HttpUtils.RequestPost(domain + WebApiConst.MES_CARRY_SIGN,JsonConvert.SerializeObject(mesCarrySignInput),header);
|
||
// Log.Information(sendResult);
|
||
// AuthResponse authResponse = JsonConvert.DeserializeObject<AuthResponse>(sendResult);
|
||
// if (authResponse.code != 200)
|
||
// {
|
||
// throw Oops.Bah(authResponse.msg);
|
||
// }
|
||
// else
|
||
// {
|
||
// result2 = await db.Ado.UseTranAsync(async () =>
|
||
// {
|
||
// await db.Insertable<PrdFeedingH>(prdFeedingH).ExecuteCommandAsync();
|
||
// await db.Insertable<PrdFeedingD>(list).ExecuteCommandAsync();
|
||
// });
|
||
//
|
||
// }
|
||
}
|
||
|
||
return !result.IsSuccess ? throw Oops.Bah(result.ErrorMessage) : (dynamic)(result.IsSuccess ? "签收成功" : result.ErrorMessage);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 生产投料
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
/// <exception cref="AppFriendlyException"></exception>
|
||
[HttpPost]
|
||
public async Task<dynamic> Feeding(MaterialReceiptNewInput input)
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
PrdMoTask moTask = await _prdMoTaskService.GetPrdMoTaskInfoByStationId(new Dictionary<string, string>()
|
||
{
|
||
{"station_id",input.station_id}
|
||
});
|
||
|
||
if (moTask == null)
|
||
{
|
||
throw Oops.Bah("找不到任务单");
|
||
}
|
||
|
||
PrdFeedingH? prdFeedingH = null;
|
||
List<PrdFeedingD> list = new();
|
||
DbResult<bool> result2 = new();
|
||
DbResult<bool> result = await db.Ado.UseTranAsync(async () =>
|
||
{
|
||
PrdMoTask parentMoTask = await db.Queryable<PrdMoTask>().FirstAsync(x => x.id == moTask.parent_id);
|
||
string worklineId = moTask.workline_id;
|
||
if (parentMoTask != null && !string.IsNullOrEmpty(parentMoTask.workline_id))
|
||
{
|
||
worklineId = parentMoTask.workline_id;
|
||
}
|
||
WmsCarryH carry = await db.Queryable<WmsCarryH>().SingleAsync(x => x.carry_code == input.carry_code);
|
||
OrganizeEntity workline = await db.Queryable<OrganizeEntity>().SingleAsync(x => x.Id == worklineId);
|
||
OrganizeEntity workshop = await db.Queryable<OrganizeEntity>().SingleAsync(x => x.Id == workline.ParentId);
|
||
List<string> inputMaterials = await db.Queryable<BasMbomInput>()
|
||
.Where(x => x.mbom_id == moTask.bom_id && x.mbom_process_id == moTask.mbom_process_id)
|
||
.Select(x => x.material_id)
|
||
.ToListAsync();
|
||
|
||
|
||
string code = await _billRullService.GetBillNumber(Tnb.BasicData.CodeTemplateConst.FEEDING_CODE);
|
||
prdFeedingH = new PrdFeedingH()
|
||
{
|
||
code = code,
|
||
station_id = input.station_id,
|
||
mo_task_id = moTask.id,
|
||
process_id = moTask.process_id,
|
||
equip_id = input.equip_id,
|
||
workshop_id = workshop?.Id,
|
||
carry_id = carry.id,
|
||
workline_id = moTask.workline_id,
|
||
carry_code = input.carry_code,
|
||
// remark = input.remark,
|
||
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 (Dictionary<string, string> item in input.details)
|
||
{
|
||
if (!inputMaterials.Contains(item["material_id"]))
|
||
{
|
||
throw new Exception("该物料不是生产bom投入物料,不能签收");
|
||
}
|
||
|
||
PrdMaterialReceiptD? detail = await db.Queryable<PrdMaterialReceiptD>()
|
||
.Where(x => x.member_carry_code == input.carry_code && 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 = carry.id,
|
||
status = "0",
|
||
use_num = 0,
|
||
});
|
||
|
||
if (detail != null)
|
||
{
|
||
if (detail.feeding_num + num > detail.num)
|
||
{
|
||
throw new Exception("投料数量不能大于签收数量");
|
||
}
|
||
else
|
||
{
|
||
_ = detail.feeding_num + num == detail.num
|
||
? await db.Updateable<PrdMaterialReceiptD>()
|
||
.SetColumns(x => x.feeding_num == x.feeding_num + num)
|
||
.SetColumns(x => x.is_all_feeding == 1)
|
||
.Where(x => x.id == detail.id)
|
||
.ExecuteCommandAsync()
|
||
: await db.Updateable<PrdMaterialReceiptD>()
|
||
.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>(prdFeedingH).ExecuteCommandAsync();
|
||
_ = await db.Insertable<PrdFeedingD>(list).ExecuteCommandAsync();
|
||
|
||
});
|
||
|
||
|
||
return !result.IsSuccess ? throw Oops.Bah(result.ErrorMessage) : (dynamic)(result.IsSuccess ? "签收成功" : result.ErrorMessage);
|
||
}
|
||
|
||
[HttpPost]
|
||
public async Task<dynamic> GetFeedingRecordTree()
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
List<FeedingRecordTreeOutput> result = await db.Queryable<PrdMoTask>()
|
||
.Where(a => a.schedule_type == 2 && a.parent_id != null)
|
||
.Select(a => new FeedingRecordTreeOutput()
|
||
{
|
||
mo_task_code = a.mo_task_code,
|
||
children = SqlFunc.Subqueryable<PrdFeedingH>().Where(b => a.id == b.mo_task_id).ToList(b => new FeedingRecordChildren()
|
||
{
|
||
id = b.id,
|
||
code = b.code,
|
||
// children = SqlFunc.Subqueryable<PrdFeedingD>().LeftJoin<BasMaterial>((c,d)=>c.material_id==d.id)
|
||
// .Where((c,d)=>SqlFunc.IIF(b==null,"0",b.id)==c.feeding_id).ToList((c,d)=>new FeedingRecordMaterialChildren()
|
||
// {
|
||
// // material_code = d.code,
|
||
// // material_name = d.name,
|
||
// })
|
||
// children = new List<FeedingRecordMaterialChildren>(),
|
||
// children = b!=null ? SqlFunc.Subqueryable<PrdFeedingD>()
|
||
// .Where((c)=>"26897270557717"=="26897270557717").ToList((c)=>new FeedingRecordMaterialChildren()
|
||
// {
|
||
// // material_code = d.code,
|
||
// // material_name = d.name,
|
||
// }) : new List<FeedingRecordMaterialChildren>()
|
||
})
|
||
}).Mapper(x =>
|
||
{
|
||
foreach (FeedingRecordChildren item in x.children)
|
||
{
|
||
item.children = db.Queryable<PrdFeedingD>()
|
||
.LeftJoin<BasMaterial>((c, d) => c.material_id == d.id)
|
||
.Where((c, d) => item.id == c.feeding_id).Select((c, d) => new FeedingRecordMaterialChildren()
|
||
{
|
||
material_code = d.code,
|
||
material_name = d.name,
|
||
}).ToList();
|
||
}
|
||
})
|
||
.ToListAsync();
|
||
return result;
|
||
}
|
||
}
|
||
} |