using Nirvana.Common;
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 ResultApp : Repository<YB_Device>
    {
        /// <summary>
        /// 测量记录列表  
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<PageParms<YB_ResultExt>> GetListAsync(QueryParams param)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                RefAsync<int> totalnum = 0;
                var temquery = dbClient.Queryable<YB_ResultExt>();
                var currentUser = OperatorProvider.Provider.GetCurrent();
                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);
                    }
                }
                if (currentUser.AccountType != (int)AccountType.platform)
                {
                    temquery = temquery.Where(x => x.BusinessId == currentUser.BusinessId);
                }
                string sorts = string.Format("{0} {1}", param.sort, param.order);
                var query = await temquery.OrderBy(sorts)

                    .ToPageListAsync(param.offset, param.limit, totalnum);
                return new PageParms<YB_ResultExt>
                {
                    page = param.offset,
                    Items = query,
                    totalnum = totalnum,
                    limit = param.limit
                };
            }
        }
    }
}
