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.PolicyBean;
import com.xyxsbj.mylibrary.tool.StringTool;

import org.json.JSONException;

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

/**
 * 创建人: mQ
 * 创建时间: 2021/2/2 14:04
 */
public class PolicyModel {
    private PolicyModel() {
    }

    private static class Holder {
        private static final PolicyModel INSTANCE = new PolicyModel();
    }

    public static PolicyModel get() {
        return Holder.INSTANCE;
    }

    /**
     * 获取列表
     */
    public void getList(int page, final DataCallback<List<PolicyBean>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", page);
        map.put("pageSize", 10);
        OkGo.<String>post(UrlPath.policy_list)
                .upJson(new Gson().toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String json = getRecordsListJson(response);
                        if (StringTool.isEmpty(json)) {
                            callback.onSuccess(new ArrayList<PolicyBean>());
                        } else {
                            List<PolicyBean> list = new Gson()
                                    .fromJson(json, new TypeToken<List<PolicyBean>>() {
                                    }.getType());
                            callback.onSuccess(list);
                        }

                    }

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

}
