From e6c4ff41f4b65dd1d925215f3019164ad31021b4 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 06 十月 2023 15:16:20 +0800 Subject: [PATCH] 添加 总行数 --- src/main/java/com/smartearth/poiexcel/controller/EntController.java | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 213 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/smartearth/poiexcel/controller/EntController.java b/src/main/java/com/smartearth/poiexcel/controller/EntController.java index 6057bab..e5838d6 100644 --- a/src/main/java/com/smartearth/poiexcel/controller/EntController.java +++ b/src/main/java/com/smartearth/poiexcel/controller/EntController.java @@ -1,13 +1,30 @@ package com.smartearth.poiexcel.controller; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.smartearth.poiexcel.entity.EntEntity; import com.smartearth.poiexcel.entity.ResponseMsg; +import com.smartearth.poiexcel.entity.StaticData; +import com.smartearth.poiexcel.mapper.EntMapper; +import com.smartearth.poiexcel.mapper.QiYeMapper; +import com.smartearth.poiexcel.service.EntService; +import com.smartearth.poiexcel.utils.HttpUtils; +import com.smartearth.poiexcel.utils.StringHelper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * 浼佷笟鎺у埗鍣� @@ -18,18 +35,206 @@ @RestController @RequestMapping("/ent") public class EntController extends BaseController { - @ApiOperation(value = "鏌ヨ璁板綍鏁�") - @ApiImplicitParams({ - @ApiImplicitParam(name = "name", value = "鍚嶇О", dataType = "String", paramType = "query", required = false, example = "") - }) - @GetMapping({"/selectCount"}) - public ResponseMsg<Integer> selectCount(String name) { + @Resource + EntService entService; + + @Resource + EntMapper entMapper; + + @Resource + QiYeMapper qiYeMapper; + + @Value("${address.code.url}") + private String addressCodeUrl; + + @ApiOperation(value = "鏌ヨ浠ょ墝") + @GetMapping({"/selectToken"}) + public ResponseMsg<Object> selectToken() { try { + String token = entService.selectToken(); - - return success(0); + return success(StringHelper.isEmpty(token) ? 0 : 1, token); } catch (Exception ex) { return fail(ex, -1); } } + + @ApiOperation(value = "鏌ヨ浼佷笟") + @ApiImplicitParams({ + @ApiImplicitParam(name = "token", value = "浠ょ墝", dataType = "String", paramType = "query", example = ""), + @ApiImplicitParam(name = "startDate", value = "寮�濮嬫棩鏈�", dataType = "String", paramType = "query", example = "2023-06-29"), + @ApiImplicitParam(name = "endDate", value = "缁撴潫鏃ユ湡", dataType = "String", paramType = "query", example = "2023-06-30"), + @ApiImplicitParam(name = "qylabel", value = "鏍囩", dataType = "Integer", paramType = "query", example = "寮�涓�"), + @ApiImplicitParam(name = "showCount", value = "鏄剧ず璁板綍鏁�", dataType = "Integer", paramType = "query", example = "10"), + @ApiImplicitParam(name = "currentPage", value = "褰撳墠鍒嗛〉鏁�", dataType = "Integer", paramType = "query", example = "1") + }) + @GetMapping({"/selectEnts"}) + public ResponseMsg<Object> selectEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) { + try { + if (StringHelper.isEmpty(token)) { + token = entService.selectToken(); + } + if (StringHelper.isEmpty(token)) { + return fail("鏌ヨ浠ょ墝澶辫触"); + } + if (null == showCount || showCount < 1) { + showCount = 10; + } + if (null == currentPage || currentPage < 1) { + currentPage = 1; + } + + List<EntEntity> list = entService.selectEnts(token, startDate, endDate, qylabel, showCount, currentPage); + + return success(null == list || list.isEmpty() ? 0 : list.size(), list); + } catch (Exception ex) { + return fail(ex, -1); + } + } + + @ApiOperation(value = "鎻掑叆浼佷笟") + @ApiImplicitParams({ + @ApiImplicitParam(name = "token", value = "浠ょ墝", dataType = "String", paramType = "query", example = ""), + @ApiImplicitParam(name = "startDate", value = "寮�濮嬫棩鏈�", dataType = "String", paramType = "query", example = "2023-06-29"), + @ApiImplicitParam(name = "endDate", value = "缁撴潫鏃ユ湡", dataType = "String", paramType = "query", example = "2023-06-30"), + @ApiImplicitParam(name = "qylabel", value = "鏍囩", dataType = "Integer", paramType = "query", example = "寮�涓�"), + @ApiImplicitParam(name = "showCount", value = "鏄剧ず璁板綍鏁�", dataType = "Integer", paramType = "query", example = "10"), + @ApiImplicitParam(name = "currentPage", value = "褰撳墠鍒嗛〉鏁�", dataType = "Integer", paramType = "query", example = "1") + }) + @GetMapping({"/insertEnts"}) + public ResponseMsg<Object> insertEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) { + try { + if (StringHelper.isEmpty(token)) { + token = entService.selectToken(); + } + if (StringHelper.isEmpty(token)) { + return fail("鏌ヨ浠ょ墝澶辫触"); + } + if (null == showCount || showCount < 1) { + showCount = 10; + } + if (null == currentPage || currentPage < 1) { + currentPage = 1; + } + + List<EntEntity> list = entService.selectEnts(token, startDate, endDate, qylabel, showCount, currentPage); + if (null == list || list.isEmpty()) { + return fail("鏌ヨ浼佷笟涓虹┖"); + } + + Integer rows = entService.insertEnts(list); + + return success(rows); + } catch (Exception ex) { + return fail(ex, -1); + } + } + + @ApiOperation(value = "鍒嗛〉鏌ヨ") + @ApiImplicitParams({ + @ApiImplicitParam(name = "pageSize", value = "姣忛〉鏉℃暟", dataType = "Integer", paramType = "query", example = "10"), + @ApiImplicitParam(name = "pageIndex", value = "鍒嗛〉鏁帮紙浠�1寮�濮嬶級", dataType = "Integer", paramType = "query", example = "1") + }) + @GetMapping(value = "/selectByPage") + public ResponseMsg<Object> selectByPage(Integer pageSize, Integer pageIndex) { + try { + if (null == pageSize || pageSize < 1) { + pageSize = 10; + } + if (null == pageIndex || pageIndex < 1) { + pageIndex = 1; + } + int count = qiYeMapper.selectCount(); + List<EntEntity> list = qiYeMapper.selectByPage(pageSize, StaticData.I100 * (pageIndex - 1)); + + return success(count, list); + } catch (Exception ex) { + return fail(ex, -1); + } + } + + @ApiOperation(value = "鎵归噺鏇存柊浼佷笟鍧愭爣") + @GetMapping({"/updateCoords"}) + public ResponseMsg<Object> updateCoords() { + try { + int rows = 0; + int count = qiYeMapper.selectCount(); + if (0 == count) { + return success("娌℃湁鏁版嵁闇�瑕佹洿鏂�", count); + } + + int pages = (count - 1) / StaticData.I100 + 1; + for (int i = 1; i <= pages; i++) { + List<EntEntity> list = qiYeMapper.selectByPage(StaticData.I100, StaticData.I100 * (i - 1)); + if (null == list || list.isEmpty()) { + continue; + } + + for (EntEntity ent : list) { + if (!StringHelper.isEmpty(ent.getAddress())) { + setEntCoord(ent); + } + } + + rows += qiYeMapper.updates(list); + } + + return success(rows); + } catch (Exception ex) { + return fail(ex, -1); + } + } + + /** + * 璁剧疆浼佷笟鍧愭爣鍊� + */ + private void setEntCoord(EntEntity ent) { + try { + String json = selectAddrCode(ent.getAddress()); + if (StringHelper.isEmpty(json)) { + return; + } + + JSONObject obj = JSONObject.parseObject(json); + if (null == obj || !StaticData.S1.equals(obj.get("status"))) { + return; + } + + JSONArray jsonArray = obj.getJSONArray("geocodes"); + if (null == jsonArray || jsonArray.isEmpty()) { + return; + } + + JSONObject object = jsonArray.getJSONObject(0); + String addr = object.getString("formatted_address"); + if (StringHelper.isEmpty(addr)) { + return; + } + + String encoding = StringHelper.getEncoding(addr); + if (encoding != null) { + addr = new String(addr.getBytes(encoding)); + } + // ent.setAddress(addr) + + String location = object.getString("location"); + String[] split = location.split(","); + ent.setX(Double.parseDouble(split[1])); + ent.setY(Double.parseDouble(split[0])); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + private String selectAddrCode(String address) throws Exception { + StringBuilder sb = new StringBuilder(addressCodeUrl); + sb.append("?address=").append(address).append("&output=json&batch=true&coord=cgcs2000&adcode=yes"); + + // 璁剧疆璇锋眰澶� + Map<String, String> headers = new HashMap<>(); + headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes())); + + // 鎵ц璇锋眰 + return HttpUtils.get(sb.toString(), null, headers); + } } -- Gitblit v1.9.3