zhibing.pu
2024-08-01 656ac293601d6da08d25e892a79604fe3edcd086
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
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
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;
 
 
    /**
     * 根据经纬度获取所属企业
     * @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(String lon, String lat) throws Exception {
        ReverseGeocodeVo reverseGeocode = GoogleMapUtil.getReverseGeocode(Double.valueOf(lat), Double.valueOf(lon));
        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<Company> query = companyMapper.queryList(city, 3);
        if(query.size() == 0){
            query = companyMapper.queryList(city, 2);
        }
        if(query.size() == 0){
            query = companyMapper.queryList(city, 1);
        }
        if(query.size() > 0){
            return query.get(0);
        }
        return null;
    }
    
    
    
    public Company query1(String[] city) throws Exception {
        List<City> cities = cityService.selectList(new EntityWrapper<City>().in("chineseName", Arrays.asList(city)).or()
                .in("englishName", Arrays.asList(city)).or().in("frenchName", Arrays.asList(city)));
        List<Integer> collect = cities.stream().map(City::getId).collect(Collectors.toList());
        if(collect.size() == 0){
            return null;
        }
        return companyMapper.query1(collect);
    }
}