【第三方接口】数据请求(Json格式请求)

HttpClientUtil工具类

	/**
     * 非表单方式提交数据,未指定编码方式
     *
     * @param url
     * @param headers
     * @return
     * @throws IOException
     */
    public static String post(String url, String requestBody, Map<String, String> headers) throws IOException {
        HttpPost post = new HttpPost(url);
        if (StringUtils.isNotBlank(requestBody)) {
            StringEntity entity = new StringEntity(requestBody, Consts.UTF_8);
            entity.setContentType("application/json");
            post.setEntity(entity);
        }
        return execute(post, null, headers, Consts.UTF_8);
    }

请求controller代码:

new Thread(() -> ogcCustomerTerminalService.executeTerminalCustomerSync()).start();

请求service代码:

	@Scheduled(cron = "0 0 1 * * ?")
    public void syncTerminalCustomerData() {
        lockTemplate.execute(BizLock.builder()
                        .bizType("sync")
                        .bizId("ogc")
                        .method("terminalCustomer")
                        .expire(300)
                        .build()
                , () -> {
                    if (executeTerminalCustomerSync()) {
                        return null;
                    }
                    return null;
                });
    }



	public boolean executeTerminalCustomerSync() {
        Integer flag = ogcCustomerMapper.selectCount(new QueryWrapper<OgcCustomerEntity>());
        // 不存在数据,直接发送请求
        if (flag.equals(BizConstant.ZERO)) {
            return syncPostOgcCustomerData();
        }else {
            // 存在数据,先删除掉,再请求
            if (ogcCustomerMapper.delete(new QueryWrapper<OgcCustomerEntity>()) > BizConstant.ZERO) {
                return syncPostOgcCustomerData();
            }
        }
        return false;
   }





	/**
     * 发送同步数据请求
     * @return
     */
    private Boolean syncPostOgcCustomerData() {
        try {
            String requestBody = "{\"total\":1,\"rows\":500,\"number\":\"\",\"customerName\":\" \"}";
            String result = HttpClientUtil.post(terminalCustomerUrl, requestBody, null);
            JSONObject jsonObject = JSONObject.parseObject(result);

            Integer pages = jsonObject.getInteger("total");
            String data = jsonObject.getString("rows");
            JSONArray jsonArray = (JSONArray) JSONObject.parse(data);

            List<OgcCustomerEntity> list = getOgcCustomerEntities(jsonArray);
            if (CollectionUtils.isEmpty(list)) {
                return true;
            }
            List<OgcCustomerEntity> listAdd = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(list)) {
                listAdd = list.stream().distinct().collect(Collectors.toList());
            }
            for (int i = BizConstant.TWO; i <= pages; i++) {
                requestBody = "{\"total\":\"" + i +"\",\"rows\":500,\"number\":\"\",\"customerName\":\" \"}";
                result = HttpClientUtil.post(terminalCustomerUrl, requestBody, null);
                jsonObject = JSONObject.parseObject(result);
                data = jsonObject.getString("rows");
                jsonArray = (JSONArray) JSONObject.parse(data);

                list = getOgcCustomerEntities(jsonArray);
                if (CollectionUtils.isNotEmpty(list)) {
                    listAdd = list.stream().distinct().collect(Collectors.toList());
                }
            }
            ogcCustomerMapper.batchInsert(listAdd);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("同步terminalCustomerData失败", e);
        }
        return false;
    }

Mapper代码:


int batchInsert(@Param("list") List<ScheduleCheckValueInfoEntity> list);



<insert id="batchInsert" parameterType="java.util.List">
        insert into schedule_check_value_info (tag_value_id, tag_id,tag_value,unique_code,tag_extra_value,tag_extra_value_en,status,create_time, update_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.tagValueId,jdbcType=INTEGER},
            #{item.tagId,jdbcType=INTEGER},
            #{item.tagValue,jdbcType=VARCHAR},
            #{item.uniqueCode,jdbcType=VARCHAR},
            #{item.tagExtraValue,jdbcType=VARCHAR},
            #{item.tagExtraValue,jdbcType=VARCHAR},
            #{item.status,jdbcType=INTEGER},

            now(),
            now())
        </foreach>
    </insert>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火火笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值