fix: 批量提交功能初始化
This commit is contained in:
parent
eeee746a22
commit
ba6364d55b
@ -1278,6 +1278,9 @@ public class ChargingStationFragment extends BaseDrawerFragment implements View.
|
||||
if (responseBodyStr!=null) {
|
||||
Gson gson = new Gson();
|
||||
ChargingPileSaveBean chargingPileSaveBean = gson.fromJson(responseBodyStr, ChargingPileSaveBean.class);
|
||||
if (chargingPileSaveBean.getCode()!=200) {
|
||||
return;
|
||||
}
|
||||
chargingPileEntity.setBodyId(chargingPileSaveBean.getBody());
|
||||
InsertAndUpdateUtils.getInstance().insertOrUpdateChargingPile(getActivity(), chargingPileEntity); // 更新当前充电桩的bodyId
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ import java.util.List;
|
||||
* */
|
||||
public class PoiSaveUtils {
|
||||
private Activity mContext;
|
||||
private Gson gson;
|
||||
private static PoiSaveUtils instance;
|
||||
|
||||
public static PoiSaveUtils getInstance(Activity mContext) {
|
||||
@ -47,35 +48,64 @@ public class PoiSaveUtils {
|
||||
|
||||
private PoiSaveUtils(Activity mContext) {
|
||||
this.mContext = mContext;
|
||||
this.gson = new Gson();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据到本地,保存后taskStatus为2
|
||||
* 批量上传数据到服务端
|
||||
* */
|
||||
public void savePoiLocal(PoiEntity poiEntity) {
|
||||
if (poiEntity!=null) {
|
||||
XXPermissions.with(mContext)
|
||||
//读写权限
|
||||
.permission(Permission.MANAGE_EXTERNAL_STORAGE)
|
||||
.request(new OnPermissionCallback() {
|
||||
@Override
|
||||
public void onGranted(List<String> permissions, boolean all) {
|
||||
poiEntity.setTaskStatus(2);
|
||||
InsertAndUpdateUtils.getInstance().insertOrUpdate(mContext, poiEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDenied(List<String> permissions, boolean never) {
|
||||
|
||||
}
|
||||
});
|
||||
public void uploadPoiEntityBatch(List<PoiEntity> poiEntityList) {
|
||||
if (poiEntityList==null||poiEntityList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (PoiEntity poiEntity: poiEntityList) {
|
||||
if (poiEntity.getType() == 2) { // 如果是充电站数据,首先检查子充电桩的状态
|
||||
if (saveChargingPileByChargingStation(poiEntity)) {
|
||||
if (savePoiNet(poiEntity) == 200) { // 网络保存成功
|
||||
// 开始上传流程
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (savePoiNet(poiEntity) == 200) { // 网络保存成功
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 保存数据到本地,保存后taskStatus为2
|
||||
// * */
|
||||
// public void savePoiLocal(PoiEntity poiEntity) {
|
||||
// if (poiEntity!=null) {
|
||||
// XXPermissions.with(mContext)
|
||||
// //读写权限
|
||||
// .permission(Permission.MANAGE_EXTERNAL_STORAGE)
|
||||
// .request(new OnPermissionCallback() {
|
||||
// @Override
|
||||
// public void onGranted(List<String> permissions, boolean all) {
|
||||
// poiEntity.setTaskStatus(2);
|
||||
// InsertAndUpdateUtils.getInstance().insertOrUpdate(mContext, poiEntity);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onDenied(List<String> permissions, boolean never) {
|
||||
//
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 保存数据到服务
|
||||
* */
|
||||
public void savePoiNet(PoiEntity poiEntity) {
|
||||
public int savePoiNet(PoiEntity poiEntity) {
|
||||
HttpParams httpParams = new HttpParams();
|
||||
httpParams.put("taskId", poiEntity.getTaskId());
|
||||
httpParams.put("name", poiEntity.getName());
|
||||
@ -83,32 +113,56 @@ public class PoiSaveUtils {
|
||||
httpParams.put("existence", poiEntity.getExistence());
|
||||
httpParams.put("geo", poiEntity.getGeoWkt());
|
||||
httpParams.put("memo", poiEntity.getMemo());
|
||||
OkGoBuilder.getInstance()
|
||||
.Builder(mContext)
|
||||
.url(HttpInterface.SUBMIT_POI_TASK)
|
||||
.method(OkGoBuilder.GET)
|
||||
.cls(PoiSaveBean.class)
|
||||
.params(httpParams)
|
||||
.callback(new Callback<PoiSaveBean>() {
|
||||
@Override
|
||||
public void onSuccess(PoiSaveBean poiSaveBean, int id) {
|
||||
Integer body = poiSaveBean.getBody();
|
||||
poiEntity.setBodyId(body);
|
||||
poiEntity.setTaskStatus(3);
|
||||
InsertAndUpdateUtils.getInstance().insertOrUpdate(mContext, poiEntity);
|
||||
Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e, int id) {
|
||||
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
Log.d("TAG", "onError: " + e.getMessage());
|
||||
}
|
||||
}).build();
|
||||
try {
|
||||
okhttp3.Response execute = OkGo.<String>get(HttpInterface.SUBMIT_POI_TASK)
|
||||
.tag(PoiSaveUtils.this)
|
||||
.params(httpParams)
|
||||
.execute();
|
||||
if (execute.code()!=200) {
|
||||
return execute.code();
|
||||
}
|
||||
String bodyStr = execute.body().string();
|
||||
if (bodyStr == null) {
|
||||
return -1;
|
||||
}
|
||||
PoiSaveBean poiSaveBean = gson.fromJson(bodyStr, PoiSaveBean.class);
|
||||
if (poiSaveBean.getCode() == 200) {
|
||||
poiEntity.setTaskStatus(3);
|
||||
} else {
|
||||
return poiSaveBean.getCode();
|
||||
}
|
||||
InsertAndUpdateUtils.getInstance().insertOrUpdate(mContext, poiEntity);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 200;
|
||||
// OkGoBuilder.getInstance()
|
||||
// .Builder(mContext)
|
||||
// .url(HttpInterface.SUBMIT_POI_TASK)
|
||||
// .method(OkGoBuilder.GET)
|
||||
// .cls(PoiSaveBean.class)
|
||||
// .params(httpParams)
|
||||
// .callback(new Callback<PoiSaveBean>() {
|
||||
// @Override
|
||||
// public void onSuccess(PoiSaveBean poiSaveBean, int id) {
|
||||
// Integer body = poiSaveBean.getBody();
|
||||
// poiEntity.setBodyId(body);
|
||||
// poiEntity.setTaskStatus(3);
|
||||
// InsertAndUpdateUtils.getInstance().insertOrUpdate(mContext, poiEntity);
|
||||
// Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onError(Throwable e, int id) {
|
||||
// Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
// Log.d("TAG", "onError: " + e.getMessage());
|
||||
// }
|
||||
// }).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 最终上传数据到服务
|
||||
* 最终上传POI数据到服务
|
||||
* */
|
||||
public void uploadPoiNet(PoiEntity poiEntity, ArrayList<File> poiPicList) {
|
||||
if (poiEntity == null) {
|
||||
@ -148,23 +202,31 @@ public class PoiSaveUtils {
|
||||
/**
|
||||
* 根据充电站数据检查充电桩数据,如果没有网络保存,则批量网络保存
|
||||
* */
|
||||
private void saveChargingPileByChargingStation(PoiEntity chargingStationPoiEntity) {
|
||||
private boolean saveChargingPileByChargingStation(PoiEntity chargingStationPoiEntity) {
|
||||
boolean result = true;
|
||||
if (chargingStationPoiEntity != null) {
|
||||
List<ChargingPileEntity> chargingPileEntityList = PoiDatabase.getInstance(mContext).getChargingPileDao().getChargingPileByStationId(chargingStationPoiEntity.getId());
|
||||
if (chargingPileEntityList!=null&&!chargingPileEntityList.isEmpty()) {
|
||||
// 依次上传充电桩数据,如果有一条数据未上传成功,则返回false
|
||||
for (ChargingPileEntity pileEntity: chargingPileEntityList) {
|
||||
if (pileEntity.getBodyId()==0) {
|
||||
saveChargingPileByWork(pileEntity);
|
||||
int saveResult = saveChargingPile2NetWork(pileEntity);
|
||||
if (saveResult == 200){
|
||||
result = result&true;
|
||||
} else {
|
||||
result = result&false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传充电桩数据到服务端
|
||||
* */
|
||||
private void saveChargingPileByWork(ChargingPileEntity chargingPileEntity) {
|
||||
private int saveChargingPile2NetWork(ChargingPileEntity chargingPileEntity) {
|
||||
HttpParams httpParams = new HttpParams();
|
||||
httpParams.put("taskId", chargingPileEntity.getTaskId());
|
||||
httpParams.put("name", chargingPileEntity.getName());
|
||||
@ -183,15 +245,25 @@ public class PoiSaveUtils {
|
||||
.params(httpParams)
|
||||
.tag(PoiSaveUtils.this)
|
||||
.execute();
|
||||
if (execute.code()!=200) {
|
||||
return execute.code();
|
||||
}
|
||||
String responseBodyStr = execute.body().string();
|
||||
if (responseBodyStr == null) {
|
||||
return -1;
|
||||
}
|
||||
if (responseBodyStr!=null) {
|
||||
Gson gson = new Gson();
|
||||
ChargingPileSaveBean chargingPileSaveBean = gson.fromJson(responseBodyStr, ChargingPileSaveBean.class);
|
||||
if (chargingPileSaveBean.getCode() != 200) {
|
||||
return chargingPileSaveBean.getCode();
|
||||
}
|
||||
chargingPileEntity.setBodyId(chargingPileSaveBean.getBody());
|
||||
InsertAndUpdateUtils.getInstance().insertOrUpdateChargingPile(mContext, chargingPileEntity); // 更新当前充电桩的bodyId
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user