| | |
| | | package com.xiaopeng.map.service.serviceimpl; |
| | | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.xiaopeng.api.dto.operation.PointAddDto; |
| | |
| | | import com.xiaopeng.common.core.utils.StringUtils; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import com.xiaopeng.evi.oss.service.AliOssService; |
| | | import com.xiaopeng.map.constant.I18nKeyConstant; |
| | | import com.xiaopeng.map.dto.NearestDto; |
| | | import com.xiaopeng.map.mapper.XpPointMapper; |
| | |
| | | import com.xiaopeng.map.vo.OrderDistanceVo; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sourceforge.pinyin4j.PinyinHelper; |
| | | import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType; |
| | | import org.apache.shardingsphere.transaction.core.TransactionType; |
| | | import org.redisson.api.RLock; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.time.LocalTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | public class XpPointServiceImpl extends ServiceImpl<XpPointMapper, XpPoint> implements XpPointService { |
| | | |
| | | private static final String POINT_BIZ_TYPE = "PUD"; |
| | | private static final int MAX_POINTS_PER_FENCE = 15; |
| | | private static final int SERIAL_WIDTH = 6; |
| | | private static final int LOCK_WAIT_SECONDS = 5; |
| | | private static final int LOCK_LEASE_SECONDS = 30; |
| | |
| | | @Autowired |
| | | private RedissonClient redissonClient; |
| | | |
| | | @Autowired |
| | | private AliOssService aliOssService; |
| | | |
| | | @Override |
| | | public void addPointWithGeneratedNumber(PointAddDto pointAddDto) { |
| | | @Transactional |
| | | @ShardingSphereTransactionType(TransactionType.BASE) |
| | | public XpPoint addPointWithGeneratedNumber(PointAddDto pointAddDto) { |
| | | if (pointAddDto == null) { |
| | | throw new ServiceException("参数不能为空", I18nKeyConstant.SYSTEM_EXCEPTION); |
| | | } |
| | | if (pointAddDto.getElectronicAreaId() == null) { |
| | | throw new ServiceException("运营区域ID不能为空", I18nKeyConstant.POINT_ELECTRONIC_AREA_ID_EMPTY); |
| | | } |
| | | validatePointNameUnique(pointAddDto.getPointName(), null); |
| | | |
| | | String cityPrefix = buildCityPrefix(pointAddDto.getElectronicAreaId()); |
| | | String lockKey = buildPointNumberLockKey(cityPrefix, POINT_BIZ_TYPE); |
| | |
| | | if (!locked) { |
| | | throw new ServiceException("点位编号生成繁忙,请稍后重试", I18nKeyConstant.POINT_NUMBER_LOCK_FAIL); |
| | | } |
| | | validateFencePointCountLimit(pointAddDto.getElectronicAreaId()); |
| | | String maxPointNumber = this.baseMapper.selectMaxPointNumber(cityPrefix, POINT_BIZ_TYPE); |
| | | int nextSerial = 1; |
| | | if (StringUtils.hasText(maxPointNumber)) { |
| | |
| | | |
| | | XpPoint xpPoint = new XpPoint(); |
| | | BeanUtils.copyProperties(pointAddDto, xpPoint); |
| | | xpPoint.setFpElectronicAreaId(pointAddDto.getElectronicAreaId()); |
| | | xpPoint.setPointNumber(pointNumber); |
| | | this.save(xpPoint); |
| | | return xpPoint; |
| | | } |
| | | |
| | | @Override |
| | | public void validatePointNameUnique(String pointName, Long excludeId) { |
| | | if (!StringUtils.hasText(pointName)) { |
| | | return; |
| | | } |
| | | long count = this.lambdaQuery() |
| | | .eq(XpPoint::getPointName, pointName) |
| | | .ne(excludeId != null, XpPoint::getId, excludeId) |
| | | .count(); |
| | | if (count > 0) { |
| | | throw new ServiceException("点位名称已存在", I18nKeyConstant.POINT_NAME_EXISTS); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | private String buildCityPrefix(Long electronicAreaId) { |
| | | //所有围栏范围之外,测试点位 |
| | | if(0 == electronicAreaId){ |
| | | return "TEST"; |
| | | } |
| | | XpElectronicFenceArea area = xpElectronicFenceAreaService.getById(electronicAreaId); |
| | | if (area == null) { |
| | | throw new ServiceException("运营区域不存在", I18nKeyConstant.POINT_ELECTRONIC_AREA_NOT_EXIST); |
| | |
| | | String name = region.getName(); |
| | | String prefix = name.length() <= 2 ? name : name.substring(0, 2); |
| | | return getPinyinInitials(prefix).toUpperCase(); |
| | | } |
| | | |
| | | private void validateFencePointCountLimit(Long electronicAreaId) { |
| | | if (electronicAreaId == null || electronicAreaId == 0L) { |
| | | return; |
| | | } |
| | | XpElectronicFenceArea currentArea = xpElectronicFenceAreaService.getById(electronicAreaId); |
| | | if (currentArea == null) { |
| | | throw new ServiceException("运营区域不存在", I18nKeyConstant.POINT_ELECTRONIC_AREA_NOT_EXIST); |
| | | } |
| | | Long fenceId = currentArea.getElectronicId(); |
| | | if (fenceId == null) { |
| | | throw new ServiceException("运营区域不存在", I18nKeyConstant.POINT_ELECTRONIC_AREA_NOT_EXIST); |
| | | } |
| | | List<Long> areaIds = xpElectronicFenceAreaService.lambdaQuery() |
| | | .eq(XpElectronicFenceArea::getElectronicId, fenceId) |
| | | .list() |
| | | .stream() |
| | | .map(XpElectronicFenceArea::getId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | if (areaIds.isEmpty()) { |
| | | return; |
| | | } |
| | | long pointCount = this.lambdaQuery() |
| | | .in(XpPoint::getElectronicAreaId, areaIds) |
| | | .count(); |
| | | if (pointCount >= MAX_POINTS_PER_FENCE) { |
| | | throw new ServiceException("当前围栏最多只能添加15个点位", I18nKeyConstant.POINT_FENCE_POINT_LIMIT_EXCEEDED); |
| | | } |
| | | } |
| | | |
| | | private static String buildPointNumberLockKey(String cityPrefix, String bizType) { |
| | |
| | | @Override |
| | | public List<XpPoint> listAll(PointListQuery query) { |
| | | List<XpPoint> list = this.baseMapper.listAll(query); |
| | | for (XpPoint xpPoint : list) { |
| | | String pointPictures = xpPoint.getPointPictures(); |
| | | log.info("pointPictures:{}", pointPictures); |
| | | if (StringUtils.isNotBlank(pointPictures)) { |
| | | log.info("pointPictures进入处理:{}", pointPictures); |
| | | |
| | | JSONArray jsonArray = JSONArray.parseArray(pointPictures); |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject jsonObject = jsonArray.getJSONObject(i); |
| | | if (Objects.isNull(jsonObject)) { |
| | | continue; |
| | | } |
| | | String downloadUrl =""; |
| | | try { |
| | | downloadUrl = aliOssService.getDownloadUrl(jsonObject.getString("id"), jsonObject.getString("name"), true); |
| | | log.info("downloadUrl:{}", downloadUrl); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("获取图片下载地址失败", e); |
| | | } |
| | | jsonObject.put("url", downloadUrl); |
| | | } |
| | | log.info("jsonArray:{}", jsonArray); |
| | | xpPoint.setPointPictures(JSONArray.toJSONString(jsonArray)); |
| | | log.info("xpPoint:{}", xpPoint.getPointPictures()); |
| | | } |
| | | |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | |
| | | @Override |
| | | public PageInfo<XpPoint> pageList(PointPageListListQuery query) { |
| | | PageInfo<XpPoint> pageInfo = new PageInfo<>(query.getPageNum(),query.getPageSize()); |
| | | if (StringUtils.isNotBlank(query.getPointLonAndLat())) { |
| | | List<String> lonAndLat = query.getPointLonAndLat().trim().contains(",") ? Arrays.asList(query.getPointLonAndLat().split(",")) : |
| | | query.getPointLonAndLat().trim().contains(" ") ? Arrays.asList(query.getPointLonAndLat().split(" ")) : |
| | | Collections.emptyList(); |
| | | if (CollUtil.isNotEmpty(lonAndLat)) { |
| | | query.setPointLon(lonAndLat.get(0)); |
| | | query.setPointLat(lonAndLat.get(1)); |
| | | query.setPointLonAndLat(null); |
| | | } |
| | | } |
| | | List<XpPoint> list = this.baseMapper.pageList(pageInfo,query); |
| | | pageInfo.setRecords(list); |
| | | return pageInfo; |