You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.2 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Constants;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
/// <summary>
///
/// </summary>
public class SysOrgService:ISysOrgService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ICommonService _commonService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SysOrgService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
_commonService = _serviceProvider.GetRequiredService<ICommonService>();
}
public DataResult<List<SysOrgRes>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysOrg>()
.Select<SysOrgRes>()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
///
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditOrg(SysOrgReq req)
{
if (req.Id == 0)
{
var isExist = db.Queryable<SysOrg>().Where(x => x.OrgName == req.OrgName).First();
if (isExist != null)
{
return DataResult.Failed("机构名称已存在!",MultiLanguageConst.OrgNameExist);
}
var data = req.Adapt<SysOrg>();
var entity = db.Insertable(data).RemoveDataCache($"{SqlSugarCacheConst.Org}{user.TenantId}").ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id,MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = db.Queryable<SysOrg>().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
db.Updateable(info).RemoveDataCache($"{SqlSugarCacheConst.Org}{user.TenantId}").IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!",MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 获取机构信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<SysOrgRes> GetOrgInfo(string id)
{
var data = db.Queryable<SysOrg>()
.Where(a => a.Id == long.Parse(id))
.Select<SysOrgRes>()
.First();
return DataResult<SysOrgRes>.Success(data,MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult BatchDelOrg(IdModel req)
{
var list = db.Queryable<SysOrg>().Where(x => req.Ids.Contains(x.Id)).ToList();
if (list.Count > 0)
{
var childs = db.Queryable<SysOrg>().Where(x => req.Ids.Contains(x.ParentId)).ToList();
if (childs.Count>0)
{
return DataResult.Failed("存在下级机构,不能删除!");
}
var users = db.Queryable<SysOrgUser>().Where(x => req.Ids.Contains((long)x.OrgId)).ToList();
if (users.Count > 0)
{
return DataResult.Failed("组织机构存在人员信息,不能删除!");
}
db.Deleteable(list).RemoveDataCache($"{SqlSugarCacheConst.Org}{user.TenantId}").ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
/// <summary>
/// 获取树型机构
/// </summary>
/// <returns></returns>
public DataResult<List<OrgTree>> GetOrgTree()
{
var list = db.Queryable<SysOrg>().Select(a => new OrgTree
{
Title = a.OrgName,
Value = a.Id,
ParentId = a.ParentId
}).ToList();
var orgList = BuildTree(list);
return DataResult<List<OrgTree>>.Success("获取数据成功!", orgList);
}
/// <summary>
/// 双层循环
/// </summary>
/// <param name="treeNodes"></param>
/// <returns></returns>
public static List<OrgTree> BuildTree(List<OrgTree> treeNodes)
{
try
{
List<OrgTree> trees = new List<OrgTree>();
foreach (var treeNode in treeNodes)
{
if (0 == (treeNode.ParentId))
{
trees.Add(treeNode);
}
foreach (var it in treeNodes)
{
if (it.ParentId == treeNode.Value)
{
treeNode.Children.Add(it);
}
}
}
return trees;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取树型机构用户
/// </summary>
/// <returns></returns>
public DataResult<List<OrgTree>> GetOrgUserTree()
{
var list = db.Queryable<SysOrg>().Select(a => new OrgTree
{
Title = a.OrgName,
Value = a.Id,
ParentId = a.ParentId
}).ToList();
var orgList = BuildOrgUserTree(list,db);
return DataResult<List<OrgTree>>.Success("获取数据成功!", orgList);
}
/// <summary>
/// 双层循环
/// </summary>
/// <param name="treeNodes"></param>
/// <returns></returns>
public static List<OrgTree> BuildOrgUserTree(List<OrgTree> treeNodes,ISqlSugarClient sugarClient)
{
try
{
List<OrgTree> trees = new List<OrgTree>();
foreach (var treeNode in treeNodes)
{
//var orgUsers = sugarClient.Queryable<SysOrgUser>().Where(x=>x.OrgId == treeNode.Value).Select(x=>x.UserId).ToList();
//if (orgUsers.Count>0)
//{
// var users = sugarClient.Queryable<SysUser>().Where(x => orgUsers.Contains(x.Id)).Select<UserSelectRes>().ToList();
// if (users.Any())
// {
// }
//}
var users = sugarClient.Queryable<SysUser>().Where(x => x.DefaultOrgId == treeNode.Value).Select<UserSelectRes>().ToList();
if (users.Count>0)
{
treeNode.UserList = users;
}
if (0 == (treeNode.ParentId))
{
trees.Add(treeNode);
}
foreach (var it in treeNodes)
{
if (it.ParentId == treeNode.Value)
{
treeNode.Children.Add(it);
}
}
}
return trees;
}
catch (Exception ex)
{
throw ex;
}
}
}