public async Task <GarmentPreparing> Handle(RemoveGarmentPreparingCommand request, CancellationToken cancellationToken)
        {
            var garmentPreparing = _garmentPreparingRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentPreparing == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            var garmentPreparingItems = _garmentPreparingItemRepository.Find(x => x.GarmentPreparingId == request.Id);

            foreach (var item in garmentPreparingItems)
            {
                item.Remove();
                await _garmentPreparingItemRepository.Update(item);
            }

            garmentPreparing.Remove();

            await _garmentPreparingRepository.Update(garmentPreparing);

            _storage.Save();

            return(garmentPreparing);
        }
Exemplo n.º 2
0
        public async Task <GarmentPreparing> Handle(UpdateGarmentPreparingCommand request, CancellationToken cancellaitonToken)
        {
            var garmentPreparing = _garmentPreparingRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentPreparing == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            garmentPreparing.setUENId(request.UENId);
            garmentPreparing.setUENNo(request.UENNo);
            garmentPreparing.SetUnitId(new UnitDepartmentId(request.Unit.Id));
            garmentPreparing.setUnitCode(request.Unit.Code);
            garmentPreparing.setUnitName(request.Unit.Name);
            garmentPreparing.setProcessDate(request.ProcessDate);
            garmentPreparing.setRONo(request.RONo);
            garmentPreparing.setArticle(request.Article);
            garmentPreparing.setIsCuttingIN(request.IsCuttingIn);

            var dbGarmentPreparing = _garmentPreparingItemRepository.Find(y => y.GarmentPreparingId == garmentPreparing.Identity);
            var updatedItems       = request.Items.Where(x => dbGarmentPreparing.Any(y => y.GarmentPreparingId == garmentPreparing.Identity));
            var addedItems         = request.Items.Where(x => !dbGarmentPreparing.Any(y => y.GarmentPreparingId == garmentPreparing.Identity));
            var deletedItems       = dbGarmentPreparing.Where(x => !request.Items.Any(y => y.GarmentPreparingId == garmentPreparing.Identity));

            foreach (var item in updatedItems)
            {
                var dbItem = dbGarmentPreparing.Find(x => x.Identity == item.Identity);
                dbItem.setBasicPrice(item.BasicPrice);
                dbItem.setDesignColor(item.DesignColor);
                dbItem.setFabricType(item.FabricType);
                dbItem.setProduct(new ProductId(item.Product.Id));
                dbItem.setProductCode(item.Product.Code);
                dbItem.setProductName(item.Product.Name);
                dbItem.setQuantity(item.Quantity);
                dbItem.setRemainingQuantity(item.RemainingQuantity);
                dbItem.setUenItemId(item.UENItemId);
                dbItem.setUomId(new UomId(item.Uom.Id));
                dbItem.setUomUnit(item.Uom.Unit);
                await _garmentPreparingItemRepository.Update(dbItem);
            }

            addedItems.Select(x => new GarmentPreparingItem(Guid.NewGuid(), x.UENItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit, x.FabricType, x.RemainingQuantity, x.BasicPrice, garmentPreparing.Identity, x.ROSource)).ToList()
            .ForEach(async x => await _garmentPreparingItemRepository.Update(x));

            foreach (var item in deletedItems)
            {
                item.SetDeleted();
                await _garmentPreparingItemRepository.Update(item);
            }


            garmentPreparing.SetModified();

            await _garmentPreparingRepository.Update(garmentPreparing);

            _storage.Save();

            return(garmentPreparing);
        }
        public async Task <IActionResult> Get(int page = 1, int size = 25, string order = "{}", [Bind(Prefix = "Select[]")] List <string> select = null, string keyword = null, string filter = "{}")
        {
            VerifyUser();
            var query                        = _garmentPreparingRepository.Read(order, select, filter);
            int totalRows                    = query.Count();
            var garmentPreparingDto          = _garmentPreparingRepository.Find(query).Select(o => new GarmentPreparingDto(o)).ToArray();
            var garmentPreparingItemDto      = _garmentPreparingItemRepository.Find(_garmentPreparingItemRepository.Query).Select(o => new GarmentPreparingItemDto(o)).ToList();
            var garmentPreparingItemDtoArray = _garmentPreparingItemRepository.Find(_garmentPreparingItemRepository.Query).Select(o => new GarmentPreparingItemDto(o)).ToArray();

            Parallel.ForEach(garmentPreparingDto, itemDto =>
            {
                var garmentPreparingItems = garmentPreparingItemDto.Where(x => x.GarmentPreparingId == itemDto.Id).ToList();

                itemDto.Items         = garmentPreparingItems;
                itemDto.Items         = itemDto.Items.OrderBy(x => x.Id).ToList();
                itemDto.TotalQuantity = garmentPreparingItems.Sum(x => x.Quantity);
            });

            if (!string.IsNullOrEmpty(keyword))
            {
                garmentPreparingItemDtoArray = garmentPreparingItemDto.Where(x => x.Product.Code.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
                List <GarmentPreparingDto> ListTemp = new List <GarmentPreparingDto>();
                foreach (var a in garmentPreparingItemDtoArray)
                {
                    var temp = garmentPreparingDto.Where(x => x.Id.Equals(a.GarmentPreparingId)).ToArray();
                    foreach (var b in temp)
                    {
                        ListTemp.Add(b);
                    }
                }
                var garmentPreparingDtoList = garmentPreparingDto.Where(x => x.UENNo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                        x.RONo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                        x.Unit.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                        (x.Article != null && x.Article.Contains(keyword, StringComparison.OrdinalIgnoreCase))
                                                                        //|| x.Items.Where(y => y.ProductId.Code.Contains(keyword, StringComparison.OrdinalIgnoreCase))
                                                                        ).ToList();

                var i = 0;
                foreach (var data in ListTemp)
                {
                    i = 0;
                    foreach (var item in garmentPreparingDtoList)
                    {
                        if (data.Id == item.Id)
                        {
                            i++;
                        }
                    }
                    if (i == 0)
                    {
                        garmentPreparingDtoList.Add(data);
                    }
                }
                var garmentPreparingDtoListArray = garmentPreparingDtoList.ToArray();
                if (order != "{}")
                {
                    Dictionary <string, string> OrderDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(order);
                    garmentPreparingDtoListArray = QueryHelper <GarmentPreparingDto> .Order(garmentPreparingDtoList.AsQueryable(), OrderDictionary).ToArray();
                }
                else
                {
                    garmentPreparingDtoListArray = garmentPreparingDtoList.OrderByDescending(x => x.LastModifiedDate).ToArray();
                }

                garmentPreparingDtoListArray = garmentPreparingDtoListArray.Skip((page - 1) * size).Take(size).ToArray();

                await Task.Yield();

                return(Ok(garmentPreparingDtoListArray, info: new
                {
                    page,
                    size,
                    total = totalRows
                }));
            }
            else
            {
                if (order != "{}")
                {
                    Dictionary <string, string> OrderDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(order);
                    garmentPreparingDto = QueryHelper <GarmentPreparingDto> .Order(garmentPreparingDto.AsQueryable(), OrderDictionary).ToArray();
                }
                else
                {
                    garmentPreparingDto = garmentPreparingDto.OrderByDescending(x => x.LastModifiedDate).ToArray();
                }

                garmentPreparingDto = garmentPreparingDto.Skip((page - 1) * size).Take(size).ToArray();

                await Task.Yield();

                return(Ok(garmentPreparingDto, info: new
                {
                    page,
                    size,
                    total = totalRows
                }));
            }
        }