/// <summary>
        /// 根据参数生成并保存盘点数据  --异动生成
        /// </summary>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ChangedAdd(string beginDate, string endDate, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse>      wareQuery     = WarehouseRepository.GetQueryable();
            IQueryable <Storage>        storageQuery  = StorageRepository.GetQueryable();
            IQueryable <InBillAllot>    inAllotQuery  = InBillAllotRepository.GetQueryable();
            IQueryable <OutBillAllot>   outAllotQuery = OutBillAllotRepository.GetQueryable();
            IQueryable <MoveBillDetail> moveBillQuery = MoveBillDetailRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (beginDate == string.Empty || beginDate == null)
                {
                    beginDate = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
                }
                if (endDate == string.Empty || endDate == null)
                {
                    endDate = DateTime.Now.ToString("yyyy-MM-dd");
                }
                DateTime begin = Convert.ToDateTime(beginDate);
                DateTime end   = Convert.ToDateTime(endDate);
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        Console.WriteLine("start");
                        var sw = new Stopwatch();
                        sw.Start();

                        #region 循环所有仓库的订单,一个仓库一个盘点单据
                        var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                        foreach (var item in warehouses.ToArray())
                        {
                            var inCells      = inAllotQuery.Where(i => i.FinishTime >= begin && i.FinishTime <= end && i.Cell.WarehouseCode == item.WarehouseCode).OrderBy(i => i.CellCode).Select(i => i.CellCode);
                            var outCells     = outAllotQuery.Where(o => o.FinishTime >= begin && o.FinishTime <= end && o.Cell.WarehouseCode == item.WarehouseCode).OrderBy(o => o.CellCode).Select(o => o.CellCode);
                            var moveInCells  = moveBillQuery.Where(m => m.FinishTime >= begin && m.FinishTime <= end && m.InCell.WarehouseCode == item.WarehouseCode).OrderBy(m => m.InCell.CellCode).Select(m => m.InCell.CellCode);
                            var moveOutCells = moveBillQuery.Where(m => m.FinishTime >= begin && m.FinishTime <= end && m.OutCell.WarehouseCode == item.WarehouseCode).OrderBy(m => m.OutCell.CellCode).Select(m => m.OutCell.CellCode);
                            var storages     = storageQuery.Where(s => s.Quantity > 0 && s.IsLock == "0" && (inCells.Any(i => i == s.CellCode) || outCells.Any(o => o == s.CellCode) || moveInCells.Any(mi => mi == s.CellCode) || moveOutCells.Any(mo => mo == s.CellCode)))
                                               .OrderBy(s => s.ProductCode)
                                               .Select(s => s).ToArray();
                            if (storages.Any())
                            {
                                string billNo = GetCheckBillNo().ToString();
                                var    check  = new CheckBillMaster();
                                check.BillNo          = billNo;
                                check.BillDate        = DateTime.Now;
                                check.BillTypeCode    = billType;
                                check.WarehouseCode   = item.WarehouseCode;
                                check.OperatePersonID = employee.ID;
                                check.Status          = "1";
                                check.IsActive        = "1";
                                check.UpdateTime      = DateTime.Now;

                                CheckBillMasterRepository.Add(check);
                                storages.AsParallel().ForAll(stor =>
                                {
                                    var checkDetail             = new CheckBillDetail();
                                    checkDetail.CellCode        = stor.CellCode;
                                    checkDetail.StorageCode     = stor.StorageCode;
                                    checkDetail.ProductCode     = stor.ProductCode;
                                    checkDetail.UnitCode        = stor.Product.UnitCode;
                                    checkDetail.Quantity        = stor.Quantity;
                                    checkDetail.RealProductCode = stor.ProductCode;
                                    checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                    checkDetail.RealQuantity    = stor.Quantity;
                                    checkDetail.Status          = "0";
                                    lock (check.CheckBillDetails)
                                    {
                                        check.CheckBillDetails.Add(checkDetail);
                                    }
                                    stor.IsLock = "1";
                                });
                                result = true;
                            }
                            else
                            {
                                info = "所选择查询的时间无数据!";
                            }
                        }

                        CheckBillMasterRepository.SaveChanges();
                        #endregion

                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);

                        sw.Restart();
                        CheckBillMasterRepository.SaveChanges();
                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);
                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    result = false;
                    info   = e.Message;
                }
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --产品生成
        /// </summary>
        /// <param name="products">产品数据</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ProductAdd(string products, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (products != null && products != string.Empty)
                {
                    products = products.Substring(0, products.Length - 1);
                    try
                    {
                        using (var scope = new TransactionScope())
                        {
                            Console.WriteLine("start");
                            var sw = new Stopwatch();
                            sw.Start();

                            #region products 这个有值,就把这个值里面所有的卷烟信息所在的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据
                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Product != null && products.Contains(s.ProductCode) && s.Cell.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的产品无数据!";
                                }
                            }
                            #endregion

                            sw.Stop();
                            Console.WriteLine(sw.ElapsedMilliseconds);

                            sw.Restart();
                            CheckBillMasterRepository.SaveChanges();
                            sw.Stop();
                            Console.WriteLine(sw.ElapsedMilliseconds);
                            scope.Complete();
                        }
                    }
                    catch (Exception e)
                    {
                        result = false;
                        info   = e.Message;
                    }
                }
                else
                {
                    info = "产品信息为空!无法生成!";
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --货位生成
        /// </summary>
        /// <param name="ware">仓库</param>
        /// <param name="area">库区</param>
        /// <param name="shelf">货架</param>
        /// <param name="cell">货位</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool CellAdd(string ware, string area, string shelf, string cell, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        Console.WriteLine("start");
                        var sw = new Stopwatch();
                        sw.Start();

                        #region ware 这个有值,就把这个值里面所有的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据

                        if (ware != null && ware != string.Empty)
                        {
                            ware = ware.Substring(0, ware.Length - 1);
                            var wares = wareQuery.Where(w => ware.Contains(w.WarehouseCode)).ToArray();

                            foreach (var item in wares)
                            {
                                var storages = storageQuery.Where(s => s.Cell.WarehouseCode == item.WarehouseCode &&
                                                                  s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的货位无数据!";
                                }
                            }
                        }
                        #endregion

                        #region ware 这个无值,把全部仓库里面包含area,shelf,cell的数据生成盘点单,一个仓库一个盘点单据

                        if (area != null && area != string.Empty || shelf != null && shelf != string.Empty || cell != null && cell != string.Empty)
                        {
                            if (area != string.Empty)
                            {
                                area = area.Substring(0, area.Length - 1);
                            }
                            if (shelf != string.Empty)
                            {
                                shelf = shelf.Substring(0, shelf.Length - 1);
                            }
                            if (cell != string.Empty)
                            {
                                cell = cell.Substring(0, cell.Length - 1);
                            }

                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);

                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Cell.WarehouseCode == item.WarehouseCode &&
                                                                  (area.Contains(s.Cell.Shelf.Area.AreaCode) ||
                                                                   shelf.Contains(s.Cell.Shelf.ShelfCode) ||
                                                                   cell.Contains(s.Cell.CellCode)) &&
                                                                  s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                            }
                        }
                        #endregion

                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);

                        sw.Restart();
                        CheckBillMasterRepository.SaveChanges();
                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);
                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    result = false;
                    info   = e.Message;
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --产品生成
        /// </summary>
        /// <param name="products">产品数据</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ProductAdd(string products, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (products != null && products != string.Empty)
                {
                    products = products.Substring(0, products.Length - 1);
                    try
                    {
                        using (var scope = new TransactionScope())
                        {
                            #region products 这个有值,就把这个值里面所有的卷烟信息所在的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据
                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.ProductCode != null && products.Contains(s.ProductCode) && s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";
                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                    }
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的产品无数据!";
                                }
                            }
                            #endregion
                            scope.Complete();
                        }
                    }
                    catch (Exception e)
                    {
                        info = e.Message;
                    }
                }
                else
                {
                    info = "产品信息为空!无法生成!";
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --货位生成
        /// </summary>
        /// <param name="ware">仓库</param>
        /// <param name="area">库区</param>
        /// <param name="shelf">货架</param>
        /// <param name="cell">货位</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool CellAdd(string ware, string area, string shelf, string cell, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        #region ware 这个有值,就把这个值里面所有的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据

                        if (ware != null && ware != string.Empty)
                        {
                            ware = ware.Substring(0, ware.Length - 1);
                            var wares = wareQuery.Where(w => ware.Contains(w.WarehouseCode));

                            foreach (var item in wares.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.IsLock,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";

                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                        scope.Complete();
                                    }
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的货位无数据!";
                                }
                            }
                        }
                        #endregion

                        #region ware 这个无值,把全部仓库里面包含area,shelf,cell的数据生成盘点单,一个仓库一个盘点单据

                        if (area != null && area != string.Empty || shelf != null && shelf != string.Empty || cell != null && cell != string.Empty)
                        {
                            if (area != string.Empty)
                            {
                                area = area.Substring(0, area.Length - 1);
                            }
                            if (shelf != string.Empty)
                            {
                                shelf = shelf.Substring(0, shelf.Length - 1);
                            }
                            if (cell != string.Empty)
                            {
                                cell = cell.Substring(0, cell.Length - 1);
                            }

                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);

                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.ToList().Where(s => s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && (area.Contains(s.Cell.Shelf.Area.AreaCode) || shelf.Contains(s.Cell.Shelf.ShelfCode) || cell.Contains(s.Cell.CellCode)) && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";
                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                        scope.Complete();
                                    }
                                    result = true;
                                }
                            }
                        }
                        #endregion

                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    info = e.Message;
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        //保存
        private void tsbSave_Click(object sender, EventArgs e)
        {
            gridView1.UpdateCurrentRow();

            if (dateTimePicker1.Text == "")
            {
                this.ShowAlertMessage("请选择日期!");
                return;
            }

            if (cboDepot.Text == "")
            {
                this.ShowAlertMessage("必须选择仓库!");
                return;
            }

            if (cboHandlePerson.Text == "")
            {
                this.ShowAlertMessage("必须选择经手人!");
                return;
            }

            if (gridView1.RowCount <= 0)
            {
                this.ShowAlertMessage("必须增加货品明细数据!");
                return;
            }

            bool boolvalue = false;

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                DataRowView dr = (DataRowView)(gridView1.GetRow(i));
                if (dr[6].ToString().Trim() != "" && dr[7].ToString().Trim() != "")
                {
                    if (decimal.Parse(dr[6].ToString()) > 0 && decimal.Parse(dr[7].ToString()) > 0)
                    {
                        boolvalue = true;
                        break;
                    }
                }
            }
            if (boolvalue == true)
            {
                this.ShowAlertMessage("同一行数据盘盈或盘亏只能填一项,另一项必须为空或0!");
                return;
            }


            CheckBill CheckBill = new CheckBill();

            CheckBill.CheckBillGuid = txtGuid.Text;
            CheckBill.BillDate      = DateTime.Parse(dateTimePicker1.Text);
            CheckBill.BillID        = txtBillID.Text;
            CheckBill.Depot         = cboDepot.Text;
            if (txtCreateDate.Text.Trim() == "")
            {
                CheckBill.CreateDate = DateTime.Now;
            }
            else
            {
                CheckBill.CreateDate = DateTime.Parse(txtCreateDate.Text);
            }
            CheckBill.CreatePerson = SysParams.UserName;
            CheckBill.CheckDate    = DateTime.Parse("1900-01-01");
            CheckBill.CheckPerson  = "";
            CheckBill.Remark       = txtRemark.Text;
            CheckBill.BillAutoID   = txtAutoBillID.Text;
            CheckBill.HandlePerson = cboHandlePerson.Text;


            List <CheckBillDetail> list            = new List <CheckBillDetail>();
            CheckBillDetail        CheckBillDetail = new CheckBillDetail();

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                DataRowView dr = (DataRowView)(gridView1.GetRow(i));

                CheckBillDetail = new CheckBillDetail();
                CheckBillDetail.CheckBillDetailGuid = Guid.NewGuid().ToString();
                CheckBillDetail.CheckBillGuid       = CheckBill.CheckBillGuid;

                CheckBillDetail.MaterialGuid = dr[0].ToString(); //gridView1.GetRowCellValue(i, gridMaterialGuid).ToString();
                CheckBillDetail.MaterialID   = dr[1].ToString(); //gridView1.GetRowCellValue(i, gridMaterialGuid).ToString();
                CheckBillDetail.MaterialName = dr[2].ToString(); //gridView1.GetRowCellValue(i, gridMaterialName).ToString();
                CheckBillDetail.BarNo        = dr[3].ToString(); //gridView1.GetRowCellValue(i, gridMaterialName).ToString();
                CheckBillDetail.Spec         = dr[4].ToString(); // gridView1.GetRowCellValue(i, gridSpec).ToString();
                CheckBillDetail.Unit         = dr[5].ToString(); //gridView1.GetRowCellValue(i, gridUnit).ToString();
                if (dr[6].ToString().Trim() != "")
                {
                    CheckBillDetail.SurplusQty = decimal.Parse(dr[6].ToString());//int.Parse(gridView1.GetRowCellValue(i, gridQty).ToString());
                }
                else
                {
                    CheckBillDetail.SurplusQty = 0;
                }
                if (dr[7].ToString().Trim() != "")
                {
                    CheckBillDetail.DeficientQty = decimal.Parse(dr[7].ToString());//int.Parse(gridView1.GetRowCellValue(i, gridQty).ToString());
                }
                else
                {
                    CheckBillDetail.DeficientQty = 0;
                }


                if (dr[8].ToString().Trim() != "")
                {
                    CheckBillDetail.Price = decimal.Parse(dr[8].ToString()); //decimal.Parse(gridView1.GetRowCellValue(i, gridPrice).ToString());
                }
                else
                {
                    CheckBillDetail.Price = 0;
                }

                if (dr[9].ToString().Trim() != "")
                {
                    CheckBillDetail.Total = decimal.Parse(dr[9].ToString()); //decimal.Parse(gridView1.GetRowCellValue(i, gridTotal ).ToString());
                }
                else
                {
                    CheckBillDetail.Total = 0;
                }

                CheckBillDetail.Remark = dr[10].ToString();//gridView1.GetRowCellValue(i, gridRemark).ToString();
                list.Add(CheckBillDetail);
            }

            //保存
            rbm.SaveCheckBill(CheckBill, list);

            //刷新
            if (SendFlag == 0)
            {
                frmCheckBill.frmcheckbill.LoadBill();
            }


            tsbCheckPass.Enabled = true;

            this.ShowMessage("保存成功");
        }