//通过产品获取工艺的信息
        //http://siemensopds31/sit-svc/Application/AppU4DM/odata/Process?$expand=AsPlannedBOP($select=Id),FinalMaterialId($expand=Material($select=NId))&$filter=FinalMaterialId/Material/NId eq 'A1003860348'
        public string GetProcessByFinalMaterialId(string token, string finalMaterialId)
        {
            string  json = OData4.QueryData(token, "Process", "?$expand=AsPlannedBOP($select=Id),FinalMaterialId($expand=Material($select=NId))&$filter=FinalMaterialId/Material/NId eq '" + finalMaterialId + "'");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //通过工单工艺获取unit的信息
        //http://opds31/sit-svc/Application/AppU4DM/odata/UADMGetFullEquipment(function=@x)?@x={"OperationId":"322db10f-cc19-4a80-80b0-e61d19155630"}&$top=10&$skip=0&$orderby=Preferred asc&$count=true
        public string GetTerminalIdFromWorkOperation(string token, string OP_ID)
        {
            string  json = OData4.QueryData(token, "UADMGetFullEquipment", "(function=@x)?@x={\"OperationId\":\"" + OP_ID + "\"}&$top=10&$skip=0&$orderby=Preferred asc&$count=true");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://SiemensOpds32/sit-svc/Application/AppU4DM/odata/WorkOrder?$select=NId&$filter=startswith(NId,'KI202007')&$orderby=NId desc
        public string GetMaxOrderId(string token, string prekey)
        {
            string  json = OData4.QueryData(token, "WorkOrder", "?$select=NId&$filter=startswith(NId,'" + prekey + "')&$orderby=NId desc&$top=1");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/MaterialTrackingUnit?$filter=NId eq 'SN2020000001'&$select=Id,NId
        public string GetMaterialTrackingUnit(string token, string serialnumber)
        {
            string  json = OData4.QueryData(token, "MaterialTrackingUnit", "?$filter=NId eq '" + serialnumber + "'&$select=Id,NId,MaterialNId");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //通过工单和工艺取得工序的Id
        //http://opds31/sit-svc/Application/AppU4DM/odata/WorkOrderOperation?$filter=WorkOrder_Id eq dae7d1bd-5219-49d6-93d2-3e77b4ccdb68 and Name eq 'NC-MACHINING'&$orderby=Sequence asc&$select=Id,NId
        public string GetOperationIdByOrderAndOperationName(string token, string WO_ID, string operationName)
        {
            string  json = OData4.QueryData(token, "WorkOrderOperation", "?$filter=WorkOrder_Id eq " + WO_ID + " and Name eq '" + operationName + "'&$orderby=Sequence asc&$select=Id,NId");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //get simulation orders
        //http://siemensopds31/sit-svc/Application/AppU4DM/odata/WorkOrder?$expand=FinalMaterial($expand=Material($select=NId))&$filter=startswith(NId, 'WO')&$orderby=Sequence asc&$select=Id,NId,Sequence,InitialQuantity,Status
        public string GetPsOrders(string token, string pre_orderId)
        {
            string  json = OData4.QueryData(token, "WorkOrder", "?$expand=FinalMaterial($expand=Material($select=NId))&$filter=startswith(NId, '" + pre_orderId + "')&$orderby=Sequence asc&$select=Id,NId,Sequence,InitialQuantity,Status");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //get serial nubmers by wo id
        //http://siemensopds31/sit-svc/Application/AppU4DM/odata/ProducedMaterialItem?$expand=DM_MaterialTrackingUnit($expand=MaterialTrackingUnit($select=NId))&$select=Id&$filter=WorkOrder_Id eq 307f0069-8189-4e38-81bd-84117dd4be97
        public string GetSerialNumberByWOId(string token, string WO_ID)
        {
            string  json = OData4.QueryData(token, "ProducedMaterialItem", "?$expand=DM_MaterialTrackingUnit($expand=MaterialTrackingUnit($select=NId))&$select=Id&$filter=WorkOrder_Id eq " + WO_ID + "");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/DM_MaterialTrackingUnit?$filter=MaterialTrackingUnit_Id eq b0db828b-f58f-4a2d-b2f5-83689f860b4b&$select=Id
        public string GetDMMaterialTrackingUnit(string token, string MTU_ID)
        {
            string  json = OData4.QueryData(token, "DM_MaterialTrackingUnit", "?$filter=MaterialTrackingUnit_Id eq " + MTU_ID + "&$select=Id");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/ToBeConsumedMaterial?&$orderby=Sequence%20asc&$count=true&$filter=WorkOrderOperation_Id eq 9c1c15ed-5634-4eeb-9f8e-e40101904b5d&$select=NId
        public string GetAssemblyMaterials(string token, string wo_op_id)
        {
            string  json = OData4.QueryData(token, "ToBeConsumedMaterial", "?&$orderby=Sequence%20asc&$count=true&$filter=WorkOrderOperation_Id eq " + wo_op_id + "&$select=NId");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://siemensopds31/sit-svc/Application/AppU4DM/odata/SetPointItem?$filter= NId eq 'RecordTime'&$select=Id,SetPoint_Id
        public string GetSetPointItem(string token, string item)
        {
            string  json = OData4.QueryData(token, "SetPointItem", "?$filter= NId eq '" + item + "'&$select=Id,SetPoint_Id");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/ItlkCheckWOOpAssociation?$filter=WorkOrderOperation_Id eq 322db10f-cc19-4a80-80b0-e61d19155630&$select=Id,WorkOrderOperation_Id&$top=0&$count=true
        public string ItlkCheckWOOpAssociation(string token, string WO_ID, string OrderID)
        {
            string  json = OData4.QueryData(token, "ItlkCheckWOOpAssociation", "?$filter=WorkOrderOperation_Id eq " + WO_ID + "&$select=Id,WorkOrderOperation_Id&$top=0&$count=true");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("Error", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://siemensopds31/sit-svc/Application/AppU4DM/odata/WorkOrderOperation?$expand=WorkOrder($select=NId)&$filter=WorkOrder/NId eq 'WO-00000008'&$orderby=Sequence desc&$top=1&$select=TargetQuantity,ProducedQuantity
        public string GetOrderProgress(string token, string OrderId)
        {
            string  json = OData4.QueryData(token, "WorkOrderOperation", "?$expand=WorkOrder($select=NId)&$filter=WorkOrder/NId eq '" + OrderId + "'&$orderby=Sequence desc&$top=1&$select=TargetQuantity,ProducedQuantity");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/ProducedMaterialItem?$filter=DM_MaterialTrackingUnit_Id eq 7e4cdd14-6aa3-46a3-8875-562a3dc95752&$select=Id,WorkOrder_Id
        public string GetWorkOrderID(string token, string DM_MTU_ID)
        {
            string  json = OData4.QueryData(token, "ProducedMaterialItem", "?$filter=DM_MaterialTrackingUnit_Id eq " + DM_MTU_ID + "&$select=Id,WorkOrder_Id");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/WorkOrder?$filter=NId eq 'WO-2020-0001'&$select=Id
        public string GetWorkOrderByOrderId(string token, string stringOrderId)
        {
            string json = OData4.QueryData(token, "WorkOrder", "?$filter=NId eq '" + stringOrderId + "'&$select=Id");

            JObject jb = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                return(res_value.ToString());
            }
            return(null);
        }
        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <returns>返回ReturnMesasge对象,角色在Results中</returns>
        public static ReturnMessage GetUser(string username)
        {
            ReturnMessage msg = new ReturnMessage {
                Succeed = false
            };

            try
            {
                string result = OData4.QueryData(Token, "User", "$filter=(Name eq '" + username + "')", DataScope.Engineering);
                if (!string.IsNullOrEmpty(result))
                {
                    msg.Succeed = true;
                    msg.Result  = result;
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <returns>返回ReturnMesasge对象,角色在Results中</returns>
        public static ReturnMessage GetFullUsers()
        {
            ReturnMessage msg = new ReturnMessage {
                Succeed = false
            };

            try
            {
                string message = OData4.QueryData(Token, "User", "", DataScope.Engineering);
                if (!string.IsNullOrEmpty(message))
                {
                    msg.Succeed = true;
                    msg.Message = message;
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
        /// <summary>
        /// 获取所有的角色信息
        /// </summary>
        /// <returns>返回ReturnMesasge对象,角色在Results中</returns>
        public static ReturnMessage GetFullRoles()
        {
            ReturnMessage msg = new ReturnMessage {
                Succeed = false
            };

            try
            {
                string result = OData4.QueryData(Token, "Role", "", DataScope.Engineering);
                if (!string.IsNullOrEmpty(result))
                {
                    msg.Succeed = true;
                    msg.Result  = result;
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
        //http://opfn30/sit-svc/engineering/odata/UserRoleAssociation?$expand=Role&$filter=Role/Id%20eq%205ec5e188-da5f-45a5-8d31-4ab0d866c9b1
        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <returns>返回ReturnMesasge对象,角色在Results中</returns>
        public static ReturnMessage GetUserRoleAssociation(string userid, string rolename)
        {
            ReturnMessage msg = new ReturnMessage {
                Succeed = false
            };

            try
            {
                string message = OData4.QueryData(Token, "UserRoleAssociation", "$expand=Role&$filter=(Role/Name eq '" + rolename + "') And (UserID eq '" + userid + "')", DataScope.Engineering);
                if (!string.IsNullOrEmpty(message))
                {
                    msg.Succeed = true;
                    msg.Message = message;
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
        //获取工单下所有Operation的信息,相当于Entry
        //http://opds31/sit-svc/Application/AppU4DM/odata/WorkOrderOperation?$filter=WorkOrder_Id eq dae7d1bd-5219-49d6-93d2-3e77b4ccdb68&$orderby=Sequence asc&$select=Id
        public string GetOrderFirstOperationId(string token, string WO_ID)
        {
            string  json = OData4.QueryData(token, "WorkOrderOperation", "?$filter=WorkOrder_Id eq " + WO_ID + "&$orderby=Sequence asc&$select=Id");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                JArray jaa = JArray.Parse(res_value.ToString());
                string woOperationId;
                if (jaa.Count == 0)
                {
                    woOperationId = null;
                }
                else
                {
                    woOperationId = jaa[0]["Id"].ToString();
                }
                return(woOperationId);
            }
            return(null);
        }
        //通过Unit的信息获取LineId的信息值
        //http://opds31/sit-svc/Application/Equipment/odata/EquipmentGraphLink?$expand=Source($select=EquipmentNId),Destination($select=EquipmentNId)&$filter=Destination/EquipmentNId eq 'SE1L1-NC-MACHINING'&$select=Id
        public string GetLineIdByUnit(string token, string unit)
        {
            string  json = OData4.QueryData(token, "EquipmentGraphLink", "?$expand=Source($select=EquipmentNId),Destination($select=EquipmentNId)&$filter=Destination/EquipmentNId eq '" + unit + "'&$select=Id");
            JObject jb   = JObject.Parse(json);
            JToken  res_value;

            if (jb.TryGetValue("value", out res_value))
            {
                JArray jaa = JArray.Parse(res_value.ToString());
                string woOperationId;
                if (jaa.Count == 0)
                {
                    woOperationId = null;
                }
                else
                {
                    woOperationId = jaa[0]["Source"]["EquipmentNId"].ToString();
                }
                return(woOperationId);
            }
            return(null);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/WorkOrderOperation/322db10f-cc19-4a80-80b0-e61d19155630?$select=ProducedQuantity,TargetQuantity
        //{
        //"@odata.context": "http://opds31/sit-svc/application/AppU4DM/odata/$metadata#WorkOrderOperation(ProducedQuantity,TargetQuantity)/$entity",
        //"ProducedQuantity": 4.000,
        //"TargetQuantity": 10.000}
        public string GetOrderOperationProcessQty(string token, string wo_op_id)
        {
            string json = OData4.QueryData(token, "WorkOrderOperation", "/" + wo_op_id + "?$select=ProducedQuantity,TargetQuantity");

            return(json);
        }
        //http://opds31/sit-svc/Application/AppU4DM/odata/WorkOrder/dae7d1bd-5219-49d6-93d2-3e77b4ccdb68?$select=Id,Nid
        public string GetWorkOrder(string token, string WO_ID)
        {
            string json = OData4.QueryData(token, "WorkOrder", "/" + WO_ID + "?$select=Id,Nid,Status");

            return(json);
        }