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

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mgq.base.utils.string.StringUtil;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL.newApi_dyrq;
import com.tofly.ghrq_route.base.URLResponseCode.ErrorCode;
import com.tofly.ghrq_route.base.URLResponseCode.URLResponse;
import com.tofly.ghrq_route.module.emergencyHandle.controller.EmergencyCallBack;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.common.util.Location.LocationUtil;
import com.tofly.ghrq_route.common.util.net.volley.MultiPartStringRequest;
import com.tofly.ghrq_route.common.util.net.volley.MultipartRequestPost;

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

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

public class EmergencyServiceImpl implements IEmergencyService {

    @Override
    public void getEmergencyList(Context context, int select_state, int work_state, String keyword,
                                 final int pageindex, final int pagesize, final EmergencyCallBack emergencyCallBack) {
        //Http post请求,获取隐患列表
        String url = newApi_dyrq.getEmergencyList() + "?current=" + pageindex + "&size=" + pagesize + "&orders[0].asc=false&orders[0].column=create_time";
        String userid = BaseApp.getInstance().getUserId();
        if (userid != null && !userid.isEmpty()) {
            switch (select_state) {
                case 0: {
                    url += "&workOrderState=1" + "&dispatchUserId=" + userid;
                }break;
                case 1: {
                    url += "&workOrderState=7" + "&userIds=" + userid;
                }break;
                case 2: {
                    url += "&workOrderState=2" + "&dispatchDisposeUserId=" + userid;
                }break;
                case 3: {
                    url += "&workOrderState=3";
                }break;
                default: {
                    if (work_state > 0) {
                        url += "&workOrderState=" + work_state;
                    }
                    if (keyword != null && !keyword.isEmpty()) {
                        url += "&address=" + keyword;
                    }
                }break;
            }
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.queryResult("records");
                        List<EmergencyHandleData> emergencyList = 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<EmergencyHandleData> emergencies = gson.fromJson(rowsArray, new TypeToken<List<EmergencyHandleData>>(){}.getType());
                                if (emergencies != null && emergencies.size() >= 0){
                                    emergencyList = emergencies;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                    //saveTroubles(troubleBeanList);
                                }
                            }catch (Exception e){
                                emergencyCallBack.onGetDatas(false, resCode, parseErrMsg, null, 0);
                            }
                        }
                        emergencyCallBack.onGetDatas(result, resCode, parseErrMsg, emergencyList, total);
                    }else {
                        emergencyCallBack.onGetDatas(
                                false, responseCode.getCode(), responseCode.getMessage(), null, 0);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            emergencyCallBack.onGetDatas(
                                    false, error.networkResponse.statusCode, errString, null, 0);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            emergencyCallBack.onGetDatas(
                                    false, error.networkResponse.statusCode, null, null, 0);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        emergencyCallBack.onGetDatas(
                                false, ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };

        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getEmergencyInfo(Context context, String troubleid, final EmergencyCallBack emergencyCallBack) {
        //Http post请求,获取隐患列表
        String url = newApi_dyrq.getTroubleInfoUrl() + troubleid;
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                String rowsArray = responseCode.getResult();
                EmergencyHandleData emergencyData = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                if (rowsArray != null){
                    try {
                        Gson gson = new Gson();
                        EmergencyHandleData emergency = gson.fromJson(rowsArray, new TypeToken<EmergencyHandleData>(){}.getType());
                        if (emergency != null){
                            emergencyData = emergency;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                        }
                    }catch (Exception e){
                        emergencyCallBack.onGetData(false, resCode, parseErrMsg, null);
                    }
                }
                emergencyCallBack.onGetData(result, resCode, parseErrMsg, emergencyData);
            }else {
                emergencyCallBack.onGetData(
                        false, responseCode.getCode(), responseCode.getMessage(), null);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onGetData(
                            false, error.networkResponse.statusCode, errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onGetData(
                            false, error.networkResponse.statusCode, null, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onGetData(
                        false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void addEmergencyNew(Context context, EmergencyHandleData handleData, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, List<TroubleAudioBean> files, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyUploadUrl();
        MultiPartStringRequest postRequest = new MultiPartStringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                headerParams.put("Charset", "utf-8");
                return headerParams;
            }

            @Override
            public Map<String, String> getStringUploads() {
                Map<String, String> params = new HashMap<>();
                /*报警人姓名*/
                String name = handleData.getAlarmPeopleName();
                params.put("alarmPeopleName", name);
                /*联系电话*/
                String phone = handleData.getContactPhone();
                params.put("contactPhone", phone);
                /*应急上报详情*/
                String notes = handleData.getDescription();
                params.put("description", notes);
                /*隐患地址*/
                String lng = LocationUtil.formatLocation(handleData.getX());
                params.put("x", lng);
                String lat = LocationUtil.formatLocation(handleData.getY());
                params.put("y", lat);
                String addr = handleData.getAddress();
                params.put("address", addr);
                /*一级抢修分类*/
                int typeMain = handleData.getTypeMain();
                params.put("typeMain", typeMain + "");
                /*二级抢修分类*/
                int type = handleData.getType();
                params.put("type", type + "");
                /*处理人员*/
                String departmentId = handleData.getDepartmentId();
                params.put("departmentId", departmentId);
                String userId = handleData.getDispatchUserId();
                params.put("dispatchUserId", userId);
                /*协同处理人员*/
                String assistDepartId = handleData.getAssistantDepartment();
                params.put("assistantDepartment", assistDepartId);
                String assistId = handleData.getAssistantUserId();
                params.put("assistantUserId", assistId);
                return params;
            }

            @Override
            public Map<String, File> getFileUploads() {
                Map<String, File> params = new HashMap<>();
                int count = 1;
                if (photos != null && photos.size() > 0){
                    for (int i = 0; i < photos.size(); i++, count++){
                        TroublePictureBean pictureBean = photos.get(i);
                        String file = pictureBean.getFilepath();
                        String time = pictureBean.getCreateTime();
                        String key = "photo" + count + "," + 0 + "," + time;
                        if (file != null && !file.isEmpty())
                            params.put(key, new File(file));
                    }
                }
                if (audios != null && audios.size() > 0){
                    for (int i = 0; i < audios.size(); i++, count++){
                        TroubleAudioBean audioBean = audios.get(i);
                        String file = audioBean.getPath();
                        int second = audioBean.getSecond();
                        String time = audioBean.getCreateTime();
                        String key = "audio" + count + "," + second + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }
                if (files != null && files.size() > 0){
                    for (int i = 0; i < files.size(); i++, count++){
                        TroubleAudioBean audioBean = files.get(i);
                        String file = audioBean.getPath();
                        String time = audioBean.getCreateTime();
                        String key = "other" + count + "," + 0 + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }

                return params;
            }
        };
        BaseApp.addMultiStatckRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void emergencySignOver(Context context, String emergencyId, double lng, double lat, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyOverSignUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                return headerParams;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                /*应急调度id*/
                params.put("id", emergencyId);
                /*签到经纬度*/
                params.put("signX", StringUtil.formatDigit6(lng));
                params.put("signY", StringUtil.formatDigit6(lat));
                return params;
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void emergencyAssignHandle(Context context, EmergencyHandleData handleData, List<TroublePictureBean> signs, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, List<TroubleAudioBean> files, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyAssignHandleUrl();
        MultiPartStringRequest postRequest = new MultiPartStringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                headerParams.put("Charset", "utf-8");
                return headerParams;
            }

            @Override
            public Map<String, String> getStringUploads() {
                Map<String, String> params = new HashMap<>();
                /*应急调度id*/
                String emergencyId = handleData.getId();
                params.put("id", emergencyId);
                /*是否抢修*/
                params.put("isMaintenance", handleData.getIsMaintenance() + "");
                /*是否审核*/
                params.put("auditFlag", handleData.getAuditFlag() + "");
                /*处置情况*/
                String handling = handleData.getEmergencyHandling();
                if (handling != null && !handling.isEmpty()){
                    params.put("emergencyHandling", handling);
                }
                /*到位时间*/
                String overTime = handleData.getOverTime();
                if (overTime != null && !overTime.isEmpty()){
                    params.put("overTime", overTime);
                }
                /*完成时间*/
                params.put("completeTime", handleData.getCompleteTime());

                return params;
            }

            @Override
            public Map<String, File> getFileUploads() {
                Map<String, File> params = new HashMap<>();
                int count = 1;
                if (signs != null && signs.size() > 0){
                    for (int i = 0; i < signs.size(); i++, count++){
                        TroublePictureBean pictureBean = signs.get(i);
                        String file = pictureBean.getFilepath();
                        String key = "confirmingFile" + count;
                        if (file != null && !file.isEmpty())
                            params.put(key, new File(file));
                    }
                }
                if (photos != null && photos.size() > 0){
                    for (int i = 0; i < photos.size(); i++, count++){
                        TroublePictureBean pictureBean = photos.get(i);
                        String file = pictureBean.getFilepath();
                        String time = pictureBean.getCreateTime();
                        String key = "photo" + count + "," + 0 + "," + time;
                        if (file != null && !file.isEmpty())
                            params.put(key, new File(file));
                    }
                }
                if (audios != null && audios.size() > 0){
                    for (int i = 0; i < audios.size(); i++, count++){
                        TroubleAudioBean audioBean = audios.get(i);
                        String file = audioBean.getPath();
                        int second = audioBean.getSecond();
                        String time = audioBean.getCreateTime();
                        String key = "audio" + count + "," + second + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }
                if (files != null && files.size() > 0){
                    for (int i = 0; i < files.size(); i++, count++){
                        TroubleAudioBean audioBean = files.get(i);
                        String file = audioBean.getPath();
                        String time = audioBean.getCreateTime();
                        String key = "other" + count + "," + 0 + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }

                return params;
            }
        };
        BaseApp.addMultiStatckRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void emergencyScheduleAssign(Context context, EmergencyHandleData handleData, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyScheduleAssignUrl();

        Map<String, String> headers = newApi_dyrq.getHeaderParams();
        MultipartRequestPost postRequest = new MultipartRequestPost(Request.Method.POST,
                url, headers, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
           /*  @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                return headerParams;
            }

           @Override
            public Map<String, String> getStringUploads() {
                Map<String, String> params = new HashMap<>();
                String emergencyId = handleData.getId();
                params.put("id", emergencyId);
                String departId = handleData.getDispatchDisposeDeptId();
                params.put("dispatchDisposeDeptId", departId);
                String userId = handleData.getDispatchDisposeUserId();
                params.put("dispatchDisposeUserId ", userId);
                String note = handleData.getDispatchDisposeNote();
                params.put("dispatchDisposeNote ", note);
                return params;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                String emergencyId = handleData.getId();
                params.put("id", emergencyId);
                String departId = handleData.getDispatchDisposeDeptId();
                params.put("dispatchDisposeDeptId", departId);
                String userId = handleData.getDispatchDisposeUserId();
                params.put("dispatchDisposeUserId ", userId);
                String note = handleData.getDispatchDisposeNote();
                params.put("dispatchDisposeNote ", note);
                return params;
            }

            @Override
            public byte[] getBody() throws AuthFailureError{
                JsonObject root = new JsonObject();
                Gson gson = new Gson();

                String emergencyId = handleData.getId();
                root.addProperty("id", Integer.parseInt(emergencyId));
                String departId = handleData.getDispatchDisposeDeptId();
                root.addProperty("dispatchDisposeDeptId", Integer.parseInt(departId));
                String userId = handleData.getDispatchDisposeUserId();
                root.addProperty("dispatchDisposeUserId ", Integer.parseInt(userId));
                String note = handleData.getDispatchDisposeNote();
                root.addProperty("dispatchDisposeNote ", note);
                String pipeDevice = gson.toJson(root);
                return pipeDevice.getBytes();
            }*/
        };

        String emergencyId = handleData.getId();
        postRequest.addPart(new MultipartRequestPost.FormPart("id", emergencyId));
        String departId = handleData.getDispatchDisposeDeptId();
        postRequest.addPart(new MultipartRequestPost.FormPart("dispatchDisposeDeptId", departId));
        String userId = handleData.getDispatchDisposeUserId();
        postRequest.addPart(new MultipartRequestPost.FormPart("dispatchDisposeUserId", userId));
        String note = handleData.getDispatchDisposeNote();
        postRequest.addPart(new MultipartRequestPost.FormPart("dispatchDisposeNote", note));

        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void emergencyRepairAssign(Context context, EmergencyHandleData handleData, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, List<TroubleAudioBean> files, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyRepairAssignUrl();
        MultiPartStringRequest postRequest = new MultiPartStringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                headerParams.put("Charset", "utf-8");
                return headerParams;
            }

            @Override
            public Map<String, String> getStringUploads() {
                Map<String, String> params = new HashMap<>();
                /*应急调度id*/
                String emergencyId = handleData.getId();
                params.put("id", emergencyId);
                /*处理人员*/
                String departId = handleData.getResponsibleDepartment();
                params.put("responsibleDepartment", departId);
                String userId = handleData.getResponsibleUserId();
                params.put("responsibleUserId", userId);
                /*协同处理人员*/
                String assistDepart = handleData.getMdispatchingDepartment();
                if (assistDepart != null && !assistDepart.isEmpty()){
                    params.put("mdispatchingDepartment", assistDepart);
                }
                String assistUsers = handleData.getMdispatchingUserId();
                if (assistUsers != null && !assistUsers.isEmpty()){
                    params.put("mdispatchingUserId", assistUsers);
                }

                /*备注*/
                String note = handleData.getMdispatchingNotes();
                if (note != null && !note.isEmpty()){
                    params.put("mdispatchingNotes", note);
                }
                return params;
            }

            @Override
            public Map<String, File> getFileUploads() {
                Map<String, File> params = new HashMap<>();
                int count = 1;
                if (photos != null && photos.size() > 0){
                    for (int i = 0; i < photos.size(); i++, count++){
                        TroublePictureBean pictureBean = photos.get(i);
                        String file = pictureBean.getFilepath();
                        String time = pictureBean.getCreateTime();
                        String key = "photo" + count + "," + 0 + "," + time;
                        if (file != null && !file.isEmpty())
                            params.put(key, new File(file));
                    }
                }
                if (audios != null && audios.size() > 0){
                    for (int i = 0; i < audios.size(); i++, count++){
                        TroubleAudioBean audioBean = audios.get(i);
                        String file = audioBean.getPath();
                        int second = audioBean.getSecond();
                        String time = audioBean.getCreateTime();
                        String key = "audio" + count + "," + second + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }
                if (files != null && files.size() > 0){
                    for (int i = 0; i < files.size(); i++, count++){
                        TroubleAudioBean audioBean = files.get(i);
                        String file = audioBean.getPath();
                        String time = audioBean.getCreateTime();
                        String key = "other" + count + "," + 0 + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }

                return params;
            }
        };
        BaseApp.addMultiStatckRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void emergencyRepairHandle(Context context, EmergencyHandleData handleData, List<TroublePictureBean> photos, List<TroubleAudioBean> audios, List<TroubleAudioBean> files, EmergencyCallBack emergencyCallBack) {
        String url = newApi_dyrq.getEmergencyRepairHandleUrl();
        MultiPartStringRequest postRequest = new MultiPartStringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                emergencyCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                emergencyCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    emergencyCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                emergencyCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                headerParams.put("Charset", "utf-8");
                return headerParams;
            }

            @Override
            public Map<String, String> getStringUploads() {
                Map<String, String> params = new HashMap<>();
                /*应急调度id*/
                String emergencyId = handleData.getId();
                params.put("id", emergencyId);
                /*抢修备注*/
                String maintenanceNotes = handleData.getMaintenanceNotes();
                if (maintenanceNotes != null && !maintenanceNotes.isEmpty()){
                    params.put("maintenanceNotes", maintenanceNotes);
                }
                return params;
            }

            @Override
            public Map<String, File> getFileUploads() {
                Map<String, File> params = new HashMap<>();
                int count = 1;
                if (photos != null && photos.size() > 0){
                    for (int i = 0; i < photos.size(); i++, count++){
                        TroublePictureBean pictureBean = photos.get(i);
                        String file = pictureBean.getFilepath();
                        String time = pictureBean.getCreateTime();
                        String key = "photo" + count + "," + 0 + "," + time;
                        if (file != null && !file.isEmpty())
                            params.put(key, new File(file));
                    }
                }
                if (audios != null && audios.size() > 0){
                    for (int i = 0; i < audios.size(); i++, count++){
                        TroubleAudioBean audioBean = audios.get(i);
                        String file = audioBean.getPath();
                        int second = audioBean.getSecond();
                        String time = audioBean.getCreateTime();
                        String key = "audio" + count + "," + second + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }
                if (files != null && files.size() > 0){
                    for (int i = 0; i < files.size(); i++, count++){
                        TroubleAudioBean audioBean = files.get(i);
                        String file = audioBean.getPath();
                        String time = audioBean.getCreateTime();
                        String key = "other" + count + "," + 0 + "," + time;;
                        if (file != null && !file.isEmpty()){
                            params.put(key, new File(file));
                        }
                    }
                }

                return params;
            }
        };
        BaseApp.addMultiStatckRequest(context.getClass().getName(), postRequest);
    }
}
