| | |
| | | import cn.huge.base.common.exception.ServiceException; |
| | | import cn.huge.base.common.utils.DateUtils; |
| | | import cn.huge.base.common.utils.IdUtils; |
| | | import cn.huge.base.common.utils.ObjectUtils; |
| | | import cn.huge.module.client.api.impl.CustClientImpl; |
| | | import cn.huge.module.client.api.impl.UtilsClientImpl; |
| | | import cn.huge.module.constant.BaseConsts; |
| | | import cn.huge.module.cust.dto.CtUnitDTO; |
| | | import cn.huge.module.kind.dao.mapper.SyRegionMapper; |
| | | import cn.huge.module.kind.domain.bo.AreaBO; |
| | | import cn.huge.module.kind.domain.dto.RegionSelectJSDTO; |
| | | import cn.huge.module.kind.domain.po.SyRegion; |
| | | import cn.huge.module.sys.dto.QueAddrBaseDTO; |
| | | import cn.huge.module.sys.dto.QueAreaDTO; |
| | | import cn.huge.module.utils.BaiduMapAddrDTO; |
| | | import cn.huge.module.utils.BaiduMapUtils; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.apache.commons.collections.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import javax.swing.plaf.synth.Region; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @title: 地域字典表业务逻辑处理 |
| | |
| | | |
| | | @Autowired |
| | | private UtilsClientImpl utilsClient; |
| | | |
| | | @Autowired |
| | | private CustClientImpl custClient; |
| | | |
| | | /** |
| | | * 更新对象 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 递归保存树形结构 |
| | | * @param childrenList |
| | | * @param level |
| | | */ |
| | | public void webSaveByJSTree(List<RegionSelectJSDTO> childrenList, int level){ |
| | | try{ |
| | | Date nowDate = new Date(); |
| | | for (RegionSelectJSDTO regionSelectJSDTO: childrenList){ |
| | | SyRegion region = new SyRegion(); |
| | | region.setId(regionSelectJSDTO.getValue()); |
| | | region.setKind(level); |
| | | region.setName(regionSelectJSDTO.getLabel()); |
| | | region.setLevel(level); |
| | | region.setParentId(regionSelectJSDTO.getParentId()); |
| | | region.setCreateTime(nowDate); |
| | | region.setUpdateTime(nowDate); |
| | | this.saveOrUpdate(region); |
| | | if (CollectionUtils.isNotEmpty(regionSelectJSDTO.getChildren())) { |
| | | this.webSaveByJSTree(regionSelectJSDTO.getChildren(), level + 1); |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("[SyRegionService.webSaveByJSTree]调用失败,异常信息:"+e, e); |
| | | throw new ServiceException("SyRegionService.webSaveByJSTree", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据百度地图经纬度获取街道 |
| | | * @param lng |
| | | * @param lat |
| | | * @return |
| | | */ |
| | | public QueAddrBaseDTO getQueAddrByBaiduiLngLat(String lng, String lat){ |
| | | try{QueAddrBaseDTO queAddrBaseDTO = new QueAddrBaseDTO(); |
| | | BaiduMapAddrDTO baiduMapAddrDTO = BaiduMapUtils.getAddrByLngLat(lng, lat, BaiduMapUtils.coordtype_bd09ll); |
| | | if (ObjectUtils.isNotEmpty(baiduMapAddrDTO)) { |
| | | QueryWrapper<SyRegion> syRegionQueryWrapper = new QueryWrapper<>(); |
| | | // 查询省 |
| | | syRegionQueryWrapper.like("name", baiduMapAddrDTO.getProvince()); |
| | | SyRegion province = mapper.selectOne(syRegionQueryWrapper); |
| | | queAddrBaseDTO.setQueProv(province.getId()); |
| | | queAddrBaseDTO.setQueProvName(province.getName()); |
| | | // 查询市 |
| | | syRegionQueryWrapper.clear(); |
| | | syRegionQueryWrapper.like("name", baiduMapAddrDTO.getCity()); |
| | | SyRegion city = mapper.selectOne(syRegionQueryWrapper); |
| | | queAddrBaseDTO.setQueCity(city.getId()); |
| | | queAddrBaseDTO.setQueCityName(city.getName()); |
| | | // 查询区 |
| | | syRegionQueryWrapper.clear(); |
| | | syRegionQueryWrapper.like("name", baiduMapAddrDTO.getDistrict()); |
| | | SyRegion district = mapper.selectOne(syRegionQueryWrapper); |
| | | queAddrBaseDTO.setQueArea(district.getId()); |
| | | queAddrBaseDTO.setQueAreaName(district.getName()); |
| | | // 查询街道 |
| | | syRegionQueryWrapper.clear(); |
| | | syRegionQueryWrapper.like("name", baiduMapAddrDTO.getTown()); |
| | | SyRegion town = mapper.selectOne(syRegionQueryWrapper); |
| | | queAddrBaseDTO.setQueRoad(town.getId()); |
| | | queAddrBaseDTO.setQueRoadName(town.getName()); |
| | | } |
| | | return queAddrBaseDTO; |
| | | }catch (Exception e){ |
| | | log.error("[SyRegionService.getQueAddrByBaiduiLngLat]调用失败,异常信息:"+e, e); |
| | | throw new ServiceException("SyRegionService.getQueAddrByBaiduiLngLat", e); |
| | | } |
| | | |
| | | } |
| | | |
| | | public List<QueAreaDTO> listByParentId(String parentId){ |
| | | List<QueAreaDTO> list = new ArrayList<>(); |
| | | QueryWrapper<SyRegion> queryWrapper = new QueryWrapper(); |
| | | queryWrapper.eq("parent_id", parentId); |
| | | List<SyRegion> syRegions = mapper.selectList(queryWrapper); |
| | | if(ObjectUtils.isNotEmpty(syRegions)){ |
| | | for (SyRegion syRegion : syRegions) { |
| | | QueAreaDTO queAreaDTO = new QueAreaDTO(); |
| | | queAreaDTO.setAreaCode(syRegion.getId()); |
| | | queAreaDTO.setAreaName(syRegion.getName()); |
| | | queAreaDTO.setParentId(syRegion.getParentId()); |
| | | list.add(queAreaDTO); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public List<AreaBO> treeByUserId(String userId){ |
| | | CtUnitDTO ctUnitDTO = custClient.getUnitByUserId(userId); |
| | | Integer unitGrade = ctUnitDTO.getUnitGrade(); |
| | | Set<AreaBO> areaSet = new HashSet<>(); |
| | | if(unitGrade.equals(1)){ |
| | | QueryWrapper<SyRegion> queryWrapper = new QueryWrapper(); |
| | | queryWrapper.gt("level", 0); |
| | | List<SyRegion> syRegions = mapper.selectList(queryWrapper); |
| | | for (SyRegion syRegion : syRegions) { |
| | | AreaBO areaBO = new AreaBO(); |
| | | areaBO.setParentId(syRegion.getParentId()); |
| | | if(syRegion.getLevel().equals(1)){ |
| | | areaBO.setParentId("root"); |
| | | } |
| | | areaBO.setLevel(String.valueOf(syRegion.getLevel())); |
| | | areaBO.setLabel(syRegion.getName()); |
| | | areaBO.setValue(syRegion.getId()); |
| | | areaBO.setSort(syRegion.getId()); |
| | | areaSet.add(areaBO); |
| | | } |
| | | |
| | | }else if(unitGrade.equals(2)){ |
| | | SyRegion areaSyRegion = mapper.selectById(ctUnitDTO.getArea()); |
| | | if(ObjectUtils.isNotEmpty(areaSyRegion)){ |
| | | AreaBO areaBO = new AreaBO(); |
| | | areaBO.setParentId("root"); |
| | | areaBO.setLabel(areaSyRegion.getName()); |
| | | areaBO.setValue(areaSyRegion.getId()); |
| | | areaBO.setLevel(String.valueOf(areaSyRegion.getLevel())); |
| | | areaBO.setSort(areaSyRegion.getId()); |
| | | areaSet.add(areaBO); |
| | | |
| | | QueryWrapper<SyRegion> queryWrapper = new QueryWrapper(); |
| | | queryWrapper.eq("parent_id", areaSyRegion.getId()); |
| | | List<SyRegion> syRegions = mapper.selectList(queryWrapper); |
| | | for (SyRegion syRegion : syRegions) { |
| | | AreaBO roadaBO = new AreaBO(); |
| | | roadaBO.setParentId(syRegion.getParentId()); |
| | | roadaBO.setLabel(syRegion.getName()); |
| | | roadaBO.setValue(syRegion.getId()); |
| | | roadaBO.setLevel(String.valueOf(syRegion.getLevel())); |
| | | roadaBO.setSort(syRegion.getId()); |
| | | areaSet.add(roadaBO); |
| | | } |
| | | } |
| | | }else { |
| | | SyRegion roadSyRegion = mapper.selectById(ctUnitDTO.getRoad()); |
| | | AreaBO roadaBO = new AreaBO(); |
| | | roadaBO.setParentId("root"); |
| | | roadaBO.setLabel(roadSyRegion.getName()); |
| | | roadaBO.setValue(roadSyRegion.getId()); |
| | | roadaBO.setLevel(String.valueOf(roadSyRegion.getLevel())); |
| | | roadaBO.setSort(roadSyRegion.getId()); |
| | | areaSet.add(roadaBO); |
| | | } |
| | | if(ObjectUtils.isEmpty(areaSet)){ |
| | | return new ArrayList<>(); |
| | | } |
| | | List<AreaBO> areaBOList = sort(areaSet); |
| | | List<AreaBO> areaBOS = sortArea(areaBOList); |
| | | return createTree(areaBOS, null); |
| | | } |
| | | |
| | | public List<AreaBO> sortArea(List<AreaBO> list) { |
| | | Map<String, String> map = new HashMap<>(); |
| | | map.put("越秀区","2"); |
| | | map.put("海珠区","3"); |
| | | map.put("荔湾区","4"); |
| | | map.put("天河区","5"); |
| | | map.put("白云区","6"); |
| | | map.put("黄埔区","7"); |
| | | map.put("花都区","8"); |
| | | map.put("番禺区","9"); |
| | | map.put("南沙区","10"); |
| | | map.put("从化区","11"); |
| | | map.put("增城区","12"); |
| | | for (AreaBO areaBO : list) { |
| | | if(map.containsKey(areaBO.getLabel())){ |
| | | areaBO.setSort(map.get(areaBO.getLabel())); |
| | | } |
| | | } |
| | | // list.sort((o1, o2) -> { |
| | | // Integer totalScore1 = Integer.valueOf(o1.getSort()); |
| | | // Integer totalScore2 = Integer.valueOf(o2.getSort()); |
| | | // if (totalScore1 > totalScore2) { |
| | | // return 1; |
| | | // } else if (totalScore1.equals(totalScore2)) { |
| | | // return 0; |
| | | // } else { |
| | | // return -1; |
| | | // } |
| | | // }); |
| | | return list; |
| | | } |
| | | |
| | | |
| | | public List<AreaBO> sort(Set<AreaBO> termsSet){ |
| | | List<AreaBO> termsList = new ArrayList<>(); |
| | | if(ObjectUtils.isNotEmpty(termsSet)){ |
| | | for (AreaBO selectTermDTO : termsSet) { |
| | | termsList.add(selectTermDTO); |
| | | } |
| | | // termsList.sort((o1, o2)->{ |
| | | // Double totalScore1 = Double.valueOf(o1.getValue()); |
| | | // Double totalScore2 = Double.valueOf(o2.getValue()); |
| | | // if(totalScore1 > totalScore2) { |
| | | // return 1; |
| | | // }else if (totalScore1.equals(totalScore2)) { |
| | | // return 0; |
| | | // }else { |
| | | // return -1; |
| | | // } |
| | | // }); |
| | | } |
| | | return termsList; |
| | | } |
| | | |
| | | /** |
| | | * 创建树形结构 |
| | | * |
| | | * @param selectTermDTOList 所有集合 |
| | | * @param firstId 某一级编号 |
| | | * @return List |
| | | */ |
| | | public List<AreaBO> createTree(List<AreaBO> selectTermDTOList, String firstId) { |
| | | List<AreaBO> firstMapList = new ArrayList<>(); |
| | | for (AreaBO selectTermDTO : selectTermDTOList) { |
| | | for (AreaBO currentParam : selectTermDTOList) { |
| | | if (currentParam.getValue().equals(selectTermDTO.getParentId())) { |
| | | addToMBean(currentParam, selectTermDTO); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | //取第一级节点 |
| | | for (AreaBO selectTermDTO : selectTermDTOList) { |
| | | if (StringUtils.isNotEmpty(firstId)) { |
| | | if (firstId.equals(selectTermDTO.getValue())) { |
| | | firstMapList.add(selectTermDTO); |
| | | } |
| | | } else { |
| | | if (BaseConsts.ROOT.equals(selectTermDTO.getParentId()) || StringUtils.isEmpty(selectTermDTO.getParentId())) { |
| | | firstMapList.add(selectTermDTO); |
| | | } |
| | | } |
| | | } |
| | | return firstMapList; |
| | | } |
| | | |
| | | public void addToMBean(AreaBO targetUnit, AreaBO currentUnit) { |
| | | List<AreaBO> childListObj = targetUnit.getChildren(); |
| | | List<AreaBO> childList = null; |
| | | if (CollectionUtils.isEmpty(childListObj)) { |
| | | childList = new ArrayList(); |
| | | targetUnit.setChildren(childList); |
| | | } else { |
| | | childList = childListObj; |
| | | } |
| | | childList.add(currentUnit); |
| | | } |
| | | |
| | | } |