using Nirvana.Common;
using Nirvana.Common.ApiBase;
using Nirvana.Data;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.Service.DBServices
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public partial class MenuApp : Repository<YB_Menu>
    {
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<YB_Menu>> GetMenuListAsync(int roleId)
        {
            var currentUser = OperatorProvider.Provider.GetCurrent();
            var data = new List<YB_Menu>();
            using (var db = ReadDbContext.GetInstance())
            {
                var tempquery = db.Queryable<YB_Menu, YB_RoleMenu>(
                    (m, ra) => new object[] {
                            JoinType.Left,m.Id==ra.MenuId
                    })
                    .Where((m, ra) => ra.RoleId == roleId && m.Status == 1)
                    ;
                data =await tempquery.ToListAsync();
            }
            return data;
        }

        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<YB_Menu>> GetListAsync()
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var modulequery = dbClient.Queryable<YB_Menu>();
                return await modulequery.OrderBy(x => x.SortCode, OrderByType.Asc).ToListAsync();
            }
        }



        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        public async Task<ResultInfo> DeleteMenuAsync(int id)
        {
            var currentUser = OperatorProvider.Provider.GetCurrent();
            using (var dbClient = ReadDbContext.GetInstance())
            {
                if (!await dbClient.Queryable<YB_Menu>().AnyAsync(x => x.Id == id))
                {
                    return new ResultInfo() { code = ResultState.FAIL, message = "此菜单未找到!" };
                }
                await dbClient.Deleteable<YB_Menu>().Where(x => x.Id == id).ExecuteCommandAsync();
                await dbClient.Deleteable<YB_Menu>().Where(x => x.ParentId == id).ExecuteCommandAsync();
                return new ResultInfo() { code = ResultState.SUCCESS, message = "删除成功!" };
            }
        }


        /// <summary>
        /// 菜单编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultInfo> SubmitFormAsync(YB_Menu model)
        {
            var currentUser = OperatorProvider.Provider.GetCurrent();
            using (var dbClient = ReadDbContext.GetInstance())
            {
                if (model.Id == 0)
                {
                    model.CreateTime = DateTime.Now;
                    model.Status = 1;
                    await dbClient.Insertable<YB_Menu>(model).ExecuteCommandAsync();
                    return new ResultInfo() { code = ResultState.SUCCESS, message = "添加成功!" };
                }
                else
                {
                    await dbClient.Updateable<YB_Menu>().SetColumns(x => new YB_Menu
                    {
                        ParentId = model.ParentId,
                        Icon = model.Icon,
                        SortCode = model.SortCode,
                        Url = model.Url,
                        Name = model.Name,
                        Remark = model.Remark
                    })
                     .Where(x => x.Id == model.Id)
                         .ExecuteCommandAsync();
                    return new ResultInfo() { code = ResultState.SUCCESS, message = "修改成功!" };
                }
            }
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<YB_Menu> DetailAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                return await dbClient.Queryable<YB_Menu>().FirstAsync(x => x.Id == id);
            }
        }
    }
}
