package com.fr.plugin; import com.fanruan.api.log.LogKit; import com.fr.plugin.models.MyListSimCardsResponse; import com.fr.plugin.models.MySimDeviceVO; import com.fr.plugin.transform.ExecuteFunctionRecord; import com.fr.plugin.transform.FunctionRecorder; import com.fr.script.AbstractFunction; import com.fr.stable.exception.FormulaException; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.http.HttpConfig; import com.huaweicloud.sdk.gsl.v3.model.ListSimCardsRequest; import com.huaweicloud.sdk.gsl.v3.region.GslRegion; import java.util.Date; import java.util.UUID; @FunctionRecorder public class GetSIMDataFun extends AbstractFunction { /** * 接口地址: * https://xxxx/api-ocgsl/gsl_07_0008.html * 表名:fine_plugin_siminfo * 数据链接名:FRDW * * @param objects * @return * @throws FormulaException */ @Override @ExecuteFunctionRecord public Object run(Object[] objects) throws FormulaException { if (objects.length<2) { return "调用参数顺序应该为getSIMData('ak','sk') 当前参数个数不足"; } HttpConfig config = HttpConfig.getDefaultHttpConfig(); String ak = objects[0].toString(); String sk = objects[1].toString(); config.withIgnoreSSLVerification(true); BasicCredentials auth = new BasicCredentials() .withAk(ak) .withSk(sk); MyGslClient ecsClient = MyGslClient.newBuilder() .withHttpConfig(config) .withRegion(GslRegion.CN_NORTH_4) .withCredential(auth) .build(); Integer count; Long offset = 1L; Integer sum = 0; DBUtils dbUtils = new DBUtils(); do { MyListSimCardsResponse cardsResponse = getSIMS(ecsClient, offset); count = cardsResponse.getSimCards().size(); if(count==0){ break; } try { for (MySimDeviceVO simCard : cardsResponse.getSimCards()) { insert2DB(simCard, dbUtils); } } catch (Exception e) { LogKit.error("SIM卡保存异常:", e); } sum += count; offset++; } while (count > 0); LogKit.info("一共同步了:{} 条记录", sum); return "success"; } private void insert2DB(MySimDeviceVO vo, DBUtils dbUtils) throws Exception { Object[] params = new Object[]{ UUID.randomUUID().toString(), vo.getSimCardId(), vo.getAccountId(), vo.getCid(), vo.getSimPoolId(), vo.getImei(), vo.getSimStatus(), vo.getDeviceStatus(), vo.getDeviceModel(), vo.getActDate(), vo.getDeviceStatusDate(), vo.getNodeId(), vo.getIccid(), vo.getNetworkType(), vo.getDbm(), vo.getSignalLevel(), vo.getSimType(), vo.getTagNames(), vo.getOrderId(), vo.getExpireTime(), vo.getPricePlanName(), vo.getFlowLeft(), vo.getFlowUsed(), vo.getOperatorStatus(), vo.getMsisdn(), vo.getImsi(), vo.getRealNamed(), vo.getCutNetFlag(), vo.getExceedCutNetFlag(), vo.getExceedCutNetQuota(), vo.getImeiBindRemainTimes(), vo.getSpeedValue(), new Date() }; String sql = "INSERT INTO `fine_plugin_siminfo`(`s_id`, `sim_card_id`, `account_id`, `cid`, `sim_pool_id`, `imei`, `sim_status`, `device_status`, `device_model`, `act_date`, `device_status_date`, `node_id`, `iccid`, `network_type`, `dbm`, `signal_level`, `sim_type`, `tag_names`, `order_id`, `expire_time`, `price_plan_name`, `flow_left`, `flow_used`, `operator_status`, `msisdn`, `imsi`, `real_named`, `cut_net_flag`, `exceed_cut_net_flag`, `exceed_cut_net_quota`, `imei_bind_remain_times`, `speed_value`, `update_time`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; dbUtils.exSqlUpdate(sql, params); } //展示的当前页 private MyListSimCardsResponse getSIMS(MyGslClient MyGslClient, Long offset) { //显示的数据数量 最大500 Long limit = 500L; //SIM卡状态-11-未激活 Integer simStatus = 20; MyListSimCardsResponse response = MyGslClient.listSimCards( new ListSimCardsRequest().withLimit(limit).withOffset(offset) ); LogKit.info("请求响应:{} ", response.toString()); return response; } }