using FrontendWebApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Repository.FrontendRepository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FrontendWebApi.ApiControllers
{
    public class OntimeAlarmDeviceRawDataController : MyBaseApiController<OntimeAlarmDeviceRawDataController>
    {
        private readonly IFrontendRepository frontendRepository;

        public OntimeAlarmDeviceRawDataController(IFrontendRepository frontendRepository)
        {
            this.frontendRepository = frontendRepository;
        }

        [HttpPost]
        [Route("api/OntimeAlarmRawData/GetAllRawData")]
        public async Task<ActionResult<ApiResult<List<OntimeAlarmRawData>>>> GetAllRawData(string account)
        {
            ApiResult<List<OntimeAlarmRawData>> apiResult = new ApiResult<List<OntimeAlarmRawData>>(jwt_str);
            if (!jwtlife)
            {
                apiResult.Code = "5000";
                return BadRequest(apiResult);
            }
            try
            {
                var sqlRawData = $@"SELECT 
                                        DISTINCT oa.device_number,
                                        oa.building_guid,
                                        oa.main_system_guid,
                                        oa.sub_system_guid,
                                        oa.floor_guid,
                                        oa.sub_system_guid
                                    FROM ontime_alarm_device_rawdata oa
                                    INNER JOIN (
                                        SELECT 
                                            ap.building_guid,
                                            ap.ShowView AS sub_system_guid
                                        FROM
                                        (
                                            SELECT *
                                            FROM role_auth ra
                                            WHERE ra.role_guid = (SELECT ui.role_guid FROM userinfo ui WHERE account = @Account)
                                        ) ra
                                        LEFT JOIN auth_page ap ON ra.AuthCode = ap.AuthCode
                                        WHERE ap.AuthType = 1
                                    ) shower ON oa.building_guid = shower.building_guid AND oa.sub_system_guid = shower.sub_system_guid
                                    WHERE source_state = 'offnormal'";

                //Stopwatch stopWatch = new Stopwatch();
                //stopWatch.Start();

                var ontimeAlarmRawDatas = await frontendRepository.GetAllAsync<OntimeAlarmRawData>(sqlRawData, new { Account = account });

                //stopWatch.Stop();
                //Logger.LogInformation("【OnTimeDeviceRawDataJob】【效能檢驗】[取得所有RawData花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);

                //stopWatch.Reset();
                //stopWatch.Start();
                

                //stopWatch.Stop();
                //Logger.LogInformation("【OnTimeDeviceRawDataJob】【效能檢驗】[RawData資料整理花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);

                apiResult.Data = ontimeAlarmRawDatas;
                apiResult.Code = "0000";
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
                return Ok(apiResult);
            }
            return Ok(apiResult);
        }
    }
}