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 { /// /// 物料档案 /// [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)] [Route("api/[area]/[controller]/[action]")] public class BasMaterialService : IBasMaterialService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; private readonly DataBaseManager _dbManager; private readonly IDictionaryDataService _dictionaryDataService; public BasMaterialService( ISqlSugarRepository repository, DataBaseManager dbManager, IDictionaryDataService dictionaryDataService) { _repository = repository; _dbManager = dbManager; _dictionaryDataService = dictionaryDataService; } /// /// 复制物料 /// /// 物料id id [HttpPost] public async Task Copy(Dictionary parameters) { string id = parameters["id"]; BasMaterial basMaterial = await _repository.GetByIdAsync(id); List materialUnits = await _repository.AsSugarClient().Queryable().Where(x => x.material_id == id).ToListAsync(); List materialIntoFactorySpecifications = await _repository.AsSugarClient().Queryable().Where(x => x.material_id == id).ToListAsync(); string newId = SnowflakeIdHelper.NextId(); basMaterial.id = newId; basMaterial.code += "_复制的请修改"; basMaterial.name += "_复制的请修改"; DbResult 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(materialUnits).ExecuteCommandAsync(); _ = await _repository.AsSugarClient().Insertable(materialIntoFactorySpecifications).ExecuteCommandAsync(); }); return !result.IsSuccess ? throw Oops.Oh(ErrorCode.COM1008) : result.IsSuccess ? "复制成功" : result.ErrorMessage; } /// /// 获取本物料清单及其所有子集物料信息 /// /// /// [HttpPost] public async Task GetMaterialSelectInfo(MaterialSelectQueryInput queryInput) { ISqlSugarClient db = _repository.AsSugarClient(); if (!string.IsNullOrEmpty(queryInput.ebom_id)) { List ids = await GetAllChildrenMaterialId(queryInput.ebom_id, 0); BasEbomH ebom = await db.Queryable().Where(x => x.id == queryInput.ebom_id).SingleAsync(); if (ebom != null && !string.IsNullOrEmpty(ebom.material_id)) { ids.Add(ebom.material_id); } SqlSugarPagedList result = await db.Queryable() .LeftJoin((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null) .LeftJoin((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.SqlSugarPageResult(result); } else { SqlSugarPagedList result = await db.Queryable() .LeftJoin((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null) .LeftJoin((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.SqlSugarPageResult(result); } } [HttpPost] public async Task GetCanCreateSubWorkOrderMaterial(MaterialSelectQueryInput queryInput) { ISqlSugarClient db = _repository.AsSugarClient(); if (!string.IsNullOrEmpty(queryInput.ebom_id)) { List ids = await GetAllChildrenMaterialId(queryInput.ebom_id, 0); SqlSugarPagedList result = await db.Queryable() .LeftJoin((a, b) => b.EnCode == DictConst.MeasurementUnit && b.DeleteMark == null) .LeftJoin((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.SqlSugarPageResult(result); } else { return PageResult.SqlSugarPageResult(new SqlSugarPagedList()); } } [HttpPost] public async Task GetMaterialByType(Dictionary dic) { string types = dic["types"]; string[] typeArr = types.Split(","); List list = await _repository.AsSugarClient().Queryable().Where(x => x.state == "1").ToListAsync(); List result = new(); foreach (string type in typeArr) { result.AddRange(list.Where(x => x.category_id.Contains(type))); } return result; } [HttpPost] public async Task GetMaterialByQueryJson(MaterialQueryInput input) { ISqlSugarClient db = _repository.AsSugarClient(); Dictionary? queryJson = new(); if (input != null && !string.IsNullOrEmpty(input.queryJson)) { queryJson = JsonConvert.DeserializeObject>(input?.queryJson ?? ""); } List typeList = new(); if (!string.IsNullOrEmpty(input!.types)) { typeList = JsonConvert.DeserializeObject>(input?.types ?? "") ?? new List(); } ISugarQueryable query = db.Queryable() .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> list = new(); foreach (string type in typeList) { list.Add(query.Clone().Where(x => x.category_id.Contains(type))); } if (list.Count <= 0) { SqlSugarPagedList result = await query.ToPagedListAsync(input?.currentPage ?? 1, input?.pageSize ?? 50); return PageResult.SqlSugarPageResult(result); } else { SqlSugarPagedList result = await db.UnionAll(list).Select().ToPagedListAsync(input?.currentPage ?? 1, input?.pageSize ?? 50); return PageResult.SqlSugarPageResult(result); } } [HttpPost] public async Task GetInboundWarehouseById(Dictionary dic) { string id = dic.ContainsKey("id") ? dic["id"] : ""; return await _repository.AsSugarClient().Queryable() .LeftJoin((a, b) => a.wh_id == b.id) .Where((a, b) => a.material_id == id) .Select((a, b) => new { warehouse_id = a.wh_id, warehouse_code = b.whcode, warehouse_name = b.whname }).ToListAsync(); } /// /// 获取物料清单下所子集物料id /// /// private async Task> GetAllChildrenMaterialId(string ebomId, int index) { if (string.IsNullOrEmpty(ebomId)) { return new List(); } List ids = new(); if (index++ > 10) { return ids; } List list = await _repository.AsSugarClient().Queryable().Where(x => x.ebom_id == ebomId) .Select(x => x.material_id).ToListAsync(); if (list != null && list.Count > 0) { foreach (string id in list) { //获取最新创建的物料清单 BasEbomH ebom = await _repository.AsSugarClient().Queryable().Where(x => x.material_id == id).OrderByDescending(x => x.create_time).FirstAsync(); ids.AddRange(await GetAllChildrenMaterialId(ebom?.id ?? "", index)); } ids.AddRange(list); } return ids; } } }