package com.jiuqi.njt.service;
import java.util.Date;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.jiuqi.mobile.nigo.comeclose.bean.app.gps.RegularReportBean;
import com.jiuqi.mobile.nigo.comeclose.manager.app.IRegularReportManger;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.register.AllTaskInterface;
import com.jiuqi.njt.register.city.TaskReturnResult;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.login.AutoLoginUtil;
import com.jqyd.android.module.lbs.util.JqydDateUtil;
import com.jqyd.android.module.lbs.util.Optsharepre_interface;
import com.jqyd.android.module.lbs.util.WriteFile;
/**
* 获取连续上报政策异步类
*
* @author joe
*
*/
public class GetPlanTask extends AsyncTask<Void, Void, Void> {
private Context context;
private String TAG = this.getClass().getName();
private String log = "";
private TaskReturnResult taskReturnResult = new TaskReturnResult();
private MyApp application;
private boolean needRepeat;
private AllTaskInterface allTaskInterface;
private RegularReportBean reportBean;
private WriteFile writeFile = new WriteFile(TAG+ JqydDateUtil.getDateDayOne(new Date()));
private OptsharepreInterface sharePre;
private Optsharepre_interface share;
public GetPlanTask(Context context) {
super();
this.context = context;
application = (MyApp) context.getApplicationContext();
sharePre = new OptsharepreInterface(context);
share = new Optsharepre_interface(context);
}
public GetPlanTask(Context context, AllTaskInterface allTaskInterface) {
super();
this.context = context;
this.allTaskInterface = allTaskInterface;
application = (MyApp) context.getApplicationContext();
sharePre = new OptsharepreInterface(context);
share = new Optsharepre_interface(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
ClientContext clientContext = application.getClientContext();
if (null == clientContext) {
clientContext = ClientContext.getClientContext(
Constants.SERVER_URL, Constants.ANONYMOUS,
Constants.ANONYMOUS);
application.setClientContext(clientContext);
}
if (application.getIsLogin()) {
IRegularReportManger manager = clientContext
.getManager(IRegularReportManger.class);
reportBean = manager.getByAccount(clientContext.getUser()
.getAccount());
if(reportBean != null){
log = "获取连续上报信息成功: 帐号 " + reportBean.getAccount()
+ " 最后一次读取时间 " + reportBean.getLastReadTime()
+ " 上班时间 " + reportBean.getOnTime()
+ " 下班时间 " + reportBean.getOffTime()
+ " 间隔时间 "
+ reportBean.getRegularInterval();
Log.i(TAG, log);
}
taskReturnResult.setSuccess(true);
boolean result = updatePlan(reportBean == null ? 2 : reportBean.getIsNull(), reportBean);
String timeTemp = (reportBean == null ? "" : reportBean.getCurrentTime());
updateServerTime(timeTemp);
}
} catch (Exception e) {
e.printStackTrace();
if (null != e.getMessage()) {
taskReturnResult.setMessage(e.getMessage());
} else {
taskReturnResult.setMessage("服务端异常,请稍后重试");
}
/**
* 如果是Session超时异常的话就重新获取一次Session
*/
// needRepeat = new AutoLoginUtil(context, e).getNewSession();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.e(TAG, taskReturnResult.getMessage());
if(null!=allTaskInterface){
allTaskInterface.taskFinishReturn(reportBean);
}
}
private boolean updatePlan(int type, RegularReportBean bean) {
boolean success = false;
if(bean == null || bean.getOnTimes()==null || bean.getOffTimes()==null){
sharePre.putPres("JGSJ", "60"); //服务端没有数据,就默认间隔时间是一个小时
return success;
}
String ontime = (String) bean.getOnTimes()[bean.getCurrentDayOfWeek()];
String offtime = (String)bean.getOffTimes()[bean.getCurrentDayOfWeek()];
switch (type) {
case 0:// 更新任务参数
share.editPres("SBSJ", ontime);
share.editPres("XBSJ", offtime);
share.editPres("JGSJ", String.valueOf(0==bean.getRegularInterval()?30:bean.getRegularInterval()));
// shareFile.editPres("JGSJ", String.valueOf(10));
share.editPres("RWZL", "1");
success = true;
log = "与服务器交互成功,更新终端保存的任务参数";
writeFile.writeToFile(log);
break;
case 1:// 删除终端数据
share.editPres("SBSJ", "7:00");
share.editPres("XBSJ", "19:00");
share.editPres("JGSJ", "12");
share.editPres("RWZL", "1");
success = true;
log = "执行删除终端保存的任务参数指令";
writeFile.writeToFile(log);
break;
case 2:
success = true;
log = "由于网络或服务器问题造成与后台交互失败,此时取昨天的任务指令执行";
// 采用手机端时间
log += ",手机端时间:" + JqydDateUtil.getHourMin(new Date());
writeFile.writeToFile(log);
break;
}
return success;
}
// 与服务器交互完毕后,更新当前的全局时间变量
private void updateServerTime(String timeTemp) {
if (!TextUtils.isEmpty(timeTemp) && timeTemp.contains(":")) {
String[] serverTim = timeTemp.split(":");
int currHour = 0;
int currMin = 0;
try {
currHour = Integer.parseInt(serverTim[0]);
currMin = Integer.parseInt(serverTim[1]);
} catch (NumberFormatException e) {
writeFile.writeToFile("与服务器交互完毕后,解析服务器端时间出现异常");
}
long currTime = currHour * 60 + currMin;
log = "与服务器任务交互完成后,记录从服务器端获取的时间:" + currHour + ":" + currMin;
Log.i(TAG, log);
writeFile.writeToFile(log);
}
}
}