BuildingServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package com.zoniot.ccrc.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.github.pagehelper.PageHelper;
  4. import com.zoniot.ccrc.commom.exception.ServiceException;
  5. import com.zoniot.ccrc.commom.model.BuildingData;
  6. import com.zoniot.ccrc.commom.model.Pagination;
  7. import com.zoniot.ccrc.commom.utils.TreeUtil;
  8. import com.zoniot.ccrc.commom.utils.UserUtil;
  9. import com.zoniot.ccrc.dao.AreaMapper;
  10. import com.zoniot.ccrc.dao.BuildingMapper;
  11. import com.zoniot.ccrc.dao.CommunityMapper;
  12. import com.zoniot.ccrc.dao.OrganizationMapper;
  13. import com.zoniot.ccrc.dto.BuildingDto;
  14. import com.zoniot.ccrc.dto.BuildingSelectDto;
  15. import com.zoniot.ccrc.dto.BuildingSelectInfoDto;
  16. import com.zoniot.ccrc.dto.LoginUser;
  17. import com.zoniot.ccrc.entity.Area;
  18. import com.zoniot.ccrc.entity.Building;
  19. import com.zoniot.ccrc.entity.Community;
  20. import com.zoniot.ccrc.service.BuildingService;
  21. import com.zoniot.ccrc.service.CommunityService;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.cache.annotation.CacheEvict;
  27. import org.springframework.cache.annotation.Caching;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import javax.annotation.Resource;
  31. import java.time.LocalDateTime;
  32. import java.util.Date;
  33. import java.util.List;
  34. import java.util.stream.Collectors;
  35. import static com.google.common.collect.Lists.newArrayList;
  36. @Slf4j
  37. @Service
  38. public class BuildingServiceImpl implements BuildingService {
  39. @Resource
  40. private BuildingMapper buildingMapper;
  41. @Resource
  42. private AreaMapper areaMapper;
  43. @Resource
  44. private CommunityMapper communityMapper;
  45. @Value("${files.path}")
  46. private String filesPath;
  47. @Autowired
  48. private CommunityService communityService;
  49. @Resource
  50. private OrganizationMapper organizationMapper;
  51. @Override
  52. public int insert(Building building) {
  53. building.setCreateBy("system");
  54. building.setCreateDate(new Date());
  55. building.setUpdateBy("system");
  56. building.setUpdateDate(new Date());
  57. return buildingMapper.insert(building);
  58. }
  59. @Override
  60. public int insertSelective(Building building) {
  61. building.setCreateBy(UserUtil.getCurrentUser().getUsername());
  62. building.setCreateDate(new Date());
  63. building.setUpdateBy(UserUtil.getCurrentUser().getUsername());
  64. building.setUpdateDate(new Date());
  65. return buildingMapper.insertSelective(building);
  66. }
  67. @Override
  68. public int insertList(List<Building> buildings) {
  69. return buildingMapper.insertList(buildings);
  70. }
  71. @Override
  72. public int updateByPrimaryKeySelective(Building building) {
  73. building.setUpdateBy(UserUtil.getCurrentUser().getUsername());
  74. building.setUpdateDate(new Date());
  75. return buildingMapper.updateByPrimaryKeySelective(building);
  76. }
  77. @Override
  78. public List<BuildingSelectDto> select(LoginUser loginUser, Integer channelId) {
  79. return null;
  80. }
  81. @Override
  82. public List<BuildingSelectDto> area(Integer sysId) {
  83. return null;
  84. }
  85. public List<BuildingSelectDto> areaList(List<BuildingSelectInfoDto> buildingList){
  86. List<Integer> ids = newArrayList();
  87. List<Integer> communityIds = newArrayList();
  88. List<BuildingSelectDto> buildingSelectDtoArrayList = newArrayList();
  89. if (buildingList != null && buildingList.size() > 0) {
  90. buildingList.forEach(building -> {
  91. ids.add(building.getProvince());
  92. ids.add(building.getCity());
  93. ids.add(building.getRegion());
  94. communityIds.add(building.getCommunity());
  95. //设备关联上级父类,按小区、区、市、省关联,为空的往上一层关联
  96. BuildingSelectDto buildingSelectDto = new BuildingSelectDto();
  97. buildingSelectDto.setCode(building.getId());
  98. buildingSelectDto.setName(building.getName());
  99. buildingSelectDto.setStatus(1);
  100. buildingSelectDto.setNum(building.getDeviceCount());
  101. buildingSelectDto.setLongitude(building.getLongitude());
  102. buildingSelectDto.setLatitude(building.getLatitude());
  103. buildingSelectDto.setDimensionCode("BUILDING");
  104. if (building.getCommunity() != null) {
  105. //小区关联建筑,把小区id设置成建筑的父类id
  106. buildingSelectDto.setPid(building.getCommunity());
  107. } else {
  108. if (building.getRegion() != null) {
  109. //区关联建筑,把区id设置成建筑的父类id
  110. buildingSelectDto.setPid(building.getRegion());
  111. } else {
  112. if (building.getCity() != null) {
  113. //市关联建筑,把市id设置成建筑的父类id
  114. buildingSelectDto.setPid(building.getCity());
  115. }
  116. }
  117. }
  118. buildingSelectDtoArrayList.add(buildingSelectDto);
  119. });
  120. }
  121. //查询所有的小区
  122. if (communityIds.size() > 0) {
  123. List<Community> communityList = communityMapper.findByIds(communityIds.stream().distinct().collect(Collectors.toList()));
  124. if (communityList != null && communityList.size() > 0) {
  125. for (Community community : communityList) {
  126. BuildingSelectDto buildingSelectDto = new BuildingSelectDto();
  127. buildingSelectDto.setCode(community.getId());
  128. buildingSelectDto.setName(community.getName());
  129. buildingSelectDto.setStatus(0);
  130. buildingSelectDto.setNum(getCommunityDeviceNum(buildingList, community.getId()));
  131. buildingSelectDto.setDimensionCode("COMMUNITY");
  132. if (community.getRegion() != null) {
  133. //区关联建筑,把区id设置成建筑的父类id
  134. buildingSelectDto.setPid(community.getRegion());
  135. } else {
  136. if (community.getCity() != null) {
  137. //市关联建筑,把市id设置成建筑的父类id
  138. buildingSelectDto.setPid(community.getCity());
  139. } else {
  140. if (community.getProvince() != null) {
  141. //省关联建筑,把省id设置成建筑的父类id
  142. buildingSelectDto.setPid(community.getProvince());
  143. }
  144. }
  145. }
  146. buildingSelectDtoArrayList.add(buildingSelectDto);
  147. }
  148. }
  149. }
  150. //查询所有建筑的区域
  151. List<Area> areaList = newArrayList();
  152. if (ids.size() > 0) {
  153. areaList = areaMapper.findByIds(ids);
  154. }
  155. if (areaList != null && areaList.size() > 0) {
  156. for (Area area : areaList) {
  157. BuildingSelectDto buildingSelectDto = new BuildingSelectDto();
  158. buildingSelectDto.setCode(area.getId());
  159. buildingSelectDto.setPid(area.getParentId());
  160. buildingSelectDto.setName(area.getName());
  161. buildingSelectDto.setStatus(0);
  162. buildingSelectDto.setNum(getAreaDeviceNum(buildingList, area.getId(), area.getLevelType()));
  163. if (area.getLevelType() == 1) {
  164. buildingSelectDto.setDimensionCode("PROVINCE");
  165. }
  166. if (area.getLevelType() == 2) {
  167. buildingSelectDto.setDimensionCode("CITY");
  168. }
  169. if (area.getLevelType() == 3) {
  170. buildingSelectDto.setDimensionCode("REGION");
  171. }
  172. buildingSelectDtoArrayList.add(buildingSelectDto);
  173. }
  174. }
  175. return TreeUtil.getBuildingSelect(buildingSelectDtoArrayList, 100000, "", 1);
  176. }
  177. private Integer getCommunityDeviceNum(List<BuildingSelectInfoDto> buildingSelectInfoDtos, Integer communityId) {
  178. Integer num = 0;
  179. if (buildingSelectInfoDtos != null && buildingSelectInfoDtos.size() > 0) {
  180. for (BuildingSelectInfoDto buildingSelectInfoDto : buildingSelectInfoDtos) {
  181. if (communityId.equals(buildingSelectInfoDto.getCommunity()))
  182. num += buildingSelectInfoDto.getDeviceCount();
  183. }
  184. }
  185. return num;
  186. }
  187. private Integer getAreaDeviceNum(List<BuildingSelectInfoDto> buildingSelectInfoDtos, Integer areaId, Integer levelType) {
  188. Integer num = 0;
  189. if (buildingSelectInfoDtos != null && buildingSelectInfoDtos.size() > 0) {
  190. for (BuildingSelectInfoDto buildingSelectInfoDto : buildingSelectInfoDtos) {
  191. if (levelType == 1) {
  192. if (areaId.equals(buildingSelectInfoDto.getProvince()))
  193. num += buildingSelectInfoDto.getDeviceCount();
  194. }
  195. if (levelType == 2) {
  196. if (areaId.equals(buildingSelectInfoDto.getCity())) num += buildingSelectInfoDto.getDeviceCount();
  197. }
  198. if (levelType == 3) {
  199. if (areaId.equals(buildingSelectInfoDto.getRegion())) num += buildingSelectInfoDto.getDeviceCount();
  200. }
  201. }
  202. }
  203. return num;
  204. }
  205. @Override
  206. public Pagination<BuildingDto> getBuildingPage(Integer sysId, String buildingName, Integer province, Integer city, Integer region, Integer community, int pageNum, int pageSize) {
  207. LoginUser loginUser = UserUtil.getCurrentUser();
  208. PageHelper.startPage(pageNum, pageSize);
  209. List<BuildingDto> list = buildingMapper.getBuildingList(loginUser.getSiteId(), sysId, buildingName, province, city, region, community);
  210. return new Pagination<>(list);
  211. }
  212. @Override
  213. public BuildingDto getBuilding(Integer buildingId) {
  214. return buildingMapper.getById(buildingId);
  215. }
  216. @Override
  217. @Transactional
  218. public void add(Building building) {
  219. log.info("begin addBuilding,building = " + JSON.toJSONString(building));
  220. LoginUser loginUser = UserUtil.getCurrentUser();
  221. int nameResult = buildingMapper.findByNameUnique(null,loginUser.getSiteId(),building.getCommunityId(), building.getName());
  222. if (nameResult > 0) {
  223. throw new ServiceException(-900, "建筑名称已经存在");
  224. }
  225. Community community = communityMapper.findById(building.getCommunityId());
  226. if(building.getProvince() == null || building.getProvince() == 0){
  227. building.setProvince(community.getProvince());
  228. }
  229. if(building.getCity() == null || building.getCity() == 0){
  230. building.setCity(community.getCity());
  231. }
  232. if(building.getRegion() == null || building.getRegion() == 0){
  233. building.setRegion(community.getRegion());
  234. }
  235. building.setSiteId(loginUser.getSiteId());
  236. building.setStatus(1);
  237. int result = this.insert(building);
  238. log.info("end addBuilding,result = " + result);
  239. }
  240. @Override
  241. @Transactional
  242. @Caching(evict = {@CacheEvict(value = "buildingInfoListDtoCache", allEntries = true), @CacheEvict(value = "monitoringBuildingInfoListDtoCache", allEntries = true), @CacheEvict(value = "buildingSelectDtoCache", allEntries = true)})
  243. public void edit(Building building) {
  244. log.info("begin editBuilding,building = " + JSON.toJSONString(building));
  245. LoginUser loginUser = UserUtil.getCurrentUser();
  246. int nameResult = buildingMapper.findByNameUnique(building.getId(),loginUser.getSiteId(),building.getCommunityId(), building.getName());
  247. if (nameResult > 0) {
  248. throw new ServiceException(-900, "建筑名称已经存在");
  249. }
  250. Community community = communityMapper.findById(building.getCommunityId());
  251. if(building.getProvince() == null || building.getProvince() == 0){
  252. building.setProvince(community.getProvince());
  253. }
  254. if(building.getCity() == null || building.getCity() == 0){
  255. building.setCity(community.getCity());
  256. }
  257. if(building.getRegion() == null || building.getRegion() == 0){
  258. building.setRegion(community.getRegion());
  259. }
  260. int result = this.updateByPrimaryKeySelective(building);
  261. //添加建筑
  262. log.info("end editBuilding,result = " + result);
  263. }
  264. @Override
  265. public void del(Integer buildingId) {
  266. log.info("begin delBuilding,buildingId = " + buildingId);
  267. Building building = new Building();
  268. building.setId(buildingId);
  269. building.setStatus(0);
  270. int result = this.updateByPrimaryKeySelective(building);
  271. log.info("end delBuilding,result = " + result);
  272. }
  273. @Override
  274. public void syncBuilding(BuildingData buildingData) {
  275. Integer siteId = organizationMapper.findSiteId(buildingData.getCustomerId());
  276. //Integer siteId = 1;
  277. Building building = new Building();
  278. building.setId(buildingData.getId());
  279. building.setSiteId(siteId);
  280. building.setCommunityId(buildingData.getCommunityId());
  281. building.setName(buildingData.getName());
  282. building.setAddress(buildingData.getAddress());
  283. building.setProvince(buildingData.getProvince());
  284. building.setCity(buildingData.getCity());
  285. building.setRegion(buildingData.getRegion());
  286. building.setLongitude(buildingData.getLongitude());
  287. building.setLatitude(buildingData.getLatitude());
  288. building.setStatus(buildingData.getStatus());
  289. building.setCreateDate(buildingData.getCreateDate());
  290. building.setCreateBy("system");
  291. building.setUpdateDate(buildingData.getUpdateDate());
  292. building.setUpdateBy("system");
  293. log.info("syncBuilding building {}", JSON.toJSONString(building));
  294. buildingMapper.replaceSelective(building);
  295. }
  296. }