Query.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:22k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using OThinker.H3.WorkItem;
  11. using OThinker.H3.WorkflowTemplate;
  12. using OThinker.H3.Instance;
  13. namespace OThinker.H3.Portal
  14. {
  15.     /// <summary>
  16.     /// 原来查询数据库
  17.     /// </summary>
  18.     public class Query
  19.     {
  20.         public static System.Data.DataTable QueryWorkflow(string[] Units)
  21.         {
  22.             string sql =
  23.                 "SELECT " +
  24.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_WorkflowPackage + " AS " + OThinker.H3.WorkflowTemplate.WorkflowTemplate.PropertyName_WorkflowPackageName + ", " + 
  25.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_WorkflowName + " AS " +OThinker.H3.WorkflowTemplate.WorkflowTemplate.PropertyName_WorkflowName + ", " + 
  26.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_DefaultVersion + " AS " + OThinker.H3.WorkflowTemplate.WorkflowTemplate.PropertyName_WorkflowVersion + " " +
  27.                 "FROM " +
  28.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + " INNER JOIN " + OThinker.H3.Acl.WorkflowAcl.TableName + " " +
  29.                 "ON " +
  30.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_WorkflowPackage + "=" + OThinker.H3.Acl.WorkflowAcl.TableName + "." + OThinker.H3.Acl.WorkflowAcl.PropertyName_WorkflowPackage + " AND " +
  31.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_WorkflowName + "=" + OThinker.H3.Acl.WorkflowAcl.TableName + "." + OThinker.H3.Acl.WorkflowAcl.PropertyName_WorkflowName + " AND " +
  32.                     OThinker.H3.WorkflowTemplate.WorkflowClause.TableName + "." + OThinker.H3.WorkflowTemplate.WorkflowClause.PropertyName_StateFlag + "&" + OThinker.H3.WorkflowTemplate.WorkflowState.ORIGINATOR_VISIBLE_MASK + ">0" + " AND " +
  33.                     "(" + OThinker.H3.Acl.WorkflowAcl.PropertyName_Administrator + "=1" + " OR " + OThinker.H3.Acl.WorkflowAcl.PropertyName_CreateInstance + "=1)" + " AND " +
  34.                     GroupIn(OThinker.H3.Acl.WorkflowAcl.PropertyName_UserID, Units);
  35.             return QueryTable(sql);
  36.         }
  37.         public static System.Data.DataTable QueryInstanceByData(
  38.             string WorkflowPackage,
  39.             string WorkflowName,
  40.             int WorkflowVersion,
  41.             string DataItem, 
  42.             string Comparison, 
  43.             string Originator)
  44.         {
  45.             OThinker.H3.WorkflowTemplate.WorkflowTemplate template = OThinker.H3.Server.Engine.WorkflowManager.GetWorkflow(WorkflowPackage, WorkflowName, WorkflowVersion);
  46.             if (template == null)
  47.             {
  48.                 return null;
  49.             }
  50.             
  51.             if (template.GetDataItem(DataItem) == null)
  52.             {
  53.                 return null;
  54.             }
  55.             string dataSql = 
  56.                 "SELECT " + OThinker.H3.Data.InternalData.PropertyName_InstanceId + " " + 
  57.                 "FROM [" + template.DataTableName + "] " + 
  58.                 "WHERE [" + DataItem + "]" + Comparison;
  59.             string[] conditions = new string[]{
  60.                 ((Originator == null)?null: (Instance.InstanceContext.PropertyName_Originator + "='" + Originator + "'")), 
  61.                 Instance.InstanceContext.PropertyName_ObjectID + " IN (" + dataSql + ")"};
  62.             string sql = GroupSql(InstanceContext.TableName, conditions, InstanceContext.PropertyName_CreatedTime, false);
  63.             return QueryTable(sql);
  64.         }
  65.         public static System.Data.DataTable QueryToken(string InstanceId)
  66.         {
  67.             if (InstanceId == null || InstanceId == "")
  68.             {
  69.                 return null;
  70.             }
  71.             else
  72.             {
  73.                 string[] conditions = new string[] { OThinker.H3.Instance.Token.PropertyName_InstanceId + "='" + InstanceId + "'" };
  74.                 string sql = GroupSql(OThinker.H3.Instance.Token.TableName, conditions, Token.PropertyName_TokenId, true);
  75.                 return QueryTable(sql);
  76.             }
  77.         }
  78.         public static System.Data.DataTable QueryHandledInstance(
  79.             string WorkflowPackage,
  80.             string WorkflowName,
  81.             int WorkflowVersion,
  82.             OThinker.H3.Instance.InstanceState InstanceState,
  83.             System.DateTime CreatedTime_From,
  84.             System.DateTime CreatedTime_To,
  85.             string Participant)
  86.         {
  87.             if (Participant == null || Participant == "")
  88.             {
  89.                 return null;
  90.             }
  91.             // 经办的条件
  92.             string handledCondition = 
  93.                 "[" + InstanceContext.PropertyName_ObjectID + "] IN (" +
  94.                     "SELECT DISTINCT [" + WorkItem.WorkItem.PropertyName_InstanceId + "] FROM [" + WorkItem.WorkItem.TableName + "] WHERE " + 
  95.                         "[" + WorkItem.WorkItem.PropertyName_Participant + "]='" + Participant + "'" + " AND " + 
  96.                         "[" + WorkItem.WorkItem.PropertyName_State + "]=" + (int)WorkItem.WorkItemState.Finished + ")";
  97.             // 非发起的条件
  98.             string notOriginateCondition = InstanceContext.PropertyName_Originator + "!='" + Participant + "'";
  99.             string[] conditions = new string[]{
  100.                 handledCondition, 
  101.                 notOriginateCondition, 
  102.                 ((WorkflowPackage == null)?null:(InstanceContext.PropertyName_WorkflowPackage + "='" + WorkflowPackage + "'")), 
  103.                 ((WorkflowName == null)? null: (InstanceContext.PropertyName_WorkflowName + "='" + WorkflowName + "'")), 
  104.                 ((WorkflowVersion == WorkflowTemplate.WorkflowTemplate.NullWorkflowVersion || WorkflowVersion == OThinker.H3.WorkflowTemplate.WorkflowTemplate.AllWorkflowVersion)? null: (WorkItem.WorkItem.PropertyName_WorkflowVersion + "='" + WorkflowVersion + "'")), 
  105.                 BuildState(InstanceState), 
  106.                 ((CreatedTime_From == System.DateTime.MinValue)?null: (InstanceContext.PropertyName_CreatedTime + ">'" + FormatFrom(CreatedTime_From) + "'")), 
  107.                 ((CreatedTime_To == System.DateTime.MaxValue)?null:(InstanceContext.PropertyName_CreatedTime + "<='" + FormatTo(CreatedTime_To) + "'")), 
  108.             };
  109.             string sql = GroupSql(InstanceContext.TableName, conditions, InstanceContext.PropertyName_CreatedTime, false);
  110.             return QueryTable(sql);
  111.         }
  112.         /// <summary>
  113.         /// 如果输入为Initiated、Starting:、Running、Finishing或者Complete,那么安装这些状态进行查找
  114.         /// 如果输入为Exceptional,则检索Exceptional == true
  115.         /// 如果输入为Suspended,则检索Suspended == true
  116.         /// 如果输入为Unfinished,则检索State != Complete
  117.         /// 如果输入为Unspecified,则无检索条件
  118.         /// </summary>
  119.         /// <param name="State"></param>
  120.         /// <returns></returns>
  121.         private static string BuildState(InstanceState InstanceState)
  122.         {
  123.             switch (InstanceState)
  124.             {
  125.                 case OThinker.H3.Instance.InstanceState.Initiated:
  126.                 case OThinker.H3.Instance.InstanceState.Starting:
  127.                 case OThinker.H3.Instance.InstanceState.Running:
  128.                 case OThinker.H3.Instance.InstanceState.Finishing:
  129.                 case OThinker.H3.Instance.InstanceState.Complete:
  130.                     return InstanceContext.PropertyName_State + "=" + (int)InstanceState;
  131.                 case OThinker.H3.Instance.InstanceState.Exceptional:
  132.                     // 如果输入为Exceptional状态,那么检索Exceptional属性是否为true
  133.                     return InstanceContext.PropertyName_Exceptional + "=" + 1;
  134.                 case OThinker.H3.Instance.InstanceState.Suspended:
  135.                     // 如果输入为Suspended状态,那么检索Suspended属性是否为true
  136.                     return InstanceContext.PropertyName_Suspended + "=" + 1;
  137.                 case OThinker.H3.Instance.InstanceState.Unfinished:
  138.                     return InstanceContext.PropertyName_State + "!=" + (int)Instance.InstanceState.Complete;
  139.                 case OThinker.H3.Instance.InstanceState.Unspecified:
  140.                     return null;
  141.                 default:
  142.                     throw new NotImplementedException();
  143.             }
  144.         }
  145.         public static System.Data.DataTable QueryInstance(
  146.             string[] Instances,
  147.             string[] Originators,
  148.             string OU,
  149.             string ParentInstanceID,
  150.             OThinker.H3.Instance.InstanceState InstanceState,
  151.             string WorkflowPackage,
  152.             string WorkflowName,
  153.             int WorkflowVersion,
  154.             System.DateTime CreatedTime_From,
  155.             System.DateTime CreatedTime_To,
  156.             string InstanceName,
  157.             PriorityType Priority,
  158.             OThinker.Data.BoolMatchValue Approval,
  159.             OThinker.Data.BoolMatchValue Elapsed)
  160.         {
  161.             string instanceCondition = GroupIn(InstanceContext.PropertyName_ObjectID, Instances);
  162.             string originatorCondition = GroupIn(InstanceContext.PropertyName_Originator, Originators);
  163.             string stateCondition = BuildState(InstanceState);
  164.             string[] conditions = new string[]{
  165.                 instanceCondition, 
  166.                 originatorCondition, 
  167.                 ((OU == null)?null: (InstanceContext.PropertyName_OrgUnit + "='" + OU + "'")), 
  168.                 ((ParentInstanceID == null)?null:(InstanceContext.PropertyName_ParentInstanceID + "='" + ParentInstanceID + "'")), 
  169.                 stateCondition, 
  170.                 ((WorkflowPackage == null)?null:(InstanceContext.PropertyName_WorkflowPackage + "='" + WorkflowPackage + "'")), 
  171.                 ((WorkflowName == null)? null: (InstanceContext.PropertyName_WorkflowName + "='" + WorkflowName + "'")), 
  172.                 ((WorkflowVersion == WorkflowTemplate.WorkflowTemplate.NullWorkflowVersion || WorkflowVersion == OThinker.H3.WorkflowTemplate.WorkflowTemplate.AllWorkflowVersion)? null: (WorkItem.WorkItem.PropertyName_WorkflowVersion + "='" + WorkflowVersion + "'")),
  173.                 ((CreatedTime_From == System.DateTime.MinValue)?null: (InstanceContext.PropertyName_CreatedTime + ">'" + FormatFrom(CreatedTime_From) + "'")), 
  174.                 ((CreatedTime_To == System.DateTime.MaxValue)?null:(InstanceContext.PropertyName_CreatedTime + "<='" + FormatTo(CreatedTime_To) + "'")), 
  175.                 ((InstanceName==null)?null:(InstanceContext.PropertyName_InstanceName  + " LIKE '%" + InstanceName + "%'")), 
  176.                 ((Priority == PriorityType.Unspecified)?null: (InstanceContext.PropertyName_Priority + "=" + (int)Priority)), 
  177.                 ((Approval == OThinker.Data.BoolMatchValue.Unspecified)? null:InstanceContext.PropertyName_Approval + "=" +(int)Approval), 
  178.                 ((Elapsed == OThinker.Data.BoolMatchValue.Unspecified)? null:InstanceContext.PropertyName_PlanFinishTime + (Elapsed == OThinker.Data.BoolMatchValue.True? "<" : ">=") + "'" +System.DateTime.Now + "'")
  179.                 };
  180.             string sql = GroupSql(InstanceContext.TableName, conditions, InstanceContext.PropertyName_CreatedTime, false);
  181.             return QueryTable(sql);
  182.         }
  183.         public static System.Data.DataTable QueryInstanceByEstimatedElapsed(
  184.             string Originator,
  185.             OThinker.Data.BoolMatchValue EstimatedElapsed)
  186.         {
  187.             string stateCondition = BuildState(InstanceState.Unfinished);
  188.             string[] conditions = new string[]{
  189.                 stateCondition, 
  190.                 ((Originator == null)?null:(InstanceContext.PropertyName_Originator + "='" + Originator + "'")), 
  191.                 ((EstimatedElapsed == OThinker.Data.BoolMatchValue.Unspecified)? null:InstanceContext.PropertyName_PlanFinishTime + (EstimatedElapsed == OThinker.Data.BoolMatchValue.True? "<" : ">=") + "[" + InstanceContext.PropertyName_EstimatedFinishTime + "]")};
  192.             string sql = GroupSql(InstanceContext.TableName, conditions, InstanceContext.PropertyName_EstimatedFinishTime, false);
  193.             return QueryTable(sql);
  194.         }
  195.         public static System.Data.DataTable QueryInstanceByRecurrences(
  196.             string Originator,
  197.             int LeastRecurrences)
  198.         {
  199.             string stateCondition = BuildState(InstanceState.Unfinished);
  200.             string[] conditions = new string[]{
  201.                 stateCondition, 
  202.                 ((Originator == null)?null:(InstanceContext.PropertyName_Originator + "='" + Originator + "'")), 
  203.                 ((LeastRecurrences <= 0)?null: InstanceContext.PropertyName_Recurrences + ">=" + LeastRecurrences)};
  204.             string sql = GroupSql(InstanceContext.TableName, conditions, InstanceContext.PropertyName_Recurrences, false);
  205.             return QueryTable(sql);
  206.         }
  207.         public static System.Data.DataTable QueryWorkItem(
  208.             string WorkflowPackage,
  209.             string WorkflowName,
  210.             int WorkflowVersion,
  211.             string ActivityName,
  212.             string[] Instances,
  213.             string[] Participants,
  214.             System.DateTime From,
  215.             System.DateTime To,
  216.             OThinker.H3.WorkItem.WorkItemState State,
  217.             OThinker.H3.Instance.PriorityType Priority,
  218.             string InstanceName,
  219.             string Receiptor,
  220.             OThinker.Data.BoolMatchValue Approval, 
  221.             OThinker.Data.BoolMatchValue Elapsed)
  222.         {
  223.             string instanceCondition = GroupIn(WorkItem.WorkItem.PropertyName_InstanceId, Instances);
  224.             string participantCondition = GroupIn(WorkItem.WorkItem.PropertyName_Participant, Participants);
  225.             string stateCondition = null;
  226.             switch (State)
  227.             {
  228.                 case WorkItemState.Waiting:
  229.                 case WorkItemState.Working:
  230.                 case WorkItemState.Finished:
  231.                 case WorkItemState.Canceled:
  232.                     stateCondition = WorkItem.WorkItem.PropertyName_State + "=" + (int)State;
  233.                     break;
  234.                 case WorkItemState.Unfinished:
  235.                     stateCondition = WorkItem.WorkItem.PropertyName_State + "!=" + (int)WorkItemState.Finished + " AND " + WorkItem.WorkItem.PropertyName_State + "!=" + (int)WorkItemState.Canceled;
  236.                     break;
  237.                 case WorkItemState.NotCanceled:
  238.                     stateCondition = WorkItem.WorkItem.PropertyName_State + "!=" + (int)WorkItemState.Canceled;
  239.                     break;
  240.                 case WorkItemState.Unspecified:
  241.                     break;
  242.                 default:
  243.                     throw new NotImplementedException();
  244.             }
  245.             string[] conditions =
  246.                 new string[]{
  247.                     ((WorkflowPackage == null)? null: (WorkItem.WorkItem.PropertyName_WorkflowPackage + "='" + WorkflowPackage + "'")), 
  248.                     ((WorkflowName == null)? null: (WorkItem.WorkItem.PropertyName_WorkflowName + "='" + WorkflowName + "'")), 
  249.                     ((WorkflowVersion == WorkflowTemplate.WorkflowTemplate.NullWorkflowVersion|| WorkflowVersion == OThinker.H3.WorkflowTemplate.WorkflowTemplate.AllWorkflowVersion)? null: (WorkItem.WorkItem.PropertyName_WorkflowVersion + "='" + WorkflowVersion + "'")), 
  250.                     ((ActivityName == null)? null: (OThinker.H3.WorkItem.WorkItem.PropertyName_ActivityName + "='" + ActivityName + "'")), 
  251.                     instanceCondition, 
  252.                     participantCondition, 
  253.                     ((From == System.DateTime.MinValue)?null: (WorkItem.WorkItem.PropertyName_ReceiveTime + ">'" + FormatFrom(From) + "'")), 
  254.                     ((To == System.DateTime.MaxValue)?null: (WorkItem.WorkItem.PropertyName_ReceiveTime + "<='" + FormatTo(To) + "'")), 
  255.                     stateCondition, 
  256.                     ((Priority == OThinker.H3.Instance.PriorityType.Unspecified)?null: OThinker.H3.WorkItem.WorkItem.PropertyName_Priority + "=" + (int)Priority), 
  257.                     ((InstanceName == null)? null: (WorkItem.WorkItem.PropertyName_InstanceName + " LIKE '%" + InstanceName + "%'")), 
  258.                     ((Receiptor == null)?null: WorkItem.WorkItem.PropertyName_Receiptor + "='" + Receiptor + "'"),
  259.                     ((Approval == OThinker.Data.BoolMatchValue.Unspecified)? null:WorkItem.WorkItem.PropertyName_Approval + "=" +(int)Approval), 
  260.                     ((Elapsed == OThinker.Data.BoolMatchValue.Unspecified)? null:WorkItem.WorkItem.PropertyName_PlanFinishTime + (Elapsed == OThinker.Data.BoolMatchValue.True? "<" : ">=") + "'" +System.DateTime.Now + "'")};
  261.             string sql = GroupSql(WorkItem.WorkItem.TableName, conditions, WorkItem.WorkItem.PropertyName_ReceiveTime, false);
  262.             return QueryTable(sql);
  263.         }
  264.         /// <summary>
  265.         /// 将搜索条件组合成ColumnName IN ('...', '...', ... )这种模式
  266.         /// </summary>
  267.         /// <param name="ColumnName"></param>
  268.         /// <param name="Values"></param>
  269.         /// <returns>如果Values为null,那么返回null,如果Length为0,则返回 IN (),如果为1,则返回ColumnName=...,否则返回ColumnName IN ('...', '...', ...)</returns>
  270.         private static string GroupIn(string ColumnName, string[] Values)
  271.         {
  272.             if (Values == null)
  273.             {
  274.                 return null;
  275.             }
  276.             else if (Values.Length == 0)
  277.             {
  278.                 return "1=2";
  279.             }
  280.             else if (Values.Length == 1)
  281.             {
  282.                 if (Values[0] == null)
  283.                 {
  284.                     return ColumnName + " is null";
  285.                 }
  286.                 else
  287.                 {
  288.                     return ColumnName + "='" + Values[0] + "'";
  289.                 }
  290.             }
  291.             else
  292.             {
  293.                 System.Text.StringBuilder builder = new System.Text.StringBuilder();
  294.                 foreach (string v in Values)
  295.                 {
  296.                     if (builder.Length != 0)
  297.                     {
  298.                         builder.Append(", ");
  299.                     }
  300.                     if (v == null)
  301.                     {
  302.                         builder.Append("null");
  303.                     }
  304.                     else
  305.                     {
  306.                         builder.Append("'" + v + "'");
  307.                     }
  308.                 }
  309.                 builder.Insert(0, ColumnName + " IN (");
  310.                 builder.Append(")");
  311.                 return builder.ToString();
  312.             }
  313.         }
  314.         private static string GroupSql(string TableName, string[] Conditions, string OrderBy, bool Ascending)
  315.         {
  316.             return "SELECT * FROM " + TableName + " " + GroupConditions(Conditions) + (OrderBy == null ? null : " ORDER BY " + OrderBy + (Ascending ? null : " DESC"));
  317.         }
  318.         private static string GroupConditions(string[] Conditions)
  319.         {
  320.             if (Conditions == null || Conditions.Length == 0)
  321.             {
  322.                 return null;
  323.             }
  324.             else
  325.             {
  326.                 System.Text.StringBuilder builder = new System.Text.StringBuilder();
  327.                 foreach (string condition in Conditions)
  328.                 {
  329.                     if (condition != null && condition != "")
  330.                     {
  331.                         if (builder.Length != 0)
  332.                         {
  333.                             builder.Append(" AND ");
  334.                         }
  335.                         builder.Append(condition);
  336.                     }
  337.                 }
  338.                 if (builder.Length != 0)
  339.                 {
  340.                     builder.Insert(0, "WHERE ");
  341.                 }
  342.                 return builder.ToString();
  343.             }
  344.         }
  345.         private static System.Data.DataTable QueryTable(string Sql)
  346.         {
  347.             OThinker.Data.Database.CommandFactory factory = new OThinker.Data.Database.CommandFactory(OThinker.H3.Configs.Config.Current.DataBase.DBType, OThinker.H3.Configs.Config.Current.DataBase.DBConnString);
  348.             OThinker.Data.Database.ICommand command = factory.CreateCommand();
  349.             System.Data.DataTable table = command.ExecuteDataTable(Sql);
  350.             command.Commit();
  351.             return table;
  352.         }
  353.         private static System.DateTime FormatFrom(System.DateTime From)
  354.         {
  355.             // 检查参数的合法性,如果不合法则重新设置参数
  356.             System.DateTime from;
  357.             // 将Sql的DateTime里面的最小值转换成System.DateTime类型
  358.             System.DateTime sqlMin = System.DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MinValue.ToString());
  359.             if (sqlMin > From)
  360.             {
  361.                 from = sqlMin;
  362.             }
  363.             else
  364.             {
  365.                 from = From;
  366.             }
  367.             return from;
  368.         }
  369.         private static System.DateTime FormatTo(System.DateTime To)
  370.         {
  371.             // 检查参数的合法性,如果不合法则重新设置参数
  372.             System.DateTime to;
  373.             // 将Sql的DateTime里面的最大值转换成System.DateTime类型
  374.             System.DateTime sqlMax = System.DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());
  375.             if (sqlMax < To)
  376.             {
  377.                 to = sqlMax;
  378.             }
  379.             else
  380.             {
  381.                 to = To;
  382.             }
  383.             return to;
  384.         }
  385.     }
  386. }