|
@@ -1,22 +1,26 @@
|
|
|
package com.huimv.guowei.admin.service.impl;
|
|
|
|
|
|
-import cn.hutool.core.bean.BeanUtil;
|
|
|
-import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.huimv.guowei.admin.common.utils.Result;
|
|
|
import com.huimv.guowei.admin.common.utils.ResultCode;
|
|
|
import com.huimv.guowei.admin.common.utils.ResultUtil;
|
|
|
import com.huimv.guowei.admin.entity.BaseBuilding;
|
|
|
-import com.huimv.guowei.admin.entity.dto.BasePigpenDto;
|
|
|
-import com.huimv.guowei.admin.entity.vo.TreeBasePigpen;
|
|
|
+import com.huimv.guowei.admin.entity.BaseDuckInfo;
|
|
|
+import com.huimv.guowei.admin.entity.EnvDevice;
|
|
|
+import com.huimv.guowei.admin.entity.vo.TreeBaseBuilding;
|
|
|
import com.huimv.guowei.admin.mapper.BaseBuildingMapper;
|
|
|
+import com.huimv.guowei.admin.mapper.BaseDuckInfoMapper;
|
|
|
+import com.huimv.guowei.admin.mapper.EnvDeviceMapper;
|
|
|
import com.huimv.guowei.admin.service.IBaseBuildingService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -34,51 +38,56 @@ import java.util.stream.Collectors;
|
|
|
public class BaseBuildingServiceImpl extends ServiceImpl<BaseBuildingMapper, BaseBuilding> implements IBaseBuildingService {
|
|
|
@Autowired
|
|
|
private BaseBuildingMapper baseBuildingMapper;
|
|
|
-// @Autowired
|
|
|
-// private EnvDeviceMapper envDeviceMapper;
|
|
|
+ @Resource
|
|
|
+ private EnvDeviceMapper envDeviceMapper;
|
|
|
+ @Resource
|
|
|
+ private BaseDuckInfoMapper baseDuckInfoMapper;
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public Result addPigpen(BasePigpenDto basePigpenDto) {
|
|
|
- Integer num = baseBuildingMapper.selectCount(new QueryWrapper<BaseBuilding>().eq("build_name", basePigpenDto.getBuildName()));
|
|
|
- if (num > 0){
|
|
|
- return ResultUtil.exist();
|
|
|
+ public Result addBuilding(BaseBuilding baseBuilding) {
|
|
|
+ if (this.count(new QueryWrapper<BaseBuilding>().eq("build_name",baseBuilding.getBuildName())) != 0) {
|
|
|
+ return new Result(10001,"栋舍名已存在",false);
|
|
|
}
|
|
|
- BaseBuilding baseBuilding = new BaseBuilding();
|
|
|
- BeanUtil.copyProperties(basePigpenDto, baseBuilding);
|
|
|
baseBuilding.setParentId(0);
|
|
|
baseBuilding.setFType(1);
|
|
|
- baseBuilding.setOther2("0");
|
|
|
- int insert = baseBuildingMapper.insert(baseBuilding);
|
|
|
- Integer id = baseBuilding.getId();
|
|
|
- String buildName = baseBuilding.getBuildName();
|
|
|
- Integer floorNum = basePigpenDto.getFloorNum();
|
|
|
- for (Integer integer = 1; integer <= floorNum; integer++) {
|
|
|
- BaseBuilding baseBuilding1 = new BaseBuilding();
|
|
|
- baseBuilding1.setFType(2);
|
|
|
- baseBuilding1.setParentId(id);
|
|
|
- baseBuilding1.setBuildName(buildName+Convert.numberToChinese(integer,false) +"层");
|
|
|
- baseBuilding1.setOther1(Convert.numberToChinese(integer,false) +"层");
|
|
|
- baseBuilding1.setOther2("0,"+id);
|
|
|
- baseBuilding1.setFarmId(baseBuilding.getFarmId());
|
|
|
- baseBuilding1.setStageCode(baseBuilding.getStageCode());
|
|
|
- baseBuildingMapper.insert(baseBuilding1);
|
|
|
+ this.save(baseBuilding);
|
|
|
+ Integer cageNum = baseBuilding.getCageNum();
|
|
|
+ List<BaseBuilding> cageList = new ArrayList<>();
|
|
|
+ for (int i = 1; i <= cageNum; i++) {
|
|
|
+ BaseBuilding building = new BaseBuilding();
|
|
|
+ building.setParentId(baseBuilding.getId());
|
|
|
+ building.setFType(2);
|
|
|
+ building.setStageCode(baseBuilding.getStageCode());
|
|
|
+ building.setBuildName(baseBuilding.getBuildName()+ i +"笼");
|
|
|
+ building.setCageName(i +"笼");
|
|
|
+ building.setFarmId(baseBuilding.getFarmId());
|
|
|
+ cageList.add(building);
|
|
|
}
|
|
|
- return ResultUtil.addResult(insert);
|
|
|
+ this.saveBatch(cageList);
|
|
|
+ return Result.SUCCESS();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public Result updatePigpen(BaseBuilding baseBuilding) {
|
|
|
+ public Result updateBuilding(BaseBuilding baseBuilding) {
|
|
|
baseBuildingMapper.updateById(baseBuilding);
|
|
|
Integer id = baseBuilding.getId();
|
|
|
- List<BaseBuilding> parentId = baseBuildingMapper.selectList(new QueryWrapper<BaseBuilding>().eq("parent_id", id));
|
|
|
- String buildName = baseBuilding.getBuildName();
|
|
|
- if (StringUtils.isNotBlank(buildName)){
|
|
|
- for (BaseBuilding pigpen : parentId) {
|
|
|
- String other1 = pigpen.getOther1();
|
|
|
- pigpen.setBuildName(buildName+other1);
|
|
|
- baseBuildingMapper.updateById(pigpen);
|
|
|
+ List<BaseBuilding> cageIds = baseBuildingMapper.selectList(new QueryWrapper<BaseBuilding>().eq("parent_id", id));
|
|
|
+ if (ObjectUtil.isNotEmpty(cageIds)){
|
|
|
+ for (BaseBuilding building : cageIds) {
|
|
|
+ String newBuildName = baseBuilding.getBuildName() + building.getCageName();
|
|
|
+ building.setStageCode(baseBuilding.getStageCode());
|
|
|
+ building.setBuildName(newBuildName);
|
|
|
+ baseBuildingMapper.updateById(building);
|
|
|
+ List<BaseDuckInfo> baseDuckInfos = baseDuckInfoMapper.selectList(new QueryWrapper<BaseDuckInfo>().eq("unit_id", building.getId()));
|
|
|
+ if (ObjectUtil.isNotEmpty(baseDuckInfos)){
|
|
|
+ for (BaseDuckInfo baseDuckInfo : baseDuckInfos) {
|
|
|
+ baseDuckInfo.setUnitName(newBuildName);
|
|
|
+ baseDuckInfoMapper.updateById(baseDuckInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return ResultUtil.updateResult(1);
|
|
@@ -86,60 +95,90 @@ public class BaseBuildingServiceImpl extends ServiceImpl<BaseBuildingMapper, Bas
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public Result deletePigpen(Map<String, Integer> map) {
|
|
|
-// Integer integer = map.get("id");
|
|
|
-// Integer integer1 = envDeviceMapper.selectCount(new QueryWrapper<EnvDevice>().eq("unit_id", integer));
|
|
|
-// if (integer1 > 0){
|
|
|
-// return new Result(10001,"删除失败,该栋舍下有采集器",false);
|
|
|
-// }
|
|
|
-// List<BasePigpen> other2 = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().like("other2", integer));
|
|
|
-// for (BasePigpen basePigpen : other2) {
|
|
|
-// Integer count = envDeviceMapper.selectCount(new QueryWrapper<EnvDevice>().eq("unit_id", basePigpen.getId()));
|
|
|
-// if (count > 0){
|
|
|
-// return new Result(10001,"删除失败,该栋舍下有采集器",false);
|
|
|
-// }
|
|
|
-// basePigpenMapper.deleteById(basePigpen);
|
|
|
-// }
|
|
|
-// basePigpenMapper.deleteById(integer);
|
|
|
-// return ResultUtil.deleteResult(1);
|
|
|
- return null;
|
|
|
+ public Result deleteBuilding(Map<String, Integer> map) {
|
|
|
+ Integer id = map.get("id");
|
|
|
+ List<Integer> cageIds = baseBuildingMapper.getCageById(id);
|
|
|
+ if (ObjectUtil.isNotEmpty(cageIds)){
|
|
|
+ if (ObjectUtil.isNotEmpty(baseDuckInfoMapper.selectList(new QueryWrapper<BaseDuckInfo>().in("unit_id",cageIds)))) {
|
|
|
+ return new Result(10001,"删除失败,该栋舍下属笼中存在鸭只",false);
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(envDeviceMapper.selectList(new QueryWrapper<EnvDevice>().in("unit_id",cageIds)))) {
|
|
|
+ return new Result(10001,"删除失败,该栋舍下属笼中存在设备",false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(envDeviceMapper.selectList(new QueryWrapper<EnvDevice>().eq("unit_id",id)))) {
|
|
|
+ return new Result(10001,"删除失败,该栋舍存在设备",false);
|
|
|
+ }
|
|
|
+ if (this.removeById(id) && this.removeByIds(cageIds)){
|
|
|
+ return new Result(10000, "删除成功", true);
|
|
|
+ }else {
|
|
|
+ return new Result(10001, "删除失败", true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result listBuilding(Map<String, String> map) {
|
|
|
+ String farmId = map.get("farmId");
|
|
|
+ String stageCode = map.get("stageCode");
|
|
|
+ String buildName = map.get("buildName");
|
|
|
+ String pageNo = map.get("pageNo");
|
|
|
+ String pageSize = map.get("pageSize");
|
|
|
+ Page<BaseBuilding> page = new Page(Integer.valueOf(pageNo),Integer.valueOf(pageSize));
|
|
|
+ Page<BaseBuilding> baseBuildingPage = this.page(page, new QueryWrapper<BaseBuilding>()
|
|
|
+ .eq("farm_id", farmId)
|
|
|
+ .eq("f_type",1)
|
|
|
+ .like(StringUtils.isNotBlank(stageCode), "stage_code", stageCode)
|
|
|
+ .like(StringUtils.isNotBlank(buildName), "build_name", buildName)
|
|
|
+ .orderByAsc("id"));
|
|
|
+ return new Result(ResultCode.SUCCESS,baseBuildingPage);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result list(String farmCode, String buildName, String stageCode) {
|
|
|
- QueryWrapper<BaseBuilding> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.like(StringUtils.isNotBlank(buildName),"build_name", buildName);
|
|
|
- queryWrapper.like(StringUtils.isNotBlank(stageCode),"stage_code", stageCode);
|
|
|
- queryWrapper.eq(StringUtils.isNotBlank(farmCode),"farm_id", farmCode);
|
|
|
- queryWrapper.orderByAsc("sort");
|
|
|
- //创建排序
|
|
|
- List<BaseBuilding> baseBuildings = baseBuildingMapper.selectList(queryWrapper);
|
|
|
- //将结果List改为树
|
|
|
- List<TreeBasePigpen> treeBasePigpens = parseBizBaseArea(baseBuildings);
|
|
|
- return new Result(ResultCode.SUCCESS,treeBasePigpens);
|
|
|
+ public Result listTreeBuilding(Map<String, String> map) {
|
|
|
+ String farmId = map.get("farmId");
|
|
|
+ List<BaseBuilding> baseBuildings = this.list(new QueryWrapper<BaseBuilding>()
|
|
|
+ .eq("farm_id", farmId)
|
|
|
+ .orderByAsc("id"));
|
|
|
+ List<TreeBaseBuilding> treeBaseBuildings = parseBizBaseArea(baseBuildings);
|
|
|
+ return new Result(ResultCode.SUCCESS,treeBaseBuildings);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public Result list(String farmCode, String buildName, String stageCode) {
|
|
|
+// QueryWrapper<BaseBuilding> queryWrapper = new QueryWrapper<>();
|
|
|
+// queryWrapper.like(StringUtils.isNotBlank(buildName),"build_name", buildName);
|
|
|
+// queryWrapper.like(StringUtils.isNotBlank(stageCode),"stage_code", stageCode);
|
|
|
+// queryWrapper.eq(StringUtils.isNotBlank(farmCode),"farm_id", farmCode);
|
|
|
+// queryWrapper.orderByAsc("sort");
|
|
|
+// //创建排序
|
|
|
+// List<BaseBuilding> baseBuildings = baseBuildingMapper.selectList(queryWrapper);
|
|
|
+// //将结果List改为树
|
|
|
+// List<TreeBasePigpen> treeBasePigpens = parseBizBaseArea(baseBuildings);
|
|
|
+// return new Result(ResultCode.SUCCESS,treeBasePigpens);
|
|
|
+// }
|
|
|
+
|
|
|
/**
|
|
|
* 查询结果 转换成树形结构
|
|
|
* @param bizBaseAreas 原始数据
|
|
|
* @return 树
|
|
|
*/
|
|
|
- private List<TreeBasePigpen> parseBizBaseArea(List<BaseBuilding> bizBaseAreas){
|
|
|
+ private List<TreeBaseBuilding> parseBizBaseArea(List<BaseBuilding> bizBaseAreas){
|
|
|
//构建需要展示的树形节点结构
|
|
|
- Map<String, TreeBasePigpen> nodeMap = bizBaseAreas.stream().map(baseBuilding -> {
|
|
|
- TreeBasePigpen baseVo = new TreeBasePigpen();
|
|
|
+ Map<String, TreeBaseBuilding> nodeMap = bizBaseAreas.stream().map(baseBuilding -> {
|
|
|
+ TreeBaseBuilding baseVo = new TreeBaseBuilding();
|
|
|
baseVo.setId(baseBuilding.getId()+"");
|
|
|
baseVo.setFarmCode(baseBuilding.getFarmId());
|
|
|
baseVo.setParentId(baseBuilding.getParentId());
|
|
|
baseVo.setSort(baseBuilding.getSort());
|
|
|
baseVo.setStageCode(baseBuilding.getStageCode());
|
|
|
- baseVo.setPigpenName(baseBuilding.getBuildName());
|
|
|
- baseVo.setType(baseBuilding.getFType());
|
|
|
+ baseVo.setBuildName(baseBuilding.getBuildName());
|
|
|
+ baseVo.setFType(baseBuilding.getFType());
|
|
|
baseVo.setChildNode(new ArrayList<>());
|
|
|
return baseVo;
|
|
|
- }).collect(Collectors.toMap(TreeBasePigpen::getId, b -> b,(k1, k2)->k1));
|
|
|
+ }).collect(Collectors.toMap(TreeBaseBuilding::getId, b -> b,(k1, k2)->k1));
|
|
|
//创建数组存父亲节点
|
|
|
- ArrayList<TreeBasePigpen> roots = new ArrayList<>();
|
|
|
+ ArrayList<TreeBaseBuilding> roots = new ArrayList<>();
|
|
|
//构建树形
|
|
|
nodeMap.values().forEach(item->{
|
|
|
String parentCode = item.getParentId()+"";
|