public void Ctor_AssignsVariables_Correctly()
        {
            var order         = OrderTestHelper.Random(new Order(), 30);
            var orderAnalysis = new OrderAnalysis(order, PriceSentiment.Negative);

            Assert.AreEqual(PriceSentiment.Negative, orderAnalysis.Sentiment);
            Assert.AreEqual(order, orderAnalysis.Order);
        }
        public void OpposingSentiment_HasFullCoverage_OfAllPriceSentiments()
        {
            var service   = this.Service();
            var rawValues = Enum.GetValues(typeof(PriceSentiment));

            var order1 = OrderTestHelper.Random(new Order());

            order1.OrderDirection = OrderDirections.BUY;
            var orderAnalysis1 = new OrderAnalysis(order1, PriceSentiment.Positive);
            var order2         = OrderTestHelper.Random(new Order());

            order2.OrderDirection = OrderDirections.SELL;
            var orderAnalysis2 = new OrderAnalysis(order2, PriceSentiment.Negative);
            var orders         = new[] { orderAnalysis1, orderAnalysis2 };

            foreach (var val in rawValues)
            {
                Assert.DoesNotThrow(() => service.OpposingSentiment(orders, (PriceSentiment)val));
            }
        }
        /// <summary>
        /// 产品订单分析
        /// </summary>
        /// <returns>{bus_name:名称,num:总数,quantity:数量,smsNum:短信数量,accNum:版本月数,money:金额,baifen:金额比,bus_mclass:类别}</returns>
        public OrderAnalysis GetBussinessOrderAnalyse(DateTime stDate, DateTime edDate)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("  select oid,busId,busQuantity,RealPayMoney,(case when busId=0 then remark else '' end ) busName,null smsCount,null banCount,0 bus_mclass  into #list  from T_OrderInfo   ");
            strSql.Append("     where  createDate<= @endTime and createDate >= @statTime and ");
            strSql.Append(" orderStatus=2 and (OrderTypeId=1 or OrderTypeId=4); ");


            strSql.Append(" update #list set smsCount=a.smsCount , banCount=a.banCount from( ");
            strSql.Append(" select oid,Sum(case when itemId=1 then itemQuantity else 0 end) smsCount,Sum(case when itemId=3 then (itemQuantity *2) when itemId=2 then itemQuantity else 0 end) banCount ");
            strSql.Append("  from T_Order_List where oid in(select oid from #list) group  by oid) a  where a.oid=#list.oid; ");
            strSql.Append(" if(EXISTS(select * from #list where smsCount is null)) ");
            strSql.Append(" begin ");
            strSql.Append(" 	update #list set smsCount=a.smsCount , banCount=a.banCount from( ");
            strSql.Append(" 	select busId,Sum(case when itemId=1 then itemQuantity else 0 end) smsCount,Sum(case when itemId=3 then (itemQuantity*2) when  ");
            strSql.Append(" 	itemId=2 then itemQuantity else 0 end) banCount from T_Order_Project_List where busId in(select busId from #list where smsCount is null) group by busId ");
            strSql.Append(" 	) a  where #list.smsCount is null and #list.banCount is null and #list.busId=a.busId ; ");
            strSql.Append(" end ");
            strSql.Append(" update #list set busName=T_Order_Project.displayName ,bus_mclass=projectType from T_Order_Project where T_Order_Project.busId=#list.busId ; ");
            strSql.Append(" select busName,bus_mclass,COUNT(oid) num,SUM(busQuantity) quantity,SUM(smsCount * busQuantity) smsNum,SUM(banCount * busQuantity) accNum, ");
            strSql.Append(" SUM(RealPayMoney) [money] from #list group by busName,bus_mclass ");

            #region 获取其他类型订单数据

            strSql.Append(" union all ");
            strSql.Append(" select mg.AliasName busName,5 bus_mclass,COUNT(oi.oid) num,COUNT(oi.oid) quantity, ");
            strSql.Append(" 0 smsNum,0 accNum,SUM(oi.RealPayMoney) money from i200.dbo.T_OrderInfo oi  ");
            strSql.Append(" left join [I200].[dbo].[T_MaterialGoods] mg on oi.busId=mg.GoodsId ");
            strSql.Append(" where oi.OrderTypeId=2 and orderStatus=1 and createDate<= @endTime and createDate >= @statTime ");
            strSql.Append(" group by mg.AliasName ");
            strSql.Append(" union all  ");
            strSql.Append(" select '京东日百供货' busName,6 bus_mclass,COUNT(oid) num,COUNT(oid) quantity,0 smsNum,0 accNum,SUM(RealPayMoney) money ");
            strSql.Append(" from i200.dbo.T_OrderInfo where OrderTypeId=3 and orderStatus=2 and createDate<= @endTime and createDate >= @statTime;  ");
            #endregion

            strSql.Append(" drop table #list; ");


            List <dynamic> ds =
                HelperForFrontend.Query <dynamic>(strSql.ToString(), new { endTime = edDate, statTime = stDate }).ToList();


            int           allNum      = 0;
            int           allQuantity = 0;
            int           allSmsNum   = 0;
            int           allAccNum   = 0;
            int           allMoney    = 0;
            OrderAnalysis orderAM     = new OrderAnalysis();
            if (ds.Count > 0)
            {
                foreach (dynamic dr in ds)
                {
                    if (dr.num != 0)
                    {
                        OrderAnalysisItem itemModel = new OrderAnalysisItem();

                        if (dr.busName != null && dr.busName != "")
                        {
                            itemModel.busName = dr.busName.ToString();
                        }
                        if (dr.bus_mclass != null && dr.bus_mclass != 0)
                        {
                            int bus_m = Convert.ToInt32(dr.bus_mclass);
                            if (bus_m == 1)
                            {
                                itemModel.bus_mclass = "短信";
                            }
                            else if (bus_m == 2)
                            {
                                itemModel.bus_mclass = "版本";
                            }
                            else if (bus_m == 3)
                            {
                                itemModel.bus_mclass = "产品";
                            }
                            else if (bus_m == 4)
                            {
                                itemModel.bus_mclass = "套餐";
                            }
                            else if (bus_m == 5)
                            {
                                itemModel.bus_mclass = "个";
                            }
                            else if (bus_m == 6)
                            {
                                itemModel.bus_mclass = "批";
                            }
                        }
                        if (dr.num != null && dr.num != 0)
                        {
                            itemModel.num = Convert.ToInt32(dr.num);
                        }
                        if (dr.quantity != null && dr.quantity != 0)
                        {
                            itemModel.quantity = Convert.ToInt32(dr.quantity);
                        }
                        if (dr.smsNum != null && dr.smsNum != 0)
                        {
                            itemModel.smsNum = Convert.ToInt32(dr.smsNum);
                        }
                        if (dr.accNum != null && dr.accNum != 0)
                        {
                            itemModel.accNum = Convert.ToInt32(dr.accNum) / 2;
                        }
                        if (dr.money != null && dr.money != 0)
                        {
                            itemModel.money = Convert.ToInt32(dr.money);
                        }

                        allNum      += itemModel.num;
                        allQuantity += itemModel.quantity;
                        allSmsNum   += itemModel.smsNum;
                        allAccNum   += itemModel.accNum;
                        allMoney    += itemModel.money;
                        orderAM.itemList.Add(itemModel);
                    }
                }
            }



            orderAM.num      = allNum;
            orderAM.quantity = allQuantity;
            orderAM.smsNum   = allSmsNum;
            orderAM.accNum   = allAccNum;
            orderAM.money    = allMoney;
            return(orderAM);
        }
        public static string getOrderAnalyzeData(string type, string dataType, DateTime BgTime, DateTime EdTime)
        {
            string   strJson = "";
            DateTime bgTime  = new DateTime();
            DateTime edTime  = new DateTime();

            if (dataType == "oth")
            {
                bgTime = BgTime;
                edTime = EdTime.AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            else if (dataType == "month")
            {
                DateTime FirstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                DateTime LastDay  = FirstDay.AddMonths(1).AddDays(-1);

                bgTime = FirstDay;
                edTime = LastDay.AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            else if (dataType == "lastmonth")
            {
                DateTime nowTime  = DateTime.Now.AddMonths(-1);
                DateTime FirstDay = new DateTime(nowTime.Year, nowTime.Month, 1);
                DateTime LastDay  = FirstDay.AddMonths(1).AddDays(-1);

                bgTime = FirstDay;
                edTime = LastDay.AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            else if (dataType == "3month")
            {
                DateTime nowTime  = DateTime.Now.AddMonths(-2);
                DateTime FirstDay = new DateTime(nowTime.Year, nowTime.Month, 1);
                DateTime LastDay  = FirstDay.AddMonths(3).AddDays(-1);

                bgTime = FirstDay;
                edTime = LastDay.AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            else if (dataType == "today")
            {
                bgTime = DateTime.Today.AddHours(0).AddMinutes(0).AddSeconds(0);
                edTime = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);
            }


            OrderAnalysis orModel = T_Order_businessBLL.GetBussinessOrderAnalyse(bgTime, edTime);


            StringBuilder pieData  = new StringBuilder();
            StringBuilder listData = new StringBuilder();

            if (orModel != null && orModel.itemList.Count > 0)
            {
                double AllNum      = orModel.num;
                double AllQuantity = orModel.quantity;
                double AllsmsNum   = orModel.smsNum;
                double AllaccNum   = orModel.accNum;
                double AllMoney    = orModel.money;

                foreach (OrderAnalysisItem dr in orModel.itemList)
                {
                    listData.Append("{\"busname\":\"" + SetBusName(dr.busName) + "\",\"num\":" + Convert.ToDouble(dr.num) + ",\"quantity\":" + Convert.ToDouble(dr.quantity) + ",\"smsNum\":" + Convert.ToDouble(dr.smsNum) + ",\"accNum\":" + Convert.ToDouble(dr.accNum) + ",\"money\":" + Convert.ToDouble(dr.money) + ",\"baifen\":" + GetBaiFen(Convert.ToDouble(dr.money), AllMoney) + ",\"busmclass\":\"" + dr.bus_mclass + "\"},");
                    pieData.Append("{\"label\":\"" + dr.busName + "\",\"value\":" + GetBaiFen(Convert.ToDouble(dr.money), AllMoney) + ",\"toolText\":\"" + dr.busName + "{br}" + GetBaiFen(Convert.ToDouble(dr.money), AllMoney) + "%\"},");
                }

                listData.Append("{\"busname\":\"合计\",\"num\":" + AllNum + ",\"quantity\":" + AllQuantity + ",\"smsNum\":" + AllsmsNum + ",\"accNum\":" + AllaccNum + ",\"money\":" + AllMoney + ",\"baifen\":100,\"busmclass\":\"总\"}");

                string pieJson = "{\"chart\":{\"caption\":\"订单类型分析\", \"showpercentvalues\":\"1\",\"baseFontSize\":\"12\"}, \"data\":[" + pieData.ToString().Trim(',') + "]} ";

                strJson = "{\"orderClass\":[" + listData.ToString() + "],\"pie\":" + pieJson + "}";
            }
            else
            {
                strJson = "none";
            }
            return(strJson);
        }