feat (farm): 农事记录定时器

- 定时任务
This commit is contained in:
达富斌
2025-09-02 16:24:36 +08:00
parent 8f3f2827b9
commit 21ae555131
11 changed files with 181 additions and 7 deletions

View File

@@ -26,6 +26,9 @@ public class FarmingRecordSaveReqVO {
@Schema(description = "任务时间") @Schema(description = "任务时间")
private String taskTime; private String taskTime;
@Schema(description = "任务计划 (1: 开始; 2: 结束;)")
private Integer taskStatus;
@Schema(description = "所属地块", example = "10480") @Schema(description = "所属地块", example = "10480")
private String plotId; private String plotId;

View File

@@ -46,6 +46,10 @@ public class FarmingRecordDO extends BaseDO {
* 任务时间 * 任务时间
*/ */
private String taskTime; private String taskTime;
/**
* 任务计划 (1: 开始; 2: 结束;)
*/
private Integer taskStatus;
/** /**
* 所属地块 * 所属地块
*/ */

View File

@@ -79,4 +79,10 @@ public class CommonConstants {
public final static String GROW_FLAG_2 = "空闲"; public final static String GROW_FLAG_2 = "空闲";
public final static Integer FARMING_RECORD_STATUS_1 = 1;//开始
public final static Integer FARMING_RECORD_STATUS_2 = 2; // "开始";
} }

View File

@@ -2,11 +2,24 @@ package cn.iocoder.yudao.module.farm.scheduled;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.farm.controller.admin.farmingrecord.vo.FarmingRecordSaveReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.farmingrecordinput.FarmingRecordInputDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.modelfarmingoperation.ModelFarmingOperationDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.modelfarmingoperationinput.ModelFarmingOperationInputDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.productionplan.ProductionPlanDO;
import cn.iocoder.yudao.module.farm.service.farmingrecord.FarmingRecordService; import cn.iocoder.yudao.module.farm.service.farmingrecord.FarmingRecordService;
import cn.iocoder.yudao.module.farm.service.modelmanagement.ModelManagementService;
import cn.iocoder.yudao.module.farm.service.productionplan.ProductionPlanService; import cn.iocoder.yudao.module.farm.service.productionplan.ProductionPlanService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.module.farm.enums.CommonConstants.*;
/** /**
* @description: 农事记录 * @description: 农事记录
* * 表关系 * * 表关系
@@ -27,20 +40,112 @@ public class FarmingRecordJob implements JobHandler {
private final ProductionPlanService productionPlanService; private final ProductionPlanService productionPlanService;
private final FarmingRecordService farmingRecordService; private final FarmingRecordService farmingRecordService;
private final ModelManagementService modelManagementService;
public FarmingRecordJob(ProductionPlanService productionPlanService, FarmingRecordService farmingRecordService) { public FarmingRecordJob(ProductionPlanService productionPlanService, FarmingRecordService farmingRecordService, ModelManagementService modelManagementService) {
this.productionPlanService = productionPlanService; this.productionPlanService = productionPlanService;
this.farmingRecordService = farmingRecordService; this.farmingRecordService = farmingRecordService;
this.modelManagementService = modelManagementService;
} }
@Override @Override
public String execute(String param) throws Exception { public String execute(String param) throws Exception {
this.insertRecordHandler(); List<FarmingRecordSaveReqVO> list = this.insertRecordHandler();
for (FarmingRecordSaveReqVO e : list) {
String[] taskTime = e.getTaskTime().split(",");
if (taskTime.length != 2) {
continue;
}
LocalDate startDate = LocalDate.parse(taskTime[0]);
LocalDate endDate = LocalDate.parse(taskTime[1]);
if (startDate.isEqual(LocalDate.now())) {
// 开始日期在今天
e.setTaskStatus(FARMING_RECORD_STATUS_1);
farmingRecordService.createFarmingRecord(e);
}
if (endDate.isEqual(LocalDate.now())) {
// 结束日期在今天
e.setTaskStatus(FARMING_RECORD_STATUS_2);
farmingRecordService.createFarmingRecord(e);
}
}
return "success"; return "success";
} }
private void insertRecordHandler() { private List<FarmingRecordSaveReqVO> insertRecordHandler() {
// 获取[进行中]状态的计划
List<ProductionPlanDO> plans = productionPlanService.getProductionPlans();
Map<String, ProductionPlanDO> planMap = plans.stream().filter(e -> Objects.equals(e.getPlanStatus(), PLAN_STATUS_2)).collect(Collectors.toMap(ProductionPlanDO::getId, e -> e, (v1, v2) -> v1));
List<String> taskIdList = plans.stream().filter(e -> Objects.equals(e.getPlanStatus(), PLAN_STATUS_2)).map(ProductionPlanDO::getId).toList();
// 获取引用模型集合
Map<String, List<ModelFarmingOperationDO>> modelIdMap = modelManagementService.getRecordList(taskIdList);
List<FarmingRecordSaveReqVO> farmingRecordSaveReqVOList = new ArrayList<>();
for (Map.Entry<String, List<ModelFarmingOperationDO>> en : modelIdMap.entrySet()) {
String modelId = en.getKey();
List<ModelFarmingOperationDO> operationList = en.getValue();
for (ModelFarmingOperationDO e : operationList) {
farmingRecordSaveReqVOList.add(this.buildFarmingRecord(planMap.get(modelId), e));
}
}
return farmingRecordSaveReqVOList;
}
private FarmingRecordSaveReqVO buildFarmingRecord(ProductionPlanDO p, ModelFarmingOperationDO e) {
LocalDate productionStartDate = p.getProductionStartDate();
// 从第startingPeriod天开始
Integer startingPeriod = e.getStartingPeriod();
// 用时天数
Integer used = e.getUsed();
// 农事记录开始日期
LocalDate startDate = productionStartDate.plusDays(startingPeriod-1);
// 农事记录结束日期
LocalDate endDate = startDate.plusDays(used);
String taskTime = startDate.toString() + "," + endDate.toString();
FarmingRecordSaveReqVO saveObj = new FarmingRecordSaveReqVO();
saveObj.setTaskType(e.getFarmingId());
saveObj.setTaskName(e.getFarmingName());
saveObj.setTaskScope(p.getPlanNumber());
saveObj.setTaskTime(taskTime);
saveObj.setProductionPlanId(p.getId());
saveObj.setExecutor("系统");
saveObj.setActualLaborCost(e.getUsed());
saveObj.setActualInputId(""); // TODO: 2025/9/2
saveObj.setFeedbackNotes(e.getDescription());
saveObj.setFeedbackImageUrl("");
saveObj.setFeedbackImageUrlList(e.getSketchList());
saveObj.setBaseId(p.getBaseId());
saveObj.setBaseName(p.getBaseName());
saveObj.setPlotId(p.getPlotId());
saveObj.setPlotName(p.getPlotName());
saveObj.setTenantId(p.getTenantId());
List<FarmingRecordInputDO> inputDOList = new ArrayList<>();
for (ModelFarmingOperationInputDO m : e.getInvestGoodsList()) {
FarmingRecordInputDO target = new FarmingRecordInputDO();
target.setInputParentCategory(m.getInputsId());
target.setInputChildCategory(m.getInputsId());
target.setDilutionRatio(m.getDiluted());
target.setSingleUseOption(m.getUnitName());
target.setSingleUseValue(BigDecimal.ZERO);
target.setSingleUseUnit(""); // TODO: 2025/9/2
target.setApplicationScope(""); // TODO: 2025/9/2
target.setDosage(m.getDose());
target.setUsage(m.getUsage());
target.setBaseId(p.getBaseId());
target.setBaseName(p.getBaseName());
target.setPlotId(p.getPlotId());
target.setPlotName(p.getPlotName());
target.setTenantId(p.getTenantId());
inputDOList.add(target);
}
saveObj.setInvestGoodsList(inputDOList);
return saveObj;
} }
} }

View File

@@ -70,4 +70,7 @@ public interface FarmingRecordService {
*/ */
PageResult<FarmingRecordDO> getFarmingRecordPage(FarmingRecordPageReqVO pageReqVO); PageResult<FarmingRecordDO> getFarmingRecordPage(FarmingRecordPageReqVO pageReqVO);
List<FarmingRecordDO> getFarmingRecordList();
void createFarmingRecordList(List<FarmingRecordSaveReqVO> farmingRecordSaveReqVOList);
} }

