package com.jiuqi.njt.ui.xxcj;
import com.jiuqi.mobile.nigo.comeclose.bean.NiGoBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.collect.DealerServiceBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.collect.DrivingSchoolServiceBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.collect.GasStationServiceBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.collect.RepairServiceBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.coop.CooperBean;
import com.jiuqi.mobile.nigo.comeclose.bean.base.UserBean;
import com.jiuqi.mobile.nigo.comeclose.exception.LoginException;
import com.jiuqi.mobile.nigo.comeclose.exception.NiGoException;
import com.jiuqi.mobile.nigo.comeclose.manager.app.IServiceCollectManager;
import com.jiuqi.mobile.nigo.comeclose.manager.master.ICooperManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.RepairUtils;
import com.jiuqi.njt.util.UIUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
/**
* 信息采集异步类
* @author qyd
*
*/
public class XxcjTask extends AsyncTask<Void, Void, Boolean> {
/**
* 信息采集类型标示
*
*/
enum DataType {
modifyData, addData
}
private Context context;
private MyApp application;
private DataType dataType;
private NiGoBean bean; //信息采集数据
private String flag; //信息采集数据数据对象标示 service 服务网点 cooper合作社
private double longitude ; //经度
private double latitude; //纬度
private Dialog dialog;
private String alertMessage = "";
public XxcjTask(Context context, DataType dataType, NiGoBean bean,
String flag, double longitude, double latitude) {
super();
this.context = context;
this.dataType = dataType;
this.bean = bean;
this.flag = flag;
this.longitude = longitude;
this.latitude = latitude;
this.application = (MyApp) context.getApplicationContext();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = RepairUtils.myShowDialog(context, 4);
super.onPreExecute();
}
@SuppressWarnings("unchecked")
@Override
protected Boolean doInBackground(Void... params) {
boolean success = false;
try {
// TODO Auto-generated method stub
ClientContext context = application.getClientContext();
if (null == context) {
context = ClientContext.getClientContext(Constants.SERVER_URL,
Constants.ANONYMOUS, Constants.ANONYMOUS);
application.setClientContext(context);
}
switch (dataType) {
case modifyData:
if ("service".equals(flag)) {
Class className = null;
IServiceCollectManager manager = context
.getManager(IServiceCollectManager.class);
if (bean instanceof RepairServiceBean) {
className = RepairServiceBean.class;
Log.e("className:",
className.getName() + "--" + bean.getGuid());
RepairServiceBean a = manager.find(className,
bean.getGuid());
a.setLongitude(longitude);
a.setLatitude(latitude);
if (longitude != 0) {
manager.update(a);
}
}
if (bean instanceof GasStationServiceBean) {
className = GasStationServiceBean.class;
Log.e("className:",
className.getName() + "--" + bean.getGuid());
GasStationServiceBean a = manager.find(className,
bean.getGuid());
a.setLongitude(longitude);
a.setLatitude(latitude);
if (longitude != 0) {
manager.update(a);
}
}
if (bean instanceof DealerServiceBean) {
className = DealerServiceBean.class;
Log.e("className:",
className.getName() + "--" + bean.getGuid());
DealerServiceBean a = manager.find(className,
bean.getGuid());
a.setLongitude(longitude);
a.setLatitude(latitude);
if (longitude != 0) {
manager.update(a);
}
}
if (bean instanceof DrivingSchoolServiceBean) {
className = RepairServiceBean.class;
Log.e("className:",
className.getName() + "--" + bean.getGuid());
DrivingSchoolServiceBean a = manager.find(className,
bean.getGuid());
a.setLongitude(longitude);
a.setLatitude(latitude);
if (longitude != 0) {
manager.update(a);
}
}
} else if ("cooper".equals(flag)) {
ICooperManager manager = context
.getManager(ICooperManager.class);
UserBean user = context.getUser();
if (null != user) {
Log.e("11", "user not null");
}
CooperBean a = manager.find(bean.getGuid());
a.setLongitude(longitude);
a.setLatitude(latitude);
if (longitude != 0) {
manager.update(a);
}
}
success = true;
break;
case addData:
if ("service".equals(flag)) {
IServiceCollectManager manager = context
.getManager(IServiceCollectManager.class);
Class className = null;
if (bean instanceof RepairServiceBean) {
className = RepairServiceBean.class;
RepairServiceBean repairServiceBean = (RepairServiceBean) bean;
repairServiceBean.setLatitude(latitude);
repairServiceBean.setLongitude(longitude);
manager.create(repairServiceBean);
}
if (bean instanceof GasStationServiceBean) {
className = GasStationServiceBean.class;
GasStationServiceBean repairServiceBean = (GasStationServiceBean) bean;
repairServiceBean.setLatitude(latitude);
repairServiceBean.setLongitude(longitude);
manager.create(repairServiceBean);
}
if (bean instanceof DealerServiceBean) {
DealerServiceBean repairServiceBean = (DealerServiceBean) bean;
repairServiceBean.setLatitude(latitude);
repairServiceBean.setLongitude(longitude);
manager.create(repairServiceBean);
}
if (bean instanceof DrivingSchoolServiceBean) {
DrivingSchoolServiceBean repairServiceBean = (DrivingSchoolServiceBean) bean;
repairServiceBean.setLatitude(latitude);
repairServiceBean.setLongitude(longitude);
manager.create(repairServiceBean);
}
} else if ("cooper".equals(flag)) {
ICooperManager manager = context
.getManager(ICooperManager.class);
CooperBean repairServiceBean = (CooperBean) bean;
repairServiceBean.setLatitude(latitude);
repairServiceBean.setLongitude(longitude);
manager.create(repairServiceBean);
}
success = true;
break;
default:
break;
}
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NiGoException e) {
// TODO Auto-generated catch block
if(null!=e.getMessage()){
alertMessage = e.getMessage();
}
e.printStackTrace();
}
return success;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
RepairUtils.myRemoveDialog(dialog);
if(result){
alertMessage = "数据上传成功";
}else{
if("".equals(alertMessage)){
alertMessage = "数据上传失败";
}
}
UIUtil.showMsg(context, alertMessage);
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
}