860 lines
42 KiB
C#
860 lines
42 KiB
C#
using JNPF.Common.Core.Manager;
|
||
using JNPF.Common.Enums;
|
||
using JNPF.Common.Extension;
|
||
using JNPF.Common.Filter;
|
||
using JNPF.Common.Security;
|
||
using JNPF.DependencyInjection;
|
||
using JNPF.DynamicApiController;
|
||
using JNPF.FriendlyException;
|
||
using JNPF.Systems.Interfaces.System;
|
||
using JNPF.VisualDev;
|
||
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using SqlSugar;
|
||
using Tnb.BasicData.Entities;
|
||
using Tnb.BasicData.Entities.Dto;
|
||
using Tnb.BasicData.Interfaces;
|
||
|
||
namespace Tnb.BasicData
|
||
{
|
||
/// <summary>
|
||
/// 生产bom
|
||
/// </summary>
|
||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)]
|
||
[Route("api/[area]/[controller]/[action]")]
|
||
[OverideVisualDev(ModelId)]
|
||
public class BasMbomService : IBasMbomService, IOverideVisualDevService, IDynamicApiController, ITransient
|
||
{
|
||
public const string ModelId = "27204627275029";
|
||
private readonly ISqlSugarRepository<BasMbom> _repository;
|
||
private readonly DataBaseManager _dbManager;
|
||
private readonly IDictionaryDataService _dictionaryDataService;
|
||
private readonly IUserManager _userManager;
|
||
private readonly ISqlSugarClient _db;
|
||
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
|
||
|
||
public BasMbomService(
|
||
ISqlSugarRepository<BasMbom> repository,
|
||
DataBaseManager dbManager,
|
||
IUserManager userManager,
|
||
IDictionaryDataService dictionaryDataService)
|
||
{
|
||
_repository = repository;
|
||
_dbManager = dbManager;
|
||
_userManager = userManager;
|
||
_dictionaryDataService = dictionaryDataService;
|
||
_db = repository.AsSugarClient();
|
||
// OverideFuncs.GetAsync = GetInfo;
|
||
OverideFuncs.GetListAsync = GetList;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生产bom列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<dynamic> GetList(VisualDevModelListQueryInput input)
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
Dictionary<string, object>? queryJson = string.IsNullOrEmpty(input.queryJson) ? null : input.queryJson.ToObject<Dictionary<string, object>>();
|
||
string materialInfo = queryJson?["query_info"]?.ToString() ?? "";
|
||
SqlSugarPagedList<MbomListOutput> list = await db.Queryable<BasMbom, BasMaterial, BasEbomH, BasRouteH>((a, b, c, d) => new object[]
|
||
{
|
||
JoinType.Left, a.material_id == b.id,
|
||
JoinType.Left, a.ebom_id == c.id,
|
||
JoinType.Left, a.route_id == d.id,
|
||
})
|
||
.WhereIF(!string.IsNullOrEmpty(materialInfo), (a, b, c, d) => b.code.Contains(materialInfo) || b.name.Contains(materialInfo))
|
||
.Select((a, b, c, d) => new MbomListOutput
|
||
{
|
||
id = a.id,
|
||
material_id = b.code + "-" + b.name,
|
||
material_id_id = b.id,
|
||
version = a.version,
|
||
ebom_id = c.version,
|
||
route_id = d.name,
|
||
route_id_id = c.id,
|
||
start_time = a.start_time == null ? "" : a.start_time.Value.ToString("yyyy-MM-dd"),
|
||
end_time = a.end_time == null ? "" : a.end_time.Value.ToString("yyyy-MM-dd"),
|
||
is_first = SqlFunc.IIF(a.is_first == 0, "否", "是"),
|
||
}).ToPagedListAsync(input.currentPage, input.pageSize);
|
||
|
||
return PageResult<MbomListOutput>.SqlSugarPageResult(list);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取编辑信息
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task<dynamic> GetInfo(string id)
|
||
{
|
||
ISqlSugarClient db = _repository.AsSugarClient();
|
||
BasMbom mbom = await _repository.GetSingleAsync(x => x.id == id);
|
||
List<BasMbomProcess> processes = await db.Queryable<BasMbomProcess>().Where(x => x.mbom_id == id).OrderBy(x => x.ordinal).ToListAsync();
|
||
List<BasMbomInput> inputs = await db.Queryable<BasMbomInput>().Where(x => x.mbom_id == id).ToListAsync();
|
||
List<BasMbomOutput> outputs = await db.Queryable<BasMbomOutput>().Where(x => x.mbom_id == id).ToListAsync();
|
||
MbomDataOutput mbomDataOutput = new();
|
||
List<MbomProcessOutDto> mbomProcessOutDtos = new();
|
||
|
||
mbomDataOutput.id = mbom.id;
|
||
mbomDataOutput.ebom_id = mbom.ebom_id;
|
||
mbomDataOutput.is_first = mbom.is_first;
|
||
mbomDataOutput.material_id = mbom.material_id;
|
||
mbomDataOutput.num = mbom.num;
|
||
mbomDataOutput.remark = mbom.remark;
|
||
mbomDataOutput.route_id = mbom.route_id;
|
||
mbomDataOutput.start_time = mbom.start_time;
|
||
mbomDataOutput.end_time = mbom.end_time;
|
||
mbomDataOutput.unit_id = mbom.unit_id;
|
||
mbomDataOutput.version = mbom.version;
|
||
|
||
foreach (BasMbomProcess process in processes)
|
||
{
|
||
mbomProcessOutDtos.Add(new MbomProcessOutDto()
|
||
{
|
||
id = process.id,
|
||
mbom_id = mbom.id,
|
||
process_id = process.process_id,
|
||
preparation_time = process.preparation_time,
|
||
station = process.station,
|
||
byproduct_status = process.byproduct_status,
|
||
production_method = process.production_method,
|
||
inputs = inputs.Where(x => x.process_id == process.process_id).ToList(),
|
||
outputs = outputs.Where(x => x.process_id == process.process_id).ToList(),
|
||
no = process.no,
|
||
last_process_no = process.last_process_no,
|
||
next_process_no = process.next_process_no
|
||
|
||
});
|
||
}
|
||
mbomDataOutput.processes = mbomProcessOutDtos;
|
||
return mbomDataOutput;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 复制生产bom
|
||
/// </summary>
|
||
/// <param name="parameters">物料id id</param>
|
||
[HttpPost]
|
||
public async Task<string> Copy(Dictionary<string, string> parameters)
|
||
{
|
||
string id = parameters["id"];
|
||
var db = _repository.AsSugarClient();
|
||
BasMbom basMbom = await _repository.GetByIdAsync(id);
|
||
|
||
List<BasMbomProcess> mbomProcesses = await db.Queryable<BasMbomProcess>().Where(x => x.mbom_id == id).ToListAsync();
|
||
List<BasMbomInput> mbomInputs = await db.Queryable<BasMbomInput>().Where(x => x.mbom_id == id).ToListAsync();
|
||
List<BasMbomOutput> mbomOutputs = await db.Queryable<BasMbomOutput>().Where(x => x.mbom_id == id).ToListAsync();
|
||
|
||
string newId = SnowflakeIdHelper.NextId();
|
||
basMbom.id = newId;
|
||
basMbom.version += "_复制的请修改";
|
||
|
||
DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
|
||
{
|
||
_ = await _repository.InsertAsync(basMbom);
|
||
foreach (BasMbomProcess item in mbomProcesses)
|
||
{
|
||
item.id = SnowflakeIdHelper.NextId();
|
||
item.mbom_id = newId;
|
||
}
|
||
|
||
foreach (BasMbomInput item in mbomInputs)
|
||
{
|
||
item.id = SnowflakeIdHelper.NextId();
|
||
item.mbom_id = newId;
|
||
}
|
||
|
||
foreach (BasMbomOutput item in mbomOutputs)
|
||
{
|
||
item.id = SnowflakeIdHelper.NextId();
|
||
item.mbom_id = newId;
|
||
}
|
||
|
||
if(mbomProcesses!=null && mbomProcesses.Count>0) _ = await db.Insertable<BasMbomProcess>(mbomProcesses).ExecuteCommandAsync();
|
||
if(mbomInputs!=null && mbomInputs.Count>0) _ = await db.Insertable<BasMbomInput>(mbomInputs).ExecuteCommandAsync();
|
||
if(mbomOutputs!=null && mbomOutputs.Count>0) _ = await db.Insertable<BasMbomOutput>(mbomOutputs).ExecuteCommandAsync();
|
||
|
||
});
|
||
|
||
return !result.IsSuccess ? throw Oops.Oh(ErrorCode.COM1008) : result.IsSuccess ? "复制成功" : result.ErrorMessage;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 根据bomid获取对应的子bom列表
|
||
/// </summary>
|
||
/// <param name="bomId">bomId</param>
|
||
/// <returns></returns>
|
||
/// <exception cref="ArgumentException"></exception>
|
||
/// <remarks>
|
||
/// returns:
|
||
/// <br/>{
|
||
/// <br/> version:bom版本
|
||
/// <br/> unit_id:单位id
|
||
/// <br/> route_name:工艺路线名称
|
||
/// <br/> process_id:工序id
|
||
/// <br/> material_id:物料id
|
||
/// <br/> material_code:物料编码
|
||
/// <br/> material_name:物料名称
|
||
/// <br/> material_category_code:类别code
|
||
/// <br/> output_qty:输出参数
|
||
/// <br/>}
|
||
/// </remarks>
|
||
[HttpGet]
|
||
public async Task<dynamic> GetSubMoListByBomId([FromRoute] string bomId)
|
||
{
|
||
|
||
if (string.IsNullOrEmpty(bomId))
|
||
{
|
||
throw new ArgumentException($"parameter {nameof(bomId)} not be null or empty");
|
||
}
|
||
|
||
List<SubBomListOutput> result = await _db.Queryable<BasMbom>()
|
||
.LeftJoin<BasMbomProcess>((a, b) => a.id == b.mbom_id)
|
||
.LeftJoin<BasRouteH>((a, b, c) => a.route_id == c.id)
|
||
// .LeftJoin<BasRouteD>((a, b, c, d) => b.process_id == d.process_id && c.id == d.route_id)
|
||
.LeftJoin<BasRouteD>((a, b, c, d) => b.route_detail_id == d.id)
|
||
.LeftJoin<BasMbomOutput>((a, b, c, d, e) => a.id == e.mbom_id && e.mbom_process_id == b.id)
|
||
.Where((a, b, c, d, e) => a.id == bomId)
|
||
.Select((a, b, c, d, e) => new SubBomListOutput
|
||
{
|
||
version = a.version,
|
||
unit_id = a.unit_id,
|
||
route_name = c.name,
|
||
process_id = b.process_id,
|
||
material_id = SqlFunc.Subqueryable<BasMaterial>().Where(it => it.id == e.material_id).Select(it => it.id),
|
||
material_code = SqlFunc.Subqueryable<BasMaterial>().Where(it => it.id == e.material_id).Select(it => it.code),
|
||
material_name = SqlFunc.Subqueryable<BasMaterial>().Where(it => it.id == e.material_id).Select(it => it.name),
|
||
material_standard = SqlFunc.Subqueryable<BasMaterial>().Where(it => it.id == e.material_id).Select(it => it.material_standard),
|
||
num = e.num,
|
||
ordinal = d.ordinal,
|
||
})
|
||
.Mapper(it => it.output_qty = it.num.ParseToInt())
|
||
.ToListAsync();
|
||
|
||
return result;
|
||
}
|
||
|
||
[HttpPost]
|
||
public async Task<dynamic> GetInputMaterialByMbomProcessId(Dictionary<string, string> dic)
|
||
{
|
||
string id = dic["id"];
|
||
var result = await _repository.AsSugarClient().Queryable<BasMbomInput>()
|
||
.Where(x => x.mbom_process_id == id)
|
||
.Select(x => new
|
||
{
|
||
x.material_id,
|
||
}).ToListAsync();
|
||
return result;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据物料id获取生产bom
|
||
/// </summary>
|
||
/// <param name="materialId"></param>
|
||
/// <remarks>
|
||
/// returns:
|
||
/// <br/>{
|
||
/// <br/> bom_id:bomid
|
||
/// <br/> material_code:物料编码
|
||
/// <br/> material_name:物料名称
|
||
/// <br/> start_time:有效开始时间
|
||
/// <br/> end_time:有效结束时间
|
||
/// <br/> version:bom版本
|
||
/// <br/> route_id:工艺路线id
|
||
/// <br/> route_name:工艺路线名称
|
||
/// <br/>}
|
||
/// </remarks>
|
||
[HttpGet]
|
||
public async Task<dynamic> GetMBomListByMaterialId([FromRoute] string materialId)
|
||
{
|
||
return await _db.Queryable<BasMbom>()
|
||
.LeftJoin<BasMaterial>((a, b) => a.material_id == b.id)
|
||
.LeftJoin<BasRouteH>((a, b, c) => a.route_id == c.id)
|
||
.Where((a, b, c) => a.material_id == materialId)
|
||
.Select((a, b, c) => new
|
||
{
|
||
bom_id = a.id,
|
||
material_code = b.code,
|
||
material_name = b.name,
|
||
start_time = a.start_time.HasValue ? a.start_time.Value.ToString(DbTimeFormat.SS) : null,
|
||
end_time = a.end_time.HasValue ? a.end_time.Value.ToString(DbTimeFormat.SS) : null,
|
||
a.version,
|
||
route_id = c.id,
|
||
route_name = c.name,
|
||
})
|
||
.ToListAsync();
|
||
}
|
||
|
||
// /// <summary>
|
||
// /// 保存生产bom
|
||
// /// </summary>
|
||
// /// <param name="mbomSaveDataInput"></param>
|
||
// /// <returns></returns>
|
||
// [HttpPost]
|
||
// public async Task<string> SaveData(MbomSaveDataInput mbomSaveDataInput)
|
||
// {
|
||
// ErrorCode errorCode = ErrorCode.COM1008;
|
||
// DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
|
||
// {
|
||
// //新增
|
||
// if (string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
// {
|
||
// string mbomId = SnowflakeIdHelper.NextId();
|
||
// string orgId = _userManager.GetUserInfo().Result.organizeId;
|
||
// BasMbom mbom = new BasMbom()
|
||
// {
|
||
// id = mbomId,
|
||
// org_id = orgId,
|
||
// material_id = mbomSaveDataInput.material_id,
|
||
// num = mbomSaveDataInput.num,
|
||
// unit_id = mbomSaveDataInput.unit_id,
|
||
// version = mbomSaveDataInput.version,
|
||
// start_time = mbomSaveDataInput.start_time,
|
||
// end_time = mbomSaveDataInput.end_time,
|
||
// ebom_id = mbomSaveDataInput.ebom_id,
|
||
// route_id = mbomSaveDataInput.route_id,
|
||
// is_first = mbomSaveDataInput.is_first,
|
||
// remark = mbomSaveDataInput.remark,
|
||
// create_id = _userManager.UserId,
|
||
// create_time = DateTime.Now,
|
||
//
|
||
// };
|
||
//
|
||
// if (await _repository.IsAnyAsync(x =>
|
||
// x.material_id == mbomSaveDataInput.material_id && x.version == mbomSaveDataInput.version))
|
||
// {
|
||
// errorCode = ErrorCode.COM1004;
|
||
// throw Oops.Oh(ErrorCode.COM1004);
|
||
// }
|
||
// await _repository.InsertAsync(mbom);
|
||
// List<BasMbomProcess> processes = new List<BasMbomProcess>();
|
||
// List<BasMbomInput> inputs = new List<BasMbomInput>();
|
||
// List<BasMbomOutput> outputs = new List<BasMbomOutput>();
|
||
//
|
||
// if (mbomSaveDataInput != null && mbomSaveDataInput.processes != null)
|
||
// {
|
||
// int index = 0;
|
||
// foreach (var process in mbomSaveDataInput.processes)
|
||
// {
|
||
// string mbomProcessId = SnowflakeIdHelper.NextId();
|
||
// processes.Add(new BasMbomProcess()
|
||
// {
|
||
// id = mbomProcessId,
|
||
// org_id = orgId,
|
||
// mbom_id = mbomId,
|
||
// process_id = process?.process_id ?? "",
|
||
// preparation_time = process?.preparation_time ?? 0,
|
||
// station = process?.station ?? "",
|
||
// byproduct_status = process?.byproduct_status ?? 0,
|
||
// production_method = process?.production_method,
|
||
// route_detail_id = process?.route_detail_id ?? "",
|
||
// ordinal = ++index,
|
||
// is_last = index==mbomSaveDataInput.processes.Count ? 1 : 0,
|
||
//
|
||
// });
|
||
//
|
||
// if (process!.inputs != null)
|
||
// {
|
||
// foreach (var input in process.inputs)
|
||
// {
|
||
// string inputId = SnowflakeIdHelper.NextId();
|
||
// inputs.Add(new BasMbomInput()
|
||
// {
|
||
// id = inputId,
|
||
// mbom_id = mbomId,
|
||
// mbom_process_id = mbomProcessId,
|
||
// process_id = process?.process_id ?? "",
|
||
// material_id = input.material_id,
|
||
// num = input.num,
|
||
// org_id = orgId,
|
||
// });
|
||
// }
|
||
// }
|
||
//
|
||
// if (process.outputs != null)
|
||
// {
|
||
// foreach (var output in process.outputs)
|
||
// {
|
||
// string outputId = SnowflakeIdHelper.NextId();
|
||
// outputs.Add(new BasMbomOutput()
|
||
// {
|
||
// id = outputId,
|
||
// mbom_id = mbomId,
|
||
// mbom_process_id = mbomProcessId,
|
||
// process_id = process?.process_id ?? "",
|
||
// material_id = output.material_id,
|
||
// num = output.num,
|
||
// org_id = orgId,
|
||
// });
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
// if (processes.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomProcess>(processes).ExecuteCommandAsync();
|
||
// }
|
||
//
|
||
// if (inputs.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomInput>(inputs).ExecuteCommandAsync();
|
||
// }
|
||
//
|
||
// if (outputs.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomOutput>(outputs).ExecuteCommandAsync();
|
||
// }
|
||
// }
|
||
// else//修改
|
||
// {
|
||
// if (await _repository.IsAnyAsync(x =>
|
||
// x.material_id == mbomSaveDataInput.material_id && x.version == mbomSaveDataInput.version && x.id != mbomSaveDataInput.id))
|
||
// {
|
||
// errorCode = ErrorCode.COM1004;
|
||
// throw Oops.Oh(ErrorCode.COM1004);
|
||
// }
|
||
//
|
||
// string orgId = _userManager.GetUserInfo().Result.organizeId;
|
||
// if (mbomSaveDataInput != null)
|
||
// {
|
||
// await _repository.UpdateAsync(x => new BasMbom()
|
||
// {
|
||
// // org_id = orgId,
|
||
// material_id = mbomSaveDataInput.material_id,
|
||
// num = mbomSaveDataInput.num,
|
||
// unit_id = mbomSaveDataInput.unit_id,
|
||
// version = mbomSaveDataInput.version,
|
||
// start_time = mbomSaveDataInput.start_time,
|
||
// end_time = mbomSaveDataInput.end_time,
|
||
// ebom_id = mbomSaveDataInput.ebom_id,
|
||
// route_id = mbomSaveDataInput.route_id,
|
||
// is_first = mbomSaveDataInput.is_first,
|
||
// remark = mbomSaveDataInput.remark,
|
||
// modify_id = _userManager.UserId,
|
||
// modify_time = DateTime.Now,
|
||
//
|
||
// }, x => x.id == mbomSaveDataInput.id);
|
||
// }
|
||
// // List<BasMbomProcess> processes = new List<BasMbomProcess>();
|
||
// List<BasMbomInput> inputs = new List<BasMbomInput>();
|
||
// List<BasMbomOutput> outputs = new List<BasMbomOutput>();
|
||
//
|
||
// if (mbomSaveDataInput != null && mbomSaveDataInput.processes != null)
|
||
// {
|
||
// foreach (var process in mbomSaveDataInput.processes)
|
||
// {
|
||
// string mbomProcessId = process.id;
|
||
// // string mbomProcessId = SnowflakeIdHelper.NextId();
|
||
// // processes.Add(new BasMbomProcess()
|
||
// // {
|
||
// // id = mbomProcessId,
|
||
// // org_id = orgId ?? "",
|
||
// // mbom_id = mbomSaveDataInput?.id ?? "",
|
||
// // process_id = process?.process_id ?? "",
|
||
// // preparation_time = process?.preparation_time ?? 0,
|
||
// // station = process.station,
|
||
// // byproduct_status = process.byproduct_status,
|
||
// // production_method = process.production_method,
|
||
// // route_detail_id = process.route_detail_id,
|
||
// //
|
||
// // });
|
||
//
|
||
// decimal preparation_time = process?.preparation_time ?? 0;
|
||
// await _repository.AsSugarClient().Updateable<BasMbomProcess>()
|
||
// .SetColumns(x => x.preparation_time == preparation_time)
|
||
// .SetColumns(x => x.station == process!.station)
|
||
// .SetColumns(x => x.byproduct_status == process!.byproduct_status)
|
||
// .SetColumns(x => x.production_method == process!.production_method)
|
||
// .Where(x => x.id == process!.id).ExecuteCommandAsync();
|
||
//
|
||
// if (process!.inputs != null)
|
||
// {
|
||
// foreach (var input in process.inputs)
|
||
// {
|
||
// string inputId = SnowflakeIdHelper.NextId();
|
||
// inputs.Add(new BasMbomInput()
|
||
// {
|
||
// id = inputId,
|
||
// mbom_id = mbomSaveDataInput?.id ?? "",
|
||
// mbom_process_id = mbomProcessId,
|
||
// process_id = process?.process_id ?? "",
|
||
// material_id = input.material_id,
|
||
// num = input.num,
|
||
// org_id = orgId,
|
||
// });
|
||
// }
|
||
// }
|
||
//
|
||
// if (process.outputs != null)
|
||
// {
|
||
// foreach (var output in process.outputs)
|
||
// {
|
||
// string outputId = SnowflakeIdHelper.NextId();
|
||
// outputs.Add(new BasMbomOutput()
|
||
// {
|
||
// id = outputId,
|
||
// mbom_id = mbomSaveDataInput?.id ?? "",
|
||
// mbom_process_id = mbomProcessId,
|
||
// process_id = process?.process_id ?? "",
|
||
// material_id = output.material_id,
|
||
// num = output.num,
|
||
// org_id = orgId,
|
||
// });
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
// }
|
||
//
|
||
// if (mbomSaveDataInput != null && !string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
// {
|
||
// // await _repository.AsSugarClient().Deleteable<BasMbomProcess>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
// await _repository.AsSugarClient().Deleteable<BasMbomInput>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
// await _repository.AsSugarClient().Deleteable<BasMbomOutput>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
// }
|
||
// // if (processes.Count > 0)
|
||
// // {
|
||
// // await _repository.AsSugarClient().Insertable<BasMbomProcess>(processes).ExecuteCommandAsync();
|
||
// // }
|
||
//
|
||
// if (inputs.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomInput>(inputs).ExecuteCommandAsync();
|
||
// }
|
||
//
|
||
// if (outputs.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomOutput>(outputs).ExecuteCommandAsync();
|
||
// }
|
||
// }
|
||
//
|
||
// });
|
||
//
|
||
// if (!result.IsSuccess)
|
||
// {
|
||
// if (!string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
// {
|
||
// if (errorCode != ErrorCode.COM1004)
|
||
// {
|
||
// throw Oops.Oh(ErrorCode.COM1001);
|
||
// }
|
||
// else
|
||
// {
|
||
// throw Oops.Oh(errorCode);
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// if (errorCode != ErrorCode.COM1004)
|
||
// {
|
||
// throw Oops.Oh(ErrorCode.COM1000);
|
||
// }
|
||
// else
|
||
// {
|
||
// throw Oops.Oh(errorCode);
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
// return result.IsSuccess ? "保存成功" : result.ErrorMessage;
|
||
// }
|
||
|
||
/// <summary>
|
||
/// 保存生产bom
|
||
/// </summary>
|
||
/// <param name="mbomSaveDataInput"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<string> SaveDataNew(MbomSaveDataInput mbomSaveDataInput)
|
||
{
|
||
ErrorCode errorCode = ErrorCode.COM1008;
|
||
DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
|
||
{
|
||
//新增
|
||
if (string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
{
|
||
string mbomId = SnowflakeIdHelper.NextId();
|
||
string orgId = _userManager.GetUserInfo().Result.organizeId;
|
||
BasMbom mbom = new()
|
||
{
|
||
id = mbomId,
|
||
org_id = orgId,
|
||
material_id = mbomSaveDataInput.material_id,
|
||
num = mbomSaveDataInput.num,
|
||
full_qty = mbomSaveDataInput.full_qty,
|
||
unit_id = mbomSaveDataInput.unit_id,
|
||
version = mbomSaveDataInput.version,
|
||
start_time = mbomSaveDataInput.start_time,
|
||
end_time = mbomSaveDataInput.end_time,
|
||
ebom_id = mbomSaveDataInput.ebom_id,
|
||
route_id = mbomSaveDataInput.route_id,
|
||
is_first = mbomSaveDataInput.is_first,
|
||
remark = mbomSaveDataInput.remark,
|
||
create_id = _userManager.UserId,
|
||
create_time = DateTime.Now,
|
||
|
||
};
|
||
|
||
if (await _repository.IsAnyAsync(x =>
|
||
x.material_id == mbomSaveDataInput.material_id && x.version == mbomSaveDataInput.version))
|
||
{
|
||
errorCode = ErrorCode.COM1004;
|
||
throw Oops.Oh(ErrorCode.COM1004);
|
||
}
|
||
_ = await _repository.InsertAsync(mbom);
|
||
List<BasMbomProcess> processes = new();
|
||
List<BasMbomInput> inputs = new();
|
||
List<BasMbomOutput> outputs = new();
|
||
|
||
if (mbomSaveDataInput != null && mbomSaveDataInput.processes != null)
|
||
{
|
||
int index = 0;
|
||
foreach (MbomProcessDto? process in mbomSaveDataInput.processes)
|
||
{
|
||
string mbomProcessId = SnowflakeIdHelper.NextId();
|
||
processes.Add(new BasMbomProcess()
|
||
{
|
||
id = mbomProcessId,
|
||
org_id = orgId,
|
||
mbom_id = mbomId,
|
||
process_id = process?.process_id ?? "",
|
||
preparation_time = process?.preparation_time ?? 0,
|
||
station = process?.station ?? "",
|
||
byproduct_status = process!.byproduct_status,
|
||
production_method = process.production_method,
|
||
route_detail_id = process.route_detail_id,
|
||
ordinal = ++index,
|
||
is_last = string.IsNullOrEmpty(process.next_process_no) ? 1 : 0,
|
||
no = process!.no,
|
||
last_process_no = process.last_process_no,
|
||
next_process_no = process.next_process_no,
|
||
|
||
});
|
||
|
||
if (process.inputs != null)
|
||
{
|
||
foreach (BasMbomInput input in process.inputs)
|
||
{
|
||
string inputId = SnowflakeIdHelper.NextId();
|
||
inputs.Add(new BasMbomInput()
|
||
{
|
||
id = inputId,
|
||
mbom_id = mbomId,
|
||
mbom_process_id = mbomProcessId,
|
||
process_id = process?.process_id ?? "",
|
||
material_id = input.material_id,
|
||
num = input.num,
|
||
molecule = input.molecule,
|
||
denominator = input.denominator,
|
||
org_id = orgId,
|
||
unit_id = input.unit_id,
|
||
});
|
||
}
|
||
}
|
||
|
||
if (process.outputs != null)
|
||
{
|
||
foreach (BasMbomOutput output in process.outputs)
|
||
{
|
||
string outputId = SnowflakeIdHelper.NextId();
|
||
outputs.Add(new BasMbomOutput()
|
||
{
|
||
id = outputId,
|
||
mbom_id = mbomId,
|
||
mbom_process_id = mbomProcessId,
|
||
process_id = process?.process_id ?? "",
|
||
material_id = output.material_id,
|
||
num = output.num,
|
||
molecule = output.molecule,
|
||
denominator = output.denominator,
|
||
org_id = orgId,
|
||
unit_id = output.unit_id,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (processes.Count > 0)
|
||
{
|
||
_ = await _repository.AsSugarClient().Insertable<BasMbomProcess>(processes).ExecuteCommandAsync();
|
||
}
|
||
|
||
if (inputs.Count > 0)
|
||
{
|
||
_ = await _repository.AsSugarClient().Insertable<BasMbomInput>(inputs).ExecuteCommandAsync();
|
||
}
|
||
|
||
if (outputs.Count > 0)
|
||
{
|
||
_ = await _repository.AsSugarClient().Insertable<BasMbomOutput>(outputs).ExecuteCommandAsync();
|
||
}
|
||
}
|
||
else//修改
|
||
{
|
||
if (await _repository.IsAnyAsync(x =>
|
||
x.material_id == mbomSaveDataInput.material_id && x.version == mbomSaveDataInput.version && x.id != mbomSaveDataInput.id))
|
||
{
|
||
errorCode = ErrorCode.COM1004;
|
||
throw Oops.Oh(ErrorCode.COM1004);
|
||
}
|
||
|
||
string orgId = _userManager.GetUserInfo().Result.organizeId;
|
||
if (mbomSaveDataInput != null)
|
||
{
|
||
_ = await _repository.UpdateAsync(x => new BasMbom()
|
||
{
|
||
// org_id = orgId,
|
||
material_id = mbomSaveDataInput.material_id,
|
||
num = mbomSaveDataInput.num,
|
||
full_qty = mbomSaveDataInput.full_qty,
|
||
unit_id = mbomSaveDataInput.unit_id,
|
||
version = mbomSaveDataInput.version,
|
||
start_time = mbomSaveDataInput.start_time,
|
||
end_time = mbomSaveDataInput.end_time,
|
||
ebom_id = mbomSaveDataInput.ebom_id,
|
||
route_id = mbomSaveDataInput.route_id,
|
||
is_first = mbomSaveDataInput.is_first,
|
||
remark = mbomSaveDataInput.remark,
|
||
modify_id = _userManager.UserId,
|
||
modify_time = DateTime.Now,
|
||
|
||
}, x => x.id == mbomSaveDataInput.id);
|
||
}
|
||
// List<BasMbomProcess> processes = new List<BasMbomProcess>();
|
||
List<BasMbomInput> inputs = new();
|
||
List<BasMbomOutput> outputs = new();
|
||
|
||
if (mbomSaveDataInput != null && mbomSaveDataInput.processes != null)
|
||
{
|
||
foreach (MbomProcessDto? process in mbomSaveDataInput.processes)
|
||
{
|
||
string mbomProcessId = process.id;
|
||
// string mbomProcessId = SnowflakeIdHelper.NextId();
|
||
// processes.Add(new BasMbomProcess()
|
||
// {
|
||
// id = mbomProcessId,
|
||
// org_id = orgId ?? "",
|
||
// mbom_id = mbomSaveDataInput?.id ?? "",
|
||
// process_id = process?.process_id ?? "",
|
||
// preparation_time = process?.preparation_time ?? 0,
|
||
// station = process.station,
|
||
// byproduct_status = process.byproduct_status,
|
||
// production_method = process.production_method,
|
||
// route_detail_id = process.route_detail_id,
|
||
//
|
||
// });
|
||
|
||
decimal preparation_time = process?.preparation_time ?? 0;
|
||
_ = await _repository.AsSugarClient().Updateable<BasMbomProcess>()
|
||
.SetColumns(x => x.preparation_time == preparation_time)
|
||
.SetColumns(x => x.station == process!.station)
|
||
.SetColumns(x => x.byproduct_status == process!.byproduct_status)
|
||
.SetColumns(x => x.production_method == process!.production_method)
|
||
.Where(x => x.id == process!.id).ExecuteCommandAsync();
|
||
|
||
if (process!.inputs != null)
|
||
{
|
||
foreach (BasMbomInput input in process.inputs)
|
||
{
|
||
string inputId = SnowflakeIdHelper.NextId();
|
||
inputs.Add(new BasMbomInput()
|
||
{
|
||
id = inputId,
|
||
mbom_id = mbomSaveDataInput?.id ?? "",
|
||
mbom_process_id = mbomProcessId,
|
||
process_id = process?.process_id ?? "",
|
||
material_id = input.material_id,
|
||
num = input.num,
|
||
molecule = input.molecule,
|
||
denominator = input.denominator,
|
||
org_id = orgId,
|
||
unit_id = input.unit_id,
|
||
});
|
||
}
|
||
}
|
||
|
||
if (process.outputs != null)
|
||
{
|
||
foreach (BasMbomOutput output in process.outputs)
|
||
{
|
||
string outputId = SnowflakeIdHelper.NextId();
|
||
outputs.Add(new BasMbomOutput()
|
||
{
|
||
id = outputId,
|
||
mbom_id = mbomSaveDataInput?.id ?? "",
|
||
mbom_process_id = mbomProcessId,
|
||
process_id = process?.process_id ?? "",
|
||
material_id = output.material_id,
|
||
num = output.num,
|
||
molecule = output.molecule,
|
||
denominator = output.denominator,
|
||
org_id = orgId,
|
||
unit_id = output.unit_id
|
||
});
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
if (mbomSaveDataInput != null && !string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
{
|
||
// await _repository.AsSugarClient().Deleteable<BasMbomProcess>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
_ = await _repository.AsSugarClient().Deleteable<BasMbomInput>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
_ = await _repository.AsSugarClient().Deleteable<BasMbomOutput>().Where(x => x.mbom_id == mbomSaveDataInput.id).ExecuteCommandAsync();
|
||
}
|
||
// if (processes.Count > 0)
|
||
// {
|
||
// await _repository.AsSugarClient().Insertable<BasMbomProcess>(processes).ExecuteCommandAsync();
|
||
// }
|
||
|
||
if (inputs.Count > 0)
|
||
{
|
||
_ = await _repository.AsSugarClient().Insertable<BasMbomInput>(inputs).ExecuteCommandAsync();
|
||
}
|
||
|
||
if (outputs.Count > 0)
|
||
{
|
||
_ = await _repository.AsSugarClient().Insertable<BasMbomOutput>(outputs).ExecuteCommandAsync();
|
||
}
|
||
}
|
||
|
||
});
|
||
|
||
if (!result.IsSuccess)
|
||
{
|
||
if (!string.IsNullOrEmpty(mbomSaveDataInput.id))
|
||
{
|
||
if (errorCode != ErrorCode.COM1004)
|
||
{
|
||
throw Oops.Oh(ErrorCode.COM1001);
|
||
}
|
||
else
|
||
{
|
||
throw Oops.Oh(errorCode);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (errorCode != ErrorCode.COM1004)
|
||
{
|
||
throw Oops.Oh(ErrorCode.COM1000);
|
||
}
|
||
else
|
||
{
|
||
throw Oops.Oh(errorCode);
|
||
}
|
||
}
|
||
|
||
}
|
||
return result.IsSuccess ? "保存成功" : result.ErrorMessage;
|
||
}
|
||
}
|
||
|
||
} |