using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nirvana.Common;
using Nirvana.Common.ApiBase;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace YBDevice.Application.MeasureInfo
{
    /// <summary>
    /// 测量记录
    /// </summary>
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class ResultAppService : IDynamicApiController
    {
        private readonly IResultService _Service;
        public ResultAppService(IResultService Service)
        {
            _Service = Service;
        }
        /// <summary>
        /// 测量记录列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultInfo> GetListAsync(QueryParams param)
        {
            var result = await _Service.GetListAsync(param);
            return new ResultInfo(ResultState.SUCCESS, "success", result);
        }
        /// <summary>
        /// 根据设备查询测量记录列表
        /// </summary>
        /// <param name="devid"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultInfo> GetDevResultListAsync([FromQuery] int devid, QueryParams param)
        {
            var result = await _Service.GetDevResultAsync(devid, param);
            return new ResultInfo(ResultState.SUCCESS, "success", result);
        }

        /// <summary>
        /// 指定家庭成语的体重记录列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        [QueryParameters]
        public async Task<ResultInfo> GetFamilyResultAsync(int id, DateTime? starttime, DateTime? endtime)
        {
            var result = await _Service.GetUserResultAsync(id, starttime, endtime);
            var weightlist = new List<decimal>();
            var timelist = new List<string>();
            result.ForEach(x =>
            {
                weightlist.Add(x.weight);
                timelist.Add(x.time);
            });
            return new ResultInfo(ResultState.SUCCESS, "success", new
            {
                weightlist = weightlist,
                timelist = timelist
            });
        }
        /// <summary>
        /// 根据用户ID查询用户汇总汇总数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultInfo> GetUserDataByUserIdAsync([FromQuery] int id, QueryParams param)
        {
            var result = await _Service.GetUserDataByUserIdAsync(id, param);
            return new ResultInfo(ResultState.SUCCESS, "success", result);
        }
        /// <summary>
        /// 根据用户ID查询用户测量记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultInfo> GetUserResultByUserIdAsync([FromQuery] int id, QueryParams param)
        {
            var result = await _Service.GetUserResultByUserIdAsync(id, param);
            return new ResultInfo(ResultState.SUCCESS, "success", result);
        }
        /// <summary>
        /// 导出信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResultInfo> ExportAsync(QueryParams param)
        {
            return await _Service.ExportAsync(param);
        }
    }
}
