puzhibing
2024-12-26 132b50b690dde85c616448840cc505056de708b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ruoyi.other.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.GeodesyUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.domain.ShopScore;
import com.ruoyi.other.mapper.ShopMapper;
import com.ruoyi.other.service.ShopScoreService;
import com.ruoyi.other.service.ShopService;
import com.ruoyi.other.vo.NearbyShopVO;
import com.ruoyi.other.vo.ShopDetailVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private ShopScoreService shopScoreService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private TokenService tokenService;
 
 
    @Override
    public IPage<Shop> getShopList(Integer PageNum, Integer pageSize, Shop shop) {
        Page<Shop> page = new Page<>();
        page.setCurrent(PageNum);
        page.setSize(pageSize);
        return shopMapper.selectShopList(page, shop);
    }
 
    @Override
    public List<NearbyShopVO> nearbyShopList(BigDecimal longitude, BigDecimal latitude) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        AppUser appUser = appUserClient.getAppUserById(userid);
        List<NearbyShopVO> nearbyShopVOS = shopMapper.selectNearbyShopList(longitude, longitude);
        if(null != appUser.getShopId()){
            NearbyShopVO nearbyShopVO = nearbyShopVOS.stream().filter(s -> s.getId().equals(appUser.getShopId().longValue())).findFirst().get();
            if(null != nearbyShopVO){
                nearbyShopVOS.remove(nearbyShopVO);
            }
 
            Shop shop = shopMapper.selectById(appUser.getShopId());
            NearbyShopVO vo = new NearbyShopVO();
            vo.setId(appUser.getShopId().longValue());
            vo.setName(shop.getName());
            vo.setAddress(shop.getAddress());
            vo.setHomePicture(shop.getHomePicture());
            Double wgs84 = GeodesyUtil.getDistance(longitude.toString() + "," + latitude.toString(), shop.getLongitude() + "," + shop.getLatitude()).get("WGS84");
            vo.setDistance(wgs84.toString());
            nearbyShopVOS.add(0, vo);
        }
        if (nearbyShopVOS == null || nearbyShopVOS.isEmpty()) {
            return Collections.emptyList();
        }
 
        List<Long> shopIds = nearbyShopVOS.stream().map(NearbyShopVO::getId).collect(Collectors.toList());
        List<ShopScore> shopScores = shopScoreService.list(new LambdaQueryWrapper<ShopScore>().in(ShopScore::getShopId, shopIds));
        if (shopScores == null || shopScores.isEmpty()) {
            return nearbyShopVOS;
        }
 
        Map<Long, List<ShopScore>> shopScoreMap = shopScores.stream().collect(Collectors.groupingBy(ShopScore::getShopId));
        nearbyShopVOS.forEach(nearbyShopVO -> {
            List<ShopScore> scores = shopScoreMap.get(nearbyShopVO.getId());
            if (scores != null && !scores.isEmpty()){
                BigDecimal score = scores.stream()
                        .map(ShopScore::getScore)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(new BigDecimal(scores.size()), 1, RoundingMode.HALF_UP);
                nearbyShopVO.setScore(score.toString());
            }
        });
        return nearbyShopVOS;
    }
 
    @Override
    public ShopDetailVO getShopDetail(Integer shopId, BigDecimal longitude, BigDecimal latitude) {
        // 查询店铺详情
        ShopDetailVO shopDetailVO = shopMapper.selectShopDetail(shopId);
        if (shopDetailVO == null) {
            throw new ServiceException("查询店铺不存在");
        }
 
        // 计算距离
        if (shopDetailVO.getLongitude() != null && shopDetailVO.getLatitude() != null){
            String shopLocation = String.format("%s,%s", shopDetailVO.getLongitude(), shopDetailVO.getLatitude());
            String userLocation = String.format("%s,%s", longitude.toString(), latitude.toString());
            Map<String, Double> distanceMap = GeodesyUtil.getDistance(userLocation, shopLocation);
            Double wGs84 = distanceMap.get("WGS84");
            shopDetailVO.setDistance(wGs84);
        }
        return shopDetailVO;
    }
 
    @Override
    public Boolean cheUserByPhone(String phone) {
        R<AppUser> r = appUserClient.getAppUserByPhone1(phone);
        if (R.isError(r)){
            return false;
        }
        return r.getData() != null;
    }
}