فهرست منبع

更新主程序以支持热重载,移除不必要的Cd通量计算接口,简化服务逻辑。

drggboy 1 هفته پیش
والد
کامیت
dad6b161dc
3فایلهای تغییر یافته به همراه3 افزوده شده و 208 حذف شده
  1. 1 80
      app/api/cd_flux_removal.py
  2. 1 127
      app/services/cd_flux_removal_service.py
  3. 1 1
      main.py

+ 1 - 80
app/api/cd_flux_removal.py

@@ -113,83 +113,4 @@ async def calculate_straw_removal(
         )
 
 
-@router.get("/both-removals",
-           summary="同时计算籽粒移除和秸秆移除Cd通量",
-           description="根据指定地区同时计算籽粒移除和秸秆移除Cd通量",
-           response_model=CdFluxRemovalResponse)
-async def calculate_both_removals(
-    area: str = Query(..., description="地区名称,如:韶关")
-) -> Dict[str, Any]:
-    """
-    同时计算籽粒移除和秸秆移除Cd通量
-    
-    @param area: 地区名称
-    @returns: 包含籽粒移除和秸秆移除Cd通量的计算结果
-    
-    计算公式:
-    - 籽粒移除(g/ha/a) = EXP(LnCropCd) * F11 * 0.5 * 15 / 1000
-    - 秸秆移除(g/ha/a) = [EXP(LnCropCd)/(EXP(LnCropCd)*0.76-0.0034)] * F11 * 0.5 * 15 / 1000
-    """
-    try:
-        service = CdFluxRemovalService()
-        result = service.calculate_both_removals_by_area(area)
-        
-        if not result["success"]:
-            raise HTTPException(
-                status_code=404, 
-                detail=result["message"]
-            )
-        
-        return result
-        
-    except HTTPException:
-        raise
-    except Exception as e:
-        logger.error(f"计算地区 '{area}' 的Cd通量移除失败: {str(e)}")
-        raise HTTPException(
-            status_code=500, 
-            detail=f"计算失败: {str(e)}"
-        )
-
-
-@router.get("/sample/{farmland_id}/{sample_id}",
-           summary="计算特定样点的Cd通量移除",
-           description="根据特定农地ID和样点ID计算籽粒移除和秸秆移除Cd通量",
-           response_model=CdFluxRemovalResponse)
-async def calculate_removal_by_sample(
-    farmland_id: int = Path(..., description="农地ID", ge=1),
-    sample_id: int = Path(..., description="样点ID", ge=1),
-    area: str = Query(..., description="地区名称,如:韶关")
-) -> Dict[str, Any]:
-    """
-    计算特定样点的Cd通量移除
-    
-    @param farmland_id: 农地ID
-    @param sample_id: 样点ID
-    @param area: 地区名称
-    @returns: 特定样点的籽粒移除和秸秆移除Cd通量计算结果
-    
-    计算公式:
-    - 籽粒移除(g/ha/a) = EXP(LnCropCd) * F11 * 0.5 * 15 / 1000
-    - 秸秆移除(g/ha/a) = [EXP(LnCropCd)/(EXP(LnCropCd)*0.76-0.0034)] * F11 * 0.5 * 15 / 1000
-    """
-    try:
-        service = CdFluxRemovalService()
-        result = service.calculate_removal_by_sample(farmland_id, sample_id, area)
-        
-        if not result["success"]:
-            raise HTTPException(
-                status_code=404, 
-                detail=result["message"]
-            )
-        
-        return result
-        
-    except HTTPException:
-        raise
-    except Exception as e:
-        logger.error(f"计算样点 {farmland_id}-{sample_id} 的Cd通量移除失败: {str(e)}")
-        raise HTTPException(
-            status_code=500, 
-            detail=f"计算失败: {str(e)}"
-        ) 
+ 

+ 1 - 127
app/services/cd_flux_removal_service.py

@@ -198,130 +198,4 @@ class CdFluxRemovalService:
                 "data": None
             }
     
