package com.zoniot.ccrc.service.impl; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.zoniot.ccrc.commom.exception.ServiceException; import com.zoniot.ccrc.commom.model.BuildingData; import com.zoniot.ccrc.commom.model.Pagination; import com.zoniot.ccrc.commom.utils.TreeUtil; import com.zoniot.ccrc.commom.utils.UserUtil; import com.zoniot.ccrc.dao.AreaMapper; import com.zoniot.ccrc.dao.BuildingMapper; import com.zoniot.ccrc.dao.CommunityMapper; import com.zoniot.ccrc.dao.OrganizationMapper; import com.zoniot.ccrc.dto.BuildingDto; import com.zoniot.ccrc.dto.BuildingSelectDto; import com.zoniot.ccrc.dto.BuildingSelectInfoDto; import com.zoniot.ccrc.dto.LoginUser; import com.zoniot.ccrc.entity.Area; import com.zoniot.ccrc.entity.Building; import com.zoniot.ccrc.entity.Community; import com.zoniot.ccrc.service.BuildingService; import com.zoniot.ccrc.service.CommunityService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import static com.google.common.collect.Lists.newArrayList; @Slf4j @Service public class BuildingServiceImpl implements BuildingService { @Resource private BuildingMapper buildingMapper; @Resource private AreaMapper areaMapper; @Resource private CommunityMapper communityMapper; @Value("${files.path}") private String filesPath; @Autowired private CommunityService communityService; @Resource private OrganizationMapper organizationMapper; @Override public int insert(Building building) { building.setCreateBy("system"); building.setCreateDate(new Date()); building.setUpdateBy("system"); building.setUpdateDate(new Date()); return buildingMapper.insert(building); } @Override public int insertSelective(Building building) { building.setCreateBy(UserUtil.getCurrentUser().getUsername()); building.setCreateDate(new Date()); building.setUpdateBy(UserUtil.getCurrentUser().getUsername()); building.setUpdateDate(new Date()); return buildingMapper.insertSelective(building); } @Override public int insertList(List buildings) { return buildingMapper.insertList(buildings); } @Override public int updateByPrimaryKeySelective(Building building) { building.setUpdateBy(UserUtil.getCurrentUser().getUsername()); building.setUpdateDate(new Date()); return buildingMapper.updateByPrimaryKeySelective(building); } @Override public List select(LoginUser loginUser, Integer channelId) { return null; } @Override public List area(Integer sysId) { return null; } public List areaList(List buildingList){ List ids = newArrayList(); List communityIds = newArrayList(); List buildingSelectDtoArrayList = newArrayList(); if (buildingList != null && buildingList.size() > 0) { buildingList.forEach(building -> { ids.add(building.getProvince()); ids.add(building.getCity()); ids.add(building.getRegion()); communityIds.add(building.getCommunity()); //设备关联上级父类,按小区、区、市、省关联,为空的往上一层关联 BuildingSelectDto buildingSelectDto = new BuildingSelectDto(); buildingSelectDto.setCode(building.getId()); buildingSelectDto.setName(building.getName()); buildingSelectDto.setStatus(1); buildingSelectDto.setNum(building.getDeviceCount()); buildingSelectDto.setLongitude(building.getLongitude()); buildingSelectDto.setLatitude(building.getLatitude()); buildingSelectDto.setDimensionCode("BUILDING"); if (building.getCommunity() != null) { //小区关联建筑,把小区id设置成建筑的父类id buildingSelectDto.setPid(building.getCommunity()); } else { if (building.getRegion() != null) { //区关联建筑,把区id设置成建筑的父类id buildingSelectDto.setPid(building.getRegion()); } else { if (building.getCity() != null) { //市关联建筑,把市id设置成建筑的父类id buildingSelectDto.setPid(building.getCity()); } } } buildingSelectDtoArrayList.add(buildingSelectDto); }); } //查询所有的小区 if (communityIds.size() > 0) { List communityList = communityMapper.findByIds(communityIds.stream().distinct().collect(Collectors.toList())); if (communityList != null && communityList.size() > 0) { for (Community community : communityList) { BuildingSelectDto buildingSelectDto = new BuildingSelectDto(); buildingSelectDto.setCode(community.getId()); buildingSelectDto.setName(community.getName()); buildingSelectDto.setStatus(0); buildingSelectDto.setNum(getCommunityDeviceNum(buildingList, community.getId())); buildingSelectDto.setDimensionCode("COMMUNITY"); if (community.getRegion() != null) { //区关联建筑,把区id设置成建筑的父类id buildingSelectDto.setPid(community.getRegion()); } else { if (community.getCity() != null) { //市关联建筑,把市id设置成建筑的父类id buildingSelectDto.setPid(community.getCity()); } else { if (community.getProvince() != null) { //省关联建筑,把省id设置成建筑的父类id buildingSelectDto.setPid(community.getProvince()); } } } buildingSelectDtoArrayList.add(buildingSelectDto); } } } //查询所有建筑的区域 List areaList = newArrayList(); if (ids.size() > 0) { areaList = areaMapper.findByIds(ids); } if (areaList != null && areaList.size() > 0) { for (Area area : areaList) { BuildingSelectDto buildingSelectDto = new BuildingSelectDto(); buildingSelectDto.setCode(area.getId()); buildingSelectDto.setPid(area.getParentId()); buildingSelectDto.setName(area.getName()); buildingSelectDto.setStatus(0); buildingSelectDto.setNum(getAreaDeviceNum(buildingList, area.getId(), area.getLevelType())); if (area.getLevelType() == 1) { buildingSelectDto.setDimensionCode("PROVINCE"); } if (area.getLevelType() == 2) { buildingSelectDto.setDimensionCode("CITY"); } if (area.getLevelType() == 3) { buildingSelectDto.setDimensionCode("REGION"); } buildingSelectDtoArrayList.add(buildingSelectDto); } } return TreeUtil.getBuildingSelect(buildingSelectDtoArrayList, 100000, "", 1); } private Integer getCommunityDeviceNum(List buildingSelectInfoDtos, Integer communityId) { Integer num = 0; if (buildingSelectInfoDtos != null && buildingSelectInfoDtos.size() > 0) { for (BuildingSelectInfoDto buildingSelectInfoDto : buildingSelectInfoDtos) { if (communityId.equals(buildingSelectInfoDto.getCommunity())) num += buildingSelectInfoDto.getDeviceCount(); } } return num; } private Integer getAreaDeviceNum(List buildingSelectInfoDtos, Integer areaId, Integer levelType) { Integer num = 0; if (buildingSelectInfoDtos != null && buildingSelectInfoDtos.size() > 0) { for (BuildingSelectInfoDto buildingSelectInfoDto : buildingSelectInfoDtos) { if (levelType == 1) { if (areaId.equals(buildingSelectInfoDto.getProvince())) num += buildingSelectInfoDto.getDeviceCount(); } if (levelType == 2) { if (areaId.equals(buildingSelectInfoDto.getCity())) num += buildingSelectInfoDto.getDeviceCount(); } if (levelType == 3) { if (areaId.equals(buildingSelectInfoDto.getRegion())) num += buildingSelectInfoDto.getDeviceCount(); } } } return num; } @Override public Pagination getBuildingPage(Integer sysId, String buildingName, Integer province, Integer city, Integer region, Integer community, int pageNum, int pageSize) { LoginUser loginUser = UserUtil.getCurrentUser(); PageHelper.startPage(pageNum, pageSize); List list = buildingMapper.getBuildingList(loginUser.getSiteId(), sysId, buildingName, province, city, region, community); return new Pagination<>(list); } @Override public BuildingDto getBuilding(Integer buildingId) { return buildingMapper.getById(buildingId); } @Override @Transactional public void add(Building building) { log.info("begin addBuilding,building = " + JSON.toJSONString(building)); LoginUser loginUser = UserUtil.getCurrentUser(); int nameResult = buildingMapper.findByNameUnique(null,loginUser.getSiteId(),building.getCommunityId(), building.getName()); if (nameResult > 0) { throw new ServiceException(-900, "建筑名称已经存在"); } Community community = communityMapper.findById(building.getCommunityId()); if(building.getProvince() == null || building.getProvince() == 0){ building.setProvince(community.getProvince()); } if(building.getCity() == null || building.getCity() == 0){ building.setCity(community.getCity()); } if(building.getRegion() == null || building.getRegion() == 0){ building.setRegion(community.getRegion()); } building.setSiteId(loginUser.getSiteId()); building.setStatus(1); int result = this.insert(building); log.info("end addBuilding,result = " + result); } @Override @Transactional @Caching(evict = {@CacheEvict(value = "buildingInfoListDtoCache", allEntries = true), @CacheEvict(value = "monitoringBuildingInfoListDtoCache", allEntries = true), @CacheEvict(value = "buildingSelectDtoCache", allEntries = true)}) public void edit(Building building) { log.info("begin editBuilding,building = " + JSON.toJSONString(building)); LoginUser loginUser = UserUtil.getCurrentUser(); int nameResult = buildingMapper.findByNameUnique(building.getId(),loginUser.getSiteId(),building.getCommunityId(), building.getName()); if (nameResult > 0) { throw new ServiceException(-900, "建筑名称已经存在"); } Community community = communityMapper.findById(building.getCommunityId()); if(building.getProvince() == null || building.getProvince() == 0){ building.setProvince(community.getProvince()); } if(building.getCity() == null || building.getCity() == 0){ building.setCity(community.getCity()); } if(building.getRegion() == null || building.getRegion() == 0){ building.setRegion(community.getRegion()); } int result = this.updateByPrimaryKeySelective(building); //添加建筑 log.info("end editBuilding,result = " + result); } @Override public void del(Integer buildingId) { log.info("begin delBuilding,buildingId = " + buildingId); Building building = new Building(); building.setId(buildingId); building.setStatus(0); int result = this.updateByPrimaryKeySelective(building); log.info("end delBuilding,result = " + result); } @Override public void syncBuilding(BuildingData buildingData) { Integer siteId = organizationMapper.findSiteId(buildingData.getCustomerId()); //Integer siteId = 1; Building building = new Building(); building.setId(buildingData.getId()); building.setSiteId(siteId); building.setCommunityId(buildingData.getCommunityId()); building.setName(buildingData.getName()); building.setAddress(buildingData.getAddress()); building.setProvince(buildingData.getProvince()); building.setCity(buildingData.getCity()); building.setRegion(buildingData.getRegion()); building.setLongitude(buildingData.getLongitude()); building.setLatitude(buildingData.getLatitude()); building.setStatus(buildingData.getStatus()); building.setCreateDate(buildingData.getCreateDate()); building.setCreateBy("system"); building.setUpdateDate(buildingData.getUpdateDate()); building.setUpdateBy("system"); log.info("syncBuilding building {}", JSON.toJSONString(building)); buildingMapper.replaceSelective(building); } }