package cn.flightfeather.thirdapp.task; import android.os.AsyncTask; import android.os.Handler; import android.widget.Toast; import com.ping.greendao.gen.ChangeAdviceDao; import com.ping.greendao.gen.ChangeEffectDao; import com.ping.greendao.gen.CityDao; import com.ping.greendao.gen.DaoSession; import com.ping.greendao.gen.DistrictDao; import com.ping.greendao.gen.DomaincatalogDao; import com.ping.greendao.gen.DomainitemDao; import com.ping.greendao.gen.EvaluationruleDao; import com.ping.greendao.gen.EvaluationsubruleDao; import com.ping.greendao.gen.GittypeDao; import com.ping.greendao.gen.ProblemtypeDao; import com.ping.greendao.gen.ProvinceDao; import com.ping.greendao.gen.ScenseDao; import com.ping.greendao.gen.SiteDao; import com.ping.greendao.gen.TownDao; import java.util.List; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.activity.SplashActivity; import cn.flightfeather.thirdapp.bean.entity.ChangeAdvice; import cn.flightfeather.thirdapp.bean.entity.ChangeEffect; import cn.flightfeather.thirdapp.bean.entity.City; import cn.flightfeather.thirdapp.bean.entity.District; import cn.flightfeather.thirdapp.bean.entity.Domaincatalog; import cn.flightfeather.thirdapp.bean.entity.Domainitem; import cn.flightfeather.thirdapp.bean.entity.Evaluationrule; import cn.flightfeather.thirdapp.bean.entity.Evaluationsubrule; import cn.flightfeather.thirdapp.bean.entity.Gittype; import cn.flightfeather.thirdapp.bean.entity.Problemtype; import cn.flightfeather.thirdapp.bean.entity.Province; import cn.flightfeather.thirdapp.bean.entity.Scense; import cn.flightfeather.thirdapp.bean.entity.Site; import cn.flightfeather.thirdapp.bean.entity.Town; import cn.flightfeather.thirdapp.httpservice.InitDataService; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; /** * Created by note_ff_1602 on 2018/1/16. * 初始数据用的task */ public class InitDataTask extends AsyncTask { private Retrofit retrofit; private CommonApplication application; private DaoSession daoSession; private ScenseDao scenseDao; private SiteDao siteDao; private DomaincatalogDao domaincatalogDao; private DomainitemDao domainitemDao; private EvaluationruleDao evaluationruleDao; private EvaluationsubruleDao evaluationsubruleDao; private ProblemtypeDao problemtypeDao; private GittypeDao gittypeDao; private ProvinceDao provinceDao; private CityDao cityDao; private DistrictDao districtDao; private TownDao townDao; private ChangeAdviceDao changeAdviceDao; private ChangeEffectDao changeEffectDao; private InitDataService initDataService; private boolean requestAgain = false; private boolean firstLoad =true; private Handler handler; public InitDataTask(CommonApplication application,Handler handler) { this.application = application; this.handler= handler; } @Override protected void onPreExecute() { super.onPreExecute(); retrofit = application.getRetrofit(); initDataService = retrofit.create(InitDataService.class); daoSession = application.getDaoSession(); scenseDao = daoSession.getScenseDao(); siteDao = daoSession.getSiteDao(); domaincatalogDao = daoSession.getDomaincatalogDao(); domainitemDao = daoSession.getDomainitemDao(); evaluationruleDao = daoSession.getEvaluationruleDao(); evaluationsubruleDao = daoSession.getEvaluationsubruleDao(); problemtypeDao = daoSession.getProblemtypeDao(); gittypeDao = daoSession.getGittypeDao(); provinceDao = daoSession.getProvinceDao(); cityDao = daoSession.getCityDao(); districtDao = daoSession.getDistrictDao(); townDao = daoSession.getTownDao(); changeAdviceDao = daoSession.getChangeAdviceDao(); changeEffectDao = daoSession.getChangeEffectDao(); } @Override protected Object doInBackground(Object[] objects) { loadScense(); return null; } //获取场景数据 private void loadScense(){ Call> loadAllScense = initDataService.loadAllScense(); loadAllScense.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List scenseList = response.body(); scenseDao.deleteAll(); scenseDao.insertInTx(scenseList); System.out.println("Scense:Success"); if (firstLoad){ loadSite(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadScense(); }else { handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); Toast.makeText(application, "获取场景数据失败", Toast.LENGTH_SHORT).show(); } System.out.println("Scense:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadScense(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Scense:"+t.toString()); } }); } //加载全部工地详情 private void loadSite(){ Call> loadAllSite = initDataService.loadAllSite(); loadAllSite.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List siteList = response.body(); siteDao.deleteAll(); siteDao.insertInTx(siteList); System.out.println("Site:Success"); if (firstLoad){ loadDoainCatalog(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadSite(); }else { handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); Toast.makeText(application, "获取工地详情数据失败", Toast.LENGTH_SHORT).show(); } System.out.println("Site:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadSite(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Site:"+t.toString()); } }); } //加载全部值域类型 private void loadDoainCatalog(){ Call> loadAllDomainCatalog = initDataService.loadAllDomainCatalog(); loadAllDomainCatalog.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List domaincatalogList = response.body(); domaincatalogDao.deleteAll(); domaincatalogDao.insertInTx(domaincatalogList); System.out.println("DomainCatalog:Success"); if (firstLoad){ loadDomainItem(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadDoainCatalog(); }else { handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); Toast.makeText(application, "获取值域类型失败", Toast.LENGTH_SHORT).show(); } System.out.println("DomainCatalog:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadDoainCatalog(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("DomainCatalog:"+t.toString()); } }); } //获取全部值域值 private void loadDomainItem(){ Call> loadAllDomainItem = initDataService.loadAllDomainItem(); loadAllDomainItem.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List domainItemList = response.body(); domainitemDao.deleteAll(); domainitemDao.insertInTx(domainItemList); System.out.println("DomainItem:Success"); if (firstLoad){ loadEvaluationrule(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadDomainItem(); }else { Toast.makeText(application, "获取值域值失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("DomainItem:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadDomainItem(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("DomainItem:"+t.toString()); } }); } //获得全部评分表 private void loadEvaluationrule(){ Call> loadAllEvaluationRule = initDataService.loadAllEvaluationRule(); loadAllEvaluationRule.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List evaluationRuleList = response.body(); evaluationruleDao.deleteAll(); evaluationruleDao.insertInTx(evaluationRuleList); System.out.println("EvaluationRule:Success"); if (firstLoad){ loadEvaluationsubrule(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain =true; loadEvaluationrule(); }else { Toast.makeText(application, "获取评分表失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("EvaluationRule:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadEvaluationrule(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("EvaluationRule"+t.toString()); } }); } //获取全部评分子项 private void loadEvaluationsubrule(){ Call> loadAllEvaluationSubRule = initDataService.loadAllEvaluationSubRule(); loadAllEvaluationSubRule.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List evualationSubRuleList = response.body(); evaluationsubruleDao.deleteAll(); evaluationsubruleDao.insertInTx(evualationSubRuleList); System.out.println("Evaluationsubrule:Success"); if (firstLoad){ loadProblemtype(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadEvaluationsubrule(); }else { Toast.makeText(application, "获取评分子项失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Evaluationsubrule:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadEvaluationsubrule(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Evaluationsubrule"+t.toString()); } }); } //加载全部问题 private void loadProblemtype(){ Call> loadAllProblemType = initDataService.loadAllProblemType(); loadAllProblemType.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List problemTypeList = response.body(); problemtypeDao.deleteAll(); problemtypeDao.insertInTx(problemTypeList); System.out.println("ProblemType:Success"); if (firstLoad){ loadGittype(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadProblemtype(); }else { Toast.makeText(application, "获取问题失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("ProblemType:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadProblemtype(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("ProblemType:"+t.toString()); } }); } //加载技防措施 private void loadGittype(){ Call> loadAllGitType = initDataService.loadAllGitType(); loadAllGitType.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List gitTypeList = response.body(); gittypeDao.deleteAll(); gittypeDao.insertInTx(gitTypeList); System.out.println("GitType:Success"); if (firstLoad){ loadProvince(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadGittype(); }else { Toast.makeText(application, "获取技防措施失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("GitType:"+response.errorBody()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadGittype(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("GitType:"+t.toString()); } }); } //加载全部省份 private void loadProvince(){ Call> loadAllProvince = initDataService.loadAllProvince(); loadAllProvince.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List provinceList = response.body(); provinceDao.deleteAll(); provinceDao.insertInTx(provinceList); System.out.println("Province:Success"); if (firstLoad){ loadCity(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadProvince(); }else { Toast.makeText(application, "获取省份失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("province:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadProvince(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Province:"+t.toString()); } }); } //加载全部城市 private void loadCity(){ Call> loadAllCity = initDataService.loadAllCity(); loadAllCity.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List cityList = response.body(); cityDao.deleteAll(); cityDao.insertInTx(cityList); System.out.println("City:Success"); if (firstLoad){ loadDistrict(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadCity(); }else { Toast.makeText(application, "获取城市失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("city:"+response.errorBody().toString()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadCity(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("City:"+t.toString()); } }); } //加载全部区县 private void loadDistrict(){ Call> loadAllDistrict = initDataService.loadAllDistrict(); loadAllDistrict.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List districtList = response.body(); districtDao.deleteAll(); districtDao.insertInTx(districtList); System.out.println("District:Success"); if (firstLoad){ loadTown(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadDistrict(); }else { Toast.makeText(application, "获取区县失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("District:"+response.errorBody()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadDistrict(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("District:"+t.toString()); } }); } //加载全部街镇 private void loadTown(){ Call> loadAllTown = initDataService.loadAllTown(); loadAllTown.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body()!=null){ requestAgain = false; List townList = response.body(); townDao.deleteAll(); townDao.insertInTx(townList); System.out.println("Town:Success"); if (firstLoad){ loadChangeAdvice(); } }else if (response.errorBody()!=null){ if (!requestAgain){ requestAgain = true; loadTown(); }else { Toast.makeText(application, "获取城市失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Town:"+response.errorBody()); } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain){ requestAgain = true; loadTown(); }else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } System.out.println("Town:"+t.toString()); } }); } //加载全部整改建议 private void loadChangeAdvice() { Call> loadAllChangeAdvice = initDataService.loadAllChangeAdvice(); loadAllChangeAdvice.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body() != null) { requestAgain = false; List changeAdviceList = response.body(); changeAdviceDao.deleteAll(); changeAdviceDao.insertInTx(changeAdviceList); System.out.println("ChangeAdvice:Success"); if (firstLoad) { loadChangeEffect(); } } else if (response.errorBody() != null) { if (!requestAgain) { requestAgain = true; loadChangeAdvice(); } else { Toast.makeText(application, "获取整改建议失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain) { requestAgain = true; loadChangeAdvice(); } else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } } }); } //加载全部整改效果 private void loadChangeEffect() { Call> loadAllChangeEffect = initDataService.loadAllChangeEffect(); loadAllChangeEffect.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body() != null) { requestAgain = false; List changeEffectList = response.body(); changeEffectDao.deleteAll(); changeEffectDao.insertInTx(changeEffectList); System.out.println("ChangeEffect:Success"); if (firstLoad) { handler.sendEmptyMessage(SplashActivity.START_LOGIN); } else if (response.errorBody() != null) { if (!requestAgain) { requestAgain = true; loadChangeEffect(); } else { Toast.makeText(application, "获取整改承诺失败", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } } } } @Override public void onFailure(Call> call, Throwable t) { if (!requestAgain) { requestAgain = true; loadChangeEffect(); } else { showNetErrorToast(); handler.sendEmptyMessage(SplashActivity.SHOW_ERROR); } } }); } private void showNetErrorToast(){ Toast.makeText(application, "网络连接失败", Toast.LENGTH_SHORT).show(); } }