using Edu.Model.Entity.System;
using Edu.Model.ViewModel.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.System
{
    /// <summary>
    /// 角色功能权限表仓储层
    /// </summary>
    public class RB_Role_FunctionPermissionRepository : BaseRepository<RB_Role_FunctionPermission>
    {
        /// <summary>
        /// 获取角色功能菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Role_FunctionPermission_ViewModel> GetRoleFunctionPermissionListRepository(RB_Role_FunctionPermission_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM RB_Role_FunctionPermission
WHERE 1=1
");
            if (query != null)
            {
                if (query.ID > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Role_FunctionPermission_ViewModel.ID), query.ID);
                }
                if (query.Action_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Role_FunctionPermission_ViewModel.Action_Id), query.Action_Id);
                }
                if (query.Role_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Role_FunctionPermission_ViewModel.Role_Id), query.Role_Id);
                }
            }
            return Get<RB_Role_FunctionPermission_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 批量插入功能权限
        /// </summary>
        /// <param name="functionPermissionList"></param>
        /// <returns></returns>
        public bool BatchInsertRoleFunctionPermissionRepository(List<RB_Role_FunctionPermission_ViewModel> functionPermissionList)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("INSERT INTO RB_Role_FunctionPermission (Role_Id, Menu_Id,Action_Id,ActionName) VALUES");
            functionPermissionList.ForEach(x =>
            {
                builder.Append($"({x.Role_Id},{x.Menu_Id},{x.Action_Id},'{x.ActionName}'),");
            });
            builder = builder.Remove(builder.Length - 1, 1);
            return Execute(builder.ToString()) > 0;
        }
    }
}
