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.Logging; using JNPF.Systems.Entitys.Dto.Department; using JNPF.Systems.Entitys.Dto.Organize; using JNPF.Systems.Entitys.Dto.SysConfig; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.Permission; using JNPF.Systems.Interfaces.System; using Mapster; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SqlSugar; using Tnb.BasicData; using Tnb.BasicData.Entities; using Tnb.EquipMgr.Entities; namespace JNPF.Systems; /// /// 业务实现:部门管理 . /// [ApiDescriptionSettings(Tag = "Permission", Name = "Organize", Order = 166)] [Route("api/permission/[controller]")] public class DepartmentService : IDepartmentService, IDynamicApiController, ITransient { /// /// 部门管理仓储. /// private readonly ISqlSugarRepository _repository; /// /// 系统配置. /// private readonly ISysConfigService _sysConfigService; /// /// 组织管理. /// private readonly IOrganizeService _organizeService; /// /// 第三方同步. /// private readonly ISynThirdInfoService _synThirdInfoService; /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public DepartmentService( ISqlSugarRepository repository, ISysConfigService sysConfigService, IOrganizeService organizeService, ISynThirdInfoService synThirdInfoService, IUserManager userManager) { _repository = repository; _sysConfigService = sysConfigService; _organizeService = organizeService; _synThirdInfoService = synThirdInfoService; _userManager = userManager; } #region GET /// /// 获取信息. /// /// 公司主键. /// 参数. /// [HttpGet("{companyId}/Department")] public async Task GetList(string companyId, [FromQuery] KeywordInput input) { List? data = new List(); // 全部部门数据 List? departmentAllList = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, b.Id == a.ManagerId)) .Where(a => a.ParentId == companyId && a.Category.Equals("department") && a.DeleteMark == null) .OrderBy(a => a.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc) .Select((a, b) => new DepartmentListOutput { id = a.Id, parentId = a.ParentId, fullName = a.FullName, enCode = a.EnCode, description = a.Description, enabledMark = a.EnabledMark, creatorTime = a.CreatorTime, manager = SqlFunc.MergeString(b.RealName, "/", b.Account), sortCode = a.SortCode }).ToListAsync(); // 当前公司部门 List? departmentList = await _repository.AsQueryable().WhereIF(!string.IsNullOrEmpty(input.keyword), d => d.FullName.Contains(input.keyword) || d.EnCode.Contains(input.keyword)) .Where(t => t.ParentId == companyId && t.Category.Equals("department") && t.DeleteMark == null) .OrderBy(a => a.SortCode) .OrderBy(a => a.CreatorTime, OrderByType.Desc) .ToListAsync(); departmentList.ForEach(item => { item.ParentId = "0"; data.AddRange(departmentAllList.TreeChildNode(item.Id, t => t.id, t => t.parentId)); }); return new { list = data.OrderBy(x => x.sortCode).ToList() }; } /// /// 获取下拉框. /// /// [HttpGet("Department/Selector/{id}")] public async Task GetSelector(string id) { // 获取组织树 var orgTree = _organizeService.GetOrgListTreeName(); List? data = await _repository.AsQueryable().Where(t => t.DeleteMark == null).OrderBy(o => o.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc).ToListAsync(); if (!"0".Equals(id)) data.RemoveAll(it => it.Id == id); List? treeList = data.Adapt>(); treeList.ForEach(item => { if (item.type != null && item.type.Equals("company")) item.icon = "icon-ym icon-ym-tree-organization3"; item.organize = orgTree.FirstOrDefault(x => x.Id.Equals(item.id))?.Description; item.organizeIds = item.organizeIdTree.Split(",").ToList(); }); return new { list = treeList.OrderBy(x => x.sortCode).ToList().ToTree("-1") }; } /// /// 获取下拉框根据权限. /// /// [HttpGet("Department/SelectorByAuth/{id}")] public async Task GetSelectorByAuth(string id) { // 获取组织树 var orgTree = _organizeService.GetOrgListTreeName(); // 获取分级管理组织 var dataScope = _userManager.DataScope.Where(x => x.Select).Select(x => x.organizeId).Distinct().ToList(); List? data = await _repository.AsQueryable().Where(t => t.DeleteMark == null) .WhereIF(!_userManager.IsAdministrator, x => dataScope.Contains(x.Id)) .OrderBy(o => o.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc).ToListAsync(); List? treeList = data.Adapt>(); treeList.ForEach(item => { if (item.type != null && item.type.Equals("company")) item.icon = "icon-ym icon-ym-tree-organization3"; item.fullName = orgTree.FirstOrDefault(x => x.Id.Equals(item.id))?.Description; item.organize = item.fullName; item.organizeIds = item.organizeIdTree.Split(",").ToList(); }); if (treeList.Count > 0 && !treeList.Any(x => x.organizeIdTree.Equals(x.id))) { var pItems = treeList.Select(x => x.organizeIds.FirstOrDefault()).Distinct().ToList(); pItems.ForEach(item => { if (treeList.Select(x => x.id).Contains(item)) { var addItem = orgTree.Find(x => x.Id.Equals(item)).Adapt(); if (addItem.type != null && addItem.type.Equals("company")) addItem.icon = "icon-ym icon-ym-tree-organization3"; addItem.fullName = orgTree.FirstOrDefault(x => x.Id.Equals(addItem.id))?.Description; addItem.organize = addItem.fullName; addItem.organizeIds = addItem.organizeIdTree.Split(",").ToList(); addItem.disabled = true; if (!treeList.Any(x => x.id.Equals(addItem.id))) treeList.Add(addItem); } }); } // 组织断层处理 treeList.Where(x => x.parentId != "-1").OrderByDescending(x => x.organizeIdTree.Length).ToList().ForEach(item => { if (!treeList.Any(x => x.id.Equals(item.parentId))) { var pItem = treeList.Find(x => x.id != item.id && item.organizeIdTree.Contains(x.organizeIdTree)); if (pItem != null) { item.parentId = pItem.id; item.fullName = item.fullName.Replace(pItem.fullName + "/", string.Empty); } else { item.parentId = "-1"; } } else { var pItem = treeList.Find(x => x.id.Equals(item.parentId)); item.fullName = item.fullName.Replace(pItem.fullName + "/", string.Empty); } }); return new { list = treeList.OrderBy(x => x.sortCode).ToList().ToTree("-1") }; } /// /// 获取信息. /// /// 主键. /// [HttpGet("Department/{id}")] public async Task GetInfo(string id) { OrganizeEntity? entity = await _repository.GetSingleAsync(d => d.Id == id); var res = entity.Adapt(); if (entity.ParentId.Equals("-1")) res.organizeIdTree = new List() { res.id }; else res.organizeIdTree = (await _repository.GetSingleAsync(p => p.Id == entity.ParentId)).OrganizeIdTree.Split(",").ToList(); //modified by ly on 20230421 var dic = await _repository.AsSugarClient().Queryable().Where(it => it.DictionaryTypeId == "24754619407381").ToDictionaryAsync(x => x.EnCode, x => x.FullName); res.categoryName = dic.ContainsKey(res.category) ? dic[res.category].ToString() : ""; return res; } #endregion #region POST /// /// 新建. /// /// 参数. /// [HttpPost("Department")] public async Task Create([FromBody] DepartmentCrInput input) { if (!_userManager.DataScope.Any(it => it.organizeId == input.parentId && it.Add) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); if (await _repository.IsAnyAsync(o => o.EnCode.Equals(input.enCode) && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2014); if (await _repository.IsAnyAsync(o => o.ParentId == input.parentId && o.FullName == input.fullName && o.Category == "department" && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2019); OrganizeEntity? entity = input.Adapt(); // entity.Category = "department"; entity.Category = input.category; entity.Id = SnowflakeIdHelper.NextId(); entity.EnabledMark = 1; entity.CreatorTime = DateTime.Now; entity.CreatorUserId = _userManager.UserId; #region 处理 上级ID列表 存储 List? idList = new List(); idList.Add(entity.Id); if (entity.ParentId != "-1") { List? ids = _repository.AsQueryable().ToParentList(it => it.ParentId, entity.ParentId).Select(x => x.Id).ToList(); idList.AddRange(ids); } idList.Reverse(); entity.OrganizeIdTree = string.Join(",", idList); #endregion OrganizeEntity? newEntity = await _repository.AsInsertable(entity).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync(); _ = newEntity ?? throw Oops.Oh(ErrorCode.D2015); #region 默认赋予分级管理权限 var adminlist = new List(); if (!_userManager.IsAdministrator) { adminlist.Add(new OrganizeAdministratorEntity() { UserId = _userManager.UserId, OrganizeId = newEntity.Id, ThisLayerAdd = 1, ThisLayerDelete = 1, ThisLayerEdit = 1, ThisLayerSelect = 1, SubLayerAdd = 0, SubLayerDelete = 0, SubLayerEdit = 0, SubLayerSelect = 0 }); } var adminUserIds = _repository.AsSugarClient().Queryable().Where(x => !x.UserId.Equals(_userManager.UserId)).Select(x => x.UserId).Distinct().ToList(); adminUserIds.ForEach(userid => { adminlist.Add(new OrganizeAdministratorEntity() { UserId = userid, OrganizeId = newEntity.Id, ThisLayerAdd = 0, ThisLayerDelete = 0, ThisLayerEdit = 0, ThisLayerSelect = 0, SubLayerAdd = 0, SubLayerDelete = 0, SubLayerEdit = 0, SubLayerSelect = 0 }); }); if (adminlist.Any()) await _repository.AsSugarClient().Insertable(adminlist).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync(); #endregion if (input.category != DictConst.RegionCategoryCompanyCode) { var jsonObj = input.propertyJson; var qrcode = jsonObj.GetValue("qrcode"); if (qrcode is not null && !string.IsNullOrEmpty(qrcode.ToString())) { if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.code == qrcode.ToString())) { throw Oops.Bah("二维码总表中已存在该二维码"); } else { BasQrcode basQrcode = new BasQrcode() { code = qrcode.ToString(), source_id = entity.Id, source_name = "BASE_ORGANIZE", create_id = _userManager.UserId, create_time = DateTime.Now, org_id = _userManager.GetUserInfo().Result.organizeId, }; await _repository.AsSugarClient().Insertable(basQrcode).ExecuteCommandAsync(); } } } #region 第三方同步 try { SysConfigOutput? sysConfig = await _sysConfigService.GetInfo(); List? orgList = new List(); orgList.Add(entity.Adapt()); if (sysConfig.dingSynIsSynOrg) await _synThirdInfoService.SynDep(2, 1, sysConfig, orgList); if (sysConfig.qyhIsSynOrg) await _synThirdInfoService.SynDep(1, 1, sysConfig, orgList); } catch (Exception) { } #endregion } /// /// 删除. /// /// 主键. /// [HttpDelete("Department/{id}")] public async Task Delete(string id) { if (!_userManager.DataScope.Any(it => it.organizeId == id && it.Delete == true) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); // 该机构下有机构,则不能删 if (await _repository.IsAnyAsync(o => o.ParentId.Equals(id) && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2005); // 该机构下有岗位,则不能删 if (await _repository.AsSugarClient().Queryable().AnyAsync(p => p.OrganizeId.Equals(id) && p.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2006); // 该机构下有用户,则不能删 if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.ObjectType == "Organize" && x.ObjectId == id)) throw Oops.Oh(ErrorCode.D2004); // 该机构下有角色,则不能删 if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.OrganizeId == id && x.ObjectType == "Role")) throw Oops.Oh(ErrorCode.D2020); OrganizeEntity? entity = await _repository.GetSingleAsync(o => o.Id == id && o.DeleteMark == null); _ = entity ?? throw Oops.Oh(ErrorCode.D2002); int isOK = await _repository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.DeleteMark, it.DeleteTime, it.DeleteUserId }).ExecuteCommandAsync(); if (!(isOK > 0)) { throw Oops.Oh(ErrorCode.D2017); } else { // 删除该组织和角色关联数据 await _repository.AsSugarClient().Deleteable().Where(x => x.OrganizeId == id && x.ObjectType == "Role").ExecuteCommandAsync(); } #region 第三方数据删除 try { SysConfigOutput? sysConfig = await _sysConfigService.GetInfo(); if (sysConfig.dingSynIsSynOrg) await _synThirdInfoService.DelSynData(2, 1, sysConfig, id); if (sysConfig.qyhIsSynOrg) await _synThirdInfoService.DelSynData(1, 1, sysConfig, id); } catch (Exception) { } #endregion } /// /// 更新. /// /// /// /// [HttpPut("Department/{id}")] public async Task Update(string id, [FromBody] DepartmentUpInput input) { OrganizeEntity? oldEntity = await _repository.GetSingleAsync(it => it.Id == id); if (oldEntity.ParentId != input.parentId && !_userManager.DataScope.Any(it => it.organizeId == oldEntity.ParentId && it.Edit) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); if (oldEntity.ParentId != input.parentId && !_userManager.DataScope.Any(it => it.organizeId == input.parentId && it.Edit) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); if (!_userManager.DataScope.Any(it => it.organizeId == id && it.Edit) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); if (input.parentId.Equals(id)) throw Oops.Oh(ErrorCode.D2001); // 父id不能为自己的子节点 List? childIdListById = await _organizeService.GetChildIdListWithSelfById(id); if (childIdListById.Contains(input.parentId)) throw Oops.Oh(ErrorCode.D2001); if (await _repository.IsAnyAsync(o => o.EnCode == input.enCode && o.Id != id && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2014); if (await _repository.IsAnyAsync(o => o.ParentId == input.parentId && o.FullName == input.fullName && o.Id != id && o.Category == "department" && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2019); OrganizeEntity? entity = input.Adapt(); entity.LastModifyTime = DateTime.Now; entity.LastModifyUserId = _userManager.UserId; #region 处理 上级ID列表 存储 if (string.IsNullOrWhiteSpace(oldEntity.OrganizeIdTree) || entity.ParentId != oldEntity.ParentId) { List? idList = new List(); idList.Add(entity.Id); if (entity.ParentId != "-1") { List? ids = _repository.AsQueryable().ToParentList(it => it.ParentId, entity.ParentId).Select(x => x.Id).ToList(); idList.AddRange(ids); } idList.Reverse(); entity.OrganizeIdTree = string.Join(",", idList); // 如果上级结构 变动 ,需要更改所有包含 该组织的id 的结构 if (entity.OrganizeIdTree != oldEntity.OrganizeIdTree) { List? oldEntityList = await _repository.AsQueryable().Where(x => x.OrganizeIdTree.Contains(oldEntity.Id) && x.Id != oldEntity.Id).ToListAsync(); oldEntityList.ForEach(item => { string? childList = item.OrganizeIdTree.Split(oldEntity.Id).LastOrDefault(); item.OrganizeIdTree = entity.OrganizeIdTree + childList; }); await _repository.AsUpdateable(oldEntityList).UpdateColumns(x => x.OrganizeIdTree).ExecuteCommandAsync(); // 批量修改 父级组织 } } #endregion int isOK = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync(); if (!(isOK > 0)) throw Oops.Oh(ErrorCode.D2018); //modified by zkd on 20230718 处理二维码 if (input.category != DictConst.RegionCategoryCompanyCode) { var jsonObj = input.propertyJson; var qrcode = jsonObj.GetValue("qrcode"); if (qrcode is not null && !string.IsNullOrEmpty(qrcode.ToString())) { if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.code == qrcode.ToString() && x.source_id != id)) { throw Oops.Bah("二维码总表中已存在该二维码"); } else { if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.source_id == id)) { await _repository.AsSugarClient().Updateable() .SetColumns(x => x.code == qrcode.ToString()).Where(x => x.source_id == id) .ExecuteCommandAsync(); } else { BasQrcode basQrcode = new BasQrcode() { code = qrcode.ToString(), source_id = id, source_name = "BASE_ORGANIZE", create_id = _userManager.UserId, create_time = DateTime.Now, org_id = _userManager.GetUserInfo().Result.organizeId, }; await _repository.AsSugarClient().Insertable(basQrcode).ExecuteCommandAsync(); } } } } //modified by ly on 20230426 处理工位信息,将工位插入到设备表 if (input.category == DictConst.RegionCategoryStationCode) { var jsonObj = input.propertyJson; var eqpVal = jsonObj.GetValue("roweqp"); if (eqpVal is not null) { var eqpList = (jsonObj.Value("roweqp")); var eqpIds = eqpList.Select(x => x.Value("id")).ToList(); isOK = await _repository.AsSugarClient().Updateable().SetColumns(it => new EqpEquipment { station_code = input.enCode }).Where(it => eqpIds.Contains(it.id)).ExecuteCommandAsync(); //工位与设备解绑操作 var eqpEntities = await _repository.AsSugarClient().Queryable().Where(it => it.station_code == input.enCode).ToListAsync(); if (eqpEntities?.Count > 0) { var unbindEqpIds = eqpEntities.Select(x => x.id).Except(eqpIds).ToList(); if (unbindEqpIds?.Count > 0) { isOK = await _repository.AsSugarClient().Updateable().SetColumns(it => new EqpEquipment { station_code = "" }).Where(it => unbindEqpIds.Contains(it.id)).ExecuteCommandAsync(); } } await _repository.AsSugarClient().Deleteable(p => p.OrganizeId == id && p.ObjectType == "Eqp").ExecuteCommandAsync(); var OrganizeRelationEntitys = new List(); foreach (var item in eqpIds) { OrganizeRelationEntity organizeRelationEntity = new OrganizeRelationEntity(); organizeRelationEntity.Id = SnowflakeIdHelper.NextId(); organizeRelationEntity.OrganizeId = id; organizeRelationEntity.ObjectType = "Eqp"; organizeRelationEntity.ObjectId = item; organizeRelationEntity.CreatorTime = DateTime.Now; organizeRelationEntity.CreatorUserId = _userManager.UserId; OrganizeRelationEntitys.Add(organizeRelationEntity); } await _repository.AsSugarClient().Insertable(OrganizeRelationEntitys).ExecuteCommandAsync(); } var processVal = jsonObj.GetValue("rowprocess"); if (processVal is not null) { //modifyby zhoukeda 2023.06.25 var dic = _repository.GetList(x => x.DeleteMark == null).ToDictionary(x => x.FullName, x => x.Id); var procList = jsonObj.Value("rowprocess"); var pids = procList.Select(x => x.Value("id")).Distinct().ToList(); var dbProcIds = await _repository.AsSugarClient().Queryable().Where(it => pids.Contains(it.process_id)).Select(it => it.process_id).ToListAsync(); var basProcessStationList = procList.Where(x => !dbProcIds.Contains(x.Value("id"))).Select(x => new BasProcessStation { id = SnowflakeIdHelper.NextId(), org_id = dic.ContainsKey(x.Value("org_id")) ? dic[x.Value("org_id")] : "", process_id = x.Value("id"), process_code = x.Value("process_code"), station_code = input.enCode, station_id = input.id, create_id = _userManager.UserId, create_time = DateTime.Now, }).ToList(); //数据库中不存在则插入 if (basProcessStationList?.Count > 0) { isOK = await _repository.AsSugarClient().Insertable(basProcessStationList).ExecuteCommandAsync(); } //工位与工序解绑操作 //>1 根据传递的工位Id获取绑定过的工序Id var processIds = await _repository.AsSugarClient().Queryable().Where(it => it.station_id == input.id).Select(it => it.process_id).ToListAsync(); var curProcIds = basProcessStationList.Select(x => x.process_id).Except(processIds).ToList(); if (curProcIds?.Count > 0) { isOK = await _repository.AsSugarClient().Updateable(it => new BasProcessStation { station_id = "", station_code = "" }).Where(it => curProcIds.Contains(it.process_id)).ExecuteCommandAsync(); } } var correlation = jsonObj.GetValue("correlation"); if (correlation is not null) { await _repository.AsSugarClient().Deleteable(p => p.OrganizeId == id && p.ObjectType == "User").ExecuteCommandAsync(); var OrganizeRelationEntitys = new List(); for (int i = 0; i < correlation.Count(); i++) { OrganizeRelationEntity organizeRelationEntity = new OrganizeRelationEntity(); organizeRelationEntity.Id = SnowflakeIdHelper.NextId(); organizeRelationEntity.OrganizeId = id; organizeRelationEntity.ObjectType = "User"; organizeRelationEntity.ObjectId = correlation[i].ToString(); organizeRelationEntity.CreatorTime = DateTime.Now; organizeRelationEntity.CreatorUserId = _userManager.UserId; OrganizeRelationEntitys.Add(organizeRelationEntity); } await _repository.AsSugarClient().Insertable(OrganizeRelationEntitys).ExecuteCommandAsync(); } if (!(isOK > 0)) throw Oops.Oh(ErrorCode.COM1001); } #region 第三方同步 try { SysConfigOutput? sysConfig = await _sysConfigService.GetInfo(); List? orgList = new List(); entity.Category = "department"; orgList.Add(entity.Adapt()); if (sysConfig.dingSynIsSynOrg) await _synThirdInfoService.SynDep(2, 1, sysConfig, orgList); if (sysConfig.qyhIsSynOrg) await _synThirdInfoService.SynDep(1, 1, sysConfig, orgList); } catch (Exception e) { Log.Error(e.Message); } #endregion } /// /// 更新状态. /// /// 主键. /// [HttpPut("Department/{id}/Actions/State")] public async Task UpdateState(string id) { if (!_userManager.DataScope.Any(it => it.organizeId == id && it.Edit == true) && !_userManager.IsAdministrator) throw Oops.Oh(ErrorCode.D1013); OrganizeEntity? entity = await _repository.GetFirstAsync(o => o.Id == id); _ = entity.EnabledMark == 1 ? 0 : 1; entity.LastModifyTime = DateTime.Now; entity.LastModifyUserId = _userManager.UserId; int isOk = await _repository.AsUpdateable(entity).UpdateColumns(o => new { o.EnabledMark, o.LastModifyTime, o.LastModifyUserId }).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.D2016); } /// /// 根据id获取组织扩展属性 /// /// /// /// [HttpPost] public async Task GetFpropertyjsonById(Dictionary dic) { string id = dic["id"]; string type = dic["type"]; var organizeEntity = await _repository.GetSingleAsync(x => x.Id == id); if (organizeEntity != null && !string.IsNullOrEmpty(organizeEntity.PropertyJson)) { Dictionary dictionary = JsonConvert.DeserializeObject>(organizeEntity.PropertyJson); if (type == "1") { if (dictionary.TryGetValue("roweqp", out var value)) { List eqpEquipments = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(value)); return new Dictionary() { ["list"] = eqpEquipments.Select(x => new { id = x.id, code = x.code, name = x.name }).ToList(), }; } } else { if (dictionary.TryGetValue("rowprocess", out var value)) { List processes = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(value)); return new Dictionary() { ["list"] = processes.Select(x => new { id = x.id, code = x.process_code, name = x.process_name }).ToList(), }; } } } return new Dictionary() { ["list"] = Enumerable.Empty() }; } /// /// 根据工位id获取产线信息 /// /// /// [HttpPost] public async Task GetWorklineByWorkstationId(Dictionary dic) { string id = dic["id"]; var db = _repository.AsSugarClient(); OrganizeEntity organizeEntity = await db.Queryable().SingleAsync(x => x.Id == id); if (organizeEntity != null && !string.IsNullOrEmpty(organizeEntity.OrganizeIdTree)) { string[] arr = organizeEntity.OrganizeIdTree.Split(","); return await db.Queryable().SingleAsync(x => arr.Contains(x.Id) && x.Category == DictConst.RegionCategoryWorklineCode); } return null; } /// /// 根据工位id获取车间信息 /// /// /// [HttpPost] public async Task GetWorkshopByWorkstationId(Dictionary dic) { string id = dic["id"]; var db = _repository.AsSugarClient(); OrganizeEntity organizeEntity = await db.Queryable().SingleAsync(x => x.Id == id); if (organizeEntity != null && !string.IsNullOrEmpty(organizeEntity.OrganizeIdTree)) { string[] arr = organizeEntity.OrganizeIdTree.Split(","); return await db.Queryable().SingleAsync(x => arr.Contains(x.Id) && x.Category == DictConst.RegionCategoryWorkshopCode); } return null; } #endregion #region PublicMethod /// /// 获取部门列表(其他服务使用). /// /// [NonAction] public async Task> GetListAsync() { return await _repository.AsQueryable().Where(t => t.Category.Equals("department") && t.EnabledMark == 1 && t.DeleteMark == null).OrderBy(o => o.SortCode).ToListAsync(); } /// /// 部门名称. /// /// /// [NonAction] public string GetDepName(string id) { OrganizeEntity? entity = _repository.GetFirst(x => x.Id == id && x.Category == "department" && x.EnabledMark == 1 && x.DeleteMark == null); return entity == null ? string.Empty : entity.FullName; } /// /// 公司名称. /// /// /// [NonAction] public string GetComName(string id) { string? name = string.Empty; OrganizeEntity? entity = _repository.GetFirst(x => x.Id == id && x.EnabledMark == 1 && x.DeleteMark == null); if (entity == null) { return name; } else { if (entity.Category == "company") { return entity.FullName; } else { OrganizeEntity? pEntity = _repository.GetFirst(x => x.Id == entity.ParentId && x.EnabledMark == 1 && x.DeleteMark == null); return GetComName(pEntity.Id); } } } /// /// 公司结构名称树. /// /// /// [NonAction] public string GetOrganizeNameTree(string id) { string? names = string.Empty; // 组织结构 List? olist = _repository.AsQueryable().ToParentList(it => it.ParentId, id).Select(x => x.FullName).ToList(); olist.Reverse(); names = string.Join("/", olist); return names; } /// /// 公司id. /// /// /// [NonAction] public string GetCompanyId(string id) { OrganizeEntity? entity = _repository.GetFirst(x => x.Id == id && x.EnabledMark == 1 && x.DeleteMark == null); if (entity == null) { return string.Empty; } else { if (entity.Category == "company") { return entity.Id; } else { OrganizeEntity? pEntity = _repository.GetFirst(x => x.Id == entity.ParentId && x.EnabledMark == 1 && x.DeleteMark == null); return GetCompanyId(pEntity.Id); } } } /// /// 获取公司下所有部门. /// /// /// public async Task> GetCompanyAllDep(string id) { return await _repository.GetListAsync(x => x.OrganizeIdTree.Contains(id) && x.Category == "department" && x.EnabledMark == 1 && x.DeleteMark == null); } #endregion }