using DocumentFormat.OpenXml.Office2010.Excel;
using Infrastructure;
using OpenAuth.App.Base;
using OpenAuth.App.FormScheme.FormHelpers;
using OpenAuth.Repository;
using SqlSugar;
using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.BaseApp.Request;

namespace OpenAuth.App.BaseApp
{
    public class SysModuleTypeApp : SqlSugarBaseApp<SysModuleType, SugarDbContext>
    {
        #region 构造函数
        public SysModuleTypeApp(ISugarUnitOfWork<SugarDbContext> unitWork,ISimpleClient<Repository.Domain.SysModuleType> repository): base(unitWork, repository, null)
        {

        }
        #endregion

        #region 数据查询
        /// <summary>
        /// 分页获取列表数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<Repository.Domain.SysModuleType>>>> LoadModuleTypeList(string keyword, int pageindex, int pagesize)
        {
            //定义且实例化分页数据
            RefAsync<int> totalCount = 0;
            //数据查询并返回
            var info = await this.Repository.AsQueryable()
                .WhereIF(!string.IsNullOrEmpty(keyword), t => t.Title.Contains(keyword))
                .OrderBy(t => t.Sort)
                .ToPageListAsync(pageindex, pagesize, totalCount);
            return new Response<PageInfo<List<Repository.Domain.SysModuleType>>>
            {
                Result = new PageInfo<List<Repository.Domain.SysModuleType>>
                {
                    Items = info,
                    Total = totalCount
                }
            };
        }

        /// <summary>
        /// 获取全部数据（不显示的除外）
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<SysModuleType>>> LoadModuleType()
        {
            //数据查询并返回
            var info = await this.Repository.AsQueryable()
                .Where(t => t.IsShow == true)
                .OrderBy(t => t.Sort)
                .ToListAsync();
            return new Response<List<SysModuleType>>
            {
                Result = info
            };
        }

        /// <summary>
        /// 获取实体(编辑时使用)
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public Task<Repository.Domain.SysModuleType> GetEntityById(string id)
        {
            return this.Repository.AsQueryable().FirstAsync(t => t.Id == id);
        }
        #endregion

        #region 增删改
        /// <summary>
        /// 保存（新增、修改）
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="moduletype">数据源实体</param>
        /// <returns></returns>
        public async Task<Response<bool>> SaveEntity(string id, ModuleTypeReq moduletype)
        {
            var flag = false;
            SysModuleType entity = moduletype.MapTo<SysModuleType>();
            if (!string.IsNullOrEmpty(id))
            {
                SysModuleType mt = await this.Repository.GetFirstAsync(t => t.Title == entity.Title && t.Id != id);
                if (mt != null)
                {
                    return new Response<bool>
                    {
                        Result = flag,
                        Message = flag == true ? "success" : "标题已存在"
                    };
                }
                entity.Id = id;
                flag = await this.Repository.UpdateAsync(entity);
            }
            else
            {
                SysModuleType mt = await this.Repository.GetFirstAsync(t => t.Title == entity.Title);
                if (mt != null)
                {
                    return new Response<bool>
                    {
                        Result = flag,
                        Message = flag == true ? "success" : "标题已存在"
                    };
                }
                entity.Id = Guid.NewGuid().ToString();
                flag = await this.Repository.InsertAsync(entity);
            }
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }

        /// <summary>
        /// 删除数据源
        /// </summary>
        /// <param name="id">主键</param>
        public async Task<Response<bool>> DeleteEntity(string id)
        {
            var flag = await this.Repository.DeleteByIdAsync(id);
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }
        #endregion

        #region 修改是否显示状态
        /// <summary>
        /// 修改是否显示状态
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="isshow">是否显示</param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateIsShow(string id, bool isshow)
        {
            var flag = false;
            var entity=await this.GetEntityById(id);
            if (entity != null)
            {
                entity.IsShow=isshow;
                flag=await this.Repository.UpdateAsync(entity);
            }
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }
        #endregion
    }
}
