Exemplo n.º 1
0
        public DataResult GetCurrentWarning([FromQuery] int wId, int sId, WarningType type, WarningDataType dataType)
        {
            wId = wId == 0 ? 1 : wId;
            var result = new DataResult();

            if (HWarningHelper.CurrentData != null && HWarningHelper.CurrentData.Any())
            {
                var r = sId == 0 ? HWarningHelper.CurrentData.Where(x => x.Key.Item3 == dataType && x.Key.Item4 == type).Select(y => y.Value)
                    : HWarningHelper.CurrentData.Where(x => x.Key.Item3 == dataType && x.Key.Item4 == type).Where(y => y.Value.SetId == sId).Select(z => z.Value);
                if (r.Any())
                {
                    var sets       = WarningSetHelper.GetMenus(wId, r.Select(x => x.SetId));
                    var devices    = DeviceHelper.GetMenus(wId, r.Select(x => x.DeviceId));
                    var categories = DeviceCategoryHelper.GetMenus(wId, r.Select(x => x.CategoryId));
                    //var classes = ServerConfig.ApiDb.Query<DeviceClass>("SELECT `Id`, `Class` FROM `device_class` WHERE `MarkedDelete` = 0 AND Id IN @Id;", new
                    //{
                    //    Id = data.Select(x => x.ClassId)
                    //});
                    foreach (var current in r.OrderBy(x => x.ItemId).ThenBy(x => x.DeviceId))
                    {
                        var d = ClassExtension.ParentCopyToChild <WarningCurrent, WarningCurrentDetail>(current);
                        d.SetName = sets.FirstOrDefault(x => x.Id == d.SetId)?.Name ?? "";
                        d.Code    = devices.FirstOrDefault(x => x.Id == d.DeviceId)?.Code ?? "";
                        //d.Class = classes.FirstOrDefault(x => x.Id == d.ClassId)?.Class ?? "";
                        d.CategoryName = categories.FirstOrDefault(x => x.Id == d.CategoryId)?.Name ?? "";
                        result.datas.Add(d);
                    }
                }
            }
            return(result);
        }
        public DataResult GetDeviceCategory([FromQuery] int qId, int wId, bool menu)
        {
            var result = new DataResult();

            result.datas.AddRange(menu
                ? DeviceCategoryHelper.GetMenu(wId, qId)
                : DeviceCategoryHelper.GetDetail(wId, qId));
            if (qId != 0 && !result.datas.Any())
            {
                result.errno = Error.DeviceCategoryNotExist;
                return(result);
            }
            return(result);
        }
        public Result PutDeviceCategory([FromBody] IEnumerable <DeviceCategory> details)
        {
            if (details == null || !details.Any())
            {
                return(Result.GenError <Result>(Error.ParamError));
            }
            if (details.Any(x => x.Name.IsNullOrEmpty()))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryNotEmpty));
            }
            if (details.GroupBy(x => x.Name).Any(y => y.Count() > 1))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryDuplicate));
            }

            var wId   = details.FirstOrDefault()?.WorkshopId ?? 0;
            var sames = details.Select(x => x.Name);
            var ids   = details.Select(x => x.Id);

            if (DeviceCategoryHelper.GetHaveSame(wId, sames, ids))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryIsExist));
            }

            var cnt = DeviceCategoryHelper.Instance.GetCountByIds(ids);

            if (cnt != details.Count())
            {
                return(Result.GenError <Result>(Error.DeviceCategoryNotExist));
            }

            var markedDateTime = DateTime.Now;

            foreach (var process in details)
            {
                process.MarkedDateTime = markedDateTime;
                process.Remark         = process.Remark ?? "";
            }

            DeviceCategoryHelper.Instance.Update(details);
            return(Result.GenError <Result>(Error.Success));
        }
        public Result PostDeviceCategory([FromBody] IEnumerable <DeviceCategory> details)
        {
            if (details == null || !details.Any())
            {
                return(Result.GenError <Result>(Error.ParamError));
            }
            if (details.Any(x => x.Name.IsNullOrEmpty()))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryNotEmpty));
            }
            if (details.GroupBy(x => x.Name).Any(y => y.Count() > 1))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryDuplicate));
            }

            var wId   = details.FirstOrDefault()?.WorkshopId ?? 0;
            var sames = details.Select(x => x.Name);

            if (DeviceCategoryHelper.GetHaveSame(wId, sames))
            {
                return(Result.GenError <Result>(Error.DeviceCategoryIsExist));
            }

            var userId         = Request.GetIdentityInformation();
            var markedDateTime = DateTime.Now;

            foreach (var process in details)
            {
                process.CreateUserId   = userId;
                process.MarkedDateTime = markedDateTime;
                process.Remark         = process.Remark ?? "";
            }

            DeviceCategoryHelper.Instance.Add(details);
            return(Result.GenError <Result>(Error.Success));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="workshopId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="setId"></param>
        /// <param name="itemId"></param>
        /// <param name="warningType"></param>
        /// <param name="dataType"></param>
        /// <param name="deviceIds"></param>
        /// <param name="itemTypes"></param>
        /// <param name="isWarning">-1all 0  1 </param>
        /// <param name="setIds"></param>
        /// <param name="itemIds"></param>
        /// <returns></returns>
        public static IEnumerable <WarningLog> GetWarningLogs(int workshopId, DateTime startTime, DateTime endTime, int setId, int itemId,
                                                              WarningType warningType, WarningDataType dataType, IEnumerable <int> deviceIds, IEnumerable <WarningItemType> itemTypes, IEnumerable <int> setIds, IEnumerable <int> itemIds, int isWarning = -1)
        {
            var param = new List <string> {
                "a.WorkshopId = @workshopId"
            };

            if (startTime != default(DateTime))
            {
                param.Add("a.WarningTime >= @startTime");
            }
            if (endTime != default(DateTime))
            {
                param.Add("a.WarningTime <= @endTime");
            }
            if (setId != 0)
            {
                param.Add("a.SetId = @setId");
            }
            if (setIds != null && setIds.Any())
            {
                param.Add("a.SetId IN @setIds");
            }
            if (itemId != 0)
            {
                param.Add("a.ItemId = @itemId");
            }
            if (warningType != WarningType.默认)
            {
                param.Add("a.WarningType = @warningType");
            }
            if (dataType != WarningDataType.默认)
            {
                param.Add("a.DataType = @dataType");
            }
            if (deviceIds != null && deviceIds.Any())
            {
                param.Add("a.DeviceId IN @deviceIds");
            }
            if (isWarning != -1)
            {
                param.Add("a.IsWarning = @isWarning");
            }

            if (itemTypes != null && itemTypes.Any())
            {
                param.Add("b.ItemType IN @itemTypes");
                //var args = new List<Tuple<string, string, dynamic>>
                //{
                //    new Tuple<string, string, dynamic>("ItemType", "IN", itemTypes)
                //};
                //if (setId != 0)
                //{
                //    args.Add(new Tuple<string, string, dynamic>("SetId", "=", setId));
                //}
                //var tItemIds = WarningSetItemHelper.Instance.CommonGet<WarningSetItem>(args).Select(x => x.Id);
                //if (!tItemIds.Any())
                //{
                //    return new List<WarningLog>();
                //}
                //param.Add("a.ItemId IN @itemIds");
                //if (itemIds.Any())
                //{
                //    itemIds = tItemIds.Intersect(itemIds);
                //}
            }
            if (itemIds != null && itemIds.Any())
            {
                param.Add("a.ItemId IN @itemIds");
            }
            var r = ServerConfig.ApiDb.Query <WarningLog>(
                $"SELECT a.*, b.ItemType FROM `warning_log` a " +
                $"JOIN warning_set_item_final b ON a.ItemId = b.Id " +
                $"{(param.Any() ? $" WHERE {param.Join(" AND ")}" : "")} ORDER BY a.WarningTime DESC;",
                new { workshopId, startTime, endTime, setId, itemId, warningType, dataType, deviceIds, setIds, itemIds, itemTypes, isWarning });

            if (r != null && r.Any())
            {
                var sets       = WarningSetHelper.GetMenus(workshopId, r.Select(x => x.SetId).Distinct()).ToDictionary(x => x.Id);
                var devices    = DeviceHelper.GetMenus(workshopId, r.Select(x => x.DeviceId).Distinct()).ToDictionary(x => x.Id);
                var categories = DeviceCategoryHelper.GetMenus(workshopId, r.Select(x => x.CategoryId).Distinct()).ToDictionary(x => x.Id);
                foreach (var d in r)
                {
                    //var d = ClassExtension.ParentCopyToChild<WarningCurrent, WarningLog>(current);
                    d.SetName = sets.ContainsKey(d.SetId) ? sets[d.SetId].Name : "";
                    d.Code    = devices.ContainsKey(d.DeviceId) ? devices[d.DeviceId].Code : "";
                    //d.Class = classes.FirstOrDefault(x => x.Id == d.ClassId)?.Class ?? "";
                    d.CategoryName = categories.ContainsKey(d.CategoryId) ? categories[d.CategoryId].Name : "";
                }
            }

            return(r);
        }
