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.grain_check.GrainCheckBean;
import com.unis.model.bean.grain_check.GrainCheckDictionaryBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * 包名: com.unis.model
 * 类名: GrainCheckModel
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/12 15:33
 * 更新人:
 * 更新时间: 2020/12/12 15:33
 * 更新说明:
 * 版本号: 1.0
 */
public class GrainCheckModel {
    private GrainCheckModel() {
    }

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

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


    /**
     * 获取粮情检查记录
     */
    public void getList(String checkPerson, int page, Integer uid, final DataCallback<List<GrainCheckBean>> callBack) {
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", page);
        map.put("pageSize", 10);
        Map<String, Object> condition = new HashMap();
        condition.put("check_person", checkPerson);
        condition.put("uid", uid);
        condition.put("depotId", DepotModel.get().getSelectDepot().depotCode);
        condition.put("houseId", HouseModel.get().getSelectHouse().houseCode);
        map.put("condition", gson.toJson(condition));
        OkGo.<String>post(UrlPath.grain_check_list)
                .upJson(gson.toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String dataJson = getDataJson(response.body());
                        JSONObject o = new JSONObject(dataJson);
                        JSONArray records = o.optJSONArray("records");
                        List<GrainCheckBean> list = new ArrayList<>();
                        if (records.length() != 0) {
                            list = new Gson().fromJson(records.toString(),
                                    new TypeToken<List<GrainCheckBean>>() {
                                    }.getType());
                        }
                        callBack.onSuccess(list);
                    }

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

    /**
     * 获取粮情检查选项字典表
     */
    public void getDictionaryList(final DataCallback<List<GrainCheckDictionaryBean>> callBack) {
        Gson gson = new Gson();

        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", 1);
        map.put("pageSize", 100);
        Map<String, String> condition = new HashMap<>();
        condition.put("table_name", "grain_condition_record");
        condition.put("field_name", "check_list");
        map.put("condition", gson.toJson(condition));

        OkGo.<String>post(UrlPath.grain_check_dictionary)
                .upJson(gson.toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String dataJson = getDataJson(response.body());
                        JSONObject o = new JSONObject(dataJson);
                        JSONArray records = o.optJSONArray("records");
                        List<GrainCheckDictionaryBean> list = new ArrayList<>();
                        if (records.length() != 0) {
                            list = new Gson().fromJson(records.toString(),
                                    new TypeToken<List<GrainCheckDictionaryBean>>() {
                                    }.getType());
                        }
                        callBack.onSuccess(list);
                    }

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

    /**
     * 新增粮情检查
     */
    public void add(GrainCheckBean bean, final DataCallback<String> callBack) {
        OkGo.<String>post(UrlPath.grain_check_add)
                .upJson(new Gson().toJson(bean))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        callBack.onSuccess(null);
                    }

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

    /**
     * 修改粮情检查
     */
    public void update(GrainCheckBean bean, final DataCallback<String> callBack) {
        OkGo.<String>post(UrlPath.grain_check_update)
                .upJson(new Gson().toJson(bean))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        callBack.onSuccess(null);
                    }

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

}
