package com.unis.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.unis.model.bean.facility_check.FacilityLastBean;

import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 设施管理
 * 创建人: mQ
 * 创建时间: 2021/3/23 16:54
 */
public class FacilityModel {
    private FacilityModel() {
    }

    private static final class SingleHolder {
        private static final FacilityModel INSTANCE = new FacilityModel();
    }

    public static FacilityModel get() {
        return SingleHolder.INSTANCE;
    }

    /**
     * 获取最新设施检查
     */
    public void getLastCheck(final DataCallback<List<FacilityLastBean>> callback) {
        OkGo.<String>get(UrlPath.facility_check_last)
                .params("depotId", DepotModel.get().getSelectDepot().depotCode)
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        Type type = new TypeToken<List<FacilityLastBean>>() {
                        }.getType();
                        List<FacilityLastBean> list = new Gson().fromJson(getDataJson(response.body()), type);
                        callback.onSuccess(list);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(getErrorMsg(response));
                    }
                });
    }
}
