public List<OrderEntity> GetOpportunitys(EnumSearchType searchtype, string typeid, string stageid, string searchuserid, string searchteamid, string searchagentid,
                                  string begintime, string endtime, string keyWords, int pageSize, int pageIndex, ref int totalCount, ref int pageCount, string userid, string agentid, string clientid)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataSet ds = OrdersDAL.BaseProvider.GetOpportunitys((int)searchtype, typeid, stageid, searchuserid, searchteamid, searchagentid, begintime, endtime, keyWords, pageSize, pageIndex, ref totalCount, ref pageCount, userid, agentid, clientid);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                list.Add(model);
            }
            return list;
        }
        public List<OrderEntity> GetOrders(EnumSearchType searchtype, string entrustClientID, string typeid, int status, EnumOrderSourceType sourceType, int orderStatus, int mark, int paystatus, int invoicestatus, int returnstatus, string searchuserid, string searchteamid, string searchagentid,
                                                string begintime, string endtime, string keyWords, string orderBy, int pageSize, int pageIndex, ref int totalCount, ref int pageCount, string userid, string agentid, string clientid)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataSet ds = OrdersDAL.BaseProvider.GetOrders((int)searchtype, entrustClientID, typeid, status, (int)sourceType, orderStatus, mark, paystatus, invoicestatus, returnstatus, searchuserid, searchteamid, searchagentid, begintime, endtime, keyWords,
                                                         orderBy, pageSize, pageIndex, ref totalCount, ref pageCount, userid, agentid, clientid);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);

                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                model.SourceTypeStr = CommonBusiness.GetEnumDesc((EnumOrderSourceType)model.SourceType);

                if (model.OrderStatus == 1 && model.Status != (int)EnumOrderStageStatus.DDH)
                {
                    if (model.PlanTime <= DateTime.Now)
                    {
                        model.WarningStatus = 2;
                        model.WarningTime = "超期:" + (DateTime.Now - model.PlanTime).Days.ToString("D2") + "天 " + (DateTime.Now - model.PlanTime).Hours.ToString("D2") + "时 " + (DateTime.Now - model.PlanTime).Minutes.ToString("D2") + "分";
                    }
                    else if ((model.PlanTime - DateTime.Now).TotalHours * 3 < (model.PlanTime - model.OrderTime).TotalHours)
                    {
                        model.WarningStatus = 1;
                        model.WarningTime = "剩余:" + (model.PlanTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.PlanTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.PlanTime - DateTime.Now).Minutes.ToString("D2") + "分";
                    }
                    else
                    {
                        model.WarningTime = "剩余:" + (model.PlanTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.PlanTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.PlanTime - DateTime.Now).Minutes.ToString("D2") + "分";
                    }
                }


                list.Add(model);
            }
            return list;
        }
        public static List<TaskEntity> GetTasksByEndTime(string startEndTime, string endEndTime, 
            int orderType, int filterType, int finishStatus,int preFinishStatus,int taskType,
            string userID, string clientID, int pageSize, int pageIndex, ref int totalCount, ref int pageCount)
        {
            List<TaskEntity> list = new List<TaskEntity>();
            DataSet ds = TaskDAL.BaseProvider.GetTasksByEndTime(startEndTime, endEndTime, 
                orderType, filterType, finishStatus,preFinishStatus,taskType,
                userID, clientID, pageSize, pageIndex, ref totalCount, ref pageCount);

            DataTable dt = ds.Tables["Tasks"];
            DataTable orders = ds.Tables["Orders"];
            foreach (DataRow dr in dt.Rows)
            {
                TaskEntity model = new TaskEntity();
                model.FillData(dr);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);
                if (orders.Rows.Count > 0)
                {
                    foreach (DataRow dr2 in orders.Select(" OrderID='" + model.OrderID + "'"))
                    {
                        OrderEntity order = new OrderEntity();
                        order.FillData(dr2);
                        model.Order = order;
                    }
                }

                if (model.FinishStatus == 1)
                {
                    if (model.EndTime <= DateTime.Now)
                    {
                        model.WarningStatus = 2;
                        model.WarningTime = "超期:" + (DateTime.Now - model.EndTime).Days.ToString("D2") + "天 " + (DateTime.Now - model.EndTime).Hours.ToString("D2") + "时 " + (DateTime.Now - model.EndTime).Minutes.ToString("D2") + "分";
                        model.WarningDays = (DateTime.Now - model.EndTime).Days;
                        model.UseDays = (model.EndTime - model.AcceptTime).Days;
                    }
                    else if ((model.EndTime - DateTime.Now).TotalHours * 3 < (model.EndTime - model.AcceptTime).TotalHours)
                    {
                        model.WarningStatus = 1;
                        model.WarningTime = "剩余:" + (model.EndTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.EndTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.EndTime - DateTime.Now).Minutes.ToString("D2") + "分";
                        model.WarningDays = (model.EndTime - DateTime.Now).Days;
                        model.UseDays = (DateTime.Now - model.AcceptTime).Days;
                    }
                    else
                    {
                        model.WarningTime = "剩余:" + (model.EndTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.EndTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.EndTime - DateTime.Now).Minutes.ToString("D2") + "分";
                        model.WarningDays = (model.EndTime - DateTime.Now).Days;
                        model.UseDays = (DateTime.Now - model.AcceptTime).Days;
                    }
                }
                else
                {
                    model.WarningStatus = 3;
                    model.UseDays = (model.EndTime - model.AcceptTime).Days;
                    model.UseDays = (DateTime.Now - model.CompleteTime).Days;
                }
                list.Add(model);
            }

            return list;
        }
        public List<OrderEntity> GetOrders(string keyWords, int pageSize, int pageIndex, ref int totalCount, ref int pageCount, string agentid, string clientid)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            string where = " ClientID='" + clientid + "' and  OrderType=1 and Status= " + (int)EnumOrderStageStatus.FYFJ;
            if (!string.IsNullOrEmpty(keyWords))
            {
                where += "and (OrderCode like '%" + keyWords + "%' or Title like '%" + keyWords + "%' or PersonName like '%" + keyWords + "%' or IntGoodsCode like '%" + keyWords + "%' or GoodsCode like '%" + keyWords + "%')";
            }
            DataTable dt = CommonBusiness.GetPagerData("Orders", "*", where, "AutoID", pageSize, pageIndex, out totalCount, out pageCount, false);
            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);

                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                model.SourceTypeStr = CommonBusiness.GetEnumDesc((EnumOrderSourceType)model.SourceType);

                list.Add(model);
            }
            return list;
        }
        public static List<OrderEntity> GetOrderPlans(string ownerID, string beginDate, string endDate, string clientID, int pageSize, int pageIndex, ref int totalCount, ref int pageCount)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataTable dt = OrdersDAL.BaseProvider.GetOrderPlans(ownerID, beginDate, endDate, clientID, pageSize, pageIndex, ref totalCount, ref pageCount);

            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);
                //model.Stage = SystemBusiness.BaseBusiness.GetOrderStageByID(model.StageID, model.ProcessID, model.AgentID, model.ClientID);
                //model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                list.Add(model);
            }

            return list;
        }
        public OrderEntity GetOrderForFentReport(string orderid, string agentid, string clientid)
        {
            DataSet ds = OrdersDAL.BaseProvider.GetOrderForFentReport(orderid, agentid, clientid);
            OrderEntity model = new OrderEntity();
            if (ds.Tables["Order"].Rows.Count > 0)
            {

                model.FillData(ds.Tables["Order"].Rows[0]);

                if (model.Status == 2)
                {
                    model.SendStatusStr = CommonBusiness.GetEnumDesc((EnumSendStatus)model.SendStatus);
                }
                else if (model.Status < 2)
                {
                    model.SendStatusStr = "--";
                }


                if (!string.IsNullOrEmpty(model.CategoryID))
                {
                    model.CategoryName = ProductsBusiness.BaseBusiness.GetCategoryByID(model.BigCategoryID).CategoryName + ">" + ProductsBusiness.BaseBusiness.GetCategoryByID(model.CategoryID).CategoryName;
                }

                model.Tasts = new List<IntFactoryEntity.Task.TaskEntity>();
                if (model.Status > 0 && ds.Tables["Tasks"].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables["Tasks"].Rows)
                    {
                        IntFactoryEntity.Task.TaskEntity task = new IntFactoryEntity.Task.TaskEntity();
                        task.FillData(dr);
                        task.Owner = OrganizationBusiness.GetUserByUserID(task.OwnerID, model.AgentID);
                        model.Tasts.Add(task);
                    }

                }

                model.City = CommonBusiness.GetCityByCode(model.CityCode);

                if (ds.Tables["Customer"].Rows.Count > 0)
                {
                    model.Customer = new CustomerEntity();
                    model.Customer.FillData(ds.Tables["Customer"].Rows[0]);
                }

                model.Details = new List<OrderDetail>();
                foreach (DataRow dr in ds.Tables["Details"].Rows)
                {
                    OrderDetail detail = new OrderDetail();
                    detail.FillData(dr);
                    if (!string.IsNullOrEmpty(detail.UnitID))
                    {
                        detail.UnitName = new ProductsBusiness().GetUnitByID(detail.UnitID).UnitName;

                    }
                    var city = CommonBusiness.GetCityByCode(detail.ProviderCityCode);
                    if (city != null)
                    {
                        detail.ProviderAddress = city.Description + detail.ProviderAddress;
                    }
                    model.Details.Add(detail);
                }

                model.OrderCoss = new List<OrderCostEntity>();
                foreach (DataRow dr in ds.Tables["OrderCoss"].Rows)
                {
                    OrderCostEntity cos = new OrderCostEntity();
                    cos.FillData(dr);

                    model.OrderCoss.Add(cos);
                }


            }
            return model;
        }
        public OrderEntity GetOrderBaseInfoByID(string orderid, string agentid, string clientid)
        {
            DataSet ds = OrdersDAL.BaseProvider.GetOrderBaseInfoByID(orderid, agentid, clientid);
            OrderEntity model = new OrderEntity();
            if (ds.Tables["Order"].Rows.Count > 0)
            {

                model.FillData(ds.Tables["Order"].Rows[0]);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);
                if (!string.IsNullOrEmpty(model.CategoryID))
                {
                    model.CategoryName = ProductsBusiness.BaseBusiness.GetCategoryByID(model.BigCategoryID).CategoryName + ">" + ProductsBusiness.BaseBusiness.GetCategoryByID(model.CategoryID).CategoryName;
                }

                model.Details = new List<OrderDetail>();
                if (ds.Tables["Details"].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables["Details"].Rows)
                    {
                        OrderDetail detail = new OrderDetail();
                        detail.FillData(dr);
                        if (!string.IsNullOrEmpty(detail.UnitID))
                        {
                            detail.UnitName = new ProductsBusiness().GetUnitByID(detail.UnitID).UnitName;
                        }
                        model.Details.Add(detail);
                    }
                }
            }
            return model;
        }
        public OrderEntity GetOrderByID(string orderid, string agentid, string clientid)
        {
            DataSet ds = OrdersDAL.BaseProvider.GetOrderByID(orderid, agentid, clientid);
            OrderEntity model = new OrderEntity();
            if (ds.Tables["Order"].Rows.Count > 0)
            {
                
                model.FillData(ds.Tables["Order"].Rows[0]);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);
                model.CreateUser = OrganizationBusiness.GetUserByUserID(model.CreateUserID, model.AgentID);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);
                model.ExpressTypeStr = CommonBusiness.GetEnumDesc((EnumExpressType)model.ExpressType);

                if (model.Status == 2)
                {
                    model.SendStatusStr = CommonBusiness.GetEnumDesc((EnumSendStatus)model.SendStatus);
                }
                else if (model.Status < 2)
                {
                    model.SendStatusStr = "--";
                }


                if (!string.IsNullOrEmpty(model.CategoryID))
                {
                    model.CategoryName = ProductsBusiness.BaseBusiness.GetCategoryByID(model.BigCategoryID).CategoryName + ">" + ProductsBusiness.BaseBusiness.GetCategoryByID(model.CategoryID).CategoryName;
                }
                model.OrderProcess = SystemBusiness.BaseBusiness.GetOrderProcessByID(model.ProcessID, model.AgentID, model.ClientID);
                                
                model.OrderProcess.OrderStages = SystemBusiness.BaseBusiness.GetOrderStages(model.ProcessID, model.AgentID, model.ClientID);

                model.Tasts = new List<IntFactoryEntity.Task.TaskEntity>();
                if (model.Status > 0 && ds.Tables["Tasks"].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables["Tasks"].Rows)
                    {
                        IntFactoryEntity.Task.TaskEntity task = new IntFactoryEntity.Task.TaskEntity();
                        task.FillData(dr);
                        task.Owner = OrganizationBusiness.GetUserByUserID(task.OwnerID, model.AgentID);
                        model.Tasts.Add(task);
                    }
                    
                }
                model.StatusItems = new List<OrderStatusEntity>();
                if (model.Status > 0 && ds.Tables["Status"].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables["Status"].Rows)
                    {
                        OrderStatusEntity status = new IntFactoryEntity.OrderStatusEntity();
                        status.FillData(dr);
                        model.StatusItems.Add(status);
                    }
                }

                model.City = CommonBusiness.GetCityByCode(model.CityCode);

                if (ds.Tables["Customer"].Rows.Count > 0)
                {
                    model.Customer = new CustomerEntity();
                    model.Customer.FillData(ds.Tables["Customer"].Rows[0]);
                }
                model.Details = new List<OrderDetail>();
                foreach (DataRow dr in ds.Tables["Details"].Rows)
                {
                    OrderDetail detail = new OrderDetail();
                    detail.FillData(dr);
                    if (!string.IsNullOrEmpty(detail.UnitID))
                    {
                        detail.UnitName = new ProductsBusiness().GetUnitByID(detail.UnitID).UnitName;
                        
                    }
                    model.Details.Add(detail);
                }
                
                model.OrderGoods = new List<OrderGoodsEntity>();
                if (ds.Tables["Goods"].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables["Goods"].Rows)
                    {
                        OrderGoodsEntity detail = new OrderGoodsEntity();
                        detail.FillData(dr);
                        model.OrderGoods.Add(detail);
                    }
                }  
            }
            return model;
        }
 public OrderEntity GetOrderByID(string orderid)
 {
     DataTable dt = OrdersDAL.BaseProvider.GetOrderByID(orderid);
     OrderEntity model = new OrderEntity();
     if (dt.Rows.Count > 0)
     {
         model.FillData(dt.Rows[0]);
     }
     return model;
 }
        public List<OrderEntity> GetNeedsOrderByCustomerID(string keyWords,string customerid, int pageSize, int pageIndex, ref int totalCount, ref int pageCount, string userid, string agentid, string clientid)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            string condition="CustomerID='" + customerid + "' and Status = 0 ";
            if (!string.IsNullOrEmpty(keyWords))
            {
                condition += " and ( OrderCode like '%" + keyWords + "%' or GoodsCode like '%" + keyWords + "%' or MobileTele like '%" + keyWords + "%' or PersonName like '%" + keyWords + "%')";
            }
            DataTable dt = CommonBusiness.GetPagerData("Orders", "*",condition , "AutoID", pageSize, pageIndex, out totalCount, out pageCount, false);
            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                list.Add(model);
            }
            return list;
        }
        public List<OrderEntity> GetOrdersByOriginalID(string originalid, int ordertype, int pageSize, int pageIndex, ref int totalCount, ref int pageCount, string userid, string agentid, string clientid)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataTable dt = CommonBusiness.GetPagerData("Orders", "*", "OriginalID='" + originalid + "' and OrderType=" + ordertype + " and Status<>9 ", "AutoID", pageSize, pageIndex, out totalCount, out pageCount, false);
            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);

                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                list.Add(model);
            }
            return list;
        }
        public List<OrderEntity> GetOrdersByPlanTime(string startPlanTime, string endPlanTime, 
            int orderType, int filterType, int orderStatus,
            string userID, string clientID, int pageSize, int pageIndex, ref int totalCount, ref int pageCount)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataTable dt = OrdersDAL.BaseProvider.GetOrdersByPlanTime(startPlanTime, endPlanTime, 
                orderType, filterType, orderStatus,
                userID, clientID,pageSize,pageIndex,ref totalCount,ref pageCount);

            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);
                model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);
                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                if (model.OrderStatus == 1)
                {
                    if (model.PlanTime <= DateTime.Now)
                    {
                        model.WarningStatus = 2;
                        model.WarningTime = "超期:" + (DateTime.Now - model.PlanTime).Days.ToString("D2") + "天 " + (DateTime.Now - model.PlanTime).Hours.ToString("D2") + "时 " + (DateTime.Now - model.PlanTime).Minutes.ToString("D2") + "分";
                        model.WarningDays = (DateTime.Now - model.PlanTime).Days;
                        model.UseDays = (model.PlanTime - model.OrderTime).Days;
                    }
                    else if ((model.PlanTime - DateTime.Now).TotalHours * 3 < (model.PlanTime - model.OrderTime).TotalHours)
                    {
                        model.WarningStatus = 1;
                        model.WarningTime = "剩余:" + (model.PlanTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.PlanTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.PlanTime - DateTime.Now).Minutes.ToString("D2") + "分";
                        model.WarningDays = (model.PlanTime - DateTime.Now).Days;
                        model.UseDays = (DateTime.Now - model.OrderTime).Days;
                    }
                    else
                    {
                        model.WarningTime = "剩余:" + (model.PlanTime - DateTime.Now).Days.ToString("D2") + "天 " + (model.PlanTime - DateTime.Now).Hours.ToString("D2") + "时 " + (model.PlanTime - DateTime.Now).Minutes.ToString("D2") + "分";
                        model.WarningDays = (model.PlanTime - DateTime.Now).Days;
                        model.UseDays = (DateTime.Now - model.OrderTime).Days;
                    }
                }
                else if(model.OrderStatus==2)
                {
                    model.WarningStatus = 3;
                    model.UseDays = (model.PlanTime - model.OrderTime).Days;
                    model.WarningDays = (DateTime.Now-model.EndTime).Days;
                }

                list.Add(model);
            }
            return list;
        }
        /// <summary>
        /// 获取订单列表根据二当家客户端编码
        /// </summary>
        /// <param name="yxCode"></param>
        /// <param name="clientid"></param>
        /// <returns></returns>
        public List<OrderEntity> GetOrdersByYXCode(string yxCode, string clientid, int pageSize, int pageIndex, ref int totalCount, ref int pageCount)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataSet ds = OrdersDAL.BaseProvider.GetOrdersByYXCode(yxCode, clientid, pageSize,pageIndex,ref totalCount,ref pageCount);
            DataTable dt = ds.Tables["Orders"];
            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);

                list.Add(model);
            }

            return list;
        }
        public List<OrderEntity> GetOrdersByMobilePhone(string mobilePhone)
        {
            List<OrderEntity> list = new List<OrderEntity>();
            DataSet ds = OrdersDAL.BaseProvider.GetOrdersByMobilePhone(mobilePhone);

            DataTable dt = ds.Tables["Orders"];
            foreach (DataRow dr in dt.Rows)
            {
                OrderEntity model = new OrderEntity();
                model.FillData(dr);

                //model.Owner = OrganizationBusiness.GetUserByUserID(model.OwnerID, model.AgentID);

                model.StatusStr = CommonBusiness.GetEnumDesc((EnumOrderStageStatus)model.Status);

                model.SourceTypeStr = CommonBusiness.GetEnumDesc((EnumOrderSourceType)model.SourceType);

                model.Client = IntFactoryBusiness.Manage.ClientBusiness.GetClientDetail(model.ClientID);

                model.StatusItems = new List<OrderStatusEntity>();
                DataTable orderStatus = ds.Tables["Status"];
                if (model.Status > 0 && orderStatus.Rows.Count > 0)
                {
                    foreach (DataRow statu in orderStatus.Select("OrderID='" + model.OrderID + "'"))
                    {
                        OrderStatusEntity status = new IntFactoryEntity.OrderStatusEntity();
                        status.FillData(statu);
                        model.StatusItems.Add(status);
                    }
                }
                list.Add(model);
            }
            return list;
        }