|
|
|
@ -1,20 +1,31 @@
|
|
|
|
|
package com.baiye.modules.distribute.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import com.baiye.domain.PageParam;
|
|
|
|
|
import com.baiye.domain.PageResult;
|
|
|
|
|
import com.baiye.exception.BadRequestException;
|
|
|
|
|
import com.baiye.extend.mybatis.plus.service.impl.ExtendServiceImpl;
|
|
|
|
|
import com.baiye.modules.distribute.dto.StoreDTO;
|
|
|
|
|
import com.baiye.modules.distribute.entity.StoreEntity;
|
|
|
|
|
import com.baiye.modules.distribute.mapper.StoreMapper;
|
|
|
|
|
import com.baiye.modules.distribute.qo.StoreQo;
|
|
|
|
|
import com.baiye.modules.distribute.service.CustomStoreService;
|
|
|
|
|
import com.baiye.modules.distribute.service.StoreService;
|
|
|
|
|
import com.baiye.modules.distribute.vo.StoreVO;
|
|
|
|
|
import com.baiye.security.util.SecurityUtils;
|
|
|
|
|
import com.baiye.util.GeoUtil;
|
|
|
|
|
import com.baiye.util.RedisUtils;
|
|
|
|
|
import com.baiye.utils.AddressSplitterUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
@ -29,11 +40,13 @@ import java.util.stream.Collectors;
|
|
|
|
|
public class StoreServiceImpl extends ExtendServiceImpl<StoreMapper, StoreEntity> implements StoreService {
|
|
|
|
|
|
|
|
|
|
private final CustomStoreService customStoreService;
|
|
|
|
|
|
|
|
|
|
private final RedisUtils redisUtils;
|
|
|
|
|
private final GeoUtil geoUtil;
|
|
|
|
|
private final String storeRedisKey = "store::";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PageResult<StoreVO> queryStore(PageParam pageParam, StoreQo storeQo) {
|
|
|
|
|
|
|
|
|
|
public PageResult<StoreVO> queryStorePage(PageParam pageParam, StoreQo storeQo) {
|
|
|
|
|
storeQo.setCreateBy(SecurityUtils.getCurrentUserId());
|
|
|
|
|
PageResult<StoreVO> storeEntityPageResult = baseMapper.queryPage(pageParam, storeQo);
|
|
|
|
|
List<StoreVO> records = storeEntityPageResult.getRecords();
|
|
|
|
|
if (CollUtil.isNotEmpty(records)) {
|
|
|
|
@ -48,14 +61,34 @@ public class StoreServiceImpl extends ExtendServiceImpl<StoreMapper, StoreEntity
|
|
|
|
|
return storeEntityPageResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<StoreVO> queryStore(StoreQo qo) {
|
|
|
|
|
LambdaQueryWrapper<StoreEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
if (qo.getStoreName() != null) {
|
|
|
|
|
wrapper.like(StoreEntity::getStoreName, qo.getStoreName());
|
|
|
|
|
}
|
|
|
|
|
wrapper.eq(StoreEntity::getCreateBy, SecurityUtils.getCurrentUserId());
|
|
|
|
|
wrapper.orderByDesc(StoreEntity::getCreateTime);
|
|
|
|
|
List<StoreEntity> storeEntities = baseMapper.selectList(wrapper);
|
|
|
|
|
return Convert.toList(StoreVO.class, storeEntities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addStore(StoreEntity storeEntity) {
|
|
|
|
|
//同一个公司门店名不能重复
|
|
|
|
|
Long currentUserId = SecurityUtils.getCurrentUserId();
|
|
|
|
|
StoreEntity storeEntity1 = baseMapper.selectOne(new LambdaQueryWrapper<StoreEntity>().eq(StoreEntity::getCreateBy, currentUserId).eq(StoreEntity::getStoreName, storeEntity.getStoreName()));
|
|
|
|
|
if (storeEntity1 != null) {
|
|
|
|
|
throw new BadRequestException("门店名称重复");
|
|
|
|
|
}
|
|
|
|
|
String address = storeEntity.getAddress();
|
|
|
|
|
Map<String, String> map = AddressSplitterUtil.getAddress(address);
|
|
|
|
|
storeEntity.setProvince(map.get("province"));
|
|
|
|
|
storeEntity.setCity(map.get("city"));
|
|
|
|
|
storeEntity.setCounty(map.get("county"));
|
|
|
|
|
storeEntity.setDetail(map.get("detail"));
|
|
|
|
|
baseMapper.insert(storeEntity);
|
|
|
|
|
addStoreRedis(storeEntity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -86,4 +119,63 @@ public class StoreServiceImpl extends ExtendServiceImpl<StoreMapper, StoreEntity
|
|
|
|
|
public void deleteStore(Long storeId) {
|
|
|
|
|
baseMapper.deleteById(storeId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<StoreDTO> queryRangeStore(Double longitude, Double latitude, Double distance) {
|
|
|
|
|
List<StoreDTO> list = new ArrayList<>();
|
|
|
|
|
// Long whichUserId = SecurityUtils.getWhichUserId();
|
|
|
|
|
// log.info("父id:{}",whichUserId);
|
|
|
|
|
Long userId = 1L;
|
|
|
|
|
|
|
|
|
|
List<StoreEntity> storeEntities = baseMapper.selectList(new LambdaQueryWrapper<StoreEntity>().eq(StoreEntity::getCreateBy, userId));
|
|
|
|
|
Map<String, Double> map = new HashMap<>();
|
|
|
|
|
if (CollUtil.isNotEmpty(storeEntities)) {
|
|
|
|
|
redisUtils.del(userId.toString());
|
|
|
|
|
for (StoreEntity storeEntity : storeEntities) {
|
|
|
|
|
geoUtil.geoAdd(storeEntity.getId().toString(), storeEntity.getLongitude().doubleValue(), storeEntity.getLatitude().doubleValue(), userId.toString());
|
|
|
|
|
}
|
|
|
|
|
String key = RandomUtil.randomString(4);
|
|
|
|
|
geoUtil.geoAdd(key, longitude, latitude, userId.toString());
|
|
|
|
|
map = geoUtil.distanceInclude(userId.toString(), key, distance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (map != null) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (Map.Entry<String, Double> range : map.entrySet()) {
|
|
|
|
|
String key = range.getKey();
|
|
|
|
|
StoreDTO storeRedis = getStoreRedis(key);
|
|
|
|
|
if (storeRedis != null && storeRedis.getId() != null) {
|
|
|
|
|
storeRedis.setDistance(range.getValue());
|
|
|
|
|
storeRedis.setIndex(index);
|
|
|
|
|
list.add(storeRedis);
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StoreDTO getStoreRedis(String id) {
|
|
|
|
|
|
|
|
|
|
Object o = redisUtils.get(storeRedisKey + id);
|
|
|
|
|
if (o != null) {
|
|
|
|
|
return BeanUtil.toBean(o, StoreDTO.class);
|
|
|
|
|
} else {
|
|
|
|
|
StoreEntity storeEntity = baseMapper.selectById(id);
|
|
|
|
|
StoreDTO storeDTO = new StoreDTO();
|
|
|
|
|
if (storeEntity != null && storeEntity.getId() != null) {
|
|
|
|
|
BeanUtil.copyProperties(storeEntity, storeDTO);
|
|
|
|
|
storeDTO.setAddress(storeEntity.getCounty() + storeEntity.getDetail());
|
|
|
|
|
redisUtils.set(storeRedisKey + storeEntity.getId(), storeDTO);
|
|
|
|
|
}
|
|
|
|
|
return storeDTO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addStoreRedis(StoreEntity storeEntity) {
|
|
|
|
|
StoreDTO storeDTO = new StoreDTO();
|
|
|
|
|
BeanUtil.copyProperties(storeEntity, storeDTO);
|
|
|
|
|
storeDTO.setAddress(storeEntity.getCounty() + storeEntity.getDetail());
|
|
|
|
|
redisUtils.set(storeRedisKey + storeEntity.getId(), storeDTO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|