public ABTestDetail GetABTestDetailByGuid(Guid testGuid)
        {
            var dt = dal.GetABTestDetailByGuid(testGuid);

            if (dt == null || dt.Rows == null || dt.Rows.Count < 1)
            {
                return(null);
            }

            ABTestDetail test = new ABTestDetail()
            {
                TestGuid           = testGuid,
                TestName           = dt.Rows[0]["TestName"].ToString(),
                TestScale          = Convert.ToDouble(dt.Rows[0]["TestScale"].ToString()) * 100,
                Status             = dt.Rows[0]["Status"].ToString(),
                CreateTime         = Convert.ToDateTime(dt.Rows[0]["CreateTime"].ToString()),
                LastUpdateDataTime = Convert.ToDateTime(dt.Rows[0]["LastUpdateDataTime"].ToString()),
                Creator            = dt.Rows[0]["Creator"].ToString(),
                GroupList          = new List <ABTestGroupDetail>(),
                GroupNum           = Convert.ToInt32(dt.Rows[0]["GroupNum"].ToString())
            };

            var dtGroup = dal.GetABTestGroupDetailByGuid(testGuid);

            test.GroupList = dtGroup.ConvertTo <ABTestGroupDetail>().ToList();

            return(test);
        }
        public JsonResult AddABTest(string jsonstr, string testName, string testScale)
        {
            bool flag = false;

            ABTestDetail test = new ABTestDetail
            {
                CreateTime         = DateTime.Now,
                Creator            = ThreadIdentity.Operator.Name,
                GroupList          = new List <ABTestGroupDetail>(),
                LastUpdateDataTime = DateTime.Now,
                Status             = "Processing",
                TestGuid           = Guid.NewGuid(),
                TestName           = testName,
                TestScale          = Convert.ToDouble(testScale) / 100
            };
            List <ABTestGroupDetail> groups = new List <ABTestGroupDetail>();

            JArray         o     = (JArray)JsonConvert.DeserializeObject(jsonstr);
            IList <JToken> oList = (IList <JToken>)o;

            foreach (JToken jt in oList)
            {
                JObject jo = jt as JObject;
                groups.Add(new ABTestGroupDetail
                {
                    CreateTime         = DateTime.Now,
                    LastUpdateDataTime = DateTime.Now,
                    TestGuid           = test.TestGuid,
                    Selected           = false,
                    GroupId            = jo["GroupId"].ToString(),
                    GroupName          = jo["GroupName"].ToString()
                });
            }

            test.GroupNum  = groups.Count();
            test.GroupList = groups;

            ABTestPlatformManager manager = new ABTestPlatformManager();

            flag = manager.CreateABTest(test);

            #region 插入日志
            ABTestEditLog log = new ABTestEditLog
            {
                TestGuid           = test.TestGuid,
                TestName           = test.TestName,
                Change             = "新建测试",
                Operator           = ThreadIdentity.Operator.Name,
                CreateTime         = DateTime.Now,
                LastUpdateDataTime = DateTime.Now
            };
            if (flag)
            {
                InsertLog(log);
            }
            #endregion

            return(Json(flag));
        }
        public JsonResult EditABTest(string jsonstr, string testGuid, string testScale)
        {
            bool flag = false;

            ABTestPlatformManager manager = new ABTestPlatformManager();
            var oldTest = manager.GetABTestDetailByGuid(new Guid(testGuid));

            ABTestDetail test = new ABTestDetail
            {
                CreateTime         = DateTime.Now,
                LastUpdateDataTime = DateTime.Now,
                TestGuid           = new Guid(testGuid),
                GroupList          = new List <ABTestGroupDetail>(),
                TestScale          = Convert.ToDouble(testScale) / 100
            };
            List <ABTestGroupDetail> groups = new List <ABTestGroupDetail>();

            JArray         o     = (JArray)JsonConvert.DeserializeObject(jsonstr);
            IList <JToken> oList = (IList <JToken>)o;

            foreach (JToken jt in oList)
            {
                JObject jo = jt as JObject;
                groups.Add(new ABTestGroupDetail
                {
                    CreateTime         = DateTime.Now,
                    LastUpdateDataTime = DateTime.Now,
                    TestGuid           = test.TestGuid,
                    GroupId            = jo["GroupId"].ToString(),
                    ExceptData         = jo["ExceptData"].ToString()
                });
            }
            test.GroupNum  = groups.Count();
            test.GroupList = groups;

            flag = manager.UpdateABTest(test);

            #region 插入日志
            if (flag)
            {
                var           change = CompareChange(oldTest, test);
                ABTestEditLog log    = new ABTestEditLog()
                {
                    TestGuid           = oldTest.TestGuid,
                    TestName           = oldTest.TestName,
                    Change             = change,
                    Operator           = ThreadIdentity.Operator.Name,
                    CreateTime         = DateTime.Now,
                    LastUpdateDataTime = DateTime.Now
                };
                InsertLog(log);
            }
            #endregion

            return(Json(flag));
        }
        public string CompareChange(ABTestDetail oldTest, ABTestDetail newTest)
        {
            string result = string.Empty;

            if (oldTest.TestScale != (newTest.TestScale * 100))
            {
                result += "总分配比例:" + oldTest.TestScale + "->" + newTest.TestScale * 100 + ";";
            }
            for (int i = 0; i < oldTest.GroupList.Count; i++)
            {
                if (string.IsNullOrWhiteSpace(oldTest.GroupList[i].ExceptData) && !string.IsNullOrWhiteSpace(newTest.GroupList[i].ExceptData))
                {
                    result +=
                        "分组" +
                        oldTest.GroupList[i].GroupId +
                        ": null->" +
                        newTest.GroupList[i].ExceptData +
                        ";";
                }
                if (!string.IsNullOrWhiteSpace(oldTest.GroupList[i].ExceptData) && string.IsNullOrWhiteSpace(newTest.GroupList[i].ExceptData))
                {
                    result +=
                        "分组" +
                        oldTest.GroupList[i].GroupId +
                        ": " +
                        oldTest.GroupList[i].ExceptData +
                        "->null;";
                }
                if (!string.IsNullOrWhiteSpace(oldTest.GroupList[i].ExceptData) &&
                    !string.IsNullOrWhiteSpace(newTest.GroupList[i].ExceptData) &&
                    oldTest.GroupList[i].ExceptData != newTest.GroupList[i].ExceptData)
                {
                    result +=
                        "分组" +
                        oldTest.GroupList[i].GroupId +
                        ": " +
                        oldTest.GroupList[i].ExceptData +
                        "->" +
                        newTest.GroupList[i].ExceptData + ";";
                }
            }

            return(result);
        }
        public bool CreateABTest(ABTestDetail test)
        {
            bool result = false;
            List <ABTestDetail> input = new List <ABTestDetail>();

            input.Add(test);
            using (var client = new ConfigClient())
            {
                var createResult = client.CreateABTestDetail(input);
                if (!createResult.Success)
                {
                    return(false);
                }
                else
                {
                    result = createResult.Result;
                }
            }
            return(result);
        }