예제 #1
0
        public async Task <ActionResult> FCRCheck([FromBody] FCRCheckModel _model)
        {
            string link = URI_API.LIVESTOCK_PROCEED_CHECK_FCR;
            ResponseConsult <int> response = await PostAsJsonAsync <int>(link, _model);

            return(Ok(response));
        }
예제 #2
0
        public async Task <int> FCRCheck(FCRCheckModel _model)
        {
            if (_model.FishPondWarehouseId <= 0)
            {
                scopeContext.AddError("Chưa chọn ao");
                return(0);
            }
            if (_model.Weight <= 0)
            {
                scopeContext.AddError("Trọng lượng không đúng");
                return(0);
            }
            // dữ liệu ao
            var thisFishPond = await svcFishPond.GetByWarehouseId(_model.FishPondWarehouseId);

            if (thisFishPond == null)
            {
                scopeContext.AddError("Lỗi dữ liệu kho-ao " + _model.FishPondWarehouseId);
                return(0);
            }
            // đợt nuôi
            var thisFarmingSeason = await svcFarmingSeason.GetByFishPondId(thisFishPond.Id);

            if (thisFarmingSeason == null)
            {
                scopeContext.AddError("Ao này chưa vào đợt nuôi");
                return(0);
            }
            // dữ liệu kho-ao
            var thisFishPondWarehouse = await svcWarehouse.GetDetail(_model.FishPondWarehouseId);

            if (thisFishPondWarehouse == null || thisFishPondWarehouse.DefaultWarehouseId <= 0)
            {
                scopeContext.AddError("Lỗi dữ liệu kho mặc định cho ao");
                return(0);
            }
            _model.CheckDate = _model.CheckDate.GetValueOrDefault(DateTime.UtcNow);
            if (_model.LivestockId <= 0)
            {
                scopeContext.AddError("Mã giống nuôi không tồn tại");
                return(0);
            }
            // dữ liệu con giống
            var livestock = await svcProduct.GetDetail(_model.LivestockId);

            if (livestock == null || livestock.ProductGroupId != (int)SystemIDEnum.ProductGroup_LivestockSeed)
            {
                scopeContext.AddError("Mã giống nuôi không tồn tại");
                return(0);
            }
            var lastFCR = await svcFeedConversionRate.GetLast(thisFarmingSeason.Id, _model.LivestockId);

            if (lastFCR == null)
            {
                scopeContext.AddError("Ao không có đợt thả nào cho giống nuôi này");
                return(0);
            }
            if (lastFCR.SurveyDate.Date > _model.CheckDate.Value.Date)
            {
                scopeContext.AddError($"Ngày {_model.CheckDate.Value.ToString("dd/MM/yyyy")} đã được kiểm tra cân trọng");
                return(0);
            }

            // tính hệ số tăng trọng:
            //Số ngày = ngày nhập sau -ngày nhập trước(1 / 7 đến 1 / 8 = 31 ngày)
            //Tỷ trọng tăng = Trọng lượng sau -trọng lượng trước
            //Hằng số biến đổi = Tỷ trọng tăng ^ (1 / số ngày)
            int    dateNumber   = DateTime.Compare(_model.CheckDate.Value.Date, lastFCR.SurveyDate.Date);
            double weightChange = (double)(_model.Weight - lastFCR.Weight);
            double tempoRatio   = Math.Pow(weightChange, (1 / dateNumber));

            // bắt đầu tạo phiếu
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    //Tỷ lệ tăng trọng
                    FeedConversionRate fcr = new FeedConversionRate();
                    fcr.FarmingSeasonId = thisFarmingSeason.Id;
                    fcr.IsAuto          = false;
                    fcr.ProductId       = livestock.Id;
                    fcr.SurveyDate      = _model.CheckDate.Value;
                    fcr.Quantity        = 0;
                    fcr.MassAmount      = 0; // kg
                    fcr.Weight          = 0;
                    //fcr.Weight = 1000 / (fcr.Quantity / fcr.MassAmount); // gram
                    fcr.ProductName = livestock.Name;
                    fcr.Id          = await svcFeedConversionRate.Add(fcr);

                    //// Lịch sử đợt nuôi (master lịch sử ao nuôi)
                    //FarmingSeasonHistory history = new FarmingSeasonHistory();
                    //history.ActionDate = _model.CheckDate.Value;
                    //history.ActionType = (int)SystemIDEnum.FarmingSeason_ActionType_FCR;
                    //history.Description = "Kiểm tra tăng trọng";
                    //history.FarmingSeasonId = thisFarmingSeason.Id;
                    //history.Id = await svcFarmingSeasonHistory.Add(history);

                    //// Lịch sử con giống (detail lịch sử ao nuôi)
                    //LivestockHistoryDetail historyDetail = new LivestockHistoryDetail();
                    //historyDetail.HistoryId = history.Id;
                    //historyDetail.LivestockId = _model.LivestockId;
                    //historyDetail.MassAmount = _model.MassAmount; // kg
                    //historyDetail.Quantity = _model.Quantity;
                    //historyDetail.Weight = fcr.Weight; // gram
                    //historyDetail.Id = await svcLivestockHistoryDetail.Add(historyDetail);

                    //transaction.Commit();
                    return(fcr.Id);
                }
                catch
                {
                    transaction.Rollback();
                    return(0);
                }
            }
        }
        public async Task <IActionResult> FCRCheck([FromBody] FCRCheckModel _model)
        {
            var result = await busLivestockProcess.FCRCheck(_model);

            return(Ok(context.WrapResponse(result)));
        }