BasePigpenServiceImpl.java 7.9 KB

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