using CommonEntity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;

namespace BLWLogServer.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PowerController : ControllerBase
    {
        public IMongoClient _client;
        public PowerController(IMongoClient mongo)
        {
            this._client = mongo;
        }
        [HttpPost()]
        public ReturnInfo GetPowerAnalysis([FromBody] QueryDate query)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                int size = query.PageSize;
                int index = query.PageIndex;
                string st = query.Start_Time;
                string et = query.End_Time;
                DateTime sst = DateTime.Parse(st);
                DateTime eet = DateTime.Parse(et);
                var collection = _client.GetDatabase("udppackage").GetCollection<NengHao_db>("powermonitor");
                //构建查询条件
                long code = 0;
                long.TryParse(query.HotelCode,out code); 

                var filter = Builders<NengHao_db>.Filter.And(
                    Builders<NengHao_db>.Filter.Gt(x => x.ReportTime, sst),
                    Builders<NengHao_db>.Filter.Lt(x => x.ReportTime, eet),
                    Builders<NengHao_db>.Filter.Eq(x => x.HotelCode, code)
                );

                long totalCount = collection.CountDocuments(filter);
                var results = collection.Find(filter).ToList(); // 跳过前面的记录
                r.isok = true;
                r.response = results;
            }
            catch (Exception ex)
            {

                r.isok = false;
                r.message = ex.Message;
            }
            return r;
        }
    }
}
