using Furion.DependencyInjection;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.CommonService;
using YBDevice.Entity;

namespace YBDevice.Application.RegUserInfo
{
    /// <summary>
    /// 注册用户管理
    /// </summary>
    public class UserService : IUserService, ITransient
    {
        private readonly ISqlSugarRepository<YB_RegUser> repository;
        private readonly SqlSugarClient dbClient;
        private readonly ICommonService _commonService;
        private readonly OperatorModel currentUser;
        public UserService(ISqlSugarRepository<YB_RegUser> sqlSugarRepository,ICommonService commonService)
        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
            _commonService = commonService;
            currentUser = BaseInfoService.GetUserInfo();
        }

        /// <summary>
        /// 注册用户列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<PageParms<YB_RegUser>> GetListAsync(QueryParams param)
        {
            RefAsync<int> totalnum = 0;
            var temquery = dbClient.Queryable<YB_RegUser>();
            if (param.queryParam != null && param.queryParam.Count > 0)
            {
                List<IConditionalModel> conModels = new List<IConditionalModel>();
                param.queryParam.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.Value))
                    {
                        conModels.Add(new ConditionalModel()
                        {
                            FieldName = x.Name,
                            ConditionalType = (ConditionalType)x.Type,
                            FieldValue = x.Value.Trim()
                        });
                    }
                });
                if (conModels.Count > 0)
                {
                    temquery = temquery.Where(conModels);
                }
            }
            string sorts = string.Format("{0} {1}", param.sort, param.order);
            var query = await temquery.OrderBy(sorts)
                .Mapper((it, cache) =>
                {
                    it.Headimg = string.IsNullOrEmpty(it.Headimg) ? DefaultService.HeadImg(it.Gender, FamilyType.Adult) : it.Headimg;
                })
                 .ToPageListAsync(param.offset, param.limit, totalnum);
            return new PageParms<YB_RegUser>
            {
                page = param.offset,
                Items = query,
                totalnum = totalnum,
                limit = param.limit
            };
        }
    }
}
