package com.tofly.ghrq_route.module.plan.service;

import android.content.Context;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.mgq.base.utils.logger.LogRecorder;
import com.mgq.base.utils.string.StringUtil;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.base.url.NetRequestURL.newApi_dyrq;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.module.plan.PlanTool;
import com.tofly.ghrq_route.module.plan.controller.PlanCallBack;
import com.tofly.ghrq_route.module.plan.domain.ChildPlanCountBean;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.common.util.net.volley.JsonRequestConstant;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Amu on 2017/3/18.
 */

public class PlanServiceImpl implements IPlanService {
    private Context mContext;

    private DBDaoHelper dbDaoHelper;

    public PlanServiceImpl(Context context) {
        mContext = context;
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    @Override
    public void getPlanListMobile(final Context context, final String userid, final int state, final int period,
                                  final int planmode, final int pageindex, final int pagesize, final PlanCallBack planCallBack) {
        String url = newApi_dyrq.getPlanLisMobiletUrl() + "?current=" + pageindex + "&size=" + pagesize + "&userId=" + userid;
        if (state >=0 && state <= PlanBean.SELECT_STATE_ALL){
            if (state != PlanBean.SELECT_STATE_TODAY){
                String planState = state + "";
                if (state == PlanBean.SELECT_STATE_ALL){
                    planState = "1,2,3,4,5,6";
                } else if (state == PlanBean.SELECT_STATE_UNSTARTED) {
                    planState = "1,5";
                } else if (state == PlanBean.SELECT_STATE_ING) {
                    planState = "2,6";
                }
                if (period == 1) { //可以申请转移的计划
                    planState = "1,2";  //1：未开始 2：正在执行
                }
                url += "&childPlanState=" + planState;
            }else {
                String planState = "2,3,4,6";
                url += "&childPlanState=" + planState + "&isToday=true";
            }
        }
        if (period != -1){
            url += "&period_id=" + period;
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        String rowsArray = responseCode.queryResult("records");
                        List<PlanBean> planBeanList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        int total = 0;
                        String strTotal = responseCode.queryResult("total");
                        if (strTotal != null && !strTotal.isEmpty()) {
                            total = Integer.parseInt(strTotal);
                        }
                        if (rowsArray != null) {
                            try {
                                Gson gson = new Gson();
                                List<PlanBean> planBeens = gson.fromJson(rowsArray, new TypeToken<List<PlanBean>>(){}.getType());
                                if (planBeens != null) {
                                    planBeanList = planBeens;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;

                                    if (period == -1 && state != PlanBean.SELECT_STATE_UNSTARTED){
                                        savePlanListToday(userid, planBeanList);
                                    }else {
                                        updatePlanPercent2(planBeanList);
                                    }
                                }
                            } catch (Exception e) {
                                planCallBack.onGetPlanList(false, resCode, parseErrMsg, null, 0);
                            }
                        }
                        planCallBack.onGetPlanList(result, resCode, parseErrMsg, planBeanList, total);
                    } else {
                        planCallBack.onGetPlanList(
                                false, responseCode.getCode(), responseCode.getMessage(), null, 0);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            planCallBack.onGetPlanList(
                                    false, error.networkResponse.statusCode, errString, null, 0);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            planCallBack.onGetPlanList(
                                    false, error.networkResponse.statusCode, null, null, 0);
                        }
                    } else {
                        String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                        planCallBack.onGetPlanList(
                                false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() {
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    private void updatePlanPercent2(List<PlanBean> planBeans) {
        for (PlanBean planBean : planBeans) {
            planBean.setPercent2(planBean.getPercent1());
        }
    }

    private void savePlanListToday(String userid, List<PlanBean> planBeans) {
        if (planBeans != null) {
            if (planBeans.size() > 0) {
                List<String> serverIds = new ArrayList<>();
                for (PlanBean planBean : planBeans) {
                    planBean.setUser_id(userid);
                    PlanBean planBean1 = dbDaoHelper.getPlan(planBean.getPlan_id());
                    if (planBean1 == null){
                        dbDaoHelper.insertNewPlan(planBean);
                    }else {
                        String serverPercent = planBean.getPercent1();
                        String localPercent = planBean1.getPercent2();
                        if (serverPercent != null) {
                            if (serverPercent.equals("1") && !serverPercent.equals(localPercent)){
                                dbDaoHelper.insertOrUpdatePlan(planBean);
                                dbDaoHelper.setPlanOfflinePointOver(planBean.getPlan_id());
                            }
                        }
                        int serverPlanState = planBean.getPlan_state();
                        int localPlanState = planBean1.getPlan_state();
                        if (localPlanState != PlanBean.PlAN_STATE_TYPE.PLAN_STATE_OVER
                                && localPlanState != serverPlanState) {
                            planBean1.setPlan_state(serverPlanState);
                            dbDaoHelper.insertOrUpdatePlan(planBean1);
                        }
                    }
                    serverIds.add(planBean.getPlan_id());
                }
                PlanTool planTool = PlanTool.getInstance();
                List<String> localPlanIds = planTool.getLocalPlanIds();
                if (localPlanIds.size() != serverIds.size()) {
                    dbDaoHelper.deletePlansNotIn(serverIds);
                }
            }else {
                dbDaoHelper.deleteAllPlans(userid);
            }
        }
    }

    @Override
    public void getPlanList(final String token, final String userid, final int state, final int period, final int planmode, final int pageindex, final int pagesize, final PlanCallBack planCallBack) {
        String url = newApi_dyrq.getPlanListUrl() + "?current=" + pageindex + "&size=" + pagesize + "&userId=" + userid;
        if (state >=0 && state <= PlanBean.SELECT_STATE_ALL){
            if (state != PlanBean.SELECT_STATE_TODAY){
                String planState = state + "";
                if (state == PlanBean.SELECT_STATE_ALL){
                    planState = "1,2,3,4,5,6";
                } else if (state == PlanBean.SELECT_STATE_UNSTARTED) {
                    planState = "1,5";
                } else if (state == PlanBean.SELECT_STATE_ING) {
                    planState = "2,6";
                }
                if (period == 1) { //可以申请转移的计划
                    planState = "1,2";  //1：未开始 2：正在执行
                }
                url += "&childPlanState=" + planState;
            }else {
                String planState = "2,3,4,6";
                url += "&childPlanState=" + planState + "&isToday=true";
            }
        }
        if (period != -1){
            url += "&period_id=" + period;
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
            URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
            if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                String rowsArray = responseCode.queryResult("records");
                List<PlanBean> planBeanList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                int total = 0;
                String strTotal = responseCode.queryResult("total");
                if (strTotal != null && !strTotal.isEmpty()) {
                    total = Integer.parseInt(strTotal);
                }
                if (rowsArray != null) {
                    try {
                        Gson gson = new Gson();
                        List<PlanBean> planBeens = gson.fromJson(rowsArray, new TypeToken<List<PlanBean>>(){}.getType());
                        if (planBeens != null) {
                            planBeanList = planBeens;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;

                            if (period == -1 && state != PlanBean.SELECT_STATE_UNSTARTED){
                                savePlanListToday(userid, planBeanList);
                            } else {
                                updatePlanPercent2(planBeanList);
                            }
                        }
                    } catch (Exception e) {
                        planCallBack.onGetPlanList(false, resCode, parseErrMsg, null, 0);
                    }
                }
                planCallBack.onGetPlanList(result, resCode, parseErrMsg, planBeanList, total);
            } else {
                planCallBack.onGetPlanList(
                        false, responseCode.getCode(), responseCode.getMessage(), null, 0);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    planCallBack.onGetPlanList(
                            false, error.networkResponse.statusCode, errString, null, 0);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    planCallBack.onGetPlanList(
                            false, error.networkResponse.statusCode, null, null, 0);
                }
            } else {
                String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                planCallBack.onGetPlanList(
                        false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getPlanInfo(final String token, String childplanid, final PlanCallBack planCallBack) {
        String url = newApi_dyrq.getPlanInfoUrl() + childplanid;
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        String rowsArray = responseCode.getResult();
                        PlanBean planBean = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null) {
                            try {
                                Gson gson = new Gson();
                                PlanBean plan = gson.fromJson(rowsArray, PlanBean.class);
                                if (plan != null) {
                                    planBean = plan;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            } catch (Exception e) {
                                planCallBack.onGetPlanInfo(false, resCode, parseErrMsg, null);
                            }
                        }
                        planCallBack.onGetPlanInfo(result, resCode, parseErrMsg, planBean);
                    } else {
                        planCallBack.onGetPlanInfo(
                                false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            planCallBack.onGetPlanInfo(
                                    false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            planCallBack.onGetPlanInfo(
                                    false, error.networkResponse.statusCode, null, null);
                        }
                    } else {
                        String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                        planCallBack.onGetPlanInfo(
                                false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }) {

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getTodayPlanCount(Context context, final String userid, final int plan_mode, final List<String> localPlanIds, final PlanCallBack planCallBack) {
        //Http post请求,获取隐患列表
        String url = newApi_dyrq.getPlanPointCountUrl();
        if (plan_mode != -1){
            url += "?planMode=" + plan_mode;
        }
/*        if (localPlanIds != null && localPlanIds.size() > 0){
            String strPlanIds = StringUtil.listToString(localPlanIds, ',');
            url += "&planChildIds=" + strPlanIds;
        }*/
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    //日志记录
                    String logMsg = String.format("获取服务器计划个数 结果：%s \n", response);
                    LogRecorder.writeDeviceToolLog(mContext, logMsg, userid);

                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        int total = 0;
                        List<ChildPlanCountBean> listPlanId = null;
                        String strPlanIds = responseCode.getResult();
                        if (strPlanIds != null) {
                            try {
                                Gson gson = new Gson();
                                List<ChildPlanCountBean> planIds = gson.fromJson(strPlanIds, new TypeToken<List<ChildPlanCountBean>>(){}.getType());
                                if (planIds != null) {
                                    listPlanId = planIds;
                                    total = listPlanId.size();
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            } catch (Exception e) {
                                planCallBack.onGetTodayPlanCount(false, resCode, parseErrMsg, 0, null);
                            }
                        }
                        planCallBack.onGetTodayPlanCount(result, resCode, parseErrMsg, total, listPlanId);
                    } else {
                        planCallBack.onGetTodayPlanCount(false, responseCode.getCode(), responseCode.getMessage(), 0, null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            planCallBack.onGetTodayPlanCount(
                                    false, error.networkResponse.statusCode, errString, 0, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            planCallBack.onGetTodayPlanCount(
                                    false, error.networkResponse.statusCode, null, 0, null);
                        }
                    } else {
                        String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                        planCallBack.onGetTodayPlanCount(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, 0, null);
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void transferPlanApply(Context context, final List<String> childplanids, final String note, final PlanCallBack planCallBack) {
        String url = newApi_dyrq.getTransferPlanApplyUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        planCallBack.onTransferPlanApply(true, responseCode.getCode(), responseCode.getMessage());
                    } else {
                        planCallBack.onTransferPlanApply(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    planCallBack.onTransferPlanApply(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    planCallBack.onTransferPlanApply(false, error.networkResponse.statusCode, null);
                }
            } else {
                String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                planCallBack.onTransferPlanApply(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }) {

            @Override
            public byte[] getBody() {
                JsonObject root = new JsonObject();
                if (childplanids != null && childplanids.size() > 0){
                    String strPlanIds = StringUtil.listToString(childplanids, ',');
                    root.addProperty("ids", strPlanIds);
                }
                if (note != null && !note.isEmpty()) {
                    root.addProperty("applyNote", note);
                }
                Gson gson = new Gson();
                String transferApply = gson.toJson(root);
                return transferApply.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getPlanPointList(final String token, String planid, final int pageindex, final int pagesize, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getPlanPointListUrl() + planid;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    String rowsArray = responseCode.getResult();
                    List<OfflinePathBean> planPointBeanList = null;
                    int resCode = -1;
                    boolean result = false;
                    String parseErrMsg = "用户解析失败";
                    if (rowsArray != null) {
                        try {
                            Gson gson = new Gson();
                            List<OfflinePathBean> planBeens = gson.fromJson(rowsArray, new TypeToken<List<OfflinePathBean>>(){}.getType());
                            if (planBeens != null && planBeens.size() >= 0) {
                                planPointBeanList =  planBeens;
                                resCode = responseCode.getCode();
                                parseErrMsg = responseCode.getMessage();
                                result = true;
                            }
                        } catch (Exception e) {
                            planCallBack.onGetPlanPointList(false, resCode, parseErrMsg, null);
                        }
                    }
                    planCallBack.onGetPlanPointList(result, resCode, parseErrMsg, planPointBeanList);
                } else {
                    planCallBack.onGetPlanPointList(
                            false, responseCode.getCode(), responseCode.getMessage(), null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onGetPlanPointList(
                                false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onGetPlanPointList(
                                false, error.networkResponse.statusCode, null, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onGetPlanPointList(
                            false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("pageindex", pageindex + "");
                params.put("pagesize", pagesize + "");
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getChildPlanList(final String token, final String planid, final int pageindex, final int pagesize, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getChildPlanListUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    String rowsArray = responseCode.queryResult("rows");
                    List<PlanBean> planBeanList = null;
                    int resCode = -1;
                    boolean result = false;
                    String parseErrMsg = "用户解析失败";
                    int total = 0;
                    String strTotal = responseCode.queryResult("total");
                    if (strTotal != null && !strTotal.isEmpty()) {
                        total = Integer.parseInt(strTotal);
                    }
                    if (rowsArray != null) {
                        try {
                            Gson gson = new Gson();
                            List<PlanBean> planBeens = gson.fromJson(rowsArray, new TypeToken<List<PlanBean>>() {
                            }.getType());
                            if (planBeens != null && planBeens.size() >= 0) {
                                planBeanList = planBeens;
                                resCode = responseCode.getCode();
                                parseErrMsg = responseCode.getMessage();
                                result = true;
                            }
                        } catch (Exception e) {
                            planCallBack.onGetChildPlanList(false, resCode, parseErrMsg, null, 0);
                        }
                    }
                    planCallBack.onGetChildPlanList(result, resCode, parseErrMsg, planBeanList, total);
                } else {
                    planCallBack.onGetChildPlanList(
                            false, responseCode.getCode(), responseCode.getMessage(), null, 0);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onGetChildPlanList(false, error.networkResponse.statusCode, errString, null, 0);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onGetChildPlanList(false, error.networkResponse.statusCode, null, null, 0);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onGetChildPlanList(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("pageindex", pageindex + "");
                params.put("pagesize", pagesize + "");
                params.put("plan_id", planid);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getChildPlanPointList(final String token, String childid, final int pageindex, final int pagesize, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getChildPlanPointListUrl() + childid;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        String rowsArray = responseCode.getResult();
                        List<OfflinePathBean> planPointBeanList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null) {
                            try {
                                Gson gson = new Gson();
                                List<OfflinePathBean> planBeens = gson.fromJson(rowsArray, new TypeToken<List<OfflinePathBean>>(){}.getType());
                                if (planBeens != null && planBeens.size() >= 0) {
                                    planPointBeanList = planBeens;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            } catch (Exception e) {
                                planCallBack.onGetChildPlanPointList(false, resCode, parseErrMsg, null);
                            }
                        }
                        planCallBack.onGetChildPlanPointList(result, resCode, parseErrMsg, planPointBeanList);
                    } else {
                        planCallBack.onGetChildPlanPointList(
                                false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            planCallBack.onGetChildPlanPointList(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            planCallBack.onGetChildPlanPointList(false, error.networkResponse.statusCode, null, null);
                        }
                    } else {
                        String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                        planCallBack.onGetChildPlanPointList(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<>();
                params.put("pageindex", pageindex + "");
                params.put("pagesize", pagesize + "");
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void getChildPlanInfo(final String token, String childplanid, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getChildPlanInfoUrl() + childplanid;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    String rowsArray = responseCode.getResult();
                    PlanBean childPlanBean = null;
                    int resCode = -1;
                    boolean result = false;
                    String parseErrMsg = "用户解析失败";
                    if (rowsArray != null) {
                        try {
                            Gson gson = new Gson();
                            PlanBean childPlan = gson.fromJson(rowsArray, PlanBean.class);
                            if (childPlan != null) {
                                childPlanBean = childPlan;
                                resCode = responseCode.getCode();
                                parseErrMsg = responseCode.getMessage();
                                result = true;
                            }
                        } catch (Exception e) {
                            planCallBack.onGetChildPlanInfo(false, resCode, parseErrMsg, null);
                        }
                    }
                    planCallBack.onGetChildPlanInfo(result, resCode, parseErrMsg, childPlanBean);
                } else {
                    planCallBack.onGetChildPlanInfo(false, responseCode.getCode(), responseCode.getMessage(), null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onGetChildPlanInfo(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onGetChildPlanInfo(false, error.networkResponse.statusCode, null, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onGetChildPlanInfo(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getOfflinePathList(final String token, final String userid, final String planIds, final PlanCallBack planCallBack) {
        /*日志记录*/
        String logMsg = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; ------------------\n", userid, planIds);
        LogRecorder.writeDeviceToolLog(mContext, logMsg, userid);

        String url = newApi_dyrq.getOfflinePointListUrl();
        if (planIds != null && !planIds.isEmpty()){
            url += "?planChildIds=" + planIds;
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    boolean isSuccess = false;
                    String  errmsg = "";
                    int resNum = 0;
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        String rowsArray = responseCode.getResult();
                        List<OfflinePathBean> offlinePathList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null) {
                            try {
                                Gson gson = new Gson();
                                List<OfflinePathBean> offlinePlans = gson.fromJson(rowsArray, new TypeToken<List<OfflinePathBean>>(){}.getType());
                                if (offlinePlans != null && offlinePlans.size() >= 0) {
                                    offlinePathList = offlinePlans;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                    resNum = offlinePlans.size();
                                }
                            } catch (Exception e) {
                                planCallBack.onGetOfflinePathList(false, resCode, parseErrMsg, null);
                                String logMsg1 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; 返回失败：%s  \n ", userid, planIds, parseErrMsg);
                                LogRecorder.writeDeviceToolLog(mContext, logMsg1, userid);
                            }
                        }
                        isSuccess = result;
                        errmsg =  parseErrMsg;
                        planCallBack.onGetOfflinePathList(result, resCode, parseErrMsg, offlinePathList);
                    } else {
                        isSuccess = false;
                        errmsg = responseCode.getMessage();
                        planCallBack.onGetOfflinePathList(
                                false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                    String logMsg1 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; \n  返回结果 -- 成功: %s, 个数：%d, 提示：%s \n", userid, planIds, Boolean.toString(isSuccess), resNum, errmsg);
                    LogRecorder.writeDeviceToolLog(mContext, logMsg1, userid);
                }, error -> {
                    String errmsg = "";
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            errmsg = errString;
                            planCallBack.onGetOfflinePathList(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            errmsg = "UnsupportedEncodingException";
                            planCallBack.onGetOfflinePathList(false, error.networkResponse.statusCode, null, null);
                        }
                    } else {
                        String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                        errmsg = errString;
                        planCallBack.onGetOfflinePathList(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                    String logMsg12 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; \n 返回结果 -- 成功: %s, 个数：%d, 提示：%s \n ", userid, planIds, Boolean.toString(false), 0, errmsg);
                    LogRecorder.writeDeviceToolLog(mContext, logMsg12, userid);
                }) {

            @Override
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<>();
                if (planIds != null && !planIds.isEmpty()){
                    params.put("planId", planIds);
                };
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS * 10,
                BaseApp.DEFAULT_FILE_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void getOfflinePathListByPage(final String token, final String userid, final String planId, int pageindex, int pagesize, final PlanCallBack planCallBack) {
        /*日志记录*/
        String logMsg = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; ------------------\n", userid, planId);
        LogRecorder.writeDeviceToolLog(mContext, logMsg, userid);

        String url = newApi_dyrq.getOfflinePointListByPageUrl();
        if (planId != null && !planId.isEmpty()){
            url += "?planChildIds=" + planId  + "&current=" + pageindex + "&size=" + pagesize;
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET, url, response -> {
            boolean isSuccess = false;
            String  errmsg = "";
            int resNum = 0;
            URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
            if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                String rowsArray = responseCode.queryResult("records");
                List<OfflinePathBean> offlinePathList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                if (rowsArray != null) {
                    try {
                        Gson gson = new Gson();
                        List<OfflinePathBean> offlinePlans = gson.fromJson(rowsArray, new TypeToken<List<OfflinePathBean>>(){}.getType());
                        if (offlinePlans != null && offlinePlans.size() >= 0) {
                            offlinePathList = offlinePlans;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                            resNum = offlinePlans.size();
                        }
                    } catch (Exception e) {
                        planCallBack.onGetOfflinePathList(false, resCode, parseErrMsg, null);
                        String logMsg1 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; 返回失败：%s  \n ", userid, planId, parseErrMsg);
                        LogRecorder.writeDeviceToolLog(mContext, logMsg1, userid);
                    }
                }
                isSuccess = result;
                errmsg =  parseErrMsg;
                planCallBack.onGetOfflinePathList(result, resCode, parseErrMsg, offlinePathList);
            } else {
                isSuccess = false;
                errmsg = responseCode.getMessage();
                planCallBack.onGetOfflinePathList(
                        false, responseCode.getCode(), responseCode.getMessage(), null);
            }
            String logMsg1 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; \n  返回结果 -- 成功: %s, 个数：%d, 提示：%s \n", userid, planId, Boolean.toString(isSuccess), resNum, errmsg);
            LogRecorder.writeDeviceToolLog(mContext, logMsg1, userid);
        }, error -> {
            String errmsg = "";
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    errmsg = errString;
                    planCallBack.onGetOfflinePathList(false, error.networkResponse.statusCode, errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    errmsg = "UnsupportedEncodingException";
                    planCallBack.onGetOfflinePathList(false, error.networkResponse.statusCode, null, null);
                }
            } else {
                String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                errmsg = errString;
                planCallBack.onGetOfflinePathList(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
            String logMsg12 = String.format("服务器下载离线点数据 输入参数-- 用户：%s 计划id: %s; \n 返回结果 -- 成功: %s, 个数：%d, 提示：%s \n ", userid, planId, Boolean.toString(false), 0, errmsg);
            LogRecorder.writeDeviceToolLog(mContext, logMsg12, userid);
        }) {

            @Override
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<>();
                if (planId != null && !planId.isEmpty()){
                    params.put("planId", planId);
                };
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS * 10,
                BaseApp.DEFAULT_FILE_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void updatePathOver(final String token, final List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getUpdatePathOverUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    List<String> transferIdList = null;
                    String rowsArray = responseCode.getResult();
                    if (rowsArray != null){
                        try {
                            Gson gson = new Gson();
                            List<String> transferIds = gson.fromJson(rowsArray, new TypeToken<List<String>>(){}.getType());
                            if (transferIds != null && transferIds.size() >= 0){
                                transferIdList = transferIds;
                            }
                        }catch (Exception e){
                        }
                    }
                    planCallBack.onUpdatePathOver(true, responseCode.getCode(), responseCode.getMessage(), transferIdList);
                } else {
                    planCallBack.onUpdatePathOver(false, responseCode.getCode(), responseCode.getMessage(), null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onUpdatePathOver(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onUpdatePathOver(false, error.networkResponse.statusCode, null, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onUpdatePathOver(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }) {
            @Override
            public byte[] getBody() {
                Gson gson = new Gson();
                String json = gson.toJson(pathBeanList);
                return json.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void updatePointOver(final String token, final List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack) {
        String url = newApi_dyrq.getUpdatePointOverUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                        List<String> transferIdList = null;
                        String rowsArray = responseCode.getResult();
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<String> transferIds = gson.fromJson(rowsArray, new TypeToken<List<String>>(){}.getType());
                                if (transferIds != null && transferIds.size() >= 0){
                                    transferIdList = transferIds;
                                }
                            }catch (Exception e){
                            }
                        }
                        planCallBack.onUpdatePointOver(true, responseCode.getCode(), responseCode.getMessage(), transferIdList);
                    } else {
                        planCallBack.onUpdatePointOver(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onUpdatePointOver(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onUpdatePointOver(false, error.networkResponse.statusCode, null, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onUpdatePointOver(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }) {
            @Override
            public byte[] getBody() {
                JsonParser parser = new JsonParser();

                Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
                String json = gson.toJson(pathBeanList);
                JsonArray array = parser.parse(json).getAsJsonArray();

                JsonObject root = new JsonObject();
                root.add("updatePointList", array);

                gson = new Gson();
                String pointList = gson.toJson(root);
                return pointList.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    /**
     *  更新当天巡检线的到位点第一次离开范围的时间
     */
    public void updatePathOffTime(final String token, final List<OfflinePathBean> pathBeanList, final PlanCallBack planCallBack){
        String url = NetRequestURL.getUpdatePathOfftimeUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    planCallBack.onUpdatePathOffTime(true, responseCode.getCode(), responseCode.getMessage());
                } else {
                    planCallBack.onUpdatePathOffTime(false, responseCode.getCode(), responseCode.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onUpdatePathOffTime(false, error.networkResponse.statusCode, errString);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onUpdatePathOffTime(false, error.networkResponse.statusCode, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onUpdatePathOffTime(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString);
                }
            }
        }) {
            @Override
            public byte[] getBody() {
                Gson gson = new Gson();
                String json = gson.toJson(pathBeanList);
                return json.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void applyRecalculatePlanPercent(final String token, final String childPlanId, final String date, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getApplyRecalculateplanUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                    planCallBack.onApplyRecalculatePlanPercent(true, responseCode.getCode(), responseCode.getMessage());
                }else {
                    planCallBack.onApplyRecalculatePlanPercent(false, responseCode.getCode(), responseCode.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onApplyRecalculatePlanPercent(false, error.networkResponse.statusCode, errString);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onApplyRecalculatePlanPercent(false, error.networkResponse.statusCode, null);
                    }
                }else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onApplyRecalculatePlanPercent(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString);
                }
            }
        }){
            @Override
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<>();
                params.put("plan_id", childPlanId);
                if (date != null && !date.isEmpty())
                    params.put("search_date", date);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getRecalculatePlanPoints(final String token, String childPlanId, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getRecalculatePointsUrl() + childPlanId;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    List<String> pointIds = null;
                    String rowsArray = responseCode.queryResult("point_ids");
                    if (rowsArray != null){
                        try {
                            Gson gson = new Gson();
                            List<String> ids = gson.fromJson(rowsArray, new TypeToken<List<String>>(){}.getType());
                            if (ids != null && ids.size() >= 0){
                                pointIds = ids;
                            }
                        }catch (Exception e){
                            planCallBack.onGetRecalculatePlanPoints(false, responseCode.getCode(), responseCode.getMessage(), null);
                        }
                    }
                    planCallBack.onGetRecalculatePlanPoints(true, responseCode.getCode(), responseCode.getMessage(), pointIds);
                } else {
                    planCallBack.onGetRecalculatePlanPoints(false, responseCode.getCode(), responseCode.getMessage(), null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onGetRecalculatePlanPoints(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onGetRecalculatePlanPoints(false, error.networkResponse.statusCode, null, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onGetRecalculatePlanPoints(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    @Override
    public void updateRecalculatePlanState(final String token, String childPlanId, final PlanCallBack planCallBack) {
        String url = NetRequestURL.getUpdateRecalculateStateUrl() + childPlanId;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                    planCallBack.onUpdateRecalculatePlanState(true, responseCode.getCode(), responseCode.getMessage());
                } else {
                    planCallBack.onUpdateRecalculatePlanState(false, responseCode.getCode(), responseCode.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        planCallBack.onUpdateRecalculatePlanState(false, error.networkResponse.statusCode, errString);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        planCallBack.onUpdateRecalculatePlanState(false, error.networkResponse.statusCode, null);
                    }
                } else {
                    String errString = URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT);
                    planCallBack.onUpdateRecalculatePlanState(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT, errString);
                }
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest, BaseApp.UPLOAD_FILE_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }
}
