|
|
@@ -0,0 +1,177 @@
|
|
|
+package com.tzld.supply.util.feishu;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.supply.util.MapBuilder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.util.Pair;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class FeishuExcelUtil {
|
|
|
+
|
|
|
+ public static void feishuSheetDelete(String sheetToken,
|
|
|
+ String sheetId,
|
|
|
+ int rowNum,
|
|
|
+ int startRowIndex,
|
|
|
+ HttpHeaders httpHeaders,
|
|
|
+ RestTemplate restTemplate,
|
|
|
+ List<String> dateStrList) {
|
|
|
+ HttpEntity<Object> queryEntity = new HttpEntity<>(httpHeaders);
|
|
|
+ int deleteRowNum = rowNum < 20 ? startRowIndex + rowNum : startRowIndex + (rowNum * 2);
|
|
|
+ ResponseEntity<String> queryResponseEntity = restTemplate.exchange(
|
|
|
+ String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/values/%s!A"
|
|
|
+ + startRowIndex + ":A" + deleteRowNum, sheetToken, sheetId),
|
|
|
+ HttpMethod.GET, queryEntity, String.class);
|
|
|
+ JSONArray values = JSONObject.parseObject(queryResponseEntity.getBody())
|
|
|
+ .getJSONObject("data")
|
|
|
+ .getJSONObject("valueRange")
|
|
|
+ .getJSONArray("values");
|
|
|
+ int count = 0;
|
|
|
+ if (!values.isEmpty() && Objects.nonNull(values.get(0)) && !((JSONArray) values.get(0)).isEmpty()
|
|
|
+ && Objects.nonNull(((JSONArray) values.get(0)).get(0))
|
|
|
+ && dateStrList.contains(((JSONArray) values.get(0)).get(0).toString())) {
|
|
|
+ for (Object value : values) {
|
|
|
+ if (((JSONArray) value).get(0) != null) {
|
|
|
+ List<String> dates = ((JSONArray) value).stream().map(Object::toString).collect(Collectors.toList());
|
|
|
+ if (dateStrList.contains(dates.get(0))) {
|
|
|
+ count++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (count > 0) {
|
|
|
+ int delNum = 0;
|
|
|
+ do {
|
|
|
+ // 删除当前日期已存在的旧数据
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<Object> deleteEntity = new HttpEntity<>(
|
|
|
+ String.format("{\n" +
|
|
|
+ " \"dimension\": {\n" +
|
|
|
+ " \"sheetId\": \"%s\",\n" +
|
|
|
+ " \"majorDimension\": \"ROWS\",\n" +
|
|
|
+ " \"startIndex\": %s,\n" +
|
|
|
+ " \"endIndex\": %s\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}", sheetId, startRowIndex, Math.min(startRowIndex + 4000, count - delNum + startRowIndex) - 1),
|
|
|
+ httpHeaders);
|
|
|
+ restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/dimension_range", sheetToken),
|
|
|
+ HttpMethod.DELETE, deleteEntity, String.class);
|
|
|
+ delNum = Math.min(delNum + 4000, count);
|
|
|
+ } while (delNum < count);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void feishuSheetInsert(String sheetToken,
|
|
|
+ String sheetId,
|
|
|
+ Integer startRowIndex,
|
|
|
+ HttpHeaders httpHeaders,
|
|
|
+ RestTemplate restTemplate,
|
|
|
+ List<List<List<Object>>> partitions) {
|
|
|
+ int startRow = startRowIndex;
|
|
|
+ for (List<List<Object>> partition : partitions) {
|
|
|
+ // 插入数据
|
|
|
+ HttpEntity<Object> postEntity = new HttpEntity<>(MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("valueRange", MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("range", String.format("%s!A" + startRow + ":CI", sheetId) + (partition.size() + startRow - 1))
|
|
|
+ .put("values", partition)
|
|
|
+ .build())
|
|
|
+ .build(), httpHeaders);
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/values_prepend",
|
|
|
+ sheetToken),
|
|
|
+ HttpMethod.POST, postEntity, String.class);
|
|
|
+ JSONObject responseJSON = JSONObject.parseObject(response.getBody());
|
|
|
+ if (0 != responseJSON.getInteger("code")) {
|
|
|
+ log.error("doSendFeishuSheet write error :{}", responseJSON.getString("msg"));
|
|
|
+ }
|
|
|
+ startRow += partition.size();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void feishuSheetStyle(String sheetToken,
|
|
|
+ String sheetId,
|
|
|
+ int rowNum,
|
|
|
+ Integer startRowIndex,
|
|
|
+ HttpHeaders httpHeaders,
|
|
|
+ RestTemplate restTemplate,
|
|
|
+ List<Pair<String, String>> styles) {
|
|
|
+ Integer startRow = startRowIndex;
|
|
|
+ do {
|
|
|
+ for (Pair<String, String> style : styles) {
|
|
|
+ HttpEntity<Map<Object, Object>> styleEntity = new HttpEntity<>(MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("appendStyle",
|
|
|
+ MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("range", String.format("%s!%s" + startRow + ":%s", sheetId,
|
|
|
+ style.getFirst(), style.getFirst())
|
|
|
+ + (Math.min(startRow + 4000, rowNum + startRowIndex) - 1))
|
|
|
+ .put("style",
|
|
|
+ MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("formatter", style.getSecond())
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ .build(), httpHeaders);
|
|
|
+ restTemplate.exchange(
|
|
|
+ String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/style",
|
|
|
+ sheetToken),
|
|
|
+ HttpMethod.PUT,
|
|
|
+ styleEntity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ }
|
|
|
+ startRow += 4000;
|
|
|
+ } while (startRow < rowNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void feishuSheetThanks(String sheetToken,
|
|
|
+ String sheetId,
|
|
|
+ int rowNum,
|
|
|
+ Integer startRowIndex,
|
|
|
+ HttpHeaders httpHeaders,
|
|
|
+ RestTemplate restTemplate,
|
|
|
+ List<Pair<String, List<Pair<String, String>>>> thanks) {
|
|
|
+ Integer startRow = startRowIndex;
|
|
|
+ do {
|
|
|
+ for (Pair<String, List<Pair<String, String>>> thank : thanks) {
|
|
|
+ List<String> keyList = thank.getSecond().stream().map(Pair::getFirst).collect(Collectors.toList());
|
|
|
+ List<String> colorList = thank.getSecond().stream().map(Pair::getSecond).collect(Collectors.toList());
|
|
|
+ HttpEntity<Map<Object, Object>> styleEntity = new HttpEntity<>(MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("range", String.format("%s!%s" + startRow + ":%s", sheetId,
|
|
|
+ thank.getFirst(), thank.getFirst())
|
|
|
+ + (Math.min(startRow + 4000, rowNum + startRowIndex) - 1))
|
|
|
+ .put("dataValidationType", "list")
|
|
|
+ .put("dataValidation", MapBuilder.builder()
|
|
|
+ .put("conditionValues", keyList)
|
|
|
+ .put("options", MapBuilder.builder()
|
|
|
+ .put("highlightValidData", true)
|
|
|
+ .put("colors", colorList)
|
|
|
+ .build()
|
|
|
+ ).build())
|
|
|
+ .build(), httpHeaders);
|
|
|
+ restTemplate.exchange(
|
|
|
+ String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/dataValidation",
|
|
|
+ sheetToken),
|
|
|
+ HttpMethod.POST,
|
|
|
+ styleEntity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ }
|
|
|
+ startRow += 4000;
|
|
|
+ } while (startRow < rowNum);
|
|
|
+ }
|
|
|
+}
|