|
|
@@ -0,0 +1,193 @@
|
|
|
+package com.ruoyi.uniapp.service.impl;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.ruoyi.uniapp.mapper.FieldMapper;
|
|
|
+import com.ruoyi.uniapp.domain.Field;
|
|
|
+import com.ruoyi.uniapp.domain.enums.FieldStatusEnum;
|
|
|
+import com.ruoyi.uniapp.domain.enums.FieldTypeEnum;
|
|
|
+import com.ruoyi.uniapp.domain.vo.FieldVO;
|
|
|
+import com.ruoyi.uniapp.service.IFieldService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 地块信息 服务层实现
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class FieldServiceImpl implements IFieldService {
|
|
|
+ @Autowired
|
|
|
+ private FieldMapper fieldMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户ID查询用户关联的地块列表
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param pageNum 页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
+ * @return 分页信息
|
|
|
+ */
|
|
|
+ public PageInfo<FieldVO> selectFieldListByUserId(Long userId, Integer pageNum, Integer pageSize) {
|
|
|
+ if (userId == null) {
|
|
|
+ return new PageInfo<>(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ PageHelper.startPage(ObjectUtil.defaultIfNull(pageNum, 1), ObjectUtil.defaultIfNull(pageSize, 10));
|
|
|
+ List<Field> fields = fieldMapper.selectFieldListByUserId(userId);
|
|
|
+ PageInfo<Field> sourcePageInfo = new PageInfo<>(fields);
|
|
|
+ return processFields(fields, sourcePageInfo);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new PageInfo<>(Collections.emptyList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户ID和关键词搜索地块
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param keyword 搜索关键词
|
|
|
+ * @return 地块信息集合
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public PageInfo<FieldVO> searchFieldsByUserIdAndKeyword(Long userId, String keyword, Integer pageNum, Integer pageSize) {
|
|
|
+ if (userId == null) {
|
|
|
+ return new PageInfo<>(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ PageHelper.startPage(ObjectUtil.defaultIfNull(pageNum, 1), ObjectUtil.defaultIfNull(pageSize, 10));
|
|
|
+ List<Field> fields = fieldMapper.searchFieldsByUserIdAndKeyword(userId, keyword);
|
|
|
+ PageInfo<Field> sourcePageInfo = new PageInfo<>(fields);
|
|
|
+ return processFields(fields, sourcePageInfo);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new PageInfo<>(Collections.emptyList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FieldVO countFieldTotalByUser(Long userId) {
|
|
|
+ Field field = fieldMapper.countFieldTotalByUser(userId);
|
|
|
+
|
|
|
+ FieldVO fieldVO = new FieldVO();
|
|
|
+ fieldVO.setPlotsTotal(field.getPlotsTotal() != null ? field.getPlotsTotal() : 0);
|
|
|
+ fieldVO.setInUseCount(field.getInUseCount() != null ? field.getInUseCount() : 0);
|
|
|
+ fieldVO.setLeiSureCount(field.getLeiSureCount() != null ? field.getLeiSureCount() : 0);
|
|
|
+ return fieldVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换地块实体为VO对象
|
|
|
+ *
|
|
|
+ * @param field 地块实体
|
|
|
+ * @return 地块VO对象
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public FieldVO convertToVO(Field field) {
|
|
|
+ if (field == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ FieldVO vo = new FieldVO();
|
|
|
+ vo.setId(field.getId());
|
|
|
+ vo.setCode(field.getFieldCode());
|
|
|
+ vo.setName(field.getFieldName());
|
|
|
+ vo.setFarmName(field.getFarmName());
|
|
|
+ vo.setManager(field.getManagerName());
|
|
|
+ vo.setArea(field.getSize());
|
|
|
+
|
|
|
+ // 使用枚举处理地块类型
|
|
|
+ vo.setType(FieldTypeEnum.getInfoByCode(field.getFieldType()));
|
|
|
+
|
|
|
+ vo.setCrop(field.getGrowCrops());
|
|
|
+
|
|
|
+ // 使用枚举处理地块状态
|
|
|
+ vo.setStatus(FieldStatusEnum.getStatusByCode(field.getStatus()));
|
|
|
+
|
|
|
+ // 设备信息
|
|
|
+ vo.setDeviceCount(field.getDeviceCount());
|
|
|
+ vo.setOnlineDevices(field.getOnlineDevices());
|
|
|
+ vo.setAlerts(field.getAlerts());
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PageInfo<FieldVO> processFields(List<Field> fields, PageInfo<Field> sourcePageInfo) {
|
|
|
+ if (CollectionUtils.isEmpty(fields)) {
|
|
|
+ return new PageInfo<>(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询设备数量和告警信息
|
|
|
+ batchEnrichFieldsWithDeviceInfo(fields);
|
|
|
+
|
|
|
+ // 转换为VO列表
|
|
|
+ List<FieldVO> voList = fields.stream()
|
|
|
+ .map(this::convertToVO)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 创建新的分页信息对象并复制属性
|
|
|
+ PageInfo<FieldVO> resultPage = new PageInfo<>(voList);
|
|
|
+ BeanUtils.copyProperties(sourcePageInfo, resultPage, "list");
|
|
|
+
|
|
|
+ return resultPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量为地块实体填充设备和告警信息
|
|
|
+ *
|
|
|
+ * @param fields 地块实体列表
|
|
|
+ */
|
|
|
+ private void batchEnrichFieldsWithDeviceInfo(List<Field> fields) {
|
|
|
+ if (fields == null || fields.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取所有地块ID
|
|
|
+ List<Long> fieldIds = fields.stream()
|
|
|
+ .map(Field::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 批量查询设备数量
|
|
|
+ List<Map<String, Object>> deviceCountResults = fieldMapper.batchCountDevicesByFieldIds(fieldIds);
|
|
|
+ Map<Long, Integer> deviceCountMap = new HashMap<>();
|
|
|
+ for (Map<String, Object> result : deviceCountResults) {
|
|
|
+ Long fieldId = ((Number) result.get("fieldId")).longValue();
|
|
|
+ Integer count = ((Number) result.get("deviceCount")).intValue();
|
|
|
+ deviceCountMap.put(fieldId, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询在线设备数量
|
|
|
+ List<Map<String, Object>> onlineDeviceCountResults = fieldMapper.batchCountOnlineDevicesByFieldIds(fieldIds);
|
|
|
+ Map<Long, Integer> onlineDeviceCountMap = new HashMap<>();
|
|
|
+ for (Map<String, Object> result : onlineDeviceCountResults) {
|
|
|
+ Long fieldId = ((Number) result.get("fieldId")).longValue();
|
|
|
+ Integer count = ((Number) result.get("onlineDevices")).intValue();
|
|
|
+ onlineDeviceCountMap.put(fieldId, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将结果应用到地块对象上
|
|
|
+ for (Field field : fields) {
|
|
|
+ // 设置设备数量
|
|
|
+ Integer deviceCount = deviceCountMap.get(field.getId());
|
|
|
+ field.setDeviceCount(deviceCount != null ? deviceCount : 0);
|
|
|
+
|
|
|
+ // 设置在线设备数量
|
|
|
+ Integer onlineDevices = onlineDeviceCountMap.get(field.getId());
|
|
|
+ field.setOnlineDevices(onlineDevices != null ? onlineDevices : 0);
|
|
|
+
|
|
|
+ // 设置告警数量 TODO 等告警列表完善之后再查询
|
|
|
+ field.setAlerts(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|