package com.malk.qiwang.Controller;
import com.malk.qiwang.Service.ICompanyTitleService;
import com.malk.qiwang.entity.CompanyTitle;
import com.malk.qiwang.mapper.CompanyTitleMapper;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Workflow;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
/**
*
* 公司抬头库表 前端控制器
*
*
* @author LQY
* @since 2026-04-24
*/
@RestController
@RequestMapping("/qw1")
@Slf4j
public class CompanyTitleController {
@Autowired
private ICompanyTitleService companyTitleService;
@Autowired
private YDClient ydClient;
@Autowired
private DDClient_Workflow ddClientWorkflow ;
@Autowired
private DDClient ddClient;
@Autowired
private CompanyTitleMapper companyTitleMapper;
@PostMapping("/companyTitle")
public McR insertTransfer2(@RequestBody(required = false) Map map) {
if (map == null) {
map = new HashMap<>();
log.info("请求体为空,使用默认参数");
}
log.info("接收到的参数: {}", map);
// 获取参数
String processCode = "PROC-187CB1F8-5BF0-403A-9D01-94EEF987DD77";
// 设置时间范围(默认查询最近30天)
Long startTime = System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000;
Long endTime = System.currentTimeMillis();
// 获取钉钉accessToken
String accessToken = ddClient.getAccessToken();
// 递归获取所有审批实例ID
List allInstanceIds = ddClientWorkflow.getInstanceIds_all(accessToken, processCode, startTime, endTime, null);
log.info("共查询到 {} 条审批实例", allInstanceIds.size());
// 批量处理审批实例
int successCount = 0;
int failCount = 0;
// 用于记录处理的税号,避免重复处理同一个税号(可选,根据业务需求)
Set processedTaxIds = new HashSet<>();
for (String instanceId : allInstanceIds) {
try {
// 获取审批实例详情
Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, instanceId);
if (processInstance != null) {
// 提取并保存公司抬头数据
boolean result = saveCompanyTitleFromInstance(processInstance, processedTaxIds);
if (result) {
successCount++;
} else {
failCount++;
}
} else {
failCount++;
}
} catch (Exception e) {
log.error("处理审批实例失败: instanceId={}", instanceId, e);
failCount++;
}
}
log.info("处理完成: 成功={}, 失败={}", successCount, failCount);
return McR.success();
}
private boolean saveCompanyTitleFromInstance(Map processInstance, Set processedTaxIds) {
List