using SqlSugar;
using System;
using System.Collections.Generic;
using System.Common;
using System.IRepositories;
using System.Model;
using System.Text;
using System.Threading.Tasks;

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

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

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

                var seatmodel = seatlist.Find(a => a.F_DeptId == item.F_DeptId);
                if (seatmodel != null)
                    newmodel.F_DeptName = seatmodel.F_DeptName;

                list.Add(newmodel);
            }
            PageData<V_DeptTeam> pd = new PageData<V_DeptTeam>();
            pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;
        }
    }
}
