//Grid数据源
        protected void GridDataBind()
        {
            try
            {
                IList <CompanyGrossProfitRecordInfo> dataList             = new List <CompanyGrossProfitRecordInfo>();
                List <CompanyGrossProfitRecordInfo>  sumDataList          = new List <CompanyGrossProfitRecordInfo>(); //汇总同一公司不同平台的数据
                List <CompanyGrossProfitRecordInfo>  sumOrderTypeDataList = new List <CompanyGrossProfitRecordInfo>(); //汇总同一公司同一订单类型不同平台的数据
                Guid     saleFilialeId = string.IsNullOrEmpty(Rcb_SaleFiliale.SelectedValue) ? Guid.Empty : new Guid(Rcb_SaleFiliale.SelectedValue);
                DateTime startTime;
                DateTime endTime;
                if (ddl_TimeType.SelectedValue == "1")//当月查询
                {
                    startTime = DateTime.Parse(txt_StartTime.Text);
                    endTime   = DateTime.Now;
                    if (!string.IsNullOrEmpty(txt_EndTime.Text))
                    {
                        endTime = DateTime.Parse(txt_EndTime.Text);
                    }
                    endTime = endTime.AddDays(1);

                    #region 汇总同一公司不同平台的数据
                    sumDataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitBySaleFilialeId(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                    #endregion

                    #region 汇总同一公司同一订单类型不同平台的数据
                    if (rbl_OrderType.SelectedValue.Equals("1"))
                    {
                        sumOrderTypeDataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitBySaleFilialeIdAndOrderType(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                    }
                    #endregion
                    else
                    {
                        #region 将查询时间段内每天的数据根据条件合并
                        dataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitDetailInfos(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value);
                        #endregion
                    }
                }
                else if (ddl_TimeType.SelectedValue == "0")
                {
                    startTime = DateTime.Parse(txt_YearAndMonth.Text);
                    if (startTime.AddMonths(1).Equals(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"))))
                    {
                        endTime = startTime.AddMonths(1);

                        #region 汇总同一公司不同平台的数据
                        sumDataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitBySaleFilialeId(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                        #endregion

                        #region 汇总同一公司同一订单类型不同平台的数据
                        if (rbl_OrderType.SelectedValue.Equals("1"))
                        {
                            sumOrderTypeDataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitBySaleFilialeIdAndOrderType(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                        }
                        #endregion
                        else
                        {
                            #region 将查询时间段内每天的数据根据条件合并
                            dataList = _companyGrossProfitRecordDetail.SumCompanyGrossProfitDetailInfos(startTime, endTime, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value);
                            #endregion
                        }
                    }
                    else
                    {
                        #region 汇总同一公司不同平台的数据
                        sumDataList = _companyGrossProfitRecord.SumCompanyGrossProfitFromMonthBySaleFilialeId(startTime, null, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                        #endregion

                        #region 汇总同一公司同一订单类型不同平台的数据
                        if (rbl_OrderType.SelectedValue.Equals("1"))
                        {
                            sumOrderTypeDataList = _companyGrossProfitRecord.SumCompanyGrossProfitFromMonthBySaleFilialeIdAndOrderType(startTime, null, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value).ToList();
                        }
                        #endregion
                        else
                        {
                            dataList = _companyGrossProfitRecord.SelectCompanyGrossProfitInfos(startTime, null, saleFilialeId, Hid_SalePlatform.Value, Hid_OrderType.Value);
                        }
                    }
                }

                #region 组合数据
                List <CompanyGrossProfitRecordInfo> list = sumDataList;
                if (rbl_OrderType.SelectedValue.Equals("1"))
                {
                    list.AddRange(sumOrderTypeDataList);
                }
                else
                {
                    list.AddRange(dataList);
                }
                #endregion

                RG_GrossProfit.DataSource = list.OrderByDescending(act => act.SaleFilialeId).ThenBy(act => act.SalesAmount).ThenByDescending(act => act.SalePlatformId).ThenByDescending(act => act.OrderType);

                #region 合计
                var totalName            = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("TotalName");
                var salesAmount          = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("SalesAmount");
                var goodsAmount          = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("GoodsAmount");
                var shipmentIncome       = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("ShipmentIncome");
                var promotionsDeductible = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("PromotionsDeductible");
                var pointsDeduction      = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("PointsDeduction");
                var shipmentCost         = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("ShipmentCost");
                var purchaseCosts        = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("PurchaseCosts");
                var profit       = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("Profit");
                var profitMargin = RG_GrossProfit.MasterTableView.Columns.FindByUniqueName("ProfitMargin");

                if (list.Any())
                {
                    var sumSalesAmount          = list.Sum(act => act.SalesAmount) / 2;
                    var sumGoodsAmount          = list.Sum(act => act.GoodsAmount) / 2;
                    var sumShipmentIncome       = list.Sum(act => act.ShipmentIncome) / 2;
                    var sumPromotionsDeductible = list.Sum(act => act.PromotionsDeductible) / 2;
                    var sumPointsDeduction      = list.Sum(act => act.PointsDeduction) / 2;
                    var sumShipmentCost         = list.Sum(act => act.ShipmentCost) / 2;
                    var sumPurchaseCosts        = list.Sum(act => act.PurchaseCosts) / 2;
                    var sumProfit       = (sumSalesAmount - sumShipmentIncome) - sumPurchaseCosts;
                    var sumProfitMargin = sumProfit / (sumSalesAmount - sumShipmentIncome);

                    totalName.FooterText            = "公司合计:";
                    salesAmount.FooterText          = string.Format("{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumSalesAmount - sumShipmentIncome));
                    goodsAmount.FooterText          = string.Format("<span style=\"color: blue;\">(&nbsp;</span>{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumGoodsAmount));
                    shipmentIncome.FooterText       = string.Format("<span style=\"color: blue;\">+&nbsp;</span>{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumShipmentIncome));
                    promotionsDeductible.FooterText = string.Format("<span style=\"color: blue;\">-&nbsp;</span>{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumPromotionsDeductible));
                    pointsDeduction.FooterText      = string.Format("<span style=\"color: blue;\">-&nbsp;</span>{0}<span style=\"color: blue;\">&nbsp;)</span>", ERP.UI.Web.Common.WebControl.NumberSeparator(sumPointsDeduction));
                    shipmentCost.FooterText         = string.Format("<span style=\"color: blue;\">-&nbsp;</span>{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumShipmentCost));
                    purchaseCosts.FooterText        = string.Format("<span style=\"color: blue;\">-&nbsp;</span>{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumPurchaseCosts));
                    profit.FooterText       = string.Format("{0}", ERP.UI.Web.Common.WebControl.NumberSeparator(sumProfit));
                    profitMargin.FooterText = string.Format("{0}{1}", (sumProfitMargin * 100).ToString("F"), "%");
                }
                else
                {
                    salesAmount.FooterText          = string.Empty;
                    goodsAmount.FooterText          = string.Empty;
                    shipmentIncome.FooterText       = string.Empty;
                    promotionsDeductible.FooterText = string.Empty;
                    pointsDeduction.FooterText      = string.Empty;
                    shipmentCost.FooterText         = string.Empty;
                    purchaseCosts.FooterText        = string.Empty;
                    profit.FooterText       = string.Empty;
                    profitMargin.FooterText = string.Empty;
                }
                #endregion
            }
            catch (Exception ex)
            {
                SAL.LogCenter.LogService.LogError("公司毛利查询异常", "财务管理", ex);
            }
        }
        /// <summary>
        /// 计算每月的数据
        /// </summary>
        /// <param name="recordTime"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool CompanyGrossProfitForEveryMonth(DateTime recordTime, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (recordTime.Day == 1)
            {
                //每月1号计算上上个月的公司毛利

                /*
                 * 例如:当前时间是6月1号,5月份的数据暂不合计(原因:5月份的数据是不准确的,一是因为可能有些订单在5月份还没有完成,即完成时间可能是在6月份的,如果此时合计5月份的数据,会遗漏完成时间是6月份的订单;二是可能有些订单还没有天猫佣金)
                 * 故此时只合计4月份的数据(原因:4月份的数据是相对准确的,一是因为大部分的订单在一个自然月内应该都已经完成了;二是因为天猫佣金是在用户确认收货的时候才生成的,为兼容天猫大型活动(如双十一活动)时收货期延长,故隔月计算时的数据是相对准确的)
                 * 此处佣金生成都按照用户未确认收货,系统自动确认收货来考虑;
                 */

                #region 4月份数据处理(合计订单时间是4月份的数据)
                DateTime startTime = recordTime.AddMonths(-2);
                DateTime endTime   = recordTime.AddMonths(-1);
                if (_companyGrossProfitRecord.Exists(startTime))
                {
                    return(true);
                }

                //合计公司毛利明细
                var data = _companyGrossProfitRecordDetail.SumCompanyGrossProfitDetailInfos(startTime, endTime, Guid.Empty, string.Empty, string.Empty);
                if (data.Any())
                {
                    using (var ts = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(10)))
                    {
                        try
                        {
                            int i = 0;
                            while (true)
                            {
                                var companyGrossProfitRecordList = data.Skip(i * _grossProfitPageSize).Take(_grossProfitPageSize).ToList();
                                if (companyGrossProfitRecordList.Count > 0)
                                {
                                    _companyGrossProfitRecord.AddData(companyGrossProfitRecordList);
                                }
                                else
                                {
                                    break;
                                }
                                i++;
                            }

                            _companyGrossProfitRecordDetail.UpdateState(startTime, endTime);
                            ts.Complete();
                        }
                        catch (Exception ex)
                        {
                            errorMsg = startTime.ToString("yyyy-MM") + "公司毛利存档失败!" + ex.Message;
                            return(false);
                        }
                        finally
                        {
                            //释放资源
                            ts.Dispose();
                        }
                    }
                }
                else
                {
                    errorMsg = startTime.ToString("yyyy-MM") + "公司毛利存档失败!(当月没有数据)";
                }
                #endregion
            }
            return(true);
        }