using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Pivotal.Discovery.Client; using System.Collections.Generic; using System.Linq.Expressions; using System; using MadRunFabric.Common; namespace Api.SignToken { public class HangfireService : IHangfireService { DiscoveryHttpClientHandler _handler; ILogger _logger; /// /// URL /// private const string URL = "http://hangfireapi/api/"; public HangfireService(IDiscoveryClient client, ILoggerFactory logFactory) { _handler = new DiscoveryHttpClientHandler(client, logFactory.CreateLogger()); _logger = logFactory.CreateLogger(); } private HttpClient GetClient() { var client = new HttpClient(_handler, false); return client; } /// /// 保养计划 /// /// /// /// /// /// /// /// /// /// /// /// /// public async Task AddWoMaintainBase(string proname, string sysname, string equipname, string id, string expirytime, int advancelong, string content,string maintype, int plancycle, int plandate, int planhour) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"proname", proname}, {"sysname", sysname}, {"equipname", equipname}, {"id", id}, {"expirytime",expirytime }, {"advancelong",advancelong.ToString() }, {"content",content }, {"maintype",maintype}, {"plancycle", plancycle.ToString()}, {"plandate", plandate.ToString()}, {"planhour", planhour.ToString()} }); var response = await client.PostAsync(URL + "equipment/addwomaintainbase", postContent); _logger.LogInformation("创建保养计划定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 保养计划 /// public async Task AddWoMaintainBase(string id, int plancycle, int planinterval, int plantimes) { _logger.LogError("HangfireService/AddWoMaintainBase start"); var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id}, {"plancycle", plancycle.ToString()}, {"planinterval", planinterval.ToString()}, {"plantimes", plantimes.ToString()} }); var response = await client.PostAsync(URL + "equipment/addwomaintainbase", postContent); _logger.LogInformation("创建保养计划定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 添加 设备运维计划定时任务 /// /// 项目id /// 系统 /// 设备 /// 计划id /// /// /// /// public async Task AddWoEquipmentRunning(string projectid, string sysnameid, string equipmentid, string id, string expirytime,int advancelong, int plancycle, int plandate, int planhour) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"projectid", projectid}, {"sysnameid", sysnameid}, {"equipmentid", equipmentid}, {"id", id}, {"expirytime",expirytime }, {"advancelong",advancelong.ToString() }, {"plancycle", plancycle.ToString()}, {"plandate", plandate.ToString()}, {"planhour", planhour.ToString()} }); var response = await client.PostAsync(URL + "equipment/addwoequipmentrunning", postContent); _logger.LogInformation("创建设备运维计划定时任务成功"); return await response.Content.ReadAsStringAsync(); } public async Task AddWoEquipmentRunning(string id, int plancycle, int planinterval, int plantimes) { _logger.LogError("HangfireService/AddWoEquipmentRunning start"); var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id}, {"plancycle", plancycle.ToString()}, {"planinterval", planinterval.ToString()}, {"plantimes", plantimes.ToString()} }); var response = await client.PostAsync(URL + "equipment/addwoequipmentrunning", postContent); _logger.LogInformation("创建设备运维计划定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 保持超时工单定时任务 /// /// /// /// /// public async Task SaveTimeOutRunning(string id, int wotype, int plancycle, int planinterval) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id}, {"wotype", wotype.ToString()}, {"plancycle", plancycle.ToString()}, {"planinterval", planinterval.ToString()}, }); var response = await client.PostAsync(URL + "equipment/savetimeout", postContent); _logger.LogInformation("创建超时工单定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 根据id删除定时任务 /// /// /// public async Task RemovePlan(string id) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id} }); var response = await client.PostAsync(URL + "equipment/remove", postContent); _logger.LogInformation("删除定时任务成功"); return await response.Content.ReadAsStringAsync(); } public string GetInfo() { var client = GetClient(); var result = client.GetStringAsync(URL + "info").Result; _logger.LogInformation("返回的权限信息为: {0}", result); return result; } #region 配送物品计划 /// /// 添加配送计划 - 来源 DistriWorkOrderPlanController /// /// /// /// /// /// public async Task AddDistriWorkOrderPlan(string id, int plancycle, int planinterval, int plantimes, string extensiontime) { _logger.LogError("HangfireService/AddWoEquipmentRunning start "); var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id}, {"plancycle", plancycle.ToString()}, {"planinterval", planinterval.ToString()}, {"plantimes", plantimes.ToString()}, {"extensiontime", extensiontime} }); //访问接口 HangfireDistributionPlanController var response = await client.PostAsync(URL + "hangfiredistributionplan/adddistributionplan", postContent); _logger.LogInformation("创建配送物品计划定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 删除配送计划 - 定时任务 - 来源 DistriWorkOrderPlanController /// /// /// public async Task RemoveDistriWorkOrderPlan(string id) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id} }); //访问接口 HangfireDistributionPlanController var response = await client.PostAsync(URL + "hangfiredistributionplan/remove", postContent); _logger.LogInformation("删除定时任务成功"); return await response.Content.ReadAsStringAsync(); } /// /// 自动定时计划 - 自动分配 /// /// /// /// /// /// public async Task SaveAutoAssign(string id, int wotype, int plancycle, int planinterval) { var client = GetClient(); HttpContent postContent = new FormUrlEncodedContent(new Dictionary() { {"id", id}, {"wotype", wotype.ToString()}, {"plancycle", plancycle.ToString()}, {"planinterval", planinterval.ToString()}, }); var response = await client.PostAsync(URL + "hangfiredistributionplan/saveautoassign", postContent); _logger.LogInformation("创建自动定时(自动分配)任务成功"); return await response.Content.ReadAsStringAsync(); } #endregion } }