北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-05 19a45e1c33a0925bc8237bd79d74902d20a1e16e
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
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.smartearth.poiexcel.service;
 
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.smartearth.poiexcel.entity.*;
import com.smartearth.poiexcel.mapper.BasicMapper;
import com.smartearth.poiexcel.mapper.EntMapper;
import com.smartearth.poiexcel.utils.RestHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 企业服务类
 * @author WWW
 * @date 2023-10-05
 */
@Service
public class EntService {
    @Resource
    EntMapper entMapper;
 
    @Value("${qylweb.host}")
    String host;
 
    @Value("${qylweb.user}")
    String user;
 
    @Value("${qylweb.pwd}")
    String pwd;
 
    private final static Log log = LogFactory.getLog(EntService.class);
 
    private final static String generateToken = "%s/yqfwg/app/generateToken";
 
    private final static String getEntBaseInfo = "%s/yqfwg/api/project/getEntBaseInfoForOtherSysListPage?ctoken=%s&showCount=%d&currentPage=%d";
 
    /**
     * 获取令牌
     */
    public String selectToken() {
        try {
            String url = String.format(generateToken, host);
 
            Map<String, Object> map = new HashMap<>(2);
            map.put("userName", user);
            map.put("passWord", pwd);
 
            // String str = RestHelper.postForRest(url, map)
            Result rs = postForRest(url, map, Result.class);
            if (null == rs || StaticData.I200 != rs.getCode()) {
                return null;
            }
 
            // TokenResult tr = JSONObject.parseObject(rs.getData().toJSONString(), TokenResult.class)
            TokenResult tr = rs.getData().toJavaObject(TokenResult.class);
 
            return tr.getCtoken();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 查询企业
     */
    public List<EntEntity> selectEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) {
        try {
            String url = String.format(getEntBaseInfo, host, token, showCount, currentPage);
            if (null != startDate) {
                url += "&buildDate_startdate=" + startDate;
            }
            if (null != endDate) {
                url += "&buildDate_enddate=" + endDate;
            }
            if (null != qylabel) {
                url += "&qylabel=" + qylabel;
            }
 
            Result rs = getForRest(url, Result.class);
            if (null == rs || StaticData.I200 != rs.getCode()) {
                return null;
            }
 
            EntResult er = JSONObject.parseObject(rs.getData().toJSONString(), EntResult.class);
 
            return er.getPd().getList();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 插入企业
     */
    public Integer insertEnts(List<EntEntity> list) {
        try {
            int rows = 0;
            List<List<EntEntity>> subLists = Lists.partition(list, StaticData.I200);
            for (List<EntEntity> sub : subLists) {
                try {
                    rows += entMapper.insertBatch(sub);
                } catch (Exception ex) {
                    log.error(ex);
                }
            }
 
            return rows;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return 0;
        }
    }
 
    /**
     * get请求(Rest)
     */
    public <T> T getForRest(String url, Class<T> clazz) {
        RestTemplate rest = RestHelper.getRestTemplate();
 
        return rest.getForObject(url, clazz);
    }
 
    /**
     * post请求(Rest)
     */
    public <T> T postForRest(String url, Map<String, Object> map, Class<T> clazz) {
        RestTemplate rest = RestHelper.getRestTemplate();
 
        return rest.postForObject(url, map, clazz);
    }
 
    /**
     * delete请求(Rest)
     */
    public <T> T deleteForRest(String url, Map<String, T> map, Class<T> clazz) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        HttpEntity<?> entity = new HttpEntity<>(map, headers);
 
        RestTemplate rest = RestHelper.getRestTemplate();
        ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz);
 
        return rs.getBody();
    }
 
    /**
     * 获取Map数据
     */
    public <T> Map<String, Object> getMapData(T t) {
        Map<String, Object> map = new HashMap<>(1);
 
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
 
                field.setAccessible(true);
                Object obj = field.get(t);
 
                map.put(field.getName(), obj);
            } catch (Exception ex) {
                //
            }
        }
 
        return map;
    }
}