-    def calculate_both_removals_by_area(self, area: str) -> Dict[str, Any]:
-        """
-        根据地区同时计算籽粒移除和秸秆移除Cd通量
-        
-        @param area: 地区名称
-        @returns: 计算结果字典
-        """
-        try:
-            # 分别计算籽粒移除和秸秆移除
-            grain_result = self.calculate_grain_removal_by_area(area)
-            straw_result = self.calculate_straw_removal_by_area(area)
-            
-            if not grain_result["success"] or not straw_result["success"]:
-                error_messages = []
-                if not grain_result["success"]:
-                    error_messages.append(f"籽粒移除计算失败: {grain_result['message']}")
-                if not straw_result["success"]:
-                    error_messages.append(f"秸秆移除计算失败: {straw_result['message']}")
-                
-                return {
-                    "success": False,
-                    "message": "; ".join(error_messages),
-                    "data": None
-                }
-            
-            return {
-                "success": True,
-                "message": f"地区 '{area}' 的Cd通量移除计算成功",
-                "data": {
-                    "area": area,
-                    "grain_removal": grain_result["data"],
-                    "straw_removal": straw_result["data"]
-                }
-            }
-            
-        except Exception as e:
-            self.logger.error(f"计算地区 '{area}' 的Cd通量移除失败: {str(e)}")
-            return {
-                "success": False,
-                "message": f"计算失败: {str(e)}",
-                "data": None
-            }
-    
-    def calculate_removal_by_sample(self, farmland_id: int, sample_id: int, area: str) -> Dict[str, Any]:
-        """
-        根据特定样点计算籽粒移除和秸秆移除Cd通量
-        
-        @param farmland_id: 农地ID
-        @param sample_id: 样点ID
-        @param area: 地区名称
-        @returns: 计算结果字典
-        """
-        try:
-            with SessionLocal() as db:
-                # 查询指定地区的参数
-                parameter = db.query(Parameters).filter(Parameters.area == area).first()
-                
-                if not parameter:
-                    return {
-                        "success": False,
-                        "message": f"未找到地区 '{area}' 的参数数据",
-                        "data": None
-                    }
-                
-                # 查询特定样点的CropCd输出数据
-                crop_cd_output = db.query(CropCdOutputData).filter(
-                    and_(
-                        CropCdOutputData.farmland_id == farmland_id,
-                        CropCdOutputData.sample_id == sample_id
-                    )
-                ).first()
-                
-                if not crop_cd_output:
-                    return {
-                        "success": False,
-                        "message": f"未找到样点 {farmland_id}-{sample_id} 的CropCd输出数据",
-                        "data": None
-                    }
-                
-                crop_cd_value = math.exp(crop_cd_output.ln_crop_cd)  # EXP(LnCropCd)
-                
-                # 计算籽粒移除
-                grain_removal = crop_cd_value * parameter.f11 * 0.5 * 15 / 1000
-                
-                # 计算秸秆移除
-                denominator = crop_cd_value * 0.76 - 0.0034
-                straw_removal = None
-                straw_calculation_valid = True
-                
-                if denominator <= 0:
-                    straw_calculation_valid = False
-                    self.logger.warning(f"样点 {farmland_id}-{sample_id} 的分母值为 {denominator},秸秆移除计算无效")
-                else:
-                    straw_removal = (crop_cd_value / denominator) * parameter.f11 * 0.5 * 15 / 1000
-                
-                return {
-                    "success": True,
-                    "message": f"样点 {farmland_id}-{sample_id} 的Cd通量移除计算成功",
-                    "data": {
-                        "farmland_id": farmland_id,
-                        "sample_id": sample_id,
-                        "area": area,
-                        "ln_crop_cd": crop_cd_output.ln_crop_cd,
-                        "crop_cd_value": crop_cd_value,
-                        "f11_yield": parameter.f11,
-                        "grain_removal": {
-                            "formula": "EXP(LnCropCd) * F11 * 0.5 * 15 / 1000",
-                            "value": grain_removal,
-                            "unit": "g/ha/a"
-                        },
-                        "straw_removal": {
-                            "formula": "[EXP(LnCropCd)/(EXP(LnCropCd)*0.76-0.0034)] * F11 * 0.5 * 15 / 1000",
-                            "value": straw_removal,
-                            "unit": "g/ha/a",
-                            "calculation_valid": straw_calculation_valid,
-                            "denominator": denominator if straw_calculation_valid else None
-                        }
-                    }
-                }
-                
-        except Exception as e:
-            self.logger.error(f"计算样点 {farmland_id}-{sample_id} 的Cd通量移除失败: {str(e)}")
-            return {
-                "success": False,
-                "message": f"计算失败: {str(e)}",
-                "data": None
-            } 
+ 

+ 1 - 1
main.py

@@ -4,7 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware  # 导入 CORS 模块
 if __name__ == "__main__":
     import uvicorn
     # uvicorn.run(app, host="0.0.0.0", port=8000, ssl_keyfile="ssl/cert.key", ssl_certfile="ssl/cert.crt")
-    uvicorn.run(app, host="0.0.0.0", port=8000)
+    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
 
 # 创建 FastAPI 应用实例
 app = FastAPI()