using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.SeedData;
using Hotline.Settings;
using Hotline.Share.Dtos;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;

namespace Hotline.Repository.SqlSugar.System
{
    public class SystemOrganizeRepository : BaseRepository<SystemOrganize>, ISystemOrganizeRepository, IScopeDependency
    {
		public SystemOrganizeRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
        {
		}


        public async Task<IReadOnlyList<SystemOrganize>> GetOrgJson()
        {
            var list = await Db.Queryable<SystemOrganize>()
                .OrderBy(d => d.Id)
                .ToTreeAsync(x => x.Children, it => it.ParentId, null);
            return list;
        }

		public async Task<IReadOnlyList<SystemOrganize>> GetOrgAll(bool isCenter, string orgCode)
		{
            var list = await Db.Queryable<SystemOrganize>()
                .WhereIF(!isCenter, d => d.Id.StartsWith(orgCode))
                .OrderBy(d => d.Id)
                .ToListAsync();
			return list;
		}

		public async Task<IReadOnlyList<SystemOrganize>> GetOrgJsonForUser(string orgCode)
        {
            var list = await Db.Queryable<SystemOrganize>()
                .Where(it => it.Id.StartsWith(orgCode))
                .OrderBy(d => d.Id)
                .ToTreeAsync(it => it.Children, it => it.ParentId, orgCode.Length > 6 ? orgCode.Substring(0, orgCode.Length - 3) : null);
            return list;
        }

        public async Task<IReadOnlyCollection<Kv>> GetOrgEnabled()
        { 
            return await Queryable().Where(m => m.IsEnable == true).Select(m => new Kv { Key = m.Id, Value = m.Name }).ToListAsync();
        }

        public async Task<IReadOnlyList<SystemOrganize>> GetCanUseOrg()
        {
            var list = await Db.Queryable<SystemOrganize>()
                .Where(x => x.IsEnable)
                .ToTreeAsync(x => x.Children, it => it.ParentId, null);
            return list;
        }

        public async Task<IReadOnlyList<SystemOrganize>> GetCanUseOrgByOrgCode(string orgCode)
        {
            var list = await Db.Queryable<SystemOrganize>()
                 .Where(it => it.Id.StartsWith(orgCode))
                .ToTreeAsync(it => it.Children, it => it.ParentId, orgCode.Length > 6 ? orgCode.Substring(0, orgCode.Length - 3) : null);
            return list;
        }

        //public async Task<string> GetNewOrgCode(string parentId)
        //{
        //    //查是否存在下级
        //    var model = await Db.Queryable<SystemOrganize>()
        //        .OrderByDescending(x => x.OrgCode)
        //        .FirstAsync(x => x.ParentId == parentId);
        //    //如果存在下级
        //    if (model != null)
        //    {
        //        //return $"{int.Parse(model.OrgCode) +1:###}";
        //        //return (int.Parse(model.OrgCode) + 1).ToString().PadLeft(3,'0');
        //        return (int.Parse(model.OrgCode) + 1).ToString("000");
        //    }
        //    //如果不存在下级
        //    else
        //    {
        //        var parentModel = await Db.Queryable<SystemOrganize>()
        //            .FirstAsync(x => x.Id == parentId);
        //        return parentModel.OrgCode + "001";
        //    }
        //}
    }
}
