|
|
@@ -0,0 +1,275 @@
|
|
|
+package com.xzjmyk.pm.activity.ui.erp.presenter;
|
|
|
+
|
|
|
+import android.os.Bundle;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baidu.mapapi.model.LatLng;
|
|
|
+import com.baidu.mapapi.search.core.PoiInfo;
|
|
|
+import com.baidu.mapapi.search.core.SearchResult;
|
|
|
+import com.baidu.mapapi.search.poi.PoiResult;
|
|
|
+import com.xzjmyk.pm.activity.AppConfig;
|
|
|
+import com.xzjmyk.pm.activity.BdLocationHelper;
|
|
|
+import com.xzjmyk.pm.activity.MyApplication;
|
|
|
+import com.xzjmyk.pm.activity.R;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.model.SelectAimModel;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.presenter.imp.ISelectAim;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.ListUtils;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.util.auto.HttpHandler;
|
|
|
+import com.xzjmyk.pm.activity.util.BaiduMapUtil;
|
|
|
+import com.xzjmyk.pm.activity.util.TimeUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Bitliker on 2017/1/12.
|
|
|
+ */
|
|
|
+
|
|
|
+public class SelectAimPresenter implements HttpHandler.OnResultListener {
|
|
|
+ private ISelectAim iSelectAim;
|
|
|
+ private final int LOAD = 0x11;
|
|
|
+ private BdLocationHelper locationHelper;
|
|
|
+ private List<SelectAimModel> models;//从服务其返回的数据列表
|
|
|
+ private String keyWork;
|
|
|
+ private boolean seachChina = false;
|
|
|
+
|
|
|
+
|
|
|
+ public SelectAimPresenter(ISelectAim iSelectAim) {
|
|
|
+ this.iSelectAim = iSelectAim;
|
|
|
+ }
|
|
|
+
|
|
|
+ /******
|
|
|
+ * start 对外接口
|
|
|
+ **********/
|
|
|
+ public void start() {
|
|
|
+ locationHelper = MyApplication.getInstance().getBdLocationHelper();
|
|
|
+ loadOutAddress();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void seachByKey(String keyWork) {
|
|
|
+ this.keyWork = keyWork;
|
|
|
+ if (ListUtils.isEmpty(models)) {
|
|
|
+ if (StringUtils.isEmpty(keyWork)) {
|
|
|
+ loadNeer();
|
|
|
+ } else {
|
|
|
+ loadDataByChina();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (StringUtils.isEmpty(keyWork)) {
|
|
|
+ if (iSelectAim != null) {
|
|
|
+ iSelectAim.showModel(models);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<SelectAimModel> chche = new ArrayList<>();
|
|
|
+ for (SelectAimModel e : models) {
|
|
|
+ if (isIncude(keyWork, e)) chche.add(e);
|
|
|
+ }
|
|
|
+ if (ListUtils.isEmpty(chche)) {
|
|
|
+ loadDataByChina();
|
|
|
+ } else {
|
|
|
+ if (iSelectAim != null)
|
|
|
+ iSelectAim.showModel(chche);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /******
|
|
|
+ * end 对外接口
|
|
|
+ **********/
|
|
|
+
|
|
|
+
|
|
|
+ /***********************
|
|
|
+ * 内部计算函数
|
|
|
+ ***********************************/
|
|
|
+
|
|
|
+ //从服务器获取数据
|
|
|
+ private void loadOutAddress() {
|
|
|
+ log("loadOutAddress");
|
|
|
+ if (!MyApplication.getInstance().isNetworkActive()) {
|
|
|
+ if (iSelectAim != null)
|
|
|
+ iSelectAim.showToast(R.string.networks_out, R.color.load_error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (iSelectAim != null)
|
|
|
+ iSelectAim.showLoading();
|
|
|
+
|
|
|
+ //获取网络数据
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ param.put("condition", "1=1");
|
|
|
+ param.put("pageIndex", 1);
|
|
|
+ param.put("pageSize", 1000);
|
|
|
+ HttpHandler.getInstance().loadERPByNet(LOAD, "mobile/getOutAddressDate.action", param, null, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ //下载附近数据
|
|
|
+ private void loadNeer() {
|
|
|
+ log("loadNeer");
|
|
|
+ BaiduMapUtil.getInstence().getNearInfo(200, 0, locationHelper.getLocation(), listener);
|
|
|
+ }
|
|
|
+
|
|
|
+ //下载全国数据
|
|
|
+ private void loadDataByChina() {
|
|
|
+ log("loadDataByChina");
|
|
|
+ seachChina = true;
|
|
|
+ BaiduMapUtil.getInstence().searchByInputOnChina(StringUtils.isEmpty(keyWork) ? "" : keyWork, 0, listener);
|
|
|
+ }
|
|
|
+
|
|
|
+ //下载本城市数据
|
|
|
+ private void loadDataByCity() {
|
|
|
+ log("loadDataByCity");
|
|
|
+ seachChina = false;
|
|
|
+ BaiduMapUtil.getInstence().searchByInput(StringUtils.isEmpty(keyWork) ? "" : keyWork, 0, listener);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BaiduMapUtil.OnSearchPoiListener listener = new BaiduMapUtil.OnSearchPoiListener() {
|
|
|
+ @Override
|
|
|
+ public void result(SearchResult result) {
|
|
|
+ if (result == null) {
|
|
|
+ if (seachChina) {
|
|
|
+ loadDataByCity();
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ showEmpty();
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ PoiResult poiResult = (PoiResult) result;
|
|
|
+ List<PoiInfo> chches = poiResult.getAllPoi();
|
|
|
+ if (ListUtils.isEmpty(chches)) {
|
|
|
+ if (seachChina) {
|
|
|
+ loadDataByCity();
|
|
|
+ return;
|
|
|
+ } else {//搜索城市为空数据
|
|
|
+ showEmpty();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ List<SelectAimModel> models = new ArrayList<>();
|
|
|
+ SelectAimModel model = null;
|
|
|
+ for (int i = 0; i < chches.size(); i++) {
|
|
|
+ if (StringUtils.isEmpty(chches.get(i).name) || StringUtils.isEmpty(chches.get(i).address)) {
|
|
|
+ chches.remove(i);
|
|
|
+ i--;
|
|
|
+ } else {
|
|
|
+ model = new SelectAimModel();
|
|
|
+ model.setType(3);
|
|
|
+ model.setName(chches.get(i).name);
|
|
|
+ model.setAddress(chches.get(i).address);
|
|
|
+ model.setLatLng(chches.get(i).location);
|
|
|
+ models.add(model);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ListUtils.isEmpty(models)) {
|
|
|
+ showEmpty();
|
|
|
+ } else {
|
|
|
+ if (StringUtils.isEmpty(keyWork)) {
|
|
|
+ SelectAimPresenter.this.models = models;
|
|
|
+ }
|
|
|
+ show(models);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void result(int what, boolean isJSON, String message, Bundle bundle) {
|
|
|
+ if (what == LOAD && isJSON) {
|
|
|
+ JSONArray array = JSON.parseObject(message).getJSONArray("data");
|
|
|
+ handleDada(array);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void error(int what, int statuCode, String message, Bundle bundle) {
|
|
|
+ if (iSelectAim != null)
|
|
|
+ iSelectAim.dimssLoading();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //处理请求下来的数据
|
|
|
+ private void handleDada(JSONArray array) {
|
|
|
+ if (ListUtils.isEmpty(array)) {
|
|
|
+ loadNeer();
|
|
|
+ if (iSelectAim != null)
|
|
|
+ iSelectAim.dimssLoading();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SelectAimModel model = null;
|
|
|
+ JSONObject object = null;
|
|
|
+ List<SelectAimModel> models = new ArrayList<>();
|
|
|
+ for (int i = 0; i < array.size(); i++) {
|
|
|
+ try {
|
|
|
+ model = new SelectAimModel();
|
|
|
+ object = array.getJSONObject(i);
|
|
|
+ model.setAddress(object.getString("MD_ADDRESS"));
|
|
|
+ model.setTimes(object.getInteger("MD_VISITCOUNT"));
|
|
|
+ model.setName(object.getString("MD_COMPANY"));
|
|
|
+ //TODO id先不要
|
|
|
+// model.seti(object.getInteger("MD_ID"));
|
|
|
+ model.setType(2);
|
|
|
+ float longitude = object.getFloatValue("MD_LONGITUDE");
|
|
|
+ float latitude = object.getFloatValue("MD_LATITUDE");
|
|
|
+ if (longitude != 0 && latitude != 0) {
|
|
|
+ model.setLatLng(new LatLng(latitude, longitude));
|
|
|
+ }
|
|
|
+ if (object.getInteger("MD_VISITTIME") != null) {
|
|
|
+ try {
|
|
|
+ model.setTime(TimeUtils.f_long_2_str(object.getLong("MD_VISITTIME")));
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e != null)
|
|
|
+ log("e.getMessage" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ models.add(model);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ showByLoad(models);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showByLoad(List<SelectAimModel> models) {
|
|
|
+ this.models = models;
|
|
|
+ if (iSelectAim != null) {
|
|
|
+ iSelectAim.showModel(models);
|
|
|
+ iSelectAim.dimssLoading();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showEmpty() {
|
|
|
+ List<SelectAimModel> models = new ArrayList<>();
|
|
|
+ SelectAimModel model = new SelectAimModel();
|
|
|
+ model.setType(1);
|
|
|
+ models.add(model);
|
|
|
+ show(models);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void show(List<SelectAimModel> models) {
|
|
|
+ if (iSelectAim != null) {
|
|
|
+ iSelectAim.showModel(models);
|
|
|
+ iSelectAim.dimssLoading();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isIncude(String key, SelectAimModel model) {
|
|
|
+ String text = model.getAddress() + model.getTime() + model.getName();
|
|
|
+ return StringUtils.isInclude(text, key);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void log(String message) {
|
|
|
+ try {
|
|
|
+ if (!AppConfig.DEBUG || StringUtils.isEmpty(message)) return;
|
|
|
+ Log.i("gongpengming", message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e != null)
|
|
|
+ Log.i("gongpengming", "show Exception" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|