using OpenAuth.App.BaseApp.Base;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.Interface;
using SqlSugar;
using Infrastructure.Extensions;
using Infrastructure;
using OpenAuth.App.Request;
using OpenAuth.App.Response;

namespace OpenAuth.App.ServiceApp.Category
{
    public class CategoryItemApp : SqlSugarBaseApp<SysDataItem, SugarDbContext>
    {
        private readonly ISqlSugarClient client;
        public CategoryItemApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysDataItem> repository, IAuth auth, ISqlSugarClient sqlSugarClient) : base(unitWork, repository, auth)
        {
            this.client = sqlSugarClient;
        }

        /// <summary> 
        /// 获取字典
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public List<OpenAuth.App.Request.Category> LoadList(string typeid)
        {
                var list = client.Queryable<SysDataItemDetail>().Where(c => c.ItemCode == typeid).OrderByDescending(c => c.CreateDate)
                .Select(c=>new App.Request.Category
                {
                    Id=c.ItemDetailId,
                    Name=c.ItemName,
                    DtCode=c.ItemCode,
                    DtValue=c.ItemValue,
                    SortNo=c.SortCode,
                    Description=c.Description,
                    Enable=c.EnabledMark,
                    TypeId=c.ItemCode,
                    CreateTime=c.CreateDate,
                    CreateUserId=c.CreateUserId,
                    CreateUserName=c.CreateUserName,
                    UpdateTime=c.ModifyDate,
                    UpdateUserId=c.ModifyUserId,
                    UpdateUserName=c.ModifyUserName
                })
                .ToList();
                return list;
        }


        #region
        /// <summary>
        /// 组织机构列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<SysOrg> LoadOrgList(string name)
        {
            List<SysOrg> resultList = new List<SysOrg>();

            var model = client.Queryable<SysOrg>().Where(c => c.Name == name).First();
            if (model == null)
            {
                throw new System.Exception("找不到数据");
            }

            //查询所有子级
            var list = client.Queryable<SysOrg>().OrderBy(c => c.SortNo).ToChildList(c => c.ParentId, model.Id).ToList();

            return list;
        }

        /// <summary>
        /// 查询组织机构对应的数据库和图片列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public List<DatabasePicture> LoadDataBase(QueryOrgRelDataBaseReq req)
        {
            int total = 0;
            var list = client.Queryable<DatabasePicture>()
                //数据库名称过滤
                .WhereIF(!string.IsNullOrEmpty(req.dataBaseName), c => c.database_name == req.dataBaseName)
                //组织机构名称过滤
                .WhereIF(!string.IsNullOrEmpty(req.orgid), c => c.org_id.Contains(req.orgid))
                .ToPageList(req.page, req.limit, ref total);
            return list;
        }
        #endregion
    }
}