Exemplo n.º 6
0
        public DataResult GetSet([FromQuery] int wId, int qId, WarningType type, WarningDataType dataType, bool first = false)
        {
            wId = wId == 0 ? 1 : wId;
            var result = new DataResult();
            var r      = ServerConfig.ApiDb.Query <WarningSetWithItems>(
                $"SELECT a.*, IFNULL(b.`Class`, '') `Class`, IFNULL(c.`StepName`, '') `StepName` FROM `warning_set` a " +
                $"LEFT JOIN `device_class` b ON a.ClassId = b.Id " +
                $"LEFT JOIN `flow_step` c ON a.StepId = c.Id " +
                $"WHERE a.`MarkedDelete` = 0 AND a.`WarningType` = @type AND a.`DataType` = @dataType{(qId == 0 ? "" : " AND a.Id = @qId")};", new { qId, type, dataType });

            var d = new List <WarningSetItemDetail>();

            if (first && r.Any())
            {
                var setId = r.FirstOrDefault().Id;
                var sql   = "";
                List <WarningSetItemDetail> s;
                switch (dataType)
                {
                case WarningDataType.设备数据:
                    var scriptId = r.FirstNonDefault().ScriptId;
                    sql = "SELECT b.*, a.VariableName Item, a.PointerAddress, a.VariableTypeId, a.Id DictionaryId " +
                          "FROM (SELECT * FROM `data_name_dictionary` WHERE ScriptId = @scriptId AND MarkedDelete = 0) a " +
                          "LEFT JOIN (SELECT * FROM `warning_set_item` WHERE SetId = @setId AND MarkedDelete = 0) b " +
                          "ON a.Id = b.DictionaryId ORDER BY a.VariableTypeId, a.PointerAddress;";
                    d.AddRange(ServerConfig.ApiDb.Query <WarningSetItemDetail>(sql, new { setId, scriptId }));
                    break;

                case WarningDataType.生产数据:
                    sql = "SELECT * FROM `warning_set_item` WHERE MarkedDelete = 0 AND SetId = @setId;";
                    s   = ServerConfig.ApiDb.Query <WarningSetItemDetail>(sql, new { setId }).ToList();
                    foreach (var val in HWarningHelper.生产数据字段汇总)
                    {
                        if (s.All(x => x.ItemType != val.Item2))
                        {
                            d.Add(new WarningSetItemDetail
                            {
                                Item      = val.Item1,
                                ItemType  = val.Item2,
                                ValueType = HWarningHelper.GetItemValueType(val.Item2)
                            });
                        }
                        else
                        {
                            var dd = s.First(x => x.Item == val.Item1);
                            dd.ValueType = HWarningHelper.GetItemValueType(val.Item2);
                            d.Add(dd);
                        }

                        d = d.OrderBy(x => HWarningHelper.生产数据字段汇总.FirstOrDefault(y => y.Item2 == x.ItemType)?.Item2).ToList();
                    }
                    break;

                case WarningDataType.故障数据:
                    //sql = "SELECT * FROM `warning_set_item` WHERE MarkedDelete = 0 AND SetId = @setId;";
                    break;

                case WarningDataType.流程卡数据:
                    sql = "SELECT * FROM `warning_set_item` WHERE MarkedDelete = 0 AND SetId = @setId;";
                    s   = ServerConfig.ApiDb.Query <WarningSetItemDetail>(sql, new { setId }).ToList();
                    foreach (var val in HWarningHelper.流程卡数据字段汇总)
                    {
                        if (s.All(x => x.ItemType != val.Item2))
                        {
                            s.Add(new WarningSetItemDetail
                            {
                                Item      = val.Item1,
                                ItemType  = val.Item2,
                                ValueType = HWarningHelper.GetItemValueType(val.Item2),
                                IsRepeat  = val.Item3,
                                IsConfig  = true
                            });
                        }
                        else
                        {
                            foreach (var dd in s.Where(x => x.ItemType == val.Item2))
                            {
                                dd.Item      = val.Item1;
                                dd.ValueType = HWarningHelper.GetItemValueType(val.Item2);
                                dd.IsRepeat  = val.Item3;
                            }
                        }
                    }

                    s = s.OrderBy(x => x.IsRepeat).ThenBy(x => x.ItemType).ThenBy(x => x.Id).ToList();
                    d.AddRange(s);
                    break;
                }

                if (!sql.IsNullOrEmpty())
                {
                    r.FirstOrDefault().Items.AddRange(d);
                }
            }

            if (r.Any())
            {
                var device     = DeviceHelper.GetMenus(wId, r.SelectMany(x => x.DeviceList).Distinct()).OrderBy(x => x.Id);
                var categories = DeviceCategoryHelper.GetMenus(wId, r.Select(x => x.CategoryId).Distinct()).OrderBy(x => x.Id);
                var script     = ScriptVersionHelper.GetMenus(wId, r.Select(x => x.ScriptId).Distinct()).OrderBy(x => x.Id);
                foreach (var dd in r)
                {
                    dd.CodeList     = device.Where(x => dd.DeviceList.Contains(x.Id)).Select(y => new Tuple <int, string>(y.Id, y.Code));
                    dd.Script       = script.FirstOrDefault(x => x.Id == dd.ScriptId)?.Name ?? "";
                    dd.CategoryName = categories.FirstOrDefault(x => x.Id == dd.CategoryId)?.Name ?? "";
                }
            }
            if (qId != 0 && !r.Any())
            {
                return(Result.GenError <DataResult>(Error.WarningSetNotExist));
            }
            result.datas.AddRange(r);
            return(result);
        }