using Infrastructure;
using OpenAuth.App.Base;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

namespace OpenAuth.App
{
    public class CategoryApp : SqlSugarBaseApp<SysCategory, SugarDbContext>
    {
        private DbExtension extension;
        public CategoryApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysCategory> repository, IAuth auth, DbExtension dbExtension) : base(unitWork, repository, auth)
        {
            extension = dbExtension;
        }
        /// <summary>
        /// 加载列表
        /// </summary>

        public void Add(AddOrUpdateCategoryReq req)
        {
            var obj = req.MapTo<SysCategory>();
            obj.CreateTime = DateTime.Now;
            var user = _auth.GetCurrentUser().User;
            obj.CreateUserId = user.Id;
            obj.CreateUserName = user.Name;
            Repository.Insert(obj);
        }

        public void Update(AddOrUpdateCategoryReq obj)
        {
            var user = _auth.GetCurrentUser().User;
            Repository.Update(u => new SysCategory
            {
                Name = obj.Name,
                Enable = obj.Enable,
                DtValue = obj.DtValue,
                DtCode = obj.DtCode,
                TypeId = obj.TypeId,
                UpdateTime = DateTime.Now,
                UpdateUserId = user.Id,
                UpdateUserName = user.Name
                //todo:要修改的字段赋值
            }, u => u.Id == obj.Id);

        }

        /// <summary>
        /// 加载一个分类类型里面的所有值，即字典的所有值
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public dynamic LoadByTypeId(string typeId, int page, int limit)
        {
            var loginContext = _auth.GetCurrentUser();

            var properties = extension.GetProperties(typeof(SysCategory));

            var p = new PageModel() { PageIndex = page, PageSize = limit };

            var exp = Expressionable.Create<SysCategory>()
                .AndIF(!string.IsNullOrEmpty(typeId), u => u.TypeId == typeId)
                .ToExpression();

            var list = Repository.GetPageList(exp, p);

            //return new TableData
            //{
            //    code = 200,
            //    count = p.TotalCount,
            //    data = list,
            //    msg = "success"
            //};
            return new
            {
                code = 200,
                columnHeaders = properties,
                count = p.TotalCount,
                data = list,
                msg = "success"
            };
        }

        public TableData QueryCategory(string type)
        {
            var table = base.Repository.AsQueryable()
                    .Where(a => a.TypeId == type)
                    .OrderBy(a => a.SortNo)
                    .Select(a => new
                    {
                        a.Name,
                        a.DtCode,
                        a.DtValue
                    }).ToList();

            return new TableData()
            {
                count = table.Count,
                data = table
            };
        }

        public void Delete(string[] ids)
        {
            Repository.DeleteByIds(ids);
        }

        public SysCategory Get(string id)
        {
            return Repository.GetById(id);
        }
    }
}