[後端] 新增水電表列表api
This commit is contained in:
		
							parent
							
								
									619fda32d3
								
							
						
					
					
						commit
						b3bbbb22a7
					
				
							
								
								
									
										161
									
								
								FrontendWebApi/ApiControllers/HydroMeterController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								FrontendWebApi/ApiControllers/HydroMeterController.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,161 @@
 | 
			
		||||
using FrontendWebApi.Models;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
using Repository.BackendRepository.Interface;
 | 
			
		||||
using Repository.FrontendRepository.Interface;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace FrontendWebApi.ApiControllers
 | 
			
		||||
{
 | 
			
		||||
    public class HydroMeterController : MyBaseApiController<HydroMeterController>
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IBackendRepository backendRepository;
 | 
			
		||||
        private readonly IFrontendRepository frontendRepository;
 | 
			
		||||
 | 
			
		||||
        public HydroMeterController(IBackendRepository backendRepository, IFrontendRepository frontendRepository)
 | 
			
		||||
        {
 | 
			
		||||
            this.backendRepository = backendRepository;
 | 
			
		||||
            this.frontendRepository = frontendRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 電表
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="input"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        [Route("api/MeterList")]
 | 
			
		||||
        public async Task<ActionResult<ApiResult<List<HydroMeterOutput>>>> MeterList([FromBody] HydroMeterInput input)
 | 
			
		||||
        {
 | 
			
		||||
            ApiResult<List<HydroMeterOutput>> apiResult = new ApiResult<List<HydroMeterOutput>>();
 | 
			
		||||
            string tableType = "day week month";
 | 
			
		||||
            if (input.building_tag == null)
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "棟別沒有被選取";
 | 
			
		||||
                return BadRequest(apiResult);
 | 
			
		||||
            }
 | 
			
		||||
            else if (input.tableType == null || tableType.Contains(input.tableType))
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "表單類別錯誤";
 | 
			
		||||
                return BadRequest(apiResult);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                string sqlWhere = "";
 | 
			
		||||
                if (input.floor_tag != null)
 | 
			
		||||
                    sqlWhere = $@" and substring_index(device_number, '_', 3) == @floor_tag";
 | 
			
		||||
                var table = "archive_electric_meter_" + input.tableType;
 | 
			
		||||
                var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : null;
 | 
			
		||||
                var sql = $@"select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp
 | 
			
		||||
                                from @table
 | 
			
		||||
                                where start_timestamp >= @startTime and end_timestamp <= @endTime and point = 'KWH' and substring_index(device_number, '_', 1) == @building_tag
 | 
			
		||||
                                {sqlWhere}
 | 
			
		||||
                                order by created_at desc;";
 | 
			
		||||
                var rawData = await backendRepository.GetAllAsync<HydroMeterRawDataOutput>(sql,
 | 
			
		||||
                                new { table = table, starTime = input.startTime, endTime = input.endTime, building_tag = input.building_tag, floor_tag = input.floor_tag });
 | 
			
		||||
                var list = rawData
 | 
			
		||||
                            .GroupBy(x => new { building_tag = x.device_number.Split("_")[0], floor_tag = x.device_number.Split("_")[2], device_serial_tag = x.device_number.Split("_")[4] })
 | 
			
		||||
                            .Select(x => new HydroMeterOutput { building_tag = x.Key.building_tag, floor_tag = x.Key.floor_tag, device_serial_tag = x.Key.device_serial_tag })
 | 
			
		||||
                            .ToList();
 | 
			
		||||
 | 
			
		||||
                foreach (var l in list)
 | 
			
		||||
                {
 | 
			
		||||
                    l.rawData = new List<HydroMeterRawDataOutput>();
 | 
			
		||||
                    l.rawData.AddRange(
 | 
			
		||||
                        rawData.Where(x => x.device_number.Split("_")[0] == l.building_tag && x.device_number.Split("_")[2] == l.floor_tag && x.device_number.Split("_")[4] == l.device_serial_tag)
 | 
			
		||||
                    );
 | 
			
		||||
                    l.total = l.rawData.Count.ToString();
 | 
			
		||||
                    l.price = (await backendRepository.GetOneAsync<int>("select system_value from variable where system_type = 'MeterPrice' and deleted = 0")).ToString();
 | 
			
		||||
                    l.total_price = (l.rawData.Count * Int32.Parse(l.price)).ToString();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                apiResult.Code = "0000";
 | 
			
		||||
                apiResult.Data = list;
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception exception)
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
 | 
			
		||||
                string json = System.Text.Json.JsonSerializer.Serialize(input);
 | 
			
		||||
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
 | 
			
		||||
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
 | 
			
		||||
            }
 | 
			
		||||
            return apiResult;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 水表
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="input"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        [Route("api/WaterList")]
 | 
			
		||||
        public async Task<ActionResult<ApiResult<List<HydroMeterOutput>>>> WaterList([FromBody] HydroMeterInput input)
 | 
			
		||||
        {
 | 
			
		||||
            ApiResult<List<HydroMeterOutput>> apiResult = new ApiResult<List<HydroMeterOutput>>();
 | 
			
		||||
            string tableType = "day week month";
 | 
			
		||||
            if (input.building_tag == null)
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "棟別沒有被選取";
 | 
			
		||||
                return BadRequest(apiResult);
 | 
			
		||||
            }
 | 
			
		||||
            else if (input.tableType == null || tableType.Contains(input.tableType))
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "表單類別錯誤";
 | 
			
		||||
                return BadRequest(apiResult);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                string sqlWhere = "";
 | 
			
		||||
                if (input.floor_tag != null)
 | 
			
		||||
                    sqlWhere = $@" and substring_index(device_number, '_', 3) == @floor_tag";
 | 
			
		||||
                var table = "archive_water_meter_" + input.tableType;
 | 
			
		||||
                var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : null;
 | 
			
		||||
                var sql = $@"select device_number, avg_rawdata, DATE_FORMAT(start_timestamp, @dateFormat) as timeStamp
 | 
			
		||||
                                from @table
 | 
			
		||||
                                where start_timestamp >= @startTime and end_timestamp <= @endTime and point = 'RCV' and substring_index(device_number, '_', 1) == @building_tag
 | 
			
		||||
                                {sqlWhere}
 | 
			
		||||
                                order by created_at desc;";
 | 
			
		||||
                var rawData = await backendRepository.GetAllAsync<HydroMeterRawDataOutput>(sql,
 | 
			
		||||
                            new { table = table, starTime = input.startTime, endTime = input.endTime, building_tag = input.building_tag, floor_tag = input.floor_tag, dateFormat = dateFormat });
 | 
			
		||||
                var list = rawData
 | 
			
		||||
                            .GroupBy(x => new { building_tag = x.device_number.Split("_")[0], floor_tag = x.device_number.Split("_")[2], device_serial_tag = x.device_number.Split("_")[4] })
 | 
			
		||||
                            .Select(x => new HydroMeterOutput { building_tag = x.Key.building_tag, floor_tag = x.Key.floor_tag, device_serial_tag = x.Key.device_serial_tag })
 | 
			
		||||
                            .ToList();
 | 
			
		||||
 | 
			
		||||
                foreach (var l in list)
 | 
			
		||||
                {
 | 
			
		||||
                    l.rawData = new List<HydroMeterRawDataOutput>();
 | 
			
		||||
                    l.rawData.AddRange(
 | 
			
		||||
                        rawData.Where(x => x.device_number.Split("_")[0] == l.building_tag && x.device_number.Split("_")[2] == l.floor_tag && x.device_number.Split("_")[4] == l.device_serial_tag)
 | 
			
		||||
                    );
 | 
			
		||||
                    l.total = l.rawData.Count.ToString();
 | 
			
		||||
                    l.price = (await backendRepository.GetOneAsync<int>("select system_value from variable where system_type = 'WaterPrice' and deleted = 0")).ToString();
 | 
			
		||||
                    l.total_price = (l.rawData.Count * Int32.Parse(l.price)).ToString();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                apiResult.Code = "0000";
 | 
			
		||||
                apiResult.Data = list;
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception exception)
 | 
			
		||||
            {
 | 
			
		||||
                apiResult.Code = "9999";
 | 
			
		||||
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
 | 
			
		||||
                string json = System.Text.Json.JsonSerializer.Serialize(input);
 | 
			
		||||
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
 | 
			
		||||
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
 | 
			
		||||
            }
 | 
			
		||||
            return apiResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								FrontendWebApi/Models/HydroMeter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								FrontendWebApi/Models/HydroMeter.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Security.Cryptography.X509Certificates;
 | 
			
		||||
 | 
			
		||||
namespace FrontendWebApi.Models
 | 
			
		||||
{
 | 
			
		||||
    public class HydroMeter
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class HydroMeterInput
 | 
			
		||||
    { 
 | 
			
		||||
        public string tableType { get; set; } //day, week, month
 | 
			
		||||
        public string building_tag { get; set; }
 | 
			
		||||
        public string floor_tag { get; set; }
 | 
			
		||||
        public DateTime startTime { get; set; }
 | 
			
		||||
        public DateTime endTime { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class HydroMeterOutput
 | 
			
		||||
    {
 | 
			
		||||
        public string building_tag { get; set; }
 | 
			
		||||
        public string floor_tag { get; set; }
 | 
			
		||||
        public string device_serial_tag { get; set; }
 | 
			
		||||
        public string total { get; set; }
 | 
			
		||||
        public string price { get; set; }
 | 
			
		||||
        public string total_price { get; set; }
 | 
			
		||||
        public List<HydroMeterRawDataOutput> rawData { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class HydroMeterRawDataOutput
 | 
			
		||||
    {
 | 
			
		||||
        public string timeStamp { get; set; }
 | 
			
		||||
        public string device_number { get; set; }
 | 
			
		||||
        public decimal avg_rawdata { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user