|  | @@ -8,6 +8,7 @@ using IMES_Middleware_Platform.Api.Domain.FuelDispenser.Dto;
 | 
	
		
			
				|  |  |  using IMES_Middleware_Platform.Api.Services.Module;
 | 
	
		
			
				|  |  |  using Microsoft.AspNetCore.Mvc;
 | 
	
		
			
				|  |  |  using Microsoft.Extensions.Configuration;
 | 
	
		
			
				|  |  | +using Newtonsoft.Json.Linq;
 | 
	
		
			
				|  |  |  using System;
 | 
	
		
			
				|  |  |  using System.Collections.Generic;
 | 
	
		
			
				|  |  |  using System.ComponentModel;
 | 
	
	
		
			
				|  | @@ -34,11 +35,15 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          private readonly IConfiguration _configuration;
 | 
	
		
			
				|  |  |          public readonly IFuelDispenserRepository _fuelDispenserRepository;
 | 
	
		
			
				|  |  | +        public readonly IAlarmHistoryRepository _alarmHistoryRepository;
 | 
	
		
			
				|  |  | +        public readonly ILifeCycleRepository _lifeCycleRepository;
 | 
	
		
			
				|  |  |          static string token = string.Empty;
 | 
	
		
			
				|  |  | -        public AssociationService(IConfiguration configuration, IFuelDispenserRepository fuelDispenserRepository)
 | 
	
		
			
				|  |  | +        public AssociationService(IConfiguration configuration, IFuelDispenserRepository fuelDispenserRepository, IAlarmHistoryRepository alarmHistoryRepository, ILifeCycleRepository lifeCycleRepository)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              _configuration = configuration;
 | 
	
		
			
				|  |  |              _fuelDispenserRepository = fuelDispenserRepository;
 | 
	
		
			
				|  |  | +            _alarmHistoryRepository = alarmHistoryRepository;
 | 
	
		
			
				|  |  | +            _lifeCycleRepository = lifeCycleRepository;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          public Task<LocalApiDto> GetAllStation(string areaType, string search)
 | 
	
	
		
			
				|  | @@ -54,21 +59,11 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |          [HttpPost]
 | 
	
		
			
				|  |  |          public async Task GetDispensersAsync(string expand1, string expand2, string search)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              HttpHelper helper = new HttpHelper();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              string postUrl = "https://cma-fds.org.cn/api/v1/dispensers";
 | 
	
		
			
				|  |  |              var size = 20;
 | 
	
		
			
				|  |  |              var page = 0;
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  |              await GetAllDataAsync(postUrl, token, expand1, expand2, search, size);
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  | -            
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          [HttpPost]
 | 
	
		
			
				|  |  |          public async Task<UserResponseDto> GetTokenAsync(UserLogin user)
 | 
	
	
		
			
				|  | @@ -94,6 +89,16 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |              token = myObject.Token;
 | 
	
		
			
				|  |  |              return myObject;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          public async Task GetAllDataAsync(string postUrl, string token, string expand1, string expand2, string search, int pageSize)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              int currentPage = 0;
 | 
	
	
		
			
				|  | @@ -108,6 +113,7 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |                  {
 | 
	
		
			
				|  |  |                      break;
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  await AddFuelDispenserAsync(myObject);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  
 | 
	
	
		
			
				|  | @@ -115,6 +121,184 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        public async Task GetLifecycleAsync(int id)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            try
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                string jsonString = @"
 | 
	
		
			
				|  |  | +        [
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ""启用"": [
 | 
	
		
			
				|  |  | +                    [""disable"", ""enable""],
 | 
	
		
			
				|  |  | +                    [""bind"", ""enable""]
 | 
	
		
			
				|  |  | +                ]
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ""维修"": [
 | 
	
		
			
				|  |  | +                    [""enable"", ""disable""],
 | 
	
		
			
				|  |  | +                    [""bind"", ""disable""]
 | 
	
		
			
				|  |  | +                ]
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ""加油站注册"": [
 | 
	
		
			
				|  |  | +                    [""factory"", ""bind""]
 | 
	
		
			
				|  |  | +                ]
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ""出厂注册"": [
 | 
	
		
			
				|  |  | +                    [""register"", ""factory""],
 | 
	
		
			
				|  |  | +                    [""bind"", ""factory""]
 | 
	
		
			
				|  |  | +                ]
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        ]";
 | 
	
		
			
				|  |  | +                JArray jsonArray = JArray.Parse(jsonString);
 | 
	
		
			
				|  |  | +                int currentPage = 0;
 | 
	
		
			
				|  |  | +                int pageSize = 20;
 | 
	
		
			
				|  |  | +                HttpHelper helper = new HttpHelper();
 | 
	
		
			
				|  |  | +                List<LifecycleDto> list = new List<LifecycleDto>();
 | 
	
		
			
				|  |  | +                List<int> intList = new List<int>();
 | 
	
		
			
				|  |  | +                while (true)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    string result = await helper.GetLifecycleAsync("https://cma-fds.org.cn/api/v1/audits?", token, id, pageSize, currentPage);
 | 
	
		
			
				|  |  | +                    LifecycleRoot myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<LifecycleRoot>(result);
 | 
	
		
			
				|  |  | +                    if (myObject == null || myObject.Content == null || myObject.Content.Count == 0)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    for (var i = 0; i < myObject.Content.Count(); i++)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        DateTime? CreatedDate = myObject.Content[i].CreatedDate.HasValue
 | 
	
		
			
				|  |  | +      ? DateTimeOffset.FromUnixTimeMilliseconds(myObject.Content[i].CreatedDate.Value).UtcDateTime
 | 
	
		
			
				|  |  | +      : (DateTime?)null;
 | 
	
		
			
				|  |  | +                        var device = myObject.Content[i].Content.DeviceStatus;
 | 
	
		
			
				|  |  | +                        string keyName = string.Empty;
 | 
	
		
			
				|  |  | +                        if (device is string)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            keyName = ((string)device) == "register" ? "备案":"";
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        else if (device is JArray)
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            
 | 
	
		
			
				|  |  | +                            JArray statusArray = (JArray)device;
 | 
	
		
			
				|  |  | +                            foreach (JObject item in jsonArray)
 | 
	
		
			
				|  |  | +                            {
 | 
	
		
			
				|  |  | +                                foreach (var kvp in item)
 | 
	
		
			
				|  |  | +                                {
 | 
	
		
			
				|  |  | +                                    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                    JArray innerArray = (JArray)kvp.Value;
 | 
	
		
			
				|  |  | +                                    foreach (JArray subArray in innerArray)
 | 
	
		
			
				|  |  | +                                    {
 | 
	
		
			
				|  |  | +                                        
 | 
	
		
			
				|  |  | +                                        bool isEqual = JArray.DeepEquals(subArray, statusArray);
 | 
	
		
			
				|  |  | +                                        if (isEqual)
 | 
	
		
			
				|  |  | +                                        {
 | 
	
		
			
				|  |  | +                                            keyName = kvp.Key;
 | 
	
		
			
				|  |  | +                                        }
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        LifecycleDto lifecycleDto = new LifecycleDto()
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            CreatedDate = CreatedDate,
 | 
	
		
			
				|  |  | +                            FuelId = id,
 | 
	
		
			
				|  |  | +                            Operation = keyName,
 | 
	
		
			
				|  |  | +                            Operator = myObject.Content[i].CreatedBy.Name,
 | 
	
		
			
				|  |  | +                            OperatorID = myObject.Content[i].CreatedBy.Id,
 | 
	
		
			
				|  |  | +                            LifeCycleID = myObject.Content[i].Id
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  | +                        list.Add(lifecycleDto);
 | 
	
		
			
				|  |  | +                        intList.Add(lifecycleDto.LifeCycleID);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    currentPage++;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                var lifeCycle = _lifeCycleRepository.Where(x => intList.Contains(x.LifeCycleID)).ToList();
 | 
	
		
			
				|  |  | +                lifeCycle.ForEach(a =>
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    list.RemoveAll(e => e.LifeCycleID == a.LifeCycleID);
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                if (list != null)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    await _lifeCycleRepository.InsertAsync(LifecycleDto.ToFuelDispenserList(list));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            catch (Exception ex)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        public async Task GetAlarmHistoryAsync(int id)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            try
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                int currentPage = 0;
 | 
	
		
			
				|  |  | +                int pageSize = 20;
 | 
	
		
			
				|  |  | +                HttpHelper helper = new HttpHelper();
 | 
	
		
			
				|  |  | +                List<int> intList = new List<int>();
 | 
	
		
			
				|  |  | +                List<AlarmHistoryDto> historyDtos = new List<AlarmHistoryDto>();
 | 
	
		
			
				|  |  | +                while (true)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    string result = await helper.GetAlarmHistoryAsync("https://cma-fds.org.cn/api/v1/notices?", token, id, pageSize, currentPage);
 | 
	
		
			
				|  |  | +                    AlarmHistoryRoot myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<AlarmHistoryRoot>(result);
 | 
	
		
			
				|  |  | +                    if (myObject == null || myObject.Content == null || myObject.Content.Count == 0)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    for (var i = 0; i < myObject.Content.Count(); i++)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        string origin = myObject.Content[i].origin;
 | 
	
		
			
				|  |  | +                        DateTime? CreatedDate = myObject.Content[i].createdDate.HasValue
 | 
	
		
			
				|  |  | +            ? DateTimeOffset.FromUnixTimeMilliseconds(myObject.Content[i].createdDate.Value).UtcDateTime
 | 
	
		
			
				|  |  | +            : (DateTime?)null;
 | 
	
		
			
				|  |  | +                        AlarmHistoryDto alarmHistoryDto = new AlarmHistoryDto
 | 
	
		
			
				|  |  | +                        {
 | 
	
		
			
				|  |  | +                            FuelId = id,
 | 
	
		
			
				|  |  | +                            GasStation = myObject.Content[i].Station?.Name,
 | 
	
		
			
				|  |  | +                            AlarmDescription = myObject.Content[i].Alarm?.description,
 | 
	
		
			
				|  |  | +                            AlarmSource = origin == "blackBox" ? "安全装置" : origin == "platform" ? "云平台" : null,
 | 
	
		
			
				|  |  | +                            SerialNumber = myObject.Content[i].dispenser.SerialNum,
 | 
	
		
			
				|  |  | +                            AlarmType = myObject.Content[i].Alarm?.type,
 | 
	
		
			
				|  |  | +                            CreatedDate = CreatedDate,
 | 
	
		
			
				|  |  | +                            Name = myObject.Content[i].dispenser.Name,
 | 
	
		
			
				|  |  | +                            AlarmHistoryID = myObject.Content[i].id
 | 
	
		
			
				|  |  | +                        };
 | 
	
		
			
				|  |  | +                        historyDtos.Add(alarmHistoryDto);
 | 
	
		
			
				|  |  | +                        intList.Add(alarmHistoryDto.AlarmHistoryID);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    currentPage++;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                var alarmHistory = _alarmHistoryRepository.Where(x => intList.Contains(x.AlarmHistoryID)).ToList();
 | 
	
		
			
				|  |  | +                alarmHistory.ForEach(a =>
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    historyDtos.RemoveAll(e => e.AlarmHistoryID == a.AlarmHistoryID);
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                if (historyDtos != null)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    await _alarmHistoryRepository.InsertAsync(AlarmHistoryDto.ToFuelDispenserList(historyDtos));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            catch (Exception ex)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          public async Task RepeatOperation(int numberOfTimes)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              for (int i = 0; i < numberOfTimes; i++)
 | 
	
	
		
			
				|  | @@ -128,7 +312,8 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              for (var i = 0; i < myObject.Content.Count(); i++)
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +                await GetLifecycleAsync(myObject.Content[i].Id);
 | 
	
		
			
				|  |  | +                await GetAlarmHistoryAsync(myObject.Content[i].Id);
 | 
	
		
			
				|  |  |                  DateTime? ProductTime = myObject.Content[i].ProductTime.HasValue
 | 
	
		
			
				|  |  |                      ? DateTimeOffset.FromUnixTimeMilliseconds(myObject.Content[i].ProductTime.Value).UtcDateTime
 | 
	
		
			
				|  |  |                      : (DateTime?)null;
 | 
	
	
		
			
				|  | @@ -148,10 +333,14 @@ namespace IMES_Middleware_Platform.Api.Services.Association
 | 
	
		
			
				|  |  |                      StartupDate = StartTime, 
 | 
	
		
			
				|  |  |                      InstallationDate = null, 
 | 
	
		
			
				|  |  |                      DeviceStatus = myObject.Content[i].DeviceStatus,
 | 
	
		
			
				|  |  | -                    OnlineStatus = myObject.Content[i].OnlineStatus
 | 
	
		
			
				|  |  | +                    OnlineStatus = myObject.Content[i].OnlineStatus,
 | 
	
		
			
				|  |  | +                    AccuracyLevel = myObject.Content[i].AccuracyLevel,
 | 
	
		
			
				|  |  | +                    MeasureRang = myObject.Content[i].MeasureRang,
 | 
	
		
			
				|  |  | +                    ModelSpec = myObject.Content[i].ModelSpec,
 | 
	
		
			
				|  |  | +                    FuelId = myObject.Content[i].Id
 | 
	
		
			
				|  |  |                  };
 | 
	
		
			
				|  |  |                  var fuelDispenser = _fuelDispenserRepository.Where(x => x.SerialNumber == fuelDispenserDto.SerialNumber).First();
 | 
	
		
			
				|  |  | -                if (fuelDispenser != null)
 | 
	
		
			
				|  |  | +                if (fuelDispenser == null)
 | 
	
		
			
				|  |  |                  {
 | 
	
		
			
				|  |  |                      await _fuelDispenserRepository.InsertAsync(fuelDispenserDto.ToFuelDispenser(fuelDispenserDto));
 | 
	
		
			
				|  |  |                  }
 |