|
@@ -6,14 +6,17 @@ import com.huimv.farm.musk.common.utils.Result;
|
|
|
import com.huimv.farm.musk.common.utils.ResultCode;
|
|
|
import com.huimv.farm.musk.common.utils.ResultUtil;
|
|
|
import com.huimv.farm.musk.entity.BaseBuilding;
|
|
|
+import com.huimv.farm.musk.entity.vo.TreeBasePigpen;
|
|
|
import com.huimv.farm.musk.mapper.BaseBuildingMapper;
|
|
|
import com.huimv.farm.musk.service.IBaseBuildingService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -48,6 +51,15 @@ public class BaseBuildingServiceImpl extends ServiceImpl<BaseBuildingMapper, Bas
|
|
|
if (this.count(new QueryWrapper<BaseBuilding>().eq("build_name", baseBuilding.getBuildName()).ne("id", baseBuilding.getId())) != 0) {
|
|
|
return new Result(10001, "栋舍名已存在", false);
|
|
|
}
|
|
|
+ QueryWrapper<BaseBuilding> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("parent_id", baseBuilding.getId());
|
|
|
+ List<BaseBuilding> buildings = baseBuildingMapper.selectList(queryWrapper);
|
|
|
+ if (buildings.size() != 0) {
|
|
|
+ for (BaseBuilding building : buildings) {
|
|
|
+ baseBuildingMapper.updateById(building);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
//TODO:后面补充羊只和设备的名称修改
|
|
|
baseBuildingMapper.updateById(baseBuilding);
|
|
|
return ResultUtil.updateResult(1);
|
|
@@ -69,6 +81,9 @@ public class BaseBuildingServiceImpl extends ServiceImpl<BaseBuildingMapper, Bas
|
|
|
// if (ObjectUtil.isNotEmpty(envDeviceMapper.selectList(new QueryWrapper<EnvDevice>().eq("unit_id",id)))) {
|
|
|
// return new Result(10001,"删除失败,该栋舍存在设备",false);
|
|
|
// }
|
|
|
+ QueryWrapper<BaseBuilding> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("parent_id", id);
|
|
|
+ baseBuildingMapper.delete(queryWrapper);
|
|
|
this.removeById(id);
|
|
|
return new Result(10000, "删除成功", true);
|
|
|
|
|
@@ -89,7 +104,40 @@ public class BaseBuildingServiceImpl extends ServiceImpl<BaseBuildingMapper, Bas
|
|
|
List<BaseBuilding> baseBuildings = this.list(new QueryWrapper<BaseBuilding>()
|
|
|
.eq("farm_id", farmId)
|
|
|
.orderByAsc("id"));
|
|
|
- return new Result(ResultCode.SUCCESS, baseBuildings);
|
|
|
+ List<TreeBasePigpen> treeBasePigpens = parseBizBaseArea(baseBuildings);
|
|
|
+ return new Result(ResultCode.SUCCESS, treeBasePigpens);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询结果 转换成树形结构
|
|
|
+ *
|
|
|
+ * @param bizBaseAreas 原始数据
|
|
|
+ * @return 树
|
|
|
+ */
|
|
|
+ private List<TreeBasePigpen> parseBizBaseArea(List<BaseBuilding> bizBaseAreas) {
|
|
|
+ //构建需要展示的树形节点结构
|
|
|
+ Map<String, TreeBasePigpen> nodeMap = bizBaseAreas.stream().map(basePigpen -> {
|
|
|
+ TreeBasePigpen baseVo = new TreeBasePigpen();
|
|
|
+ baseVo.setId(basePigpen.getId() + "");
|
|
|
+ baseVo.setFarmId(basePigpen.getFarmId());
|
|
|
+ baseVo.setParentId(basePigpen.getParentId());
|
|
|
+ baseVo.setBuildName(basePigpen.getBuildName());
|
|
|
+ baseVo.setChildNode(new ArrayList<>());
|
|
|
+ return baseVo;
|
|
|
+ }).collect(Collectors.toMap(TreeBasePigpen::getId, b -> b, (k1, k2) -> k1));
|
|
|
+ //创建数组存父亲节点
|
|
|
+ ArrayList<TreeBasePigpen> roots = new ArrayList<>();
|
|
|
+ //构建树形
|
|
|
+ nodeMap.values().forEach(item -> {
|
|
|
+ String parentCode = item.getParentId() + "";
|
|
|
+ if (nodeMap.get(parentCode) == null) {
|
|
|
+ //父节点为空,说明当前节点就已经是父节点了 将该节点存起来
|
|
|
+ roots.add(item);
|
|
|
+ } else {
|
|
|
+ //父节点 不为空,说明有父节点,拿到该节点的父节点的孩子节点(就是我自己),将该节点存起来
|
|
|
+ nodeMap.get(parentCode).getChildNode().add(item);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return roots;
|
|
|
+ }
|
|
|
}
|