using System.Common;
using Microsoft.Extensions.Logging;
using System;
using System.IRepositories;
using System.Model;
using System.Threading.Tasks;
using SqlSugar;
using System.Collections.Generic;

namespace System.Repositories
{
    public class Sys_UserAccountRepository : BaseRepository<T_Sys_UserAccount>, ISys_UserAccountRepository
    {
        public async Task<PageData<V_UserAccount>> GetListViewByPage(List<IConditionalModel> conModels, MyPageModel pagemodel, string orderby)
        {
            var userlist=await Db.SqlQueryable<V_UserAccount>("select * from T_Sys_UserAccount").Where(conModels).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);

            var depts= await Db.SqlQueryable<V_UserAccount>("select * from T_Sys_UserAccount").Where(conModels).OrderBy(u => u.F_CreateOn, OrderByType.Desc).Select(n => n.F_DeptId).ToListAsync();
            var deptlist = await Db.Queryable<T_Sys_Department>().With(SqlWith.NoLock).Where(c => c.F_State==(int)EnumDelState.Enabled && depts.Contains(c.F_DeptId)).ToListAsync();

            var rolelist= await Db.Queryable<T_Sys_RoleInfo>().With(SqlWith.NoLock).Where(c => c.F_State == (int)EnumDelState.Enabled).ToListAsync();

            var seatlist= await Db.Queryable<T_Sys_SeatGroup>().With(SqlWith.NoLock).Where(c => c.F_State == (int)EnumDelState.Enabled).ToListAsync();

            var deptteamlist=await Db.Queryable<T_Sys_DeptTeam>().With(SqlWith.NoLock).Where(c => c.F_State == (int)EnumDelState.Enabled).ToListAsync();
            
            var positionlist= await Db.Queryable<T_Sys_Position>().With(SqlWith.NoLock).Where(c => c.F_State == (int)EnumDelState.Enabled).ToListAsync();

            var list = new List<V_UserAccount>();
            foreach (var item in userlist)
            {
                var newmodel = item;

                var deptmodel = deptlist.Find(a => a.F_DeptId == item.F_DeptId);
                if (deptmodel != null)
                    newmodel.F_Dept = deptmodel.F_DeptName;

                var rolemodel = rolelist.Find(a => a.F_RoleId == item.F_RoleId);
                if (rolemodel != null)
                    newmodel.F_Role = rolemodel.F_RoleName;

                var seatmodel = seatlist.Find(a => a.F_ZXZID == item.F_SeartGroupID);
                if (seatmodel != null)
                    newmodel.F_SeartGroup = seatmodel.F_ZXZName;

                var deptteammodel = deptteamlist.Find(a => a.F_Id == item.F_DeptTeamId);
                if (deptteammodel != null)
                    newmodel.F_DeptTeam = deptteammodel.F_Name;

                var positionmodel = positionlist.Find(a => a.F_Id == item.F_DutyId);
                if (positionmodel != null)
                    newmodel.F_Duty = positionmodel.F_Name;

                list.Add(newmodel);
            }           
            PageData<V_UserAccount> pd = new PageData<V_UserAccount>();
            pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;// CurrentDb.GetPageList(conModels, pagemodel);
        }
    }
}