View File

@@ -217,4 +217,14 @@ public class FarmingRecordServiceImpl implements FarmingRecordService {
farmingRecord.setFeedbackImageUrl(reqVO.getFeedbackImageUrl()); farmingRecord.setFeedbackImageUrl(reqVO.getFeedbackImageUrl());
} }
} }
@Override
public List<FarmingRecordDO> getFarmingRecordList() {
return null;
}
@Override
public void createFarmingRecordList(List<FarmingRecordSaveReqVO> farmingRecordSaveReqVOList) {
}
} }

View File

@@ -4,7 +4,6 @@ import cn.iocoder.yudao.module.farm.controller.admin.modelcycle.vo.ModelCyclePag
import cn.iocoder.yudao.module.farm.controller.admin.modelcycle.vo.ModelCycleSaveReqVO; import cn.iocoder.yudao.module.farm.controller.admin.modelcycle.vo.ModelCycleSaveReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.modelcycle.ModelCycleDO; import cn.iocoder.yudao.module.farm.dal.dataobject.modelcycle.ModelCycleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import java.util.List; import java.util.List;

View File

@@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementPageReqVO; import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementPageReqVO;
import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementRespVO; import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementRespVO;
import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementSaveReqVO; import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementSaveReqVO;
import cn.iocoder.yudao.module.farm.dal.dataobject.modelfarmingoperation.ModelFarmingOperationDO;
import cn.iocoder.yudao.module.farm.dal.dataobject.modelmanagement.ModelManagementDO; import cn.iocoder.yudao.module.farm.dal.dataobject.modelmanagement.ModelManagementDO;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@@ -81,4 +82,11 @@ public interface ModelManagementService {
*/ */
String getModelCycleName(String planId); String getModelCycleName(String planId);
/**
* 获取当前计划下所有农事操作
* @param taskIdList
* @return
*/
Map<String, List<ModelFarmingOperationDO>> getRecordList(List<String> taskIdList);
} }

