Pu Zhibing
2025-04-25 b35b859957e3f8aac2f7d70b7a13f5eb16056840
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.CompanyCityMapper;
import com.stylefeng.guns.modular.system.dao.CompanyMapper;
import com.stylefeng.guns.modular.system.model.City;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.CompanyCity;
import com.stylefeng.guns.modular.system.service.ICityService;
import com.stylefeng.guns.modular.system.service.ICompanyCityService;
import com.stylefeng.guns.modular.system.util.GDMapGeocodingUtil;
import com.stylefeng.guns.modular.system.util.GoogleMap.AddressComponentsVo;
import com.stylefeng.guns.modular.system.util.GoogleMap.GoogleMapUtil;
import com.stylefeng.guns.modular.system.util.GoogleMap.ReverseGeocodeVo;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
 
@Service
public class CompanyCityServiceImpl extends ServiceImpl<CompanyCityMapper, CompanyCity> implements ICompanyCityService {
 
    @Resource
    private CompanyMapper companyMapper;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
    
    @Autowired
    private ICityService cityService;
    
    @Resource
    private RedisUtil redisUtil;
 
 
    /**
     * 根据经纬度获取所属企业
     * @param lon
     * @param lat
     * @return
     * @throws Exception
     */
    @Override
    public Company query(String lon, String lat) throws Exception {
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(String.valueOf(lon), String.valueOf(lat));
        String districtCode = geocode.get("districtCode");
        Company query = this.query(districtCode);
        return query;
    }
    
    @Override
    public Company query1(Integer uid, String lon, String lat) throws Exception {
        String tripId = redisUtil.getValue("trip" + uid);
        ReverseGeocodeVo reverseGeocode = GoogleMapUtil.getReverseGeocode(Double.valueOf(lat), Double.valueOf(lon), tripId);
        if(null == reverseGeocode){
            return null;
        }
        AddressComponentsVo[] addressComponentsVos = reverseGeocode.getAddressComponentsVos();
        String[] citys = new String[addressComponentsVos.length];
        for (int i = 0; i < addressComponentsVos.length; i++) {
            citys[i] = addressComponentsVos[i].getLongName();
        }
        Company query = this.query1(citys);
        return query;
    }
    
    /**
     * 根据行政编号获取所属企业
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public Company query(String code) throws Exception {
        String province = code.substring(0, 2) + "0000";
        String city = code.substring(0, 4) + "00";
        List<Company> query = companyMapper.query(province, city, code);
        if(query.size() == 0){
            query = companyMapper.query(province, city, null);
        }
        if(query.size() == 0){
            query = companyMapper.query(province, null, null);
        }
        for(int i = 3; i > 0; i--){
            for(Company company : query){
                if(company.getType() == i){
                    return company;
                }
            }
        }
        return null;
    }
 
    @Override
    public Company query(String[] city) throws Exception {
        List<City> cities1 = cityService.selectList(null);
        List<Integer> collect = new ArrayList<>();
        for (City city1 : cities1) {
            String chineseName = city1.getChineseName();
            String englishName = city1.getEnglishName();
            String frenchName = city1.getFrenchName();
            for (String s : city) {
                if(s.contains(chineseName)){
                    collect.add(city1.getId());
                    break;
                }
                if(s.contains(englishName)){
                    collect.add(city1.getId());
                    break;
                }
                if(s.contains(frenchName)){
                    collect.add(city1.getId());
                    break;
                }
            }
        }
        if(collect.size() == 0){
            return null;
        }
        List<Company> query = companyMapper.queryList(collect, 3);
        if(query.size() == 0){
            query = companyMapper.queryList(collect, 2);
        }
        if(query.size() == 0){
            query = companyMapper.queryList(collect, 1);
        }
        if(query.size() > 0){
            return query.get(0);
        }
        return null;
    }
    
    
    
    public Company query1(String[] city) throws Exception {
        List<City> cities1 = cityService.selectList(null);
        List<Integer> collect = new ArrayList<>();
        for (City city1 : cities1) {
            String chineseName = city1.getChineseName();
            String englishName = city1.getEnglishName();
            String frenchName = city1.getFrenchName();
            for (String s : city) {
                if(s.contains(chineseName)){
                    collect.add(city1.getId());
                    break;
                }
                if(s.contains(englishName)){
                    collect.add(city1.getId());
                    break;
                }
                if(s.contains(frenchName)){
                    collect.add(city1.getId());
                    break;
                }
            }
        }
        if(collect.size() == 0){
            return null;
        }
        return companyMapper.query1(collect);
    }
}