BasePigpenServiceImpl.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.huimv.admin.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.convert.Convert;
  4. import cn.hutool.core.util.NumberUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  7. import com.baomidou.mybatisplus.extension.api.R;
  8. import com.huimv.admin.common.utils.Result;
  9. import com.huimv.admin.common.utils.ResultCode;
  10. import com.huimv.admin.common.utils.ResultUtil;
  11. import com.huimv.admin.entity.BasePigpen;
  12. import com.huimv.admin.entity.EnvDevice;
  13. import com.huimv.admin.entity.dto.BasePigpenDto;
  14. import com.huimv.admin.entity.vo.TreeBasePigpen;
  15. import com.huimv.admin.mapper.BasePigpenMapper;
  16. import com.huimv.admin.mapper.EnvDeviceMapper;
  17. import com.huimv.admin.service.IBasePigpenService;
  18. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.ArrayList;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. /**
  28. * <p>
  29. * 服务实现类
  30. * </p>
  31. *
  32. * @author author
  33. * @since 2023-02-13
  34. */
  35. @Service
  36. public class BasePigpenServiceImpl extends ServiceImpl<BasePigpenMapper, BasePigpen> implements IBasePigpenService {
  37. @Autowired
  38. private BasePigpenMapper basePigpenMapper;
  39. @Autowired
  40. private EnvDeviceMapper envDeviceMapper;
  41. @Override
  42. @Transactional
  43. public Result addPigpen(BasePigpenDto basePigpenDto) {
  44. Integer num = basePigpenMapper.selectCount(new QueryWrapper<BasePigpen>().eq("build_name", basePigpenDto.getBuildName()));
  45. if (num > 0){
  46. return ResultUtil.exist();
  47. }
  48. BasePigpen basePigpen = new BasePigpen();
  49. BeanUtil.copyProperties(basePigpenDto,basePigpen);
  50. basePigpen.setParentId(0);
  51. basePigpen.setFType(1);
  52. basePigpen.setOther2("0");
  53. int insert = basePigpenMapper.insert(basePigpen);
  54. Integer id = basePigpen.getId();
  55. String buildName = basePigpen.getBuildName();
  56. Integer floorNum = basePigpenDto.getFloorNum();
  57. for (Integer integer = 1; integer <= floorNum; integer++) {
  58. BasePigpen basePigpen1 = new BasePigpen();
  59. basePigpen1.setFType(2);
  60. basePigpen1.setParentId(id);
  61. basePigpen1.setBuildName(buildName+Convert.numberToChinese(integer,false) +"层");
  62. basePigpen1.setOther1(Convert.numberToChinese(integer,false) +"层");
  63. basePigpen1.setOther2("0,"+id);
  64. basePigpen1.setFarmId(basePigpen.getFarmId());
  65. basePigpen1.setStageCode(basePigpen.getStageCode());
  66. basePigpenMapper.insert(basePigpen1);
  67. }
  68. return ResultUtil.addResult(insert);
  69. }
  70. @Override
  71. @Transactional
  72. public Result updatePigpen(BasePigpen basePigpen) {
  73. basePigpenMapper.updateById(basePigpen);
  74. Integer id = basePigpen.getId();
  75. List<BasePigpen> parentId = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("parent_id", id));
  76. String buildName = basePigpen.getBuildName();
  77. if (StringUtils.isNotBlank(buildName)){
  78. for (BasePigpen pigpen : parentId) {
  79. String other1 = pigpen.getOther1();
  80. pigpen.setBuildName(buildName+other1);
  81. basePigpenMapper.updateById(pigpen);
  82. }
  83. }
  84. return ResultUtil.updateResult(1);
  85. }
  86. @Override
  87. @Transactional
  88. public Result deletePigpen(Map<String, Integer> map) {
  89. Integer integer = map.get("id");
  90. Integer integer1 = envDeviceMapper.selectCount(new QueryWrapper<EnvDevice>().eq("unit_id", integer));
  91. if (integer1 > 0){
  92. return new Result(10001,"删除失败,该栋舍下有采集器",false);
  93. }
  94. List<BasePigpen> other2 = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().like("other2", integer));
  95. for (BasePigpen basePigpen : other2) {
  96. Integer count = envDeviceMapper.selectCount(new QueryWrapper<EnvDevice>().eq("unit_id", basePigpen.getId()));
  97. if (count > 0){
  98. return new Result(10001,"删除失败,该栋舍下有采集器",false);
  99. }
  100. basePigpenMapper.deleteById(basePigpen);
  101. }
  102. basePigpenMapper.deleteById(integer);
  103. return ResultUtil.deleteResult(1);
  104. }
  105. @Override
  106. public Result list(String farmCode, String buildName, String stageCode) {
  107. QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
  108. queryWrapper.like(StringUtils.isNotBlank(buildName),"build_name", buildName);
  109. queryWrapper.like(StringUtils.isNotBlank(stageCode),"stage_code", stageCode);
  110. queryWrapper.eq(StringUtils.isNotBlank(farmCode),"farm_id", farmCode);
  111. queryWrapper.orderByAsc("sort");
  112. //创建排序
  113. List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);
  114. //将结果List改为树
  115. List<TreeBasePigpen> treeBasePigpens = parseBizBaseArea(basePigpens);
  116. return new Result(ResultCode.SUCCESS,treeBasePigpens);
  117. }
  118. /**
  119. * 查询结果 转换成树形结构
  120. * @param bizBaseAreas 原始数据
  121. * @return 树
  122. */
  123. private List<TreeBasePigpen> parseBizBaseArea(List<BasePigpen> bizBaseAreas){
  124. //构建需要展示的树形节点结构
  125. Map<String, TreeBasePigpen> nodeMap = bizBaseAreas.stream().map(basePigpen -> {
  126. TreeBasePigpen baseVo = new TreeBasePigpen();
  127. baseVo.setId(basePigpen.getId()+"");
  128. baseVo.setFarmCode(basePigpen.getFarmId());
  129. baseVo.setParentId(basePigpen.getParentId());
  130. baseVo.setSort(basePigpen.getSort());
  131. baseVo.setStageCode(basePigpen.getStageCode());
  132. baseVo.setPigpenName(basePigpen.getBuildName());
  133. baseVo.setType(basePigpen.getFType());
  134. baseVo.setChildNode(new ArrayList<>());
  135. return baseVo;
  136. }).collect(Collectors.toMap(TreeBasePigpen::getId, b -> b,(k1, k2)->k1));
  137. //创建数组存父亲节点
  138. ArrayList<TreeBasePigpen> roots = new ArrayList<>();
  139. //构建树形
  140. nodeMap.values().forEach(item->{
  141. String parentCode = item.getParentId()+"";
  142. if (nodeMap.get(parentCode) == null){
  143. //父节点为空,说明当前节点就已经是父节点了 将该节点存起来
  144. roots.add(item);
  145. }else {
  146. //父节点 不为空,说明有父节点,拿到该节点的父节点的孩子节点(就是我自己),将该节点存起来
  147. nodeMap.get(parentCode).getChildNode().add(item);
  148. }
  149. });
  150. return roots;
  151. }
  152. }