/// <summary>
        /// Insert into XMInventoryInfoStatistics
        /// </summary>
        /// <param name="xminventoryinfostatistics">XMInventoryInfoStatistics</param>
        public void InsertXMInventoryInfoStatistics(XMInventoryInfoStatistics xminventoryinfostatistics)
        {
            if (xminventoryinfostatistics == null)
            {
                return;
            }

            if (!this._context.IsAttached(xminventoryinfostatistics))
            {
                this._context.XMInventoryInfoStatistics.AddObject(xminventoryinfostatistics);
            }

            this._context.SaveChanges();
        }
        /// <summary>
        /// Update into XMInventoryInfoStatistics
        /// </summary>
        /// <param name="xminventoryinfostatistics">XMInventoryInfoStatistics</param>
        public void UpdateXMInventoryInfoStatistics(XMInventoryInfoStatistics xminventoryinfostatistics)
        {
            if (xminventoryinfostatistics == null)
            {
                return;
            }

            if (this._context.IsAttached(xminventoryinfostatistics))
            {
                this._context.XMInventoryInfoStatistics.Attach(xminventoryinfostatistics);
            }

            this._context.SaveChanges();
        }
        /// <summary>
        /// 定时执行根据库存信息统计进销存明细
        /// </summary>
        public void AutoStatisticsInventoryInfo()
        {
            int      year          = DateTime.Now.Year;
            int      month         = DateTime.Now.Month - 1;
            DateTime startDate     = new DateTime(year, month, 1, 0, 0, 0);
            DateTime endDate       = DateTime.Now;
            var      inventoryInfo = IoC.Resolve <XMInventoryInfoService>().GetXMInventoryInfoList();

            if (inventoryInfo != null && inventoryInfo.Count > 0)
            {
                foreach (XMInventoryInfo Info in inventoryInfo)
                {
                    var products = IoC.Resolve <XMProductService>().getXMProductByManufacturersCode(Info.PlatformMerchantCode);

                    var lastInventoryStatistics = GetXMInventoryInfoStatisticsByParm(year, month - 1, Info.WfId, Info.PlatformMerchantCode); //统计月份上个月
                    if (lastInventoryStatistics != null)                                                                                     //上上个月数据存在   上上个月库存 等于下个月的期初库存
                    {
                        var InventoryStatistics = GetXMInventoryInfoStatisticsByParm(year, month, Info.WfId, Info.PlatformMerchantCode);
                        if (InventoryStatistics == null)   //数据不存在新增
                        {
                            int     purStorageCount   = 0; //采购入库数量
                            decimal purStorageMoney   = 0; //采购入库金额
                            int     saleDeliveryCount = 0; //出库数量
                            decimal saleDeliveryMoney = 0; //出库金额
                            //新增
                            XMInventoryInfoStatistics parm = new XMInventoryInfoStatistics();
                            parm.WfID  = Info.WfId;
                            parm.Year  = year;
                            parm.Month = month;
                            if (products != null)
                            {
                                parm.ManufacturersCode = Info.PlatformMerchantCode;
                                parm.ProductName       = products.ProductName;
                                parm.Specifications    = products.Specifications;
                            }
                            parm.InventoryCount = Info.StockNumber;           //库存数量
                            //通过移动加权法计算成本平均单价
                            decimal inventPrice = GetInventPrice(Info.PlatformMerchantCode);
                            parm.InventoryMoney = parm.InventoryCount * inventPrice;     //库存金额
                            //采购入库记录
                            var storageProductDetails = IoC.Resolve <XMStorageProductDetailsService>().GetXMStorageProductDetailsListByParm(startDate, endDate, Info.WfId, Info.PlatformMerchantCode);
                            if (storageProductDetails != null && storageProductDetails.Count > 0)
                            {
                                foreach (XMStorageProductDetails p in storageProductDetails)
                                {
                                    purStorageCount += p.ProductsCount;
                                    purStorageMoney += p.ProductsPrice * p.ProductsCount;
                                }
                            }
                            parm.StorageCount = purStorageCount;
                            parm.StorageMoney = purStorageMoney;
                            //销售出库记录
                            var saleDeliveryProductDetails = IoC.Resolve <XMSaleDeliveryProductDetailsService>().GetXMSaleDeliveryProductDetailsByParm(startDate, endDate, Info.WfId, Info.PlatformMerchantCode);
                            if (saleDeliveryProductDetails != null && saleDeliveryProductDetails.Count > 0)
                            {
                                foreach (XMSaleDeliveryProductDetails t in saleDeliveryProductDetails)
                                {
                                    saleDeliveryCount += t.SaleCount.Value;
                                    saleDeliveryMoney += t.SaleCount.Value * t.ProductPrice.Value;
                                }
                            }
                            parm.DeliveryCount = saleDeliveryCount;
                            parm.DeliveryMoney = saleDeliveryMoney;
                            parm.InitialCount  = lastInventoryStatistics.InventoryCount;
                            parm.InitialMoney  = lastInventoryStatistics.InventoryMoney;
                            parm.IsEnable      = false;
                            parm.CreateDate    = DateTime.Now;
                            IoC.Resolve <XMInventoryInfoStatisticsService>().InsertXMInventoryInfoStatistics(parm);
                        }
                    }
                    else                                  //上上个月数据不存在    下月期初库存需要通过库存进行计算
                    {
                        var InventoryStatistics = GetXMInventoryInfoStatisticsByParm(year, month, Info.WfId, Info.PlatformMerchantCode);
                        if (InventoryStatistics == null)    //数据不存在新增
                        {
                            int     purStorageCount2   = 0; //采购入库数量
                            decimal purStorageMoney2   = 0; //采购入库金额
                            int     saleDeliveryCount2 = 0; //出库数量
                            decimal saleDeliveryMoney2 = 0; //出库金额
                            //新增
                            XMInventoryInfoStatistics Statistics = new XMInventoryInfoStatistics();
                            Statistics.WfID  = Info.WfId;
                            Statistics.Year  = year;
                            Statistics.Month = month;
                            if (products != null)
                            {
                                Statistics.ManufacturersCode = Info.PlatformMerchantCode;
                                Statistics.ProductName       = products.ProductName;
                                Statistics.Specifications    = products.Specifications;
                            }
                            Statistics.InventoryCount = Info.StockNumber;           //库存数量
                            //通过移动加权法计算成本平均单价
                            decimal inventPrice = GetInventPrice(Info.PlatformMerchantCode);
                            Statistics.InventoryMoney = Statistics.InventoryCount * inventPrice;     //库存金额
                            //采购入库记录
                            var storageProductDetails = IoC.Resolve <XMStorageProductDetailsService>().GetXMStorageProductDetailsListByParm(startDate, endDate, Info.WfId, Info.PlatformMerchantCode);
                            if (storageProductDetails != null && storageProductDetails.Count > 0)
                            {
                                foreach (XMStorageProductDetails p in storageProductDetails)
                                {
                                    purStorageCount2 += p.ProductsCount;
                                    purStorageMoney2 += p.ProductsPrice * p.ProductsCount;
                                }
                            }
                            Statistics.StorageCount = purStorageCount2;
                            Statistics.StorageMoney = purStorageMoney2;
                            //销售出库记录
                            var saleDeliveryProductDetails = IoC.Resolve <XMSaleDeliveryProductDetailsService>().GetXMSaleDeliveryProductDetailsByParm(startDate, endDate, Info.WfId, Info.PlatformMerchantCode);
                            if (saleDeliveryProductDetails != null && saleDeliveryProductDetails.Count > 0)
                            {
                                foreach (XMSaleDeliveryProductDetails t in saleDeliveryProductDetails)
                                {
                                    saleDeliveryCount2 += t.SaleCount.Value;
                                    saleDeliveryMoney2 += t.SaleCount.Value * t.ProductPrice.Value;
                                }
                            }
                            Statistics.DeliveryCount = saleDeliveryCount2;
                            Statistics.DeliveryMoney = saleDeliveryMoney2;
                            //期初数据 通过库存进行推算
                            int storageRejectedCount = StorgedRejectdCount(startDate, endDate, Info.WfId, Info.PlatformMerchantCode);           //已入库退货数量
                            Statistics.InitialCount = Info.StockNumber.Value == 0 ? 0 : Info.StockNumber.Value - purStorageCount2 + saleDeliveryCount2 + storageRejectedCount;
                            Statistics.InitialMoney = inventPrice * Statistics.InitialCount;
                            Statistics.IsEnable     = false;
                            Statistics.CreateDate   = DateTime.Now;
                            IoC.Resolve <XMInventoryInfoStatisticsService>().InsertXMInventoryInfoStatistics(Statistics);
                        }
                    }
                }
            }
        }