public Result PostDeviceOperateOperator([FromBody] IEnumerable <DeviceOperateOperator> operators) { if (operators == null || !operators.Any()) { return(Result.GenError <Result>(Error.ParamError)); } if (operators.Any(x => x.AccountId == 0)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorNotEmpty)); } if (operators.GroupBy(x => x.AccountId).Any(y => y.Count() > 1)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorDuplicate)); } var opId = operators.FirstOrDefault()?.OpId ?? 0; var sames = operators.Select(x => x.AccountId); if (DeviceOperateOperatorHelper.GetHaveSame(opId, sames)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorIsExist)); } var userId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; foreach (var op in operators) { op.CreateUserId = userId; op.MarkedDateTime = markedDateTime; op.Remark = op.Remark ?? ""; } DeviceOperateOperatorHelper.Instance.Add(operators); return(Result.GenError <Result>(Error.Success)); }
public Result CancelDeviceOperateSchedule([FromBody] DeviceOperateSchedule schedule) { var qId = schedule.Id; var old = DeviceOperateScheduleHelper.GetOpDetail(qId); if (old == null) { return(Result.GenError <Result>(Error.DeviceOperateScheduleNotExist)); } //if (old.OperatorId != schedule.OperatorId) //{ // return Result.GenError<Result>(Error.DeviceOperateScheduleOperatorError); //} if (old.State != DeviceOperateScheduleState.未开始) { return(Result.GenError <Result>(Error.DeviceOperateScheduleNotWait)); } var optor = schedule.OperatorId != 0 ? DeviceOperateOperatorHelper.GetDetail(schedule.OpId, schedule.OperatorId) : null; schedule.OperatorId = optor?.Id ?? 0; var userId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; old.MarkedDateTime = markedDateTime; var log = ClassExtension.CopyTo <DeviceOperateScheduleDetail, DeviceOperateLog>(old); log.Type = DeviceOperateLogType.延后; log.CreateUserId = userId; log.EndTime = markedDateTime; DeviceOperateScheduleHelper.BackSchedule(old); DeviceOperateLogHelper.Instance.Add(log); return(Result.GenError <Result>(Error.Success)); }
public DataResult GetDeviceOperate([FromQuery] bool menu, int wId, int qId, int oporId) { var result = new DataResult(); if (oporId != 0) { var operates = DeviceOperateOperatorHelper.GetOperates(oporId); var opIds = operates.Select(x => x.OpId).Distinct().OrderBy(x => x); result.datas.AddRange(menu ? DeviceOperateHelper.GetMenus(opIds) : DeviceOperateHelper.GetDetails(opIds)); } else { result.datas.AddRange(menu ? DeviceOperateHelper.GetMenus(wId, qId) : DeviceOperateHelper.GetDetails(wId, qId)); } if (qId != 0 && !result.datas.Any()) { result.errno = Error.DeviceOperateNotExist; return(result); } return(result); }
public Result CompleteDeviceOperateSchedule([FromBody] DeviceOperateSchedule schedule) { var qId = schedule.Id; var old = DeviceOperateScheduleHelper.GetOpDetail(qId); if (old == null) { return(Result.GenError <Result>(Error.DeviceOperateScheduleNotExist)); } var opr = DeviceOperateOperatorHelper.GetDetail(schedule.OpId, schedule.OperatorId); if (opr == null) { return(Result.GenError <Result>(Error.DeviceOperateOperatorNotExist)); } schedule.OperatorId = opr.Id; if (old.OperatorId != schedule.OperatorId) { return(Result.GenError <Result>(Error.DeviceOperateScheduleOperatorError)); } if (old.State == DeviceOperateScheduleState.未开始) { return(Result.GenError <Result>(Error.DeviceOperateScheduleNotStart)); } var optor = DeviceOperateOperatorHelper.Instance.Get <DeviceOperateOperator>(schedule.OperatorId); if (optor == null) { return(Result.GenError <Result>(Error.DeviceOperateOperatorIsExist)); } var userId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; if (old.State == DeviceOperateScheduleState.进行中) { var total = (int)(markedDateTime - old.StartTime).TotalSeconds; old.OpCost += total; } else if (old.State == DeviceOperateScheduleState.已暂停) { var total = (int)(markedDateTime - old.StartTime).TotalSeconds; old.OpCost += total; } old.MarkedDateTime = markedDateTime; old.EndTime = markedDateTime; old.EndCount++; var log = ClassExtension.CopyTo <DeviceOperateScheduleDetail, DeviceOperateLog>(old); log.CreateUserId = userId; log.Type = DeviceOperateLogType.完成; old.LastLogId = DeviceOperateLogHelper.Instance.AddBackId(log); DeviceOperateScheduleHelper.CompleteSchedule(old); return(Result.GenError <Result>(Error.Success)); }
public DataResult GetDeviceOperateOperator([FromQuery] bool menu, int opId, int qId) { var result = new DataResult(); result.datas.AddRange(menu ? DeviceOperateOperatorHelper.GetMenus(opId, qId) : DeviceOperateOperatorHelper.GetOpDetails(opId, qId)); if (qId != 0 && !result.datas.Any()) { result.errno = Error.DeviceOperateOperatorNotExist; return(result); } return(result); }
public Result StartDeviceOperateSchedule([FromBody] DeviceOperateSchedule schedule) { var qId = schedule.Id; var old = DeviceOperateScheduleHelper.GetOpDetail(qId); if (old == null) { return(Result.GenError <Result>(Error.DeviceOperateScheduleNotExist)); } var opr = DeviceOperateOperatorHelper.GetDetail(schedule.OpId, schedule.OperatorId); if (opr == null) { return(Result.GenError <Result>(Error.DeviceOperateOperatorNotExist)); } schedule.OperatorId = opr.Id; if (old.OperatorId != schedule.OperatorId) { return(Result.GenError <Result>(Error.DeviceOperateScheduleOperatorError)); } if (old.State == DeviceOperateScheduleState.进行中) { return(Result.GenError <Result>(Error.DeviceOperateScheduleDoing)); } var userId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; if (old.State == DeviceOperateScheduleState.已暂停) { var total = (int)(markedDateTime - old.PauseTime).TotalSeconds; old.PauseCost += total; } old.MarkedDateTime = markedDateTime; old.FirstStartTime = old.FirstStartTime == default(DateTime) ? markedDateTime : old.FirstStartTime; old.StartTime = markedDateTime; old.StartCount++; old.Remark = schedule.Remark ?? old.Remark; var log = ClassExtension.CopyTo <DeviceOperateScheduleDetail, DeviceOperateLog>(old); log.CreateUserId = userId; log.Type = DeviceOperateLogType.开始; DeviceOperateScheduleHelper.StartSchedule(old); DeviceOperateLogHelper.Instance.Add(log); return(Result.GenError <Result>(Error.Success)); }
public Result PutDeviceOperateOperator([FromBody] IEnumerable <DeviceOperateOperator> operators) { if (operators == null || !operators.Any()) { return(Result.GenError <Result>(Error.ParamError)); } if (operators.Any(x => x.AccountId == 0)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorNotEmpty)); } if (operators.GroupBy(x => x.AccountId).Any(y => y.Count() > 1)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorDuplicate)); } var opId = operators.FirstOrDefault()?.OpId ?? 0; var sames = operators.Select(x => x.AccountId); var ids = operators.Select(x => x.Id); if (DeviceOperateOperatorHelper.GetHaveSame(opId, sames, ids)) { return(Result.GenError <Result>(Error.DeviceOperateOperatorIsExist)); } var cnt = DeviceOperateOperatorHelper.Instance.GetCountByIds(ids); if (cnt != operators.Count()) { return(Result.GenError <Result>(Error.DeviceOperateOperatorNotExist)); } var markedDateTime = DateTime.Now; foreach (var op in operators) { op.MarkedDateTime = markedDateTime; op.Remark = op.Remark ?? ""; } DeviceOperateOperatorHelper.Instance.Update(operators); return(Result.GenError <Result>(Error.Success)); }
/// <summary> /// 删除3个月前的监控日志 /// </summary> private static void OperateSchedule() { if (RedisHelper.SetIfNotExist(lockKey, ServerConfig.GetLockValue)) { try { RedisHelper.SetExpireAt(lockKey, DateTime.Now.AddMinutes(30)); var now = DateTime.Now; //当前排班 var schedules = DeviceOperateScheduleHelper.Instance.GetAllData <DeviceOperateScheduleOp>().OrderBy(x => x.Order); var maxOrder = schedules.Any() ? schedules.Max(x => x.Order) : 0; var ops = DeviceOperateHelper.Instance.GetAll <DeviceOperate>(); //var devices = DeviceLibraryHelper.Instance.GetAll<DeviceLibrary>(); //排班设备 var opDevices = DeviceOperateDeviceHelper.GetScheduleDevices(); //var op = DeviceOperateOperatorHelper.Instance.GetAll<DeviceOperateOperator>(); //排班操作工 var operators = DeviceOperateOperatorHelper.GetScheduleOperates(); var newScs = new List <DeviceOperateSchedule>(); var upScs = new List <DeviceOperateSchedule>(); var delScs = new List <DeviceOperateSchedule>(); foreach (var op in ops) { if (op.IsEnable && op.IsSchedule) { var opId = op.Id; //排班设备 var opDs = opDevices.Where(x => x.OpId == opId); //排班操作工 var opOs = operators.Where(x => x.OpId == opId); var opScs = new List <DeviceOperateScheduleOp>(); var scs = schedules.Where(x => x.OpId == opId); foreach (var sc in scs) { if (sc.State != DeviceOperateScheduleState.未开始) { opScs.Add(sc); continue; } //排班设备 var device = opDs.FirstOrDefault(x => x.Id == sc.DeviceId); if (sc.OperatorId != 0) { //排班操作工 var optor = opOs.FirstOrDefault(x => x.Id == sc.OperatorId); if (optor == null || !optor.CanAssign(now)) { sc.MarkedDateTime = now; sc.Update = true; sc.OperatorId = 0; sc.AssignTime = default(DateTime); } } if (device != null) { opScs.Add(sc); continue; } sc.MarkedDateTime = now; sc.Add = false; sc.Update = false; delScs.Add(sc); } foreach (var device in opDs) { var deviceId = device.Id; var sc = opScs.FirstOrDefault(x => x.DeviceId == deviceId); if (sc == null) { opScs.Add(new DeviceOperateScheduleOp(now, ++maxOrder, opId, deviceId)); } else { if (sc.MarkedDelete) { sc.MarkedDateTime = now; sc.MarkedDelete = false; sc.Order = ++maxOrder; sc.Update = true; } if (sc.Remark != device.Remark) { sc.MarkedDateTime = now; sc.Remark = device.Remark; sc.Update = true; } } } var canOps = opOs.Where(x => x.CanAssign(now)); var assignScs = opScs.Where(x => x.OperatorId != 0); var assignOps = assignScs.GroupBy(x => x.OperatorId).Select(x => x.Key).ToList(); var cnt = canOps.Count() * 3; var waitScs = opScs.Where(x => x.OperatorId == 0).Take(cnt).ToArray(); var scsOrder = waitScs.Select(x => x.Order).OrderBy(x => x).ToArray(); var deviceIds = waitScs.Select(x => x.DeviceId); var deviceLeftTimes = deviceIds.ToDictionary(x => x, x => 0m); if (deviceIds.Any()) { var deviceLibraryDetails = DeviceHelper.Instance.GetAllByIds <DeviceDetail>(deviceIds).ToDictionary(x => x.Id); try { #if DEBUG var url = "http://192.168.22.222:61102" + UrlMappings.Urls[UrlMappings.deviceListGate]; #else var url = ServerConfig.GateUrl + UrlMappings.Urls[UrlMappings.deviceListGate]; #endif //向GateProxyLink请求数据 var resp = HttpServer.Get(url, new Dictionary <string, string> { { "ids", deviceIds.Join() } }); if (resp != "fail") { var scriptIds = deviceLibraryDetails.Values.Select(x => x.ScriptId); var dataNameDictionaries = scriptIds.Any() ? DataNameDictionaryHelper.GetDataNameDictionaryDetails(scriptIds) : new List <DataNameDictionaryDetail>(); var dataResult = JsonConvert.DeserializeObject <DeviceResult>(resp); if (dataResult.errno == Error.Success) { foreach (DeviceInfo deviceInfo in dataResult.datas) { var deviceId = deviceInfo.DeviceId; if (deviceLibraryDetails.ContainsKey(deviceId)) { deviceInfo.ScriptId = deviceLibraryDetails[deviceId].ScriptId; var deviceData = deviceInfo.DeviceData; if (AnalysisHelper.GetValue(deviceData, dataNameDictionaries, deviceInfo.ScriptId, AnalysisHelper.leftProcessTimeDId, out var v)) { if (deviceLeftTimes.ContainsKey(deviceId)) { deviceLeftTimes[deviceId] = v; } } } } } } } catch (Exception e) { Log.Error($"OperateSchedule {UrlMappings.Urls[UrlMappings.deviceListGate]},信息:{e}"); } } waitScs = waitScs.OrderBy(x => deviceLeftTimes[x.DeviceId]).ThenBy(x => x.Order).ToArray(); for (var i = 0; i < waitScs.Length; i++) { var waitSc = waitScs[i]; var sc = opScs.FirstOrDefault(x => x.Id == waitSc.Id); if (sc != null && sc.Order != scsOrder[i]) { sc.MarkedDateTime = now; sc.Update = true; sc.Order = scsOrder[i]; } } if (assignOps.Count() != canOps.Count()) { var nowAssignOps = canOps.Where(x => assignOps.All(y => y != x.Id)); foreach (var opr in nowAssignOps) { var operatorId = opr.Id; var sc = opScs.FirstOrDefault(x => x.OperatorId == 0); if (sc != null) { sc.MarkedDateTime = now; sc.Update = true; sc.AssignTime = now; sc.OperatorId = operatorId; } } } newScs.AddRange(opScs.Where(x => x.Add)); upScs.AddRange(opScs.Where(x => x.Id != 0 && x.Update)); } } if (newScs.Any()) { DeviceOperateScheduleHelper.Instance.Add(newScs); } if (upScs.Any()) { DeviceOperateScheduleHelper.Instance.Update <DeviceOperateSchedule>(upScs); } if (delScs.Any()) { DeviceOperateScheduleHelper.Instance.Delete(delScs.Select(x => x.Id)); } RedisHelper.SetForever(upKey, 0); } catch (Exception e) { Log.Error(e); } RedisHelper.Remove(lockKey); } }