377 lines
17 KiB
C#
377 lines
17 KiB
C#
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Enums;
|
|
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.Interfaces;
|
|
using Tnb.BasicData.Entities.Dto;
|
|
|
|
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 = "25574817510421";
|
|
private readonly ISqlSugarRepository<BasMbom> _repository;
|
|
private readonly DataBaseManager _dbManager;
|
|
private readonly IDictionaryDataService _dictionaryDataService;
|
|
private readonly IUserManager _userManager;
|
|
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;
|
|
// OverideFuncs.GetAsync = GetInfo;
|
|
OverideFuncs.GetListAsync = GetList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生产bom列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public async Task<dynamic> GetList(VisualDevModelListQueryInput input)
|
|
{
|
|
var db = _repository.AsSugarClient();
|
|
Dictionary<string, object> queryJson = string.IsNullOrEmpty(input.queryJson) ? null : input.queryJson.ToObject<Dictionary<string, object>>();
|
|
string materialInfo = queryJson?["query_info"]?.ToString();
|
|
var 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.Value.ToString("yyyy-MM-dd"),
|
|
end_time = a.start_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)
|
|
{
|
|
var 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).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 MbomDataOutput();
|
|
List<MbomProcessOutDto> mbomProcessOutDtos = new List<MbomProcessOutDto>();
|
|
|
|
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()
|
|
{
|
|
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(),
|
|
|
|
});
|
|
}
|
|
mbomDataOutput.processes = mbomProcessOutDtos;
|
|
return mbomDataOutput;
|
|
}
|
|
|
|
/// <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>();
|
|
|
|
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,
|
|
station = process.station,
|
|
byproduct_status = process.byproduct_status,
|
|
production_method = process.production_method,
|
|
|
|
});
|
|
|
|
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;
|
|
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>();
|
|
|
|
foreach (var process in mbomSaveDataInput.processes)
|
|
{
|
|
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,
|
|
station = process.station,
|
|
byproduct_status = process.byproduct_status,
|
|
production_method = process.production_method,
|
|
|
|
});
|
|
|
|
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,
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |