public async Task <IActionResult> Create()
        {
            var vm = new OutStorageViewModel();

            await Init(vm);

            List <OutStoDetailViewModel> outStorDetails = new List <OutStoDetailViewModel>();

            HttpContext.Session.Set("OutStorDetail", SerializeHelper.SerializeToBinary(outStorDetails));
            return(View(vm));
        }
        public async Task Init(OutStorageViewModel vm)
        {
            vm.Customers = await _basicInfoServices.GetCustomers(GetAccount());

            vm.Customers.Insert(0, new SelectListItem()
            {
                Value = "0", Text = ""
            });
            //vm.Goods = await _basicInfoServices.GetGoods(GetAccount());
            //vm.Customers = await _basicInfoServices.GetCustomers(GetAccount());
            //vm.Measures = await _basicInfoServices.GetMeasures(GetAccount());
        }
        public async Task <ServiceResponseBase> Create(OutStorageViewModel vm, List <OutStoDetailViewModel> outStorDetails, AccountViewModel account)
        {
            try
            {
                var outStorage = Mapper.Map <OutStorage>(vm);
                outStorage.Status     = (int)OutOpStatus.待下架;
                outStorage.Num        = outStorDetails.Sum(o => o.Num);
                outStorage.CreateTime = DateTime.Now;
                outStorage.CreateUser = account.Name;
                outStorage.CompanyId  = account.CompanyId;
                outStorage.StorageNum = account.BranchId.ToString();
                var cusId = Convert.ToInt32(vm.CusNum);
                var cus   = _context.Customer.FirstOrDefault(o => o.Id == cusId);
                outStorage.CusName = cus.CusName;
                await _context.OutStorage.AddAsync(outStorage);

                foreach (var outStorDetail in outStorDetails)
                {
                    var entry = Mapper.Map <OutStoDetail>(outStorDetail);
                    entry.OrderNum   = outStorage.OrderNum;
                    entry.CreateTime = DateTime.Now;
                    entry.BarCode    = entry.BarCode;
                    entry.SnNum      = Guid.NewGuid().ToString("N");
                    await _context.OutStoDetail.AddAsync(entry);
                }
                await _context.SaveChangesAsync();

                return(new ServiceResponseBase()
                {
                    Status = Status.ok
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return(new ServiceResponseBase()
                {
                    Status = Status.error, Message = ex.Message
                });
            }
        }
        public async Task <IActionResult> Edit(int id, OutStorageViewModel vm)
        {
            List <OutStoDetailViewModel> outStorDetails = new List <OutStoDetailViewModel>();

            byte[] outStorDetailsByte = null;
            if (HttpContext.Session.TryGetValue("OutStorDetail", out outStorDetailsByte))
            {
                outStorDetails = (List <OutStoDetailViewModel>)SerializeHelper.DeserializeWithBinary(outStorDetailsByte);
            }
            var result = await _outStorageServices.Update(vm, outStorDetails, GetAccount());

            if (result.Status == Status.ok)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                ModelState.AddModelError("", "保存失败: " + result.Message);
                await Init(vm);

                return(View(vm));
            }
        }
        public async Task <ServiceResponseBase> Update(OutStorageViewModel vm, List <OutStoDetailViewModel> outStorDetails, AccountViewModel account)
        {
            try
            {
                var outStorage = await _context.OutStorage.FirstOrDefaultAsync(o => o.Id == vm.Id);

                outStorage.OutType = vm.OutType;
                outStorage.CusNum  = vm.CusNum;
                outStorage.CusName = vm.CusName;
                outStorage.Contact = vm.Contact;
                outStorage.Phone   = vm.Phone;
                outStorage.Remark  = vm.Remark;
                var cusId = Convert.ToInt32(vm.CusNum);
                var cus   = _context.Customer.FirstOrDefault(o => o.Id == cusId);
                outStorage.CusName = cus.CusName;
                var status = (OutOpStatus)outStorage.Status;
                if (status == OutOpStatus.待下架)
                {
                    outStorage.Num = outStorDetails.Sum(o => o.Num);
                }
                _context.Entry(outStorage).State = EntityState.Modified;
                var _outStorDetails = await _context.OutStoDetail.Where(o => o.OrderNum == outStorage.OrderNum).ToListAsync();

                foreach (var _outStorDetail in _outStorDetails)
                {
                    var outStorDetail = outStorDetails.FirstOrDefault(o => o.ProductNum == _outStorDetail.ProductNum &&
                                                                      o.LocalNum == _outStorDetail.LocalNum);
                    if (outStorDetail == null)
                    {
                        _context.OutStoDetail.Remove(_outStorDetail);
                    }
                }

                foreach (var outStorDetail in outStorDetails)
                {
                    var _outStorDetail = _outStorDetails.FirstOrDefault(o => o.ProductNum == outStorDetail.ProductNum &&
                                                                        o.LocalNum == outStorDetail.LocalNum);
                    if (_outStorDetail != null)
                    {
                        if (status == OutOpStatus.待下架)
                        {
                            _outStorDetail.Num = outStorDetail.Num;
                        }
                        if (status == OutOpStatus.已下架)
                        {
                            _outStorDetail.RealNum = outStorDetail.Num;
                        }
                        if (status == OutOpStatus.已出库)
                        {
                            _outStorDetail.PutRealNum = outStorDetail.Num;
                        }
                        _outStorDetail.OutPrice = outStorDetail.OutPrice;
                    }
                    else
                    {
                        var entry = Mapper.Map <OutStoDetail>(outStorDetail);
                        entry.OrderNum   = outStorage.OrderNum;
                        entry.CreateTime = DateTime.Now;
                        entry.BarCode    = entry.BarCode;
                        entry.SnNum      = Guid.NewGuid().ToString("N");
                        if (status == OutOpStatus.待下架)
                        {
                            entry.Num = outStorDetail.Num;
                        }
                        if (status == OutOpStatus.已下架)
                        {
                            entry.RealNum = outStorDetail.Num;
                        }
                        if (status == OutOpStatus.已出库)
                        {
                            entry.PutRealNum = outStorDetail.Num;
                        }
                        await _context.OutStoDetail.AddAsync(entry);
                    }
                }
                await _context.SaveChangesAsync();

                return(new ServiceResponseBase()
                {
                    Status = Status.ok
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return(new ServiceResponseBase()
                {
                    Status = Status.error, Message = ex.Message
                });
            }
        }