using Microsoft.AspNetCore.Hosting;
using SqlSugar;
using System;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain.SystemSecurity;
using WaterCloud.Service.SystemSecurity;

namespace WaterCloud.Service.AutoJob
{
	[ServiceDescription("服务器监控")]
	public class SaveServerStateJob : IJobTask
	{
		private IWebHostEnvironment _hostingEnvironment;
		private ServerStateService _server;

		public SaveServerStateJob(ISqlSugarClient context)
		{
			_hostingEnvironment = GlobalContext.HostingEnvironment;
			_server = new ServerStateService(context);
		}

		public async Task<AlwaysResult> Start()
		{
			AlwaysResult obj = new AlwaysResult();
			try
			{
				ServerStateEntity entity = new ServerStateEntity();
				var computer = ComputerHelper.GetComputerInfo();
				entity.F_ARM = computer.RAMRate;
				entity.F_CPU = computer.CPURate;
				entity.F_IIS = "0";
				entity.F_WebSite = _hostingEnvironment.ContentRootPath;
				await _server.SubmitForm(entity);
				obj.state = ResultType.success.ToString();
				obj.message = "服务器状态更新成功！";
			}
			catch (Exception ex)
			{
				obj.state = ResultType.error.ToString();
				obj.message = "服务器状态更新失败！" + ex.Message;
			}
			return obj;
		}
	}
}