package com.jiuqi.njt.register;
import java.util.ArrayList;
import com.jiuqi.mobile.nigo.comeclose.bean.base.KindsOfCarBean;
import com.jiuqi.mobile.nigo.comeclose.exception.LoginException;
import com.jiuqi.mobile.nigo.comeclose.manager.base.IKindsOfCarManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.Optdb_interfce;
import com.jiuqi.njt.son.MyState;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.RepairUtils;
import com.jiuqi.njt.util.UIUtil;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
/**
* 获取农机类别异步类,获取成功就把数据存储到数据库中
* @author Joe
*
*/
public class GetDriverDataTask extends AsyncTask<Void, Void, String>{
private Context context ;
private AllTaskInterface allTaskInterface;
private MyApp application;
private Optdb_interfce database;
private Dialog pd;
private ArrayList<KindsOfCarBean> agriculturalBCategoryList = new ArrayList<KindsOfCarBean>();
private ArrayList<KindsOfCarBean> agriculturalSCategoryList = new ArrayList<KindsOfCarBean>();
private ArrayList<KindsOfCarBean> agriculturalPCategoryList = new ArrayList<KindsOfCarBean>();
public GetDriverDataTask(Context context, AllTaskInterface allTaskInterface) {
super();
this.context = context;
this.allTaskInterface = allTaskInterface;
application = (MyApp) context.getApplicationContext();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
database = new Optdb_interfce(context);
database.createNJTable(); //创建数据库表(表存在时不执行)
pd =RepairUtils.myShowDialog(context, 5);
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
ClientContext context;
String result = "";
try {
context = application.getClientContext();
if (null == context) {
context = ClientContext.getClientContext(Constants.SERVER_URL,Constants.ANONYMOUS, Constants.ANONYMOUS);
application.setClientContext(context);
}
IKindsOfCarManager manager = context.getManager(IKindsOfCarManager.class);
KindsOfCarBean[] kindsOfCarBeanArray = manager.getChildren(0L);//获取到农机大类
if(null!=kindsOfCarBeanArray&&kindsOfCarBeanArray.length>0){
for (int j = 0; j < kindsOfCarBeanArray.length; j++)
{
agriculturalBCategoryList.add(kindsOfCarBeanArray[j]); //将所有农机大类转存到List中
KindsOfCarBean[] mapsNJ_XL = manager.getChildren(kindsOfCarBeanArray[j].getCode());
for (int i = 0; i < mapsNJ_XL.length; i++)
{
if (null != mapsNJ_XL[i])
{
agriculturalSCategoryList.add(mapsNJ_XL[i]); //将所有农机小类转存到List中
KindsOfCarBean[] mapsNJ_PM = manager.getChildren(mapsNJ_XL[i].getCode());
for (int k = 0; k < mapsNJ_PM.length; k++)
{
if (null != mapsNJ_PM[k])
{
agriculturalPCategoryList.add(mapsNJ_PM[k]);//将所有农机品目转存到List中
}
}
}
}
}
}
}catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result=e.getMessage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
result="服务端异常,请稍后重试";
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
RepairUtils.myRemoveDialog(pd);
if("".equals(result)){
/**
* 返回结果是空字符串标示获取数据成功,就把农机类别数据保存到数据库中
*/
database.InsertToDb(Optdb_interfce.TABLE_NJLEIBIE,agriculturalBCategoryList);
database.InsertToDb(Optdb_interfce.TABLE_NJLEIBIE,agriculturalSCategoryList);
database.InsertToDb(Optdb_interfce.TABLE_NJLEIBIE,agriculturalPCategoryList);
/**
* 取出第一个农机大类对应的农机小类
* 在取出第一个农机小类对应的农机品目
*/
if(agriculturalBCategoryList.size()!=0){
agriculturalSCategoryList = database.getSmallKindsOfCarBeanInfo(MyState.CAR_SMALL_TYPE_MARK+"", agriculturalBCategoryList.get(0).getBigClassName());
if(agriculturalSCategoryList.size()!=0){
agriculturalPCategoryList = database.getSmallKindsOfCarBeanPMInfo(MyState.CAR_TYPE_MARK+"" ,agriculturalSCategoryList.get(0).getSmallClassName());
}
}
/**
* 将农机类别数据返回
*/
allTaskInterface.taskFinishReturn(agriculturalBCategoryList,agriculturalSCategoryList,agriculturalPCategoryList);
}else{
UIUtil.showMsg(context, result);
allTaskInterface.taskFinishReturn(null);
}
database.close_SqlDb();
}
}