package com.tofly.ghrq_route.module.planPointRepair;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean.OfflineWorkJobType;
import com.tofly.ghrq_route.module.plan.PlanTool;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.planPointRepair.controller.PointRepairCallBack;
import com.tofly.ghrq_route.module.planPointRepair.controller.PointRepairController;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainAdjustParam;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainCommonParam;
import com.tofly.ghrq_route.module.planPointRepair.domain.NFCInfoBean;
import com.tofly.ghrq_route.module.planPointRepair.domain.PlanPointRepair;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.module.uploadFile.controller.UploadFileCallBack;
import com.tofly.ghrq_route.module.uploadFile.controller.UploadFileController;
import com.tofly.ghrq_route.module.uploadFile.domain.UploadFileBean;
import com.tofly.ghrq_route.module.uploadFile.domain.UploadFileTypeSort;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;
import com.tofly.ghrq_route.common.util.file.FileUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Amu on 2017/4/26.
 */

public class PointRepairTool {

    private static PointRepairTool repairTool = null;

    private PointRepairController repairController;

    private UploadFileController uploadFileController;

    private DBDaoHelper dbDaoHelper;

    private PointRepairTool(){
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    public static synchronized PointRepairTool getInstance(){
        if (repairTool == null){
            repairTool = new PointRepairTool();
        }
        return repairTool;
    }

    public void loadRepairPointInfo(final Context context, String point_id, String plan_id, boolean online, final PointRepairCallBack repairCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && point_id != null && plan_id != null){
            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.getPointRepairInfo(userBean.getToken(), point_id, plan_id, online, new PointRepairCallBack() {
                @Override
                public void onGetPointRepairInfo(boolean isSuccess, int errorCode, String errMsg, PlanPointRepair repairBean) {
                    super.onGetPointRepairInfo(isSuccess, errorCode, errMsg, repairBean);
                    if (isSuccess) {
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (repairCallBack != null)
                        repairCallBack.onGetPointRepairInfo(isSuccess, errorCode, errMsg, repairBean);
                }
            });
        }
    }

    public void loadPointRepairInfoFromServer(Context context, String point_id, String plan_id, PointRepairCallBack repairCallBack){
        loadRepairPointInfo(context, point_id, plan_id, true, repairCallBack);
    }

    public void loadPointRepairInfoFromLocal(Context context, String point_id, String plan_id, PointRepairCallBack repairCallBack){
        loadRepairPointInfo(context, point_id, plan_id,false, repairCallBack);
    }

    public PlanPointRepair getLocalPointRepairInfo(String repair_id){
        return dbDaoHelper.getLocalRepairInfo(repair_id);
    }


    public void loadRepairPointInfo(final Context context, final String point_id, final String plan_id, final PointRepairCallBack repairCallBack){
        loadPointRepairInfoFromServer(context, point_id, plan_id, new PointRepairCallBack(){
            @Override
            public void onGetPointRepairInfo(boolean isSuccess, int errorCode, String errMsg, PlanPointRepair repairBean) {
                super.onGetPointRepairInfo(isSuccess, errorCode, errMsg, repairBean);
                if (isSuccess){
                    repairCallBack.onGetPointRepairInfo(true, errorCode, errMsg, repairBean);
                }else {
                    loadPointRepairInfoFromLocal(context, point_id, plan_id, repairCallBack);
                }
            }
        });
    }

    public void loadCommonDeviceRepairHistoryList(final Context context, String plan_id, String point_id, final int pageindex, final PointRepairCallBack repairCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
        if (userBean != null) {
            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.getCommonDeviceChildRepairList(context, userBean.getUserid(), plan_id, point_id, pageindex, pagesize, new PointRepairCallBack() {
                @Override
                public void onGetPointChildRepairList(boolean isSuccess, int errorCode, String errMsg, List<PlanPointRepair> repairList, int total) {
                    super.onGetPointChildRepairList(isSuccess, errorCode, errMsg, repairList, total);
                    List<PlanPointRepair> records = new ArrayList<>();
                    int total_count = 0;
                    if (isSuccess) {
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (pageindex == 1) {
                        List<PlanPointRepair> localRecords = getLocalRepairRecords(plan_id, point_id);
                        if (localRecords != null && localRecords.size() > 0) {
                            records.addAll(localRecords);
                            total_count = localRecords.size();
                            isSuccess = true;
                        }
                    }
                    if (repairCallBack != null)
                        repairCallBack.onGetPointChildRepairList(isSuccess, errorCode, errMsg, records, total_count);
                }
            });
        }
    }

    public void loadAdjustDeviceRepairHistoryList(final Context context, String plan_id, String point_id, final int pageindex, final PointRepairCallBack repairCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
        if (userBean != null) {
            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.getAdjustDeviceChildRepairList(context, userBean.getUserid(), plan_id, point_id, pageindex, pagesize, new PointRepairCallBack() {
                @Override
                public void onGetPointChildRepairList(boolean isSuccess, int errorCode, String errMsg, List<PlanPointRepair> repairList, int total) {
                    super.onGetPointChildRepairList(isSuccess, errorCode, errMsg, repairList, total);
                    List<PlanPointRepair> records = new ArrayList<>();
                    int total_count = 0;
                    if (isSuccess) {
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (pageindex == 1) {
                        List<PlanPointRepair> localRecords = getLocalRepairRecords(plan_id, point_id);
                        if (localRecords != null && localRecords.size() > 0){
                            records.addAll(localRecords);
                            total_count = localRecords.size();
                            isSuccess = true;
                        }
                    }
                    if (repairCallBack != null)
                        repairCallBack.onGetPointChildRepairList(isSuccess, errorCode, errMsg, records, total_count);
                }
            });
        }
    }

    public List<PlanPointRepair> getLocalRepairRecords(String plan_id, String point_id){
        if (plan_id != null && point_id != null)
            return dbDaoHelper.getLocalRepairRecordList(plan_id, point_id);
        else
            return null;
    }

    public List<TroublePictureBean> getLocalRepairPhotos(String record_id){
        List<TroublePictureBean> pictures = dbDaoHelper.queryTroublePictures(record_id);
        return pictures;
    }

    public void getRepairNFCInfo(final Context context, String rfid, int deviceTypeId, final PointRepairCallBack repairCallBack){
        UserBean user = BaseApp.getInstance().getLoginUser();
        if (user == null)
            return;

        if (repairController == null)
            repairController = new PointRepairController(context);
        repairController.getRepairNFCInfo(user.getToken(), rfid, deviceTypeId, new PointRepairCallBack(){
            @Override
            public void onGetRepairNFCInfo(boolean isSuccess, int errorCode, String errMsg, NFCInfoBean nfcInfo) {
                super.onGetRepairNFCInfo(isSuccess, errorCode, errMsg, nfcInfo);
                if (isSuccess){
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    String strWarn = context.getResources().getString(R.string.repair_get_nfc_error) + errMsg;
                    ToastUtil.showToast(context, strWarn);
                }
                if (repairCallBack != null)
                    repairCallBack.onGetRepairNFCInfo(isSuccess, errorCode, errMsg, nfcInfo);
            }
        });
    }

    public interface GetRepairUploadFileCallback{
        void onGetUploadPhotos(boolean isSuccess, List<TroublePictureBean> photos);
    }

    public void getRepairUploadFiles(final Context context, String repair_id, int pageindex, final GetRepairUploadFileCallback uploadFileCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && repair_id != null) {

            if (uploadFileController == null)
                uploadFileController = new UploadFileController(context);
            final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
            uploadFileController.getUploadFileList(userBean.getToken(), repair_id,
                    UploadFileBean.FILE_TABLE_PLAN_REPAIR, pageindex, pagesize, new UploadFileCallBack() {
                        @Override
                        public void onGetUploadFileList(boolean isSuccess, int errorCode, String errMsg, List<UploadFileBean> fileList) {
                            super.onGetUploadFileList(isSuccess, errorCode, errMsg, fileList);
                            List<TroublePictureBean> photos = new ArrayList<>();
                            if (isSuccess) {
                                if (fileList != null && fileList.size() > 0) {
                                    String[] repairTYSBType6 = context.getResources().getStringArray(R.array.repair_tysb_photo_type_6);
                                    Collections.sort(fileList, new UploadFileTypeSort(repairTYSBType6));
                                    for (UploadFileBean fileBean : fileList) {
                                        if (fileBean != null) {
                                            TroublePictureBean pictureBean = new TroublePictureBean();
                                            String url = NetRequestURL.getHttpHead() + fileBean.getFilename();
                                            pictureBean.setFilepath(url);
                                            pictureBean.setFiletype(fileBean.getFilenote());
                                            pictureBean.setStatus(TroublePictureBean.PHOTO_UPLOADED);
                                            pictureBean.setId(fileBean.getId());
                                            photos.add(pictureBean);
                                        }
                                    }
                                }
                            } else {
                                errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                                ToastUtil.showToast(context, errMsg);
                            }
                            if (uploadFileCallBack != null)
                                uploadFileCallBack.onGetUploadPhotos(isSuccess, photos);
                        }
                    });
        }
    }

    public boolean isUploadingRepairtRecord(String record_id){
        PlanPointRepair repairInfo = getLocalPointRepairInfo(record_id);
        return (repairInfo != null && repairInfo.getState() == TroubleBean.SAVE_UPLOADING_STATE);
    }

    public void savePlanPointRepairRecord(PlanPointRepair pointRepair, List<TroublePictureBean> pictures,
                                          boolean isUpdateRepair, boolean commonDevice){
        if (pointRepair != null ){
            /*保存隐患的数据信息*/
            String recordId = pointRepair.getTemp_id();
            boolean isLocal = pointRepair.getState() == TroubleBean.SAVED_STATE;
            /*原来巡检点的id,*/
            pointRepair.setCreate_time(DateUtils.getCurrentDate());
            if (isUpdateRepair) {
                pointRepair.setState(TroubleBean.SERVER_UPDATE_UNUPLOAD_STATE);
            }else {
                pointRepair.setState(TroubleBean.SAVED_STATE);
            }
            dbDaoHelper.insertOrUpdateRepairRecord(pointRepair);

            OfflineWorkJobBean workJobBean = null;
            if (isUpdateRepair || isLocal) {
                workJobBean = dbDaoHelper.getOfflineWorkJob(recordId);
            }

            if (workJobBean == null) {
                workJobBean = new OfflineWorkJobBean();
                String job_id = UUID.randomUUID().toString();
                workJobBean.setId(job_id);
            }

            UserBean userBean = BaseApp.getInstance().getLoginUser();
            workJobBean.setUser_id(userBean.getUserid());
            workJobBean.setJob_type(commonDevice ? OfflineWorkJobType.JOB_COMMON_DEVICE_ROUTE_ADD :
                    OfflineWorkJobType.JOB_ADJUST_DEVICE_ROUTE_ADD);
            workJobBean.setRef_id(recordId);
            workJobBean.setRef_data_id(pointRepair.getPlan_point_id());

            /*保存隐患的图片*/
            if (pictures != null && pictures.size() > 0) {
                List<String> pic_ids = new ArrayList<>();
                for (TroublePictureBean pictureBean : pictures) {
                    if (pictureBean != null) {
                        String filepath = pictureBean.getFilepath();
                        if (filepath != null && FileUtil.fileExists(filepath)) {
                            pictureBean.setTroubleid(recordId);
                            pictureBean.setStatus(TroubleBean.SAVED_STATE);
                            dbDaoHelper.insertOrUpdateTroublePicture(pictureBean);

                            pic_ids.add(pictureBean.getId());
                        }
                    }
                }
                workJobBean.setRef_photo_ids(pic_ids);
            }

            dbDaoHelper.insertOfflineWorkJob(workJobBean);
        }
    }

    /*阀门设备录入上传*/
    public void uploadCommonDeviceRepairRecord(final Context context, final PlanPointRepair pointRepair,
                                               final List<TroublePictureBean> pictures, final boolean saveOnFail, final PointRepairCallBack repairCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && pointRepair != null ) {
            final boolean isUpdateRepair = isUpdatingServerRepair(pointRepair);
            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.addCommonDeviceRepairRecord(context,  pointRepair, pictures, isUpdateRepair, new PointRepairCallBack() {
                @Override
                public void onAddPointChildRepair(boolean isSuccess, int errorCode, String errMsg) {
                    super.onAddPointChildRepair(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        setPlanPointFinished(pointRepair.getPoint_id(), pointRepair.getPlan_id());
                        deleteLocalRouteRecord(pointRepair, pictures);
                    } else {
                        if (errorCode == URLResponseCode.ErrorCode.ALREADY_EXISTS) {  //工单已完成
                            deleteLocalRouteRecord(pointRepair, pictures);
                            errMsg = context.getString(R.string.repair_finished_no_commit);
                        }else {
                            if (saveOnFail){
                                setPlanPointFinished(pointRepair.getPoint_id(), pointRepair.getPlan_id());
                                savePlanPointRepairRecord(pointRepair, pictures, isUpdateRepair, true);
                            }else {
                            }
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        }
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (repairCallBack != null)
                        repairCallBack.onAddPointChildRepair(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    public void uploadCommonDeviceRepairRecord(Context context, PlanPointRepair pointRepair,
                                               List<TroublePictureBean> pictures, PointRepairCallBack repairCallBack){
        uploadCommonDeviceRepairRecord(context, pointRepair,  pictures, true, repairCallBack);
    }

    public void uploadCommonDeviceRepairRecordNoSave(Context context, PlanPointRepair pointRepair,
                                                     List<TroublePictureBean> pictures, PointRepairCallBack repairCallBack){
        uploadCommonDeviceRepairRecord(context, pointRepair,  pictures, false, repairCallBack);
    }

    private boolean isUpdatingServerRepair(PlanPointRepair pointRepair) {
        int repair_state = pointRepair.getState();
        return repair_state == TroubleBean.SERVER_STATE ||
                repair_state == TroubleBean.SERVER_UPDATE_UNUPLOAD_STATE;
    }

    private void savePlanPointRepairRecord(PlanPointRepair pointRepair, int state){
        if (pointRepair != null) {
            pointRepair.setState(state);
            dbDaoHelper.insertOrUpdateRepairRecord(pointRepair);
        }
    }

    public void setRepairRecordUpdating(PlanPointRepair pointRepair, boolean isUpdating) {
        if (pointRepair != null) {
            pointRepair.setIsUpdating(isUpdating);
            dbDaoHelper.insertOrUpdateRepairRecord(pointRepair);
        }
    }

    public void deleteLocalRouteRecord(PlanPointRepair pointRepair, List<TroublePictureBean> photos){
        dbDaoHelper.deleteLocalRepairRecord(pointRepair);
        if (photos != null && photos.size() > 0){
            dbDaoHelper.deleteSavedTroublePictures(photos);
            for (TroublePictureBean photo : photos){
                FileUtil.deleteFile(photo.getFilepath());
            }
        }

        OfflineWorkJobBean workJobBean = dbDaoHelper.getOfflineWorkJob(pointRepair.getTemp_id());
        if (workJobBean != null){
            dbDaoHelper.deleteOfflineWorkJob(workJobBean);
        }
    }

    public void deleteRepairPhoto(TroublePictureBean photo) {
        if (photo != null) {
            String filePath = photo.getFilepath();
            if (filePath != null)
                FileUtil.deleteFile(filePath);
            dbDaoHelper.deleteSavedTroublePicture(photo);
        }
    }

    public void setPlanPointFinished(String point_id, String plan_id){
        OfflinePathBean planPoint = dbDaoHelper.getPlanOfflinePath(point_id, plan_id);
        if (planPoint != null){
            planPoint.setState(OfflinePathBean.POINT_STATE_FINISHED);
            planPoint.setFinish_time(DateUtils.getDate());
            dbDaoHelper.updateOfflinePath(planPoint);

            addPlanOfflineJob(planPoint.getPlan_id());
        }
    }

    private void addPlanOfflineJob(String plan_id){
        PlanTool planTool = PlanTool.getInstance();
        planTool.addPlanOfflineJob(plan_id);
    }

    /*调压设备录入上传*/
    public void uploadAdjustDeviceRepairRecord(final Context context, final PlanPointRepair pointRepair,
                                               final List<TroublePictureBean> pictures, final boolean saveOnFail, final PointRepairCallBack repairCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && pointRepair != null) {
            final boolean isUpdateRepair = isUpdatingServerRepair(pointRepair);

            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.addAdjustPressDeviceChildRepair(context, pointRepair, pictures, isUpdateRepair, new PointRepairCallBack() {
                @Override
                public void onAddPointChildRepair(boolean isSuccess, int errorCode, String errMsg) {
                    super.onAddPointChildRepair(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        setPlanPointFinished(pointRepair.getPoint_id(), pointRepair.getPlan_id());
                        deleteLocalRouteRecord(pointRepair, pictures);
                    } else {
                        if (errorCode == URLResponseCode.ErrorCode.ALREADY_EXISTS) {  //工单已完成
                            deleteLocalRouteRecord(pointRepair, pictures);
                            errMsg = context.getString(R.string.repair_finished_no_commit);
                        }else  {
                            if (saveOnFail){
                                setPlanPointFinished(pointRepair.getPoint_id(), pointRepair.getPlan_id());
                                savePlanPointRepairRecord(pointRepair,  pictures, isUpdateRepair, false);
                            }else {
                            }
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        }
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (repairCallBack != null)
                        repairCallBack.onAddPointChildRepair(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    public void uploadAdjustDeviceRepairRecord(Context context, PlanPointRepair pointRepair,
                                               List<TroublePictureBean> pictures, PointRepairCallBack repairCallBack){
        uploadAdjustDeviceRepairRecord(context, pointRepair, pictures, true, repairCallBack);
    }

    public void uploadAdjustDeviceRepairRecordNoSave(Context context, PlanPointRepair pointRepair,
                                                     List<TroublePictureBean> pictures, PointRepairCallBack repairCallBack){
        uploadAdjustDeviceRepairRecord(context, pointRepair, pictures, false, repairCallBack);
    }


    public interface AddRepairUploadFileCallback{
        void onAddUploadPhoto(boolean isSuccess, TroublePictureBean photo);
    }

    public void addRepairUploadFile(final Context context, String repair_id, final TroublePictureBean picture, final AddRepairUploadFileCallback fileCallback) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && repair_id != null) {
            picture.setStatus(TroublePictureBean.PHOTO_UPLOADING);

            if (uploadFileController == null)
                uploadFileController = new UploadFileController(context);
            final String photoPath = picture.getFilepath();
            final String fileNote = picture.getFiletype();
            uploadFileController.addUploadRepairPhoto(userBean.getToken(), repair_id, photoPath, fileNote, new UploadFileCallBack() {
                @Override
                public void onAddUploadFile(boolean isSuccess, int errorCode, String errMsg, UploadFileBean fileBean) {
                    super.onAddUploadFile(isSuccess, errorCode, errMsg, fileBean);
                    if (isSuccess) {
                        picture.setStatus(TroublePictureBean.PHOTO_UPLOADED);
                        FileUtil.deleteFile(photoPath);
                        if (fileBean != null) {
                            picture.setId(fileBean.getId());
                            String filepath = fileBean.getFilename();
                            if (filepath != null && !filepath.isEmpty()) {
                                picture.setFilepath(filepath);
                            }
                        }
                    } else {
                        picture.setStatus(TroublePictureBean.PHOTO_UPLOAD_FAILED);

                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (fileCallback != null)
                        fileCallback.onAddUploadPhoto(isSuccess, picture);
                }
            });
        }
    }

    public void getAdjustDeviceSettingList(final Context context, final boolean online, final PointRepairCallBack repairCallBack){
        if (repairController == null)
            repairController = new PointRepairController(context);
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        repairController.getAdjustDeviceSettingList(context, pageindex, pagesize, online, new PointRepairCallBack(){
            @Override
            public void onGetAdjustDeviceSettingList(boolean isSuccess, int errorCode, String errMsg, List<MaintainAdjustParam> paramList, int total) {
                super.onGetAdjustDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                if (isSuccess){
                }else {
                }
                if (repairCallBack != null)
                    repairCallBack.onGetAdjustDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
            }
        });
    }

    public void getAdjustDeviceSettingListFromServer(Context context, PointRepairCallBack repairCallBack){
        getAdjustDeviceSettingList(context, true, repairCallBack);
    }

    public void getAdjustDeviceSettingListFromLocal(Context context, PointRepairCallBack repairCallBack){
        getAdjustDeviceSettingList(context, false, repairCallBack);
    }

    public void getAdjustDeviceSettingList(final Context context, final PointRepairCallBack repairCallBack){
        getAdjustDeviceSettingList(context, true, new PointRepairCallBack(){
            @Override
            public void onGetAdjustDeviceSettingList(boolean isSuccess, int errorCode, String errMsg, List<MaintainAdjustParam> paramList, int total) {
                super.onGetAdjustDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                if (isSuccess){
                    if (repairCallBack != null)
                        repairCallBack.onGetAdjustDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                }else {
                    getAdjustDeviceSettingListFromLocal(context, repairCallBack);
                }
            }
        });
    }

    public List<MaintainAdjustParam> getLocalAdjustDeviceSettings(){
        return dbDaoHelper.getAdjustDeviceSettings();
    }

    public Map<MaintainAdjustParam, List<MaintainAdjustParam>> getLocalPairedAdjustDeviceSettings(){
        return dbDaoHelper.getAdjustDeviceSettingsMapped();
    }

    public void getCommonDeviceSettingList(final Context context, final boolean online, final PointRepairCallBack repairCallBack){
        if (repairController == null)
            repairController = new PointRepairController(context);
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        repairController.getCommonDeviceSettingList(context, pageindex, pagesize, online, new PointRepairCallBack(){
            @Override
            public void onGetCommonDeviceSettingList(boolean isSuccess, int errorCode, String errMsg, List<MaintainCommonParam> paramList, int total) {
                super.onGetCommonDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                if (isSuccess){
                }else {
                }
                if (repairCallBack != null)
                    repairCallBack.onGetCommonDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
            }
        });
    }

    public void getCommonDeviceSettingListFromServer(Context context, PointRepairCallBack repairCallBack){
        getCommonDeviceSettingList(context, true, repairCallBack);
    }

    public void getCommonDeviceSettingListFromLocal(Context context, PointRepairCallBack repairCallBack){
        getCommonDeviceSettingList(context, false, repairCallBack);
    }

    public void getCommonDeviceSettingList(final Context context, final PointRepairCallBack repairCallBack){
        getCommonDeviceSettingList(context, true, new PointRepairCallBack(){
            @Override
            public void onGetCommonDeviceSettingList(boolean isSuccess, int errorCode, String errMsg, List<MaintainCommonParam> paramList, int total) {
                super.onGetCommonDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                if (isSuccess){
                    if (repairCallBack != null)
                        repairCallBack.onGetCommonDeviceSettingList(isSuccess, errorCode, errMsg, paramList, total);
                }else {
                    getCommonDeviceSettingListFromLocal(context, repairCallBack);
                }
            }
        });
    }

    public List<MaintainCommonParam> getLocalCommonDeviceSettings(){
        return dbDaoHelper.getCommonDeviceSettings();
    }

    public void removeRepairUploadFile(final Context context, String file_id, final TroublePictureBean picture, final UploadFileCallBack uploadFileCallBack) {
        if (file_id != null) {
            UserBean userBean = BaseApp.getInstance().getLoginUser();
            if (userBean == null)
                return;

            if (uploadFileController == null)
                uploadFileController = new UploadFileController(context);
            uploadFileController.removeUploadFile(userBean.getToken(), file_id, new UploadFileCallBack() {
                @Override
                public void onRemoveUploadFile(boolean isSuccess, int errorCode, String errMsg) {
                    super.onRemoveUploadFile(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        FileUtil.deleteFile(picture.getFilepath());
                    } else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (uploadFileCallBack != null)
                        uploadFileCallBack.onRemoveUploadFile(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    private void getRouteDeviceTypeList(final Context context, String typeIds, final boolean online, final PointRepairCallBack repairCallBack){
        if (repairController == null)
            repairController = new PointRepairController(context);
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        repairController.getRouteDeviceTypeList(context, typeIds, pageindex, pagesize, new PointRepairCallBack(){
            @Override
            public void onGetDatas(boolean isSuccess, int errorCode, String errMsg, List deviceList, int total) {
                super.onGetDatas(isSuccess, errorCode, errMsg, deviceList, total);
                if (isSuccess){
                }else {
                }
                if (repairCallBack != null)
                    repairCallBack.onGetDatas(isSuccess, errorCode, errMsg, deviceList, total);
            }
        });
    }

    public void getRouteDeviceTypeList(final Context context, String typeIds, final PointRepairCallBack repairCallBack){
        getRouteDeviceTypeList(context, typeIds,true, repairCallBack);
    }

    private void getDeviceRouteParamList(final Context context, String deviceId, final boolean online, final PointRepairCallBack repairCallBack) {
        if (repairController == null)
            repairController = new PointRepairController(context);
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        repairController.getDeviceRouteParamList(context, deviceId, pageindex, pagesize, new PointRepairCallBack(){
            @Override
            public void onGetDatas(boolean isSuccess, int errorCode, String errMsg, List paramList, int total) {
                super.onGetDatas(isSuccess, errorCode, errMsg, paramList, total);
                if (isSuccess){
                }else {
                }
                if (repairCallBack != null)
                    repairCallBack.onGetDatas(isSuccess, errorCode, errMsg, paramList, total);
            }
        });
    }

    public void getDeviceRouteParamList(final Context context, String deviceId, final PointRepairCallBack repairCallBack){
        getDeviceRouteParamList(context, deviceId,true, repairCallBack);
    }

    /*阀门设备录入上传*/
    public void uploadDeviceRouteRecord(final Context context, final JSONObject jsonObject, final List<TroublePictureBean> pictures,
                                        final List<TroubleAudioBean> audioBeans, final boolean isUpdateRepair, final PointRepairCallBack repairCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null && jsonObject != null ) {
            if (repairController == null)
                repairController = new PointRepairController(context);
            repairController.uploadDeviceRouteRecord(context, jsonObject, pictures, audioBeans, isUpdateRepair, new PointRepairCallBack() {
                @Override
                public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                    super.onSetData(isSuccess, errorCode, errMsg);
                    if (isSuccess) {
                        String planId = jsonObject.getString("childPlanId");
                        String pointId = jsonObject.getString("planTempPointId");
                        setPlanPointFinished(pointId, planId);
                        deleteLocalPhotoAudio(pictures, audioBeans);
                    } else {
                        if (errorCode == URLResponseCode.ErrorCode.ALREADY_EXISTS) {  //工单已完成
                            deleteLocalPhotoAudio(pictures, audioBeans);
                            errMsg = context.getString(R.string.repair_finished_no_commit);
                        }else {
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        }
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (repairCallBack != null)
                        repairCallBack.onSetData(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    private void deleteLocalPhotoAudio(List<TroublePictureBean> pictureBeans, List<TroubleAudioBean> audioBeans) {
                /*更新本地隐患的图片*/
        for (TroublePictureBean pictureBean : pictureBeans) {
            if (pictureBean != null) {
                dbDaoHelper.deleteSavedTroublePicture(pictureBean);
                FileUtil.deleteFile(pictureBean.getFilepath());
            }
        }

        /*更新本地隐患的音频*/
        for (TroubleAudioBean audioBean : audioBeans){
            if (audioBean != null) {
                dbDaoHelper.deleteSavedTroubleAudio(audioBean);
                FileUtil.deleteFile(audioBean.getPath());
            }
        }
    }

    public void loadDeviceRouteHistoryList(final Context context, String plan_id, String point_id, final int pageindex, final PointRepairCallBack repairCallBack){
        final int pagesize = C.DEFAULT_LOAD_PAGESIZE;
        if (repairController == null)
            repairController = new PointRepairController(context);
        repairController.getDeviceRouteRecordList(context, plan_id, point_id, pageindex, pagesize, new PointRepairCallBack() {
            @Override
            public void onGetDatas(boolean isSuccess, int errorCode, String errMsg, List routeList, int total) {
                super.onGetDatas(isSuccess, errorCode, errMsg, routeList, total);
                if (isSuccess) {
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (repairCallBack != null)
                    repairCallBack.onGetDatas(isSuccess, errorCode, errMsg, routeList, total);
            }
        });
    }
}