View File

@@ -42,8 +42,7 @@ import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.module.farm.enums.CommonConstants.COMMON_DISASTER; import static cn.iocoder.yudao.module.farm.enums.CommonConstants.*;
import static cn.iocoder.yudao.module.farm.enums.CommonConstants.WEATHER_DISASTER;
import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.MODEL_MANAGEMENT_NOT_EXISTS; import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.MODEL_MANAGEMENT_NOT_EXISTS;
/** /**
@@ -131,7 +130,7 @@ public class ModelManagementServiceImpl implements ModelManagementService {
ModelManagementDO newModelCase = BeanUtils.toBean(modelManagementDO, ModelManagementDO.class); ModelManagementDO newModelCase = BeanUtils.toBean(modelManagementDO, ModelManagementDO.class);
String newModelId = IdUtil.fastUUID(); String newModelId = IdUtil.fastUUID();
newModelCase.setId(newModelId); newModelCase.setId(newModelId);
newModelCase.setType(CommonConstants.MODEL_TYPE_2); // 设置为引用案例类型 newModelCase.setType(MODEL_TYPE_2); // 设置为引用案例类型
newModelCase.setTaskId(taskId); newModelCase.setTaskId(taskId);
newModelCase.setModelName(modelManagementDO.getModelName()); newModelCase.setModelName(modelManagementDO.getModelName());
newModelCase.setQuoteCount(0); newModelCase.setQuoteCount(0);
@@ -454,4 +453,35 @@ public class ModelManagementServiceImpl implements ModelManagementService {
public String getModelCycleName(String planId) { public String getModelCycleName(String planId) {
return null; return null;
} }
@Override
public Map<String, List<ModelFarmingOperationDO>> getRecordList(List<String> taskIdList) {
List<ModelManagementDO> modelList = modelManagementMapper.selectList("task_id", taskIdList);
List<String> modelIdList = modelList.stream().filter(Objects::nonNull).filter(e -> Objects.equals(e.getType(), MODEL_TYPE_2)).map(ModelManagementDO::getId).toList();
List<ModelCycleDO> modelCycleList = modelCycleMapper.selectList("model_id", modelIdList);
// key modelId , value List<ModelCycleDO>
Map<String, List<ModelCycleDO>> modelCycleMap = modelCycleList.stream().collect(Collectors.groupingBy(ModelCycleDO::getModelId));
Map<String, List<ModelFarmingOperationDO>> resMap = new HashMap<>();
for (Map.Entry<String, List<ModelCycleDO>> entry : modelCycleMap.entrySet()) {
String modelId = entry.getKey();
List<ModelCycleDO> mcList = entry.getValue();
List<ModelFarmingOperationDO> tempList = new ArrayList<>();
for (ModelCycleDO e : mcList) {
List<ModelFarmingOperationDO> modelOperationList = modelFarmingOperationMapper.selectList("cycle_id", e.getId());
for (ModelFarmingOperationDO o : modelOperationList) {
List<ModelFarmingOperationInputDO> inputList = modelFarmingOperationInputMapper.selectList("farming_id", o.getId());
o.setInvestGoodsList(inputList);
}
tempList.addAll(modelOperationList);
}
resMap.put(modelId, tempList);
}
return resMap;
}
} }

View File

@@ -60,6 +60,7 @@ public interface ProductionPlanService {
* @param plotId 地块ID * @param plotId 地块ID
* @return * @return
*/ */
List<ProductionPlanDO> getProductionPlans();
List<ProductionPlanDO> getProductionPlans(String plotId); List<ProductionPlanDO> getProductionPlans(String plotId);
List<ProductionPlanDO> getProductionPlans(String baseId,String plotId); List<ProductionPlanDO> getProductionPlans(String baseId,String plotId);

View File

@@ -99,6 +99,11 @@ public class ProductionPlanServiceImpl implements ProductionPlanService {
return productionPlanMapper.selectById(id); return productionPlanMapper.selectById(id);
} }
@Override
public List<ProductionPlanDO> getProductionPlans() {
return productionPlanMapper.selectList();
}
@Override @Override
public List<ProductionPlanDO> getProductionPlans(String plotId) { public List<ProductionPlanDO> getProductionPlans(String plotId) {
LambdaQueryWrapperX<ProductionPlanDO> qw = new LambdaQueryWrapperX<>(); LambdaQueryWrapperX<ProductionPlanDO> qw = new LambdaQueryWrapperX<>();