畅捷通同步报表数据

畅捷通报表
YXY 1 year ago
parent 012246dbf0
commit a5c9796320

@ -182,6 +182,31 @@ public class LarkTableHelper extends LarkHelper{
} }
} }
public BatchCreateAppTableRecordRespBody addTableRecordBatch(LarkTableRequest request) {
BatchCreateAppTableRecordResp batchCreateAppTableRecordResp = null;
try {
batchCreateAppTableRecordResp = buildClient(request).bitable().appTableRecord()
.batchCreate(
BatchCreateAppTableRecordReq.newBuilder()
.appToken(request.getAppToken())
.tableId(request.getAppTable())
.batchCreateAppTableRecordReqBody(
BatchCreateAppTableRecordReqBody.newBuilder()
.records(request.getAppTableRecords())
.build()
)
.build()
);
} catch (Exception e) {
throw new RuntimeException(new LarkException("LarkTableHelper.addTableRecord",e.getMessage(),request).getErrorMessageBody());
}
if (batchCreateAppTableRecordResp != null && batchCreateAppTableRecordResp.getCode() == 0){
return batchCreateAppTableRecordResp.getData();
}else {
throw new RuntimeException(new LarkException("LarkTableHelper.addTableRecordBatch",batchCreateAppTableRecordResp.getMsg(),request).getErrorMessageBody());
}
}
public DeleteAppTableRecordRespBody deleteTableRecord(LarkTableRequest request) { public DeleteAppTableRecordRespBody deleteTableRecord(LarkTableRequest request) {
DeleteAppTableRecordResp delete = null; DeleteAppTableRecordResp delete = null;
try { try {

@ -0,0 +1,34 @@
package com.ruoyi.flyingbook.domain;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
/**
* erp_lark_temp_relation
*
* @author ruoyi
* @date 2023-09-01
*/
@Data
public class ErpLarkTempRelation extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** $column.columnComment */
private Long id;
/** 唯一键,判断新增删除的依据 */
private String key;
/** 飞书行id */
private String larkKey;
/** 数据来源 */
private String method;
/**
* @see com.ruoyi.common.enums.FlagStatus
*/
private Long flag;
}

@ -1,6 +1,7 @@
package com.ruoyi.flyingbook.domain.lark; package com.ruoyi.flyingbook.domain.lark;
import com.lark.oapi.service.bitable.v1.model.AppTableCreateHeader; import com.lark.oapi.service.bitable.v1.model.AppTableCreateHeader;
import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -23,7 +24,7 @@ public class LarkTableRequest extends LarkRequest{
private String defaultViewName; private String defaultViewName;
private Map<String,Object> body; private Map<String,Object> body;
private AppTableCreateHeader[] fields; private AppTableCreateHeader[] fields;
private AppTableRecord[] appTableRecords;
public LarkTableRequest(String appId,String appSecret,String tableName) { public LarkTableRequest(String appId,String appSecret,String tableName) {
super(appId,appSecret); super(appId,appSecret);
this.tableName = tableName; this.tableName = tableName;

@ -46,6 +46,7 @@ public interface ErpLarkRelationMapper
*/ */
public int updateErpLarkRelation(ErpLarkRelation erpLarkRelation); public int updateErpLarkRelation(ErpLarkRelation erpLarkRelation);
/** /**
* *
* *

@ -0,0 +1,71 @@
package com.ruoyi.flyingbook.mapper;
import com.ruoyi.flyingbook.domain.ErpLarkTempRelation;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Mapper
*
* @author ruoyi
* @date 2023-09-01
*/
public interface ErpLarkTempRelationMapper
{
/**
*
*
* @param id ID
* @return
*/
public ErpLarkTempRelation selectErpLarkTempRelationById(Long id);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public List<ErpLarkTempRelation> selectErpLarkTempRelationList(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public int insertErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public int updateErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param id ID
* @return
*/
public int deleteErpLarkTempRelationById(Long id);
/**
*
*
* @param ids ID
* @return
*/
public int deleteErpLarkTempRelationByIds(Long[] ids);
public int updateByMethod(@Param("method") String method, @Param("flag") Long flag);
public int deleteByMethod(@Param("method") String method,@Param("flag") Long flag);
public int batchInsert(@Param("erpLarkTempRelationList") List<ErpLarkTempRelation> erpLarkTempRelationList);
}

@ -0,0 +1,63 @@
package com.ruoyi.flyingbook.service;
import com.ruoyi.flyingbook.domain.ErpLarkTempRelation;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-09-01
*/
public interface IErpLarkTempRelationService
{
/**
*
*
* @param id ID
* @return
*/
public ErpLarkTempRelation selectErpLarkTempRelationById(Long id);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public List<ErpLarkTempRelation> selectErpLarkTempRelationList(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public int insertErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param erpLarkTempRelation
* @return
*/
public int updateErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation);
/**
*
*
* @param ids ID
* @return
*/
public int deleteErpLarkTempRelationByIds(Long[] ids);
/**
*
*
* @param id ID
* @return
*/
public int deleteErpLarkTempRelationById(Long id);
}

@ -0,0 +1,97 @@
package com.ruoyi.flyingbook.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.flyingbook.domain.ErpLarkTempRelation;
import com.ruoyi.flyingbook.mapper.ErpLarkTempRelationMapper;
import com.ruoyi.flyingbook.service.IErpLarkTempRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-09-01
*/
@Service
public class ErpLarkTempRelationServiceImpl implements IErpLarkTempRelationService
{
@Autowired
private ErpLarkTempRelationMapper erpLarkTempRelationMapper;
/**
*
*
* @param id ID
* @return
*/
@Override
public ErpLarkTempRelation selectErpLarkTempRelationById(Long id)
{
return erpLarkTempRelationMapper.selectErpLarkTempRelationById(id);
}
/**
*
*
* @param erpLarkTempRelation
* @return
*/
@Override
public List<ErpLarkTempRelation> selectErpLarkTempRelationList(ErpLarkTempRelation erpLarkTempRelation)
{
return erpLarkTempRelationMapper.selectErpLarkTempRelationList(erpLarkTempRelation);
}
/**
*
*
* @param erpLarkTempRelation
* @return
*/
@Override
public int insertErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation)
{
erpLarkTempRelation.setCreateTime(DateUtils.getNowDate());
return erpLarkTempRelationMapper.insertErpLarkTempRelation(erpLarkTempRelation);
}
/**
*
*
* @param erpLarkTempRelation
* @return
*/
@Override
public int updateErpLarkTempRelation(ErpLarkTempRelation erpLarkTempRelation)
{
erpLarkTempRelation.setUpdateTime(DateUtils.getNowDate());
return erpLarkTempRelationMapper.updateErpLarkTempRelation(erpLarkTempRelation);
}
/**
*
*
* @param ids ID
* @return
*/
@Override
public int deleteErpLarkTempRelationByIds(Long[] ids)
{
return erpLarkTempRelationMapper.deleteErpLarkTempRelationByIds(ids);
}
/**
*
*
* @param id ID
* @return
*/
@Override
public int deleteErpLarkTempRelationById(Long id)
{
return erpLarkTempRelationMapper.deleteErpLarkTempRelationById(id);
}
}

@ -136,6 +136,8 @@
where id = #{id} where id = #{id}
</update> </update>
<delete id="deleteErpLarkRelationById" parameterType="Long"> <delete id="deleteErpLarkRelationById" parameterType="Long">
delete delete
from erp_lark_relation from erp_lark_relation

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.flyingbook.mapper.ErpLarkTempRelationMapper">
<resultMap type="com.ruoyi.flyingbook.domain.ErpLarkTempRelation" id="ErpLarkTempRelationResult">
<result property="id" column="id" />
<result property="key" column="key" />
<result property="larkKey" column="lark_key" />
<result property="method" column="method" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="flag" column="flag" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectErpLarkTempRelationVo">
select id, key, lark_key, method, create_by, create_time, update_by, update_time, flag, remark from erp_lark_temp_relation
</sql>
<select id="selectErpLarkTempRelationList" parameterType="com.ruoyi.flyingbook.domain.ErpLarkTempRelation" resultMap="ErpLarkTempRelationResult">
<include refid="selectErpLarkTempRelationVo"/>
<where>
<if test="key != null and key != ''"> and key = #{key}</if>
<if test="larkKey != null and larkKey != ''"> and lark_key = #{larkKey}</if>
<if test="method != null and method != ''"> and method = #{method}</if>
<if test="flag != null "> and flag = #{flag}</if>
</where>
</select>
<select id="selectErpLarkTempRelationById" parameterType="Long" resultMap="ErpLarkTempRelationResult">
<include refid="selectErpLarkTempRelationVo"/>
where id = #{id}
</select>
<insert id="insertErpLarkTempRelation" parameterType="com.ruoyi.flyingbook.domain.ErpLarkTempRelation" useGeneratedKeys="true" keyProperty="id">
insert into erp_lark_temp_relation
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="key != null">key,</if>
<if test="larkKey != null">lark_key,</if>
<if test="method != null">method,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="flag != null">flag,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="key != null">#{key},</if>
<if test="larkKey != null">#{larkKey},</if>
<if test="method != null">#{method},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="flag != null">#{flag},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateErpLarkTempRelation" parameterType="com.ruoyi.flyingbook.domain.ErpLarkTempRelation">
update erp_lark_temp_relation
<trim prefix="SET" suffixOverrides=",">
<if test="key != null">key = #{key},</if>
<if test="larkKey != null">lark_key = #{larkKey},</if>
<if test="method != null">method = #{method},</if>
<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="flag != null">flag = #{flag},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteErpLarkTempRelationById" parameterType="Long">
delete from erp_lark_temp_relation where id = #{id}
</delete>
<delete id="deleteErpLarkTempRelationByIds" parameterType="String">
delete from erp_lark_temp_relation where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<update id="updateByMethod" parameterType="com.ruoyi.flyingbook.domain.ErpLarkRelation">
update erp_lark_relation
set flag = #{flag}
where method = #{method}
</update>
<update id="deleteByMethod" parameterType="com.ruoyi.flyingbook.domain.ErpLarkRelation">
delete from erp_lark_relation
where method = #{method} and flag = #{flag}
</update>
<insert id="batchInsert" parameterType="com.ruoyi.flyingbook.domain.ErpLarkTempRelation" useGeneratedKeys="true" keyProperty="id">
insert into erp_lark_temp_relation(lark_key,`method`,create_by,create_time,flag)
values <foreach collection="erpLarkTempRelationList" item="item" separator=",">
(
#{item.larkKey},
#{item.method},
#{item.createBy},
#{item.createTime},
#{item.flag}
)
</foreach>
</insert>
</mapper>

@ -58,9 +58,9 @@ public class MailInfoController extends BaseController {
public void scheduledReminders(Integer number,String cjt) { public void scheduledReminders(Integer number,String cjt) {
// odoo(number); // odoo(number);
// if (number > 10){ // if (number > 10){
// cjtCreateLarkTableJob.execute(); cjtCreateLarkTableJob.execute();
// }else { // }else {
cjt(number,cjt); // cjt(number,cjt);
// } // }
} }

@ -1,33 +1,32 @@
package com.ruoyi.quartz.task.CJT; package com.ruoyi.quartz.task.CJT;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody; import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
import com.lark.oapi.service.bitable.v1.model.BatchCreateAppTableRecordRespBody;
import com.ruoyi.common.constant.RedisConstants; import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.*; import com.ruoyi.common.enums.AppType;
import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.enums.CjtAccountEnum;
import com.ruoyi.common.enums.FlagStatus;
import com.ruoyi.common.enums.TableRelationTypeEnum;
import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper; import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper; import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
import com.ruoyi.flyingbook.domain.ErpLarkRelation; import com.ruoyi.flyingbook.domain.ErpLarkTempRelation;
import com.ruoyi.flyingbook.domain.LarkCompanyRelation; import com.ruoyi.flyingbook.domain.LarkCompanyRelation;
import com.ruoyi.flyingbook.domain.LarkTableRelation; import com.ruoyi.flyingbook.domain.LarkTableRelation;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest; import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mapper.ErpLarkRelationMapper; import com.ruoyi.flyingbook.mapper.ErpLarkTempRelationMapper;
import com.ruoyi.flyingbook.mapper.LarkCompanyRelationMapper; import com.ruoyi.flyingbook.mapper.LarkCompanyRelationMapper;
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper; import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
import com.ruoyi.quartz.domain.*; import com.ruoyi.quartz.domain.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.ruoyi.quartz.helper.OkHttpHelper.post; import static com.ruoyi.quartz.helper.OkHttpHelper.post;
@ -38,22 +37,20 @@ import static com.ruoyi.quartz.helper.OkHttpHelper.post;
* @create 2023-07-17 20:29 * @create 2023-07-17 20:29
*/ */
@Slf4j @Slf4j
public abstract class SyncReportJob { public class SyncReportJob {
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@Autowired @Autowired
protected ErpLarkRelationMapper erpLarkRelationMapper;
@Autowired
private LarkCompanyRelationMapper larkCompanyRelationMapper; private LarkCompanyRelationMapper larkCompanyRelationMapper;
@Autowired @Autowired
private LarkTableRelationMapper larkTableRelationMapper; private LarkTableRelationMapper larkTableRelationMapper;
@Autowired @Autowired
private ErpLarkTempRelationMapper erpLarkTempRelationMapper;
@Autowired
private LarkTableHelper larkTableHelper; private LarkTableHelper larkTableHelper;
@Autowired @Autowired
private LarkRobotHelper larkRobotHelper; private LarkRobotHelper larkRobotHelper;
@Autowired
private CJTCreateLarkTableJob cjtCreateLarkTableJob;
@Value("${lark.robot.group}") @Value("${lark.robot.group}")
private String ROBOT_GROUP; private String ROBOT_GROUP;
@Value("${sync.flag}") @Value("${sync.flag}")
@ -95,8 +92,10 @@ public abstract class SyncReportJob {
initLarkInfo(context); initLarkInfo(context);
//重置ticket //重置ticket
resetTicket(context); resetTicket(context);
preSync(context);
//执行分页同步 //执行分页同步
sync(context); sync(context);
finish(context);
} catch (Exception e) { } catch (Exception e) {
log.error("{} 执行失败", getClassName(), e); log.error("{} 执行失败", getClassName(), e);
larkRobotHelper.sendMessageByBot(ROBOT_GROUP, buildRobotErrorCountMessage(e)); larkRobotHelper.sendMessageByBot(ROBOT_GROUP, buildRobotErrorCountMessage(e));
@ -113,7 +112,7 @@ public abstract class SyncReportJob {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("同步报表任务", getClassName()); jsonObject.put("同步报表任务", getClassName());
String errorMessage = e.getMessage(); String errorMessage = e.getMessage();
if (org.apache.commons.lang3.StringUtils.isNotBlank(errorMessage)) { if (StringUtils.isNotBlank(errorMessage)) {
errorMessage = errorMessage.replaceAll("\\\\", ""); errorMessage = errorMessage.replaceAll("\\\\", "");
} }
jsonObject.put("异常信息", errorMessage); jsonObject.put("异常信息", errorMessage);
@ -155,13 +154,10 @@ public abstract class SyncReportJob {
*/ */
protected String getQueryFields() { protected String getQueryFields() {
return "Warehouse,InventoryCode,Inventory,BaseQuantity,canuseBaseQuantity"; return "Warehouse,InventoryCode,Inventory,BaseQuantity,canuseBaseQuantity";
}; }
;
/**
*
*/
protected abstract String getQueryKey();
/** /**
* *
@ -170,68 +166,94 @@ public abstract class SyncReportJob {
return this.getClass().getSimpleName(); return this.getClass().getSimpleName();
} }
;
/** /**
* *
* *
* @param context * @param context
*/ */
protected void sync(CJTJobContext context) { protected void sync(CJTJobContext context) {
String errorMessage = null; List<String> errorMessageList = new ArrayList<>();
JSONObject cjtRequestReport = new JSONObject(); JSONObject cjtRequestReport = new JSONObject();
CJTRequestReportBody cjtRequestBody = new CJTRequestReportBody(0, PAGE_SIZE, getReportName(), getQueryFields()); CJTRequestReportBody cjtRequestBody = new CJTRequestReportBody(0, PAGE_SIZE, getReportName(), getQueryFields());
cjtRequestReport.put("request", cjtRequestBody); cjtRequestReport.put("request", cjtRequestBody);
CJTRequest cjtRequest = buildCJTRequest(context, cjtRequestReport); CJTRequest cjtRequest = buildCJTRequest(context, cjtRequestReport);
List<Integer> pageList = new ArrayList<>();
LarkCompanyRelation companyRelation = context.getCompanyRelation(); LarkCompanyRelation companyRelation = context.getCompanyRelation();
LarkTableRelation tableRelation = context.getTableRelation(); LarkTableRelation tableRelation = context.getTableRelation();
LarkTableRequest addRecordRequest = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken(), tableRelation.getToTableId()); LarkTableRequest addRecordRequest = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken(), tableRelation.getToTableId());
List<CJTResponseReportBodyDetailRow> rows = new ArrayList<>(); Boolean flag = Boolean.TRUE;
do { do {
try { try {
cjtRequest.setOpenToken(generateToken(context)); flag = executeDetail(context, cjtRequest, addRecordRequest);
//请求接口并序列化数据
CJTResponseReportBodyDetail bodyDetail = request(cjtRequest);
//实际返回数据
rows = bodyDetail.getRows();
if (!CollectionUtils.isEmpty(rows)) {
//批量同步飞书
List<String> errorCodeList = syncLarkBatch(rows, addRecordRequest, context.getCjt());
if (!CollectionUtils.isEmpty(errorCodeList)) {
String errorKey = String.join(",", errorCodeList);
throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey));
}
}
} catch (Exception e) { } catch (Exception e) {
log.error("{} exception", this.getClassName(), e); log.error("{} exception", this.getClassName(), e);
errorMessage = buildErrorBody(cjtRequest, e.getMessage()); pageList.add(cjtRequestBody.getPageIndex());
} finally { } finally {
cjtRequestBody.addPage(); cjtRequestBody.addPage();
} }
} while (!CollectionUtils.isEmpty(rows)); } while (flag);
if (!StringUtils.isEmpty(errorMessage)) { for (Integer pageIndex : pageList) {
throw new RuntimeException(errorMessage); try {
cjtRequestBody.setPageIndex(pageIndex);
executeDetail(context, cjtRequest, addRecordRequest);
} catch (Exception e) {
errorMessageList.add(String.valueOf(pageIndex));
}
}
if (CollectionUtils.isNotEmpty(errorMessageList)) {
throw new RuntimeException(buildErrorBody(cjtRequest, errorMessageList));
} }
} }
private void preSync(CJTJobContext context) {
erpLarkTempRelationMapper.updateByMethod(getReportName(), FlagStatus.DELETED.getCode());
}
private Object changeValueType(String value, CJTSyncTypeRelation cjtSyncTypeRelation) { private void finish(CJTJobContext context) {
TableFieldTypeEnum type = cjtSyncTypeRelation.getType(); try {
if (org.apache.commons.lang3.StringUtils.isBlank(value) || type == null) { LarkCompanyRelation companyRelation = context.getCompanyRelation();
return value; LarkTableRelation tableRelation = context.getTableRelation();
LarkTableRequest request = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken(), tableRelation.getToTableId());
ErpLarkTempRelation query = new ErpLarkTempRelation();
query.setMethod(getReportName());
query.setFlag(FlagStatus.DELETED.getCode());
List<ErpLarkTempRelation> erpLarkTempRelations = erpLarkTempRelationMapper.selectErpLarkTempRelationList(query);
List<String> larkKeyList = new ArrayList<>();
for (ErpLarkTempRelation erpLarkTempRelation : erpLarkTempRelations) {
String larkKey = erpLarkTempRelation.getLarkKey();
if (StringUtils.isBlank(larkKey)) {
continue;
}
larkKeyList.add(larkKey);
if (larkKeyList.size() == 500) {
request.setRecords(larkKeyList.toArray(new String[0]));
larkTableHelper.deleteTableRecordBatch(request);
larkKeyList = new ArrayList<>();
}
}
erpLarkTempRelationMapper.deleteByMethod(getReportName(), FlagStatus.DELETED.getCode());
} catch (Exception e) {
log.info("Sync report job finish error", e);
}
} }
switch (type) { private Boolean executeDetail(CJTJobContext context, CJTRequest cjtRequest, LarkTableRequest addRecordRequest) {
case NUMBER: cjtRequest.setOpenToken(generateToken(context));
return Double.valueOf(value); //请求接口并序列化数据
case DATE: CJTResponseReportBodyDetail bodyDetail = request(cjtRequest);
LocalDateTime localDateTime = DateUtils.str2ldt(value, cjtSyncTypeRelation.getPattern()); //实际返回数据
return DateUtils.ldt2Long(localDateTime); List<CJTResponseReportBodyDetailRow> rows = bodyDetail.getRows();
default: if (CollectionUtils.isNotEmpty(rows)) {
return value; //批量同步飞书
List<String> errorCodeList = syncLarkBatch(rows, addRecordRequest, context.getCjt());
if (!CollectionUtils.isEmpty(errorCodeList)) {
String errorKey = String.join(",", errorCodeList);
throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey));
}
} }
return CollectionUtils.isNotEmpty(rows);
} }
protected Map<String, Object> buildLarkBody(CJTResponseReportBodyDetailRow rowDetail) { protected Map<String, Object> buildLarkBody(CJTResponseReportBodyDetailRow rowDetail) {
@ -247,21 +269,45 @@ public abstract class SyncReportJob {
* *
*/ */
protected List<String> syncLarkBatch(List<CJTResponseReportBodyDetailRow> rows, LarkTableRequest addRecordRequest, String cjt) { protected List<String> syncLarkBatch(List<CJTResponseReportBodyDetailRow> rows, LarkTableRequest addRecordRequest, String cjt) {
//错误唯一键
List<String> errorKey = new ArrayList<>(); List<String> errorKey = new ArrayList<>();
for (CJTResponseReportBodyDetailRow row : rows) {
try { try {
//错误唯一键
List<AppTableRecord> list = new ArrayList<>();
List<ErpLarkTempRelation> waitCreateList = new ArrayList<>();
for (CJTResponseReportBodyDetailRow row : rows) {
Map<String, Object> body = buildLarkBody(row); Map<String, Object> body = buildLarkBody(row);
addRecordRequest.setBody(body); addRecordRequest.setBody(body);
AppTableRecord appTableRecord = new AppTableRecord();
appTableRecord.setFields(body);
list.add(appTableRecord);
}
if (CollectionUtils.isNotEmpty(list)){
addRecordRequest.setAppTableRecords(list.toArray(new AppTableRecord[0]));
//在飞书创建一行并根据创建返回的行id在本地保留一条映射纪律 //在飞书创建一行并根据创建返回的行id在本地保留一条映射纪律
larkTableHelper.addTableRecord(addRecordRequest); BatchCreateAppTableRecordRespBody resp = larkTableHelper.addTableRecordBatch(addRecordRequest);
for (AppTableRecord record : resp.getRecords()) {
buildTempList(waitCreateList,record.getRecordId());
}
if (CollectionUtils.isNotEmpty(waitCreateList)){
erpLarkTempRelationMapper.batchInsert(waitCreateList);
}
}
} catch (Exception e) { } catch (Exception e) {
log.error("{} addOrUpdate exception", this.getClassName(), e); log.error("{} addOrUpdate exception", this.getClassName(), e);
} }
}
return errorKey; return errorKey;
} }
private void buildTempList(List<ErpLarkTempRelation> waitCreateList,String recordId){
ErpLarkTempRelation erpLarkTempRelation = new ErpLarkTempRelation();
erpLarkTempRelation.setCreateTime(new Date());
erpLarkTempRelation.setCreateBy("System");
erpLarkTempRelation.setMethod(getReportName());
erpLarkTempRelation.setFlag(FlagStatus.OK.getCode());
erpLarkTempRelation.setLarkKey(recordId);
waitCreateList.add(erpLarkTempRelation);
}
private String buildCacheUniqueKey(String key, String appKey) { private String buildCacheUniqueKey(String key, String appKey) {
return String.format("%s:%s", key, appKey); return String.format("%s:%s", key, appKey);
@ -296,7 +342,7 @@ public abstract class SyncReportJob {
private String generateToken(CJTJobContext context) { private String generateToken(CJTJobContext context) {
String tokenCacheKey = buildCacheUniqueKey(RedisConstants.CJT_TOKEN_CACHE_KEY, context.getAppKey()); String tokenCacheKey = buildCacheUniqueKey(RedisConstants.CJT_TOKEN_CACHE_KEY, context.getAppKey());
String openToken = (String) redisCache.getCacheObject(tokenCacheKey); String openToken = (String) redisCache.getCacheObject(tokenCacheKey);
if (StringUtils.isEmpty(openToken)) { if (StringUtils.isBlank(openToken)) {
CJTRequest cjtRequest = new CJTRequest(REQUEST_GENERATE_TOKEN_PATH CJTRequest cjtRequest = new CJTRequest(REQUEST_GENERATE_TOKEN_PATH
, context.getAppKey() , context.getAppKey()
, context.getAppSecret() , context.getAppSecret()
@ -357,16 +403,16 @@ public abstract class SyncReportJob {
CJTResponseReportBody responseBody = JSONObject.parseObject(post, CJTResponseReportBody.class); CJTResponseReportBody responseBody = JSONObject.parseObject(post, CJTResponseReportBody.class);
if (!"0".equals(responseBody.getStatus())) { if (!"0".equals(responseBody.getStatus())) {
log.error("{} exception", this.getClassName(), responseBody.getErrorMessage()); log.error("{} exception", this.getClassName(), responseBody.getErrorMessage());
throw new RuntimeException(buildErrorBody(req, responseBody.getErrorMessage())); throw new RuntimeException(buildErrorBody(req, Arrays.asList(responseBody.getErrorMessage())));
} }
return responseBody.getDataSource(); return responseBody.getDataSource();
} }
protected String buildErrorBody(CJTRequest req, String errorMessage) { protected String buildErrorBody(CJTRequest req, List<String> errorPageIndexList) {
JSONObject errorInfo = new JSONObject(); JSONObject errorInfo = new JSONObject();
errorInfo.put("url", req.getUrl()); errorInfo.put("url", req.getUrl());
errorInfo.put("body", JSONObject.toJSONString(req.getBody())); errorInfo.put("body", JSONObject.toJSONString(req.getBody()));
errorInfo.put("errorMessage", errorMessage); errorInfo.put("errorMessage", String.join(",", errorPageIndexList));
return errorInfo.toJSONString(); return errorInfo.toJSONString();
} }
} }

Loading…
Cancel
Save