using AnfiniL.SqlServerTools.Data;
using SqlServerTools;
using SqlServerTools.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;namespace windowsApiAcitonSimulation.Help
{
    public class DBProfiler
    {
        public static void StartSqlServerProfiler()
        {
            IProfiler profiler = ToolsFactory.Instance.CreateProfiler("192.168.0.83,9920", "sa", "zdsoft123", null);
            profiler.Initialize(TraceOptions.FileRollover, DateTime.Now.ToString(".yyyy.MM.dd.HH.mm.ss"));
            var TraceFieldArr = new TraceField[] {
                    TraceField.TextData,
                   TraceField.StartTime,
                   TraceField.Writes,
                   TraceField.Reads,
                   TraceField.ApplicationName
                };
            TraceEventProperties eventPropSql = new TraceEventProperties()
            {
                Event = TraceEvent.SQLBatchCompleted,
                Fields = TraceFieldArr
            };
            TraceEventProperties eventPropSP = new TraceEventProperties()
            {
                Event = TraceEvent.RPCCompleted,
                Fields = TraceFieldArr
            };
            profiler.AddTraceEvent(eventPropSql.Event, eventPropSql.Fields);
            profiler.AddTraceEvent(eventPropSP.Event, eventPropSP.Fields);
            var filter = new FilterProperties(
                   TraceField.ApplicationName,
                   (ComparisonOperator)Enum.Parse(typeof(ComparisonOperator), "Equal"),
                   "cszhidacysoft");
            if (filter.CheckFilter())
            {
                profiler.AddTraceFilter(filter.Field, LogicalOperator.AND, filter.Operator, filter.TypedValue);
            }
            profiler.TraceEvent += profiler_TraceEvent;
            profiler.Start();
        }
        private static void profiler_TraceEvent(object sender, TraceEventArgs e)
        {
            if (e.EventsTable.Rows.Count > 0)
            {
                foreach (DataRow row in e.EventsTable.Rows)
                {
                    if (row["TextData"].ToString().IndexOf("insert into ba_consume(vcvipno,vcNo,vcCust_c,vcCust_n,iPeople,vcTable_c,vcTable_n,vcTimes,vcServer,dtTime,vcMemo,vcOper,dtOper,vcSaler,cType,bPrint,vcID)") > -1
                        ||
                        row["TextData"].ToString().IndexOf("delete from ba_consume where") > -1
                        )
                    {
                        //此时通知用户开台列表有变动
                        string url = "http://localhost:62123/api/server/GetKTChange";
                        WebClient client = new WebClient();
                        //上传并接收数据
                        string result = client.DownloadString(url);
                        break;
                    }
                    //insert into ba_consume_d(zsr,jb,costprice,jstcmny,jsmc,isdz,szsj,jsbm,tcbm,tcsl,tcdj,tcpkid,ddlx,vcNo,vcCode,vcName,fNumber,fNumber_s,vcUnit,mPrice,mMoney,iAgio,vcMemo,vcChoose,vcClass,vcPrint,dtChoose,vcEnter,bPrint,addmoney)
                    if (row["TextData"].ToString().IndexOf("insert into ba_consume_d(zsr,jb,costprice,jstcmny,jsmc,isdz,szsj,jsbm,tcbm,tcsl,tcdj,tcpkid,ddlx,vcNo,vcCode,vcName,fNumber,fNumber_s,vcUnit,mPrice,mMoney,iAgio,vcMemo,vcChoose,vcClass,vcPrint,dtChoose,vcEnter,bPrint,addmoney)") > -1
                        ||
                         row["TextData"].ToString().IndexOf("delete from ba_consume_d") > -1
                        )
                    {
                        //点菜操作
                        //此时通知用户点菜列表有变动
                        string url = "http://localhost:62123/api/server/GetDCChange";
                        WebClient client = new WebClient();
                        //上传并接收数据
                        string result = client.DownloadString(url);
                        break;
                    }
                }
            }
        }
    }
}