diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/mapper/ErpLarkTempRelationMapper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/mapper/ErpLarkTempRelationMapper.java index 163ab0f..df4f9c6 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/mapper/ErpLarkTempRelationMapper.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/mapper/ErpLarkTempRelationMapper.java @@ -62,9 +62,11 @@ public interface ErpLarkTempRelationMapper */ public int deleteErpLarkTempRelationByIds(Long[] ids); - public int updateByMethod(@Param("method") String method, @Param("flag") Long flag); + public Long selectMaxId(@Param("method") String method, @Param("flag") Long flag,@Param("key") String key); - public int deleteByMethod(@Param("method") String method,@Param("flag") Long flag); + public int updateByMethod(@Param("method") String method, @Param("flag") Long flag,@Param("id") Long id,@Param("key") String key); + + public int deleteByMethod(@Param("method") String method,@Param("flag") Long flag,@Param("id") Long id,@Param("key") String key); public int batchInsert(@Param("erpLarkTempRelationList") List erpLarkTempRelationList); diff --git a/ruoyi-flyingbook/src/main/resources/mapper/ErpLarkTempRelationMapper.xml b/ruoyi-flyingbook/src/main/resources/mapper/ErpLarkTempRelationMapper.xml index 0978915..7117eea 100644 --- a/ruoyi-flyingbook/src/main/resources/mapper/ErpLarkTempRelationMapper.xml +++ b/ruoyi-flyingbook/src/main/resources/mapper/ErpLarkTempRelationMapper.xml @@ -1,42 +1,55 @@ + PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + - - - - - - - - - - + + + + + + + + + + - select id, key, lark_key, method, create_by, create_time, update_by, update_time, flag, remark from erp_lark_temp_relation + select id, + `key`, + lark_key, + method, + create_by, + create_time, + update_by, + update_time, + flag, + remark + from erp_lark_temp_relation - - - and key = #{key} - and lark_key = #{larkKey} - and method = #{method} - and flag = #{flag} + + and `key` = #{key} + and lark_key = #{larkKey} + and method = #{method} + and flag = #{flag} + order by id - + - - + + insert into erp_lark_temp_relation key, @@ -48,7 +61,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update_time, flag, remark, - + #{key}, #{larkKey}, @@ -59,7 +72,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{updateTime}, #{flag}, #{remark}, - + @@ -79,37 +92,55 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - delete from erp_lark_temp_relation where id = #{id} + delete + from erp_lark_temp_relation + where id = #{id} - delete from erp_lark_temp_relation where id in + delete from erp_lark_temp_relation where id in #{id} + + - update erp_lark_relation + update erp_lark_temp_relation set flag = #{flag} - where method = #{method} + where method = #{method} and `key` = #{key} + and #{id} >= id - delete from erp_lark_relation - where method = #{method} and flag = #{flag} + delete + from erp_lark_temp_relation + where method = #{method} + and flag = #{flag} + and `key` = #{key} + and #{id} >= id - - insert into erp_lark_temp_relation(lark_key,`method`,create_by,create_time,flag) - values - ( - #{item.larkKey}, - #{item.method}, - #{item.createBy}, - #{item.createTime}, - #{item.flag} - ) - + + insert into erp_lark_temp_relation(`key`,lark_key,`method`,create_by,create_time,flag) + values + + ( + #{item.key}, + #{item.larkKey}, + #{item.method}, + #{item.createBy}, + #{item.createTime}, + #{item.flag} + ) + \ No newline at end of file diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTJobContext.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTJobContext.java index 3b6ae7e..754783f 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTJobContext.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTJobContext.java @@ -50,4 +50,6 @@ public class CJTJobContext implements Serializable { private LarkCompanyRelation companyRelation; private LarkTableRelation tableRelation; + private Long erpLarkTempRelationMaxId; + } diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTRequest.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTRequest.java index 92d4ffa..b88039f 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTRequest.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/CJTRequest.java @@ -39,6 +39,8 @@ public class CJTRequest implements Serializable { */ private JSONObject jsonBody; + private String taskSessionId; + /** * 获取列信息 */ diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/CJT/SyncReportJob.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/CJT/SyncReportJob.java index 0ff711d..00e5705 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/CJT/SyncReportJob.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/CJT/SyncReportJob.java @@ -20,10 +20,11 @@ import com.ruoyi.flyingbook.mapper.LarkCompanyRelationMapper; import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper; import com.ruoyi.quartz.domain.*; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; import java.util.*; import java.util.concurrent.TimeUnit; @@ -37,6 +38,7 @@ import static com.ruoyi.quartz.helper.OkHttpHelper.post; * @create 2023-07-17 20:29 */ @Slf4j +@Component("syncReportJob") public class SyncReportJob { @Autowired @@ -174,7 +176,7 @@ public class SyncReportJob { protected void sync(CJTJobContext context) { List errorMessageList = new ArrayList<>(); JSONObject cjtRequestReport = new JSONObject(); - CJTRequestReportBody cjtRequestBody = new CJTRequestReportBody(0, PAGE_SIZE, getReportName(), getQueryFields()); + CJTRequestReportBody cjtRequestBody = new CJTRequestReportBody(1, PAGE_SIZE, getReportName(), getQueryFields()); cjtRequestReport.put("request", cjtRequestBody); CJTRequest cjtRequest = buildCJTRequest(context, cjtRequestReport); @@ -191,6 +193,7 @@ public class SyncReportJob { log.error("{} exception", this.getClassName(), e); pageList.add(cjtRequestBody.getPageIndex()); } finally { + cjtRequestBody.setTaskSessionID(cjtRequest.getTaskSessionId()); cjtRequestBody.addPage(); } } while (flag); @@ -208,11 +211,21 @@ public class SyncReportJob { } private void preSync(CJTJobContext context) { - erpLarkTempRelationMapper.updateByMethod(getReportName(), FlagStatus.DELETED.getCode()); + CjtAccountEnum cjtAccountEnum = CjtAccountEnum.getByCode(context.getCjt()); + if (cjtAccountEnum == null){ + return; + } + Long maxId = erpLarkTempRelationMapper.selectMaxId(getReportName(), FlagStatus.OK.getCode(),cjtAccountEnum.getAppKey()); + context.setErpLarkTempRelationMaxId(maxId); + erpLarkTempRelationMapper.updateByMethod(getReportName(), FlagStatus.DELETED.getCode(),maxId,cjtAccountEnum.getAppKey()); } private void finish(CJTJobContext context) { try { + CjtAccountEnum cjtAccountEnum = CjtAccountEnum.getByCode(context.getCjt()); + if (cjtAccountEnum == null){ + return; + } LarkCompanyRelation companyRelation = context.getCompanyRelation(); LarkTableRelation tableRelation = context.getTableRelation(); LarkTableRequest request = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken(), tableRelation.getToTableId()); @@ -233,7 +246,7 @@ public class SyncReportJob { larkKeyList = new ArrayList<>(); } } - erpLarkTempRelationMapper.deleteByMethod(getReportName(), FlagStatus.DELETED.getCode()); + erpLarkTempRelationMapper.deleteByMethod(getReportName(), FlagStatus.DELETED.getCode(),context.getErpLarkTempRelationMaxId(),cjtAccountEnum.getAppKey()); } catch (Exception e) { log.info("Sync report job finish error", e); } @@ -243,6 +256,9 @@ public class SyncReportJob { cjtRequest.setOpenToken(generateToken(context)); //请求接口并序列化数据 CJTResponseReportBodyDetail bodyDetail = request(cjtRequest); + if (bodyDetail == null){ + return Boolean.FALSE; + } //实际返回数据 List rows = bodyDetail.getRows(); if (CollectionUtils.isNotEmpty(rows)) { @@ -253,15 +269,15 @@ public class SyncReportJob { throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey)); } } - return CollectionUtils.isNotEmpty(rows); + return CollectionUtils.isNotEmpty(rows) && StringUtils.isNotBlank(cjtRequest.getTaskSessionId()); } protected Map buildLarkBody(CJTResponseReportBodyDetailRow rowDetail) { Map body = new HashMap<>(); body.put("仓库", rowDetail.getWarehouse()); body.put("存货编码", rowDetail.getInventoryCode()); - body.put("现存量", rowDetail.getBaseQuantity()); - body.put("可用量", rowDetail.getCanuseBaseQuantity()); + body.put("现存量", StringUtils.isNotBlank(rowDetail.getBaseQuantity()) ? Double.valueOf(rowDetail.getBaseQuantity()) : 0D); + body.put("可用量", StringUtils.isNotBlank(rowDetail.getCanuseBaseQuantity()) ? Double.valueOf(rowDetail.getCanuseBaseQuantity()) : 0D); return body; } @@ -286,7 +302,7 @@ public class SyncReportJob { //在飞书创建一行,并根据创建返回的行id在本地保留一条映射纪律 BatchCreateAppTableRecordRespBody resp = larkTableHelper.addTableRecordBatch(addRecordRequest); for (AppTableRecord record : resp.getRecords()) { - buildTempList(waitCreateList,record.getRecordId()); + buildTempList(waitCreateList,record.getRecordId(),cjt); } if (CollectionUtils.isNotEmpty(waitCreateList)){ erpLarkTempRelationMapper.batchInsert(waitCreateList); @@ -298,13 +314,18 @@ public class SyncReportJob { return errorKey; } - private void buildTempList(List waitCreateList,String recordId){ + private void buildTempList(List waitCreateList,String recordId, String cjt){ + CjtAccountEnum byCode = CjtAccountEnum.getByCode(cjt); + if (byCode == null){ + return; + } ErpLarkTempRelation erpLarkTempRelation = new ErpLarkTempRelation(); erpLarkTempRelation.setCreateTime(new Date()); erpLarkTempRelation.setCreateBy("System"); erpLarkTempRelation.setMethod(getReportName()); erpLarkTempRelation.setFlag(FlagStatus.OK.getCode()); erpLarkTempRelation.setLarkKey(recordId); + erpLarkTempRelation.setKey(byCode.getAppKey()); waitCreateList.add(erpLarkTempRelation); } @@ -401,10 +422,11 @@ public class SyncReportJob { protected CJTResponseReportBodyDetail request(CJTRequest req) { String post = post(req); CJTResponseReportBody responseBody = JSONObject.parseObject(post, CJTResponseReportBody.class); - if (!"0".equals(responseBody.getStatus())) { + if (responseBody.getStatus() != 0) { log.error("{} exception", this.getClassName(), responseBody.getErrorMessage()); throw new RuntimeException(buildErrorBody(req, Arrays.asList(responseBody.getErrorMessage()))); } + req.setTaskSessionId(responseBody.getTaskSessionID()); return responseBody.getDataSource(); }