/// <summary>
        /// 新增后台客户订单
        /// </summary>
        public static string AddClientOrder(ClientOrder model)
        {
            string orderID = Guid.NewGuid().ToString();
            SqlConnection conn = new SqlConnection(ClientOrderDAL.ConnectionString);
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();

            try
            {
                bool bl = ClientOrderDAL.BaseProvider.AddClientOrder(orderID, model.UserQuantity, model.Years, model.Amount, model.RealAmount,model.Type, model.AgentID, model.ClientID, model.CreateUserID,model.PayType,model.SystemType, model.SourceType,tran);
                if (bl)
                {
                    //单据明细
                    foreach (var detail in model.Details)
                    {
                        if (!ClientOrderDAL.BaseProvider.AddClientOrderDetail(orderID, detail.ProductID, detail.Price, detail.Qunatity, detail.CreateUserID, tran))
                        {
                            orderID = string.Empty;
                            tran.Rollback();
                            conn.Dispose();
                        }
                    }

                    tran.Commit();
                    conn.Dispose();
                }
                else
                {
                    orderID = string.Empty;
                    tran.Rollback();
                    conn.Dispose();
                }
            }
            catch
            {
                orderID = string.Empty;
                tran.Rollback();
                conn.Dispose();
            }

            return orderID;
        }
        /// <summary>
        /// 升级客户服务
        /// </summary>
        /// <param name="client"></param> 
        public JsonResult SaveClientAuthorize(string clientID, int serviceType, int giveType, int userQuantity, string endTime, int buyType, int buyUserQuantity, int buyUserYears)
        {
            bool flag = false;
            var client = ClientBusiness.GetClientDetail(clientID);
            var agent = AgentsBusiness.GetAgentDetail(client.AgentID);
            ClientAuthorizeLog log = new ClientAuthorizeLog();

            log.CreateUserID = CurrentUser.UserID;
            log.ClientID = clientID;
            log.AgentID = client.AgentID;
            log.OrderID = string.Empty;
            
            if (serviceType == 1)//赠送
            {
                if (giveType == 1)//赠送人数
                {
                    flag = AgentsBusiness.AddClientAgentUserQuantity(client.AgentID, userQuantity);

                    log.BeginTime = agent.EndTime;
                    log.EndTime = agent.EndTime;
                    log.UserQuantity = userQuantity;
                    log.Type = 2;
                }
                else//赠送时间
                {
                    log.BeginTime = agent.EndTime;
                    flag = AgentsBusiness.SetClientAgentEndTime(client.AgentID, DateTime.Parse(endTime));

                    log.EndTime = DateTime.Parse(endTime);
                    log.UserQuantity = agent.UserQuantity;
                    log.Type = 3;
                }
                ClientBusiness.UpdatetClientCache(client.ClientID);
                AgentsBusiness.UpdatetAgentCache(client.AgentID);
                ClientBusiness.InsertClientAuthorizeLog(log);
                ClearClientCache(client.AgentID);
            }
            else//购买生成订单
            {
                log.Type = buyType;
                int remainderMonths = 0;//剩余月份
                int years = 1;

                if (buyType == 2)//购买人数
                {
                    remainderMonths = (agent.EndTime.Year - DateTime.Now.Year) * 12 + (agent.EndTime.Month - DateTime.Now.Month) - 1;
                    if (agent.EndTime.Day >= DateTime.Now.Day)
                        remainderMonths += 1;

                    years = remainderMonths / 12 == 0 ? 1 : remainderMonths / 12;

                    log.BeginTime = agent.EndTime;
                    log.EndTime = agent.EndTime;
                    log.UserQuantity = userQuantity;
                }
                else
                {
                    years = buyUserYears;

                    log.BeginTime = agent.EndTime;
                    log.EndTime = agent.EndTime.AddYears(years);
                    log.UserQuantity = agent.UserQuantity;
                }

                int pageCount = 0;
                int totalCount = 0;
                List<ModulesProduct> list = ModulesProductBusiness.GetModulesProducts(string.Empty, int.MaxValue, 1, ref totalCount, ref pageCount);

                //获取订单产品的最佳组合
                var way = ModulesProductBusiness.GetBestWay(buyUserQuantity, list.OrderByDescending(m => m.UserQuantity).Where(m => m.PeriodQuantity == years).ToList());

                //获取订单参数
                ClientOrder model = new ClientOrder();
                model.UserQuantity = way.TotalQuantity;
                model.Type = buyType;
                model.Years = years;
                model.Amount = way.TotalMoney;
                model.RealAmount = way.TotalMoney;
                model.SourceType = 1;
                //购买人数
                float remainderYears = 1;
                if (buyType == 2)
                {
                    remainderYears = (float)remainderMonths / (12 * years);
                    model.Amount = decimal.Parse((float.Parse(model.Amount.ToString()) * remainderYears).ToString("f2"));
                    model.RealAmount = model.Amount;
                }
                model.AgentID = client.AgentID;
                model.ClientID = client.ClientID;
                model.CreateUserID = CurrentUser.UserID;

                model.Details = new List<ClientOrderDetail>();
                foreach (var p in way.Products)
                {
                    ClientOrderDetail detail = new ClientOrderDetail();
                    detail.ProductID = p.Key;
                    detail.Qunatity = p.Value;
                    detail.CreateUserID = CurrentUser.CreateUserID;
                    detail.Price = list.Find(m => m.ProductID == p.Key).Price;
                    //购买人数
                    if (buyType == 2)
                    {
                        detail.Price = decimal.Parse((float.Parse(detail.Price.ToString()) * remainderYears).ToString("f2"));
                    }
                    model.Details.Add(detail);
                }

                string orderID = ClientOrderBusiness.AddClientOrder(model);
                log.OrderID = orderID;

                flag = string.IsNullOrEmpty(orderID) ? false : true;
            }

            JsonDictionary.Add("Result", flag ? 1 : 0);

            return new JsonResult()
            {
                Data = JsonDictionary,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        /// <summary>
        /// 获取订单基本信息
        /// </summary>
        public static ClientOrder GetClientOrderInfo(string orderID)
        {
            DataTable dt = ClientOrderDAL.BaseProvider.GetClientOrderInfo(orderID);
            ClientOrder model = new ClientOrder();
            if (dt.Rows.Count == 1)
            {
                DataRow row = dt.Rows[0];
                model.FillData(row);
                model.CreateUser = OrganizationBusiness.GetUserByUserID(model.CreateUserID, model.AgentID);
                if (string.IsNullOrEmpty(model.CreateUser.Name))
                {
                    M_Users mUser = M_UsersBusiness.GetUserDetail(model.CreateUserID);
                    if (mUser != null && !string.IsNullOrEmpty(mUser.Name))
                    {
                        model.CreateUser.Name = mUser.Name;
                    }
                }
                if (!string.IsNullOrEmpty(model.CheckUserID))
                {
                    model.CheckUser = OrganizationBusiness.GetUserByUserID(model.CheckUserID, model.AgentID);
                    if (string.IsNullOrEmpty(model.CheckUser.Name))
                    {
                        M_Users mUser = M_UsersBusiness.GetUserDetail(model.CheckUserID);
                        if (mUser != null && !string.IsNullOrEmpty(mUser.Name))
                        {
                            model.CheckUser.Name = mUser.Name;
                        }
                    }
                }
            }

            return model;
        }
 public static List<ClientOrder> GetBase(string keyWords, int status, int type, string beginDate, string endDate, string agentID, string clientID, int userType, int pageSize, int pageIndex, ref int totalCount, ref int pageCount)
 {
     DataTable dt = ClientOrderDAL.BaseProvider.GetClientOrders(keyWords,status, type, beginDate, endDate, agentID, clientID, pageSize, pageIndex, ref totalCount, ref pageCount);
     List<ClientOrder> list = new List<ClientOrder>();
     if (dt.Rows.Count > 0)
     {
         foreach (DataRow row in dt.Rows)
         {
             ClientOrder model = new ClientOrder();
             model.FillData(row);
             if (!string.IsNullOrEmpty(model.CheckUserID))
             {
                 model.CheckUser = OrganizationBusiness.GetUserByUserID(model.CheckUserID, model.AgentID);
                 if (string.IsNullOrEmpty(model.CheckUser.Name))
                 {
                     M_Users mUser = M_UsersBusiness.GetUserDetail(model.CheckUserID);
                     if(mUser!=null && !string.IsNullOrEmpty(mUser.Name)){
                         model.CheckUser.Name = mUser.Name;
                     }
                 }                        
             }
             model.CreateUser = OrganizationBusiness.GetUserByUserID(model.CreateUserID, model.AgentID);
             if (string.IsNullOrEmpty(model.CreateUser.Name))
             {
                 M_Users mUser = M_UsersBusiness.GetUserDetail(model.CreateUserID);
                 if (mUser != null && !string.IsNullOrEmpty(mUser.Name))
                 {
                     model.CreateUser.Name = mUser.Name;
                 }
             }
             list.Add(model);
         }
     }
     return list;
 } 
        /// <summary>
        /// 根据人数、年数生成客户订单
        /// </summary>
        public JsonResult AddClientOrder(int quantity, int years, int type)
        {
            int remainderMonths = 12;//剩余月份

            //购买人数
            if (type == 2)
            {
                remainderMonths = (CurrentAgent.EndTime.Year - DateTime.Now.Year) * 12 + (CurrentAgent.EndTime.Month - DateTime.Now.Month) - 1;
                if (CurrentAgent.EndTime.Day >= DateTime.Now.Day)
                    remainderMonths += 1;

                years = remainderMonths / 12 == 0 ? 1 : remainderMonths / 12;
            }

            int pageCount = 0;
            int totalCount = 0;
            List<ModulesProduct> list = ModulesProductBusiness.GetModulesProducts(string.Empty, int.MaxValue, 1, ref totalCount, ref pageCount);

            //获取订单产品的最佳组合
            var way = ModulesProductBusiness.GetBestWay(quantity, list.OrderByDescending(m => m.UserQuantity).Where(m => m.PeriodQuantity == years).ToList());

            //获取订单参数
            ClientOrder model = new ClientOrder();
            model.UserQuantity = way.TotalQuantity;
            model.Type = type;
            model.Years = years;
            model.Amount = way.TotalMoney;

            decimal discount = 1M;
            if (!string.IsNullOrEmpty(CurrentUser.AliMemberID))
            {
                if (type == 1 || type == 2)
                {
                    discount = 0.50M;
                }
                else
                {
                    discount = 0.88M;
                }

            }
            else
            {
                if (type == 1 || type == 2)
                {
                    discount = 0.5M;
                }
            }

            model.RealAmount = way.TotalMoney * discount;

            //购买人数
            float remainderYears = 1;
            if (type == 2)
            {
                remainderYears = (float)remainderMonths / (12 * years);
                model.Amount = decimal.Parse((float.Parse(model.Amount.ToString()) * remainderYears).ToString("f2"));
                model.RealAmount = decimal.Parse((float.Parse(model.RealAmount.ToString()) * remainderYears).ToString("f2"));
            }
            model.AgentID = CurrentUser.AgentID;
            model.ClientID = CurrentUser.ClientID;
            model.CreateUserID = CurrentUser.UserID;
            model.SourceType = 0;
            model.Details = new List<ClientOrderDetail>();
            foreach (var p in way.Products)
            {
                ClientOrderDetail detail = new ClientOrderDetail();
                detail.ProductID = p.Key;
                detail.Qunatity = p.Value;
                detail.CreateUserID = CurrentUser.CreateUserID;
                detail.Price = list.Find(m => m.ProductID == p.Key).Price;
                //购买人数
                if (type == 2)
                {
                    detail.Price = decimal.Parse((float.Parse(detail.Price.ToString()) * remainderYears).ToString("f2"));
                }
                model.Details.Add(detail);
            }

            string orderID = ClientOrderBusiness.AddClientOrder(model);
            JsonDictionary.Add("ID", orderID);
            JsonDictionary.Add("RealAmount", model.RealAmount);

            return new JsonResult
            {
                Data = JsonDictionary,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }