256 lines
12 KiB
C#
256 lines
12 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.Entitys.System;
|
|
using JNPF.Systems.Interfaces.System;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using SqlSugar;
|
|
using Tnb.BasicData.Entities;
|
|
using Tnb.BasicData.Entities.Dto;
|
|
using Tnb.BasicData.Interfaces;
|
|
|
|
namespace Tnb.BasicData
|
|
{
|
|
/// <summary>
|
|
/// 物料档案
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
public class BasMaterialService : IBasMaterialService, IDynamicApiController, ITransient
|
|
{
|
|
private readonly ISqlSugarRepository<BasMaterial> _repository;
|
|
private readonly DataBaseManager _dbManager;
|
|
private readonly IDictionaryDataService _dictionaryDataService;
|
|
|
|
public BasMaterialService(
|
|
ISqlSugarRepository<BasMaterial> repository, DataBaseManager dbManager, IDictionaryDataService dictionaryDataService)
|
|
{
|
|
_repository = repository;
|
|
_dbManager = dbManager;
|
|
_dictionaryDataService = dictionaryDataService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 复制物料
|
|
/// </summary>
|
|
/// <param name="parameters">物料id id</param>
|
|
[HttpPost]
|
|
public async Task<string> Copy(Dictionary<string, string> parameters)
|
|
{
|
|
string id = parameters["id"];
|
|
BasMaterial basMaterial = await _repository.GetByIdAsync(id);
|
|
|
|
List<BasMaterialUnit> materialUnits = await _repository.AsSugarClient().Queryable<BasMaterialUnit>().Where(x => x.material_id == id).ToListAsync();
|
|
List<BasMaterialIntoFactorySpecifications> materialIntoFactorySpecifications = await _repository.AsSugarClient().Queryable<BasMaterialIntoFactorySpecifications>().Where(x => x.material_id == id).ToListAsync();
|
|
|
|
string newId = SnowflakeIdHelper.NextId();
|
|
basMaterial.id = newId;
|
|
basMaterial.code += "_复制的请修改";
|
|
basMaterial.name += "_复制的请修改";
|
|
|
|
DbResult<bool> result = await _repository.AsSugarClient().Ado.UseTranAsync(async () =>
|
|
{
|
|
_ = await _repository.InsertAsync(basMaterial);
|
|
foreach (BasMaterialUnit basMaterialUnit in materialUnits)
|
|
{
|
|
basMaterialUnit.id = SnowflakeIdHelper.NextId();
|
|
basMaterialUnit.material_id = newId;
|
|
}
|
|
|
|
foreach (BasMaterialIntoFactorySpecifications basMaterialIntoFactorySpecification in materialIntoFactorySpecifications)
|
|
{
|
|
basMaterialIntoFactorySpecification.id = SnowflakeIdHelper.NextId();
|
|
basMaterialIntoFactorySpecification.material_id = newId;
|
|
}
|
|
|
|
_ = await _repository.AsSugarClient().Insertable<BasMaterialUnit>(materialUnits).ExecuteCommandAsync();
|
|
_ = await _repository.AsSugarClient().Insertable<BasMaterialIntoFactorySpecifications>(materialIntoFactorySpecifications).ExecuteCommandAsync();
|
|
});
|
|
|
|
return !result.IsSuccess ? throw Oops.Oh(ErrorCode.COM1008) : result.IsSuccess ? "复制成功" : result.ErrorMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取本物料清单及其所有子集物料信息
|
|
/// </summary>
|
|
/// <param name="queryInput"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<dynamic> GetMaterialSelectInfo(MaterialSelectQueryInput queryInput)
|
|
{
|
|
ISqlSugarClient db = _repository.AsSugarClient();
|
|
if (!string.IsNullOrEmpty(queryInput.ebom_id))
|
|
{
|
|
List<string> ids = await GetAllChildrenMaterialId(queryInput.ebom_id, 0);
|
|
BasEbomH ebom = await db.Queryable<BasEbomH>().Where(x => x.id == queryInput.ebom_id).SingleAsync();
|
|
if (ebom != null && !string.IsNullOrEmpty(ebom.material_id))
|
|
{
|
|
ids.Add(ebom.material_id);
|
|
}
|
|
|
|
SqlSugarPagedList<MaterialSelectOutput> result = await db.Queryable<BasMaterial>()
|
|
.LeftJoin<DictionaryTypeEntity>((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null)
|
|
.LeftJoin<DictionaryDataEntity>((a, b, c) => c.DictionaryTypeId == b.Id && a.unit_id == c.EnCode)
|
|
.WhereIF(!string.IsNullOrEmpty(queryInput.material_info), (a, b, c) => a.code.Contains(queryInput.material_info) || a.name.Contains(queryInput.material_info))
|
|
.WhereIF(!string.IsNullOrEmpty(queryInput.ebom_id), (a, b, c) => ids.Contains(a.id))
|
|
.Select((a, b, c) => new MaterialSelectOutput()
|
|
{
|
|
id = a.id,
|
|
code = a.code,
|
|
name = a.name,
|
|
descrip = a.descrip,
|
|
unit_id = a.unit_id,
|
|
unit_name = c.FullName,
|
|
}).ToPagedListAsync(queryInput.currentPage, queryInput.pageSize);
|
|
|
|
return PageResult<MaterialSelectOutput>.SqlSugarPageResult(result);
|
|
|
|
}
|
|
else
|
|
{
|
|
SqlSugarPagedList<MaterialSelectOutput> result = await db.Queryable<BasMaterial>()
|
|
.LeftJoin<DictionaryTypeEntity>((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null)
|
|
.LeftJoin<DictionaryDataEntity>((a, b, c) => c.DictionaryTypeId == b.Id && a.unit_id == c.EnCode)
|
|
.WhereIF(!string.IsNullOrEmpty(queryInput.material_info), (a, b, c) => a.code.Contains(queryInput.material_info) || a.name.Contains(queryInput.material_info))
|
|
.Select((a, b, c) => new MaterialSelectOutput()
|
|
{
|
|
id = a.id,
|
|
code = a.code,
|
|
name = a.name,
|
|
descrip = a.descrip,
|
|
unit_id = a.unit_id,
|
|
unit_name = c.FullName,
|
|
}).ToPagedListAsync(queryInput.currentPage, queryInput.pageSize);
|
|
|
|
return PageResult<MaterialSelectOutput>.SqlSugarPageResult(result);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<dynamic> GetCanCreateSubWorkOrderMaterial(MaterialSelectQueryInput queryInput)
|
|
{
|
|
ISqlSugarClient db = _repository.AsSugarClient();
|
|
if (!string.IsNullOrEmpty(queryInput.ebom_id))
|
|
{
|
|
List<string> ids = await GetAllChildrenMaterialId(queryInput.ebom_id, 0);
|
|
SqlSugarPagedList<MaterialSelectOutput> result = await db.Queryable<BasMaterial>()
|
|
.LeftJoin<DictionaryTypeEntity>((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null)
|
|
.LeftJoin<DictionaryDataEntity>((a, b, c) => c.DictionaryTypeId == b.Id && a.unit_id == c.EnCode)
|
|
.WhereIF(!string.IsNullOrEmpty(queryInput.material_info), (a, b, c) => a.code.Contains(queryInput.material_info) || a.name.Contains(queryInput.material_info))
|
|
.WhereIF(!string.IsNullOrEmpty(queryInput.ebom_id), (a, b, c) => ids.Contains(a.id))
|
|
.Where((a, b, c) => a.is_create_sub_work_order == "1")
|
|
.Select((a, b, c) => new MaterialSelectOutput()
|
|
{
|
|
id = a.id,
|
|
code = a.code,
|
|
name = a.name,
|
|
descrip = a.descrip,
|
|
unit_id = a.unit_id,
|
|
unit_name = c.FullName,
|
|
}).ToPagedListAsync(queryInput.currentPage, queryInput.pageSize);
|
|
|
|
return PageResult<MaterialSelectOutput>.SqlSugarPageResult(result);
|
|
|
|
}
|
|
else
|
|
{
|
|
return PageResult<MaterialSelectOutput>.SqlSugarPageResult(new SqlSugarPagedList<MaterialSelectOutput>());
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<dynamic> GetMaterialByType(Dictionary<string, string> dic)
|
|
{
|
|
string types = dic["types"];
|
|
string[] typeArr = types.Split(",");
|
|
List<BasMaterial> list = await _repository.AsSugarClient().Queryable<BasMaterial>().Where(x => x.state == "1").ToListAsync();
|
|
List<BasMaterial> result = new();
|
|
foreach (string type in typeArr)
|
|
{
|
|
result.AddRange(list.Where(x => x.category_id.Contains(type)));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<dynamic> GetMaterialByQueryJson(MaterialQueryInput input)
|
|
{
|
|
ISqlSugarClient db = _repository.AsSugarClient();
|
|
Dictionary<string, string>? queryJson = new();
|
|
if (input != null && !string.IsNullOrEmpty(input.queryJson))
|
|
{
|
|
queryJson = JsonConvert.DeserializeObject<Dictionary<string, string>>(input?.queryJson ?? "");
|
|
}
|
|
|
|
List<string> typeList = new();
|
|
if (!string.IsNullOrEmpty(input!.types))
|
|
{
|
|
typeList = JsonConvert.DeserializeObject<List<string>>(input?.types ?? "") ?? new List<string>();
|
|
}
|
|
ISugarQueryable<BasMaterial> query = db.Queryable<BasMaterial>()
|
|
.Where(x => x.state == "1")
|
|
.WhereIF(queryJson != null && queryJson.ContainsKey("name"), x => x.name.Contains(queryJson!["name"]))
|
|
.WhereIF(queryJson != null && queryJson.ContainsKey("code"), x => x.code.Contains(queryJson!["code"]))
|
|
.WhereIF(queryJson != null && queryJson.ContainsKey("material_standard"),
|
|
x => x.material_standard!.Contains(queryJson!["material_standard"]))
|
|
.Select(x => x);
|
|
|
|
List<ISugarQueryable<BasMaterial>> list = new();
|
|
foreach (string type in typeList)
|
|
{
|
|
list.Add(query.Clone().Where(x => x.category_id.Contains(type)));
|
|
}
|
|
|
|
if (list.Count <= 0)
|
|
{
|
|
SqlSugarPagedList<BasMaterial> result = await query.ToPagedListAsync(input?.currentPage ?? 1, input?.pageSize ?? 50);
|
|
return PageResult<BasMaterial>.SqlSugarPageResult(result);
|
|
}
|
|
else
|
|
{
|
|
SqlSugarPagedList<BasMaterial> result = await db.UnionAll(list).Select<BasMaterial>().ToPagedListAsync(input?.currentPage ?? 1, input?.pageSize ?? 50);
|
|
return PageResult<BasMaterial>.SqlSugarPageResult(result);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取物料清单下所子集物料id
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<List<string>> GetAllChildrenMaterialId(string ebomId, int index)
|
|
{
|
|
if (string.IsNullOrEmpty(ebomId))
|
|
{
|
|
return new List<string>();
|
|
}
|
|
|
|
List<string> ids = new();
|
|
if (index++ > 10)
|
|
{
|
|
return ids;
|
|
}
|
|
|
|
List<string> list = await _repository.AsSugarClient().Queryable<BasEbomD>().Where(x => x.ebom_id == ebomId)
|
|
.Select<string>(x => x.material_id).ToListAsync();
|
|
if (list != null && list.Count > 0)
|
|
{
|
|
foreach (string id in list)
|
|
{
|
|
//获取最新创建的物料清单
|
|
BasEbomH ebom = await _repository.AsSugarClient().Queryable<BasEbomH>().Where(x => x.material_id == id).OrderByDescending(x => x.create_time).FirstAsync();
|
|
ids.AddRange(await GetAllChildrenMaterialId(ebom?.id ?? "", index));
|
|
}
|
|
ids.AddRange(list);
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
}
|
|
} |