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.QuantityQualityBean;

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;


/**
 * 库存质量
 * 创建人: mQ
 * 时间: 2021/2/1 16:07
 */
public class QuantityQualityModel {
    private QuantityQualityModel() {
    }

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

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

    /**
     * 获取库存质量列表
     */
    public void getList(String depotYpte, String depotId, String warehouseId, String qType, int page,
                        final DataCallback<List<QuantityQualityBean>> 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("depotId", depotId);
        condition.put("warehouseId", warehouseId);
        // 0=库存质量 1=出库质量；必填
        condition.put("depotYpte", depotYpte);
        // 检查类别(枚举id)
        condition.put("qType", qType);
        map.put("condition", gson.toJson(condition));
        OkGo.<String>post(UrlPath.quantity_quality_list)
                .upJson(gson.toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String json = getDataJson(response);
                        JSONObject object = new JSONObject(json);
                        JSONArray jsonArray = object.getJSONArray("records");
                        if (jsonArray == null || jsonArray.length() == 0) {
                            callback.onSuccess(new ArrayList<QuantityQualityBean>());
                            return;
                        }
                        List<QuantityQualityBean> list =
                                new Gson().fromJson(jsonArray.toString(),
                                        new TypeToken<List<QuantityQualityBean>>() {
                                        }.getType());
                        callback.onSuccess(list);
                    }

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

    /**
     * 根据id查询详情
     */
    public void detail(String id, final DataCallback<QuantityQualityBean> callback) {
        OkGo.<String>get(UrlPath.quantity_quality_detail)
                .params("id", id)
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String json = getDataJson(response);
                        callback.onSuccess(new Gson().fromJson(json, QuantityQualityBean.class));
                    }

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

    }


    /**
     * 新增库存质量
     */
    public void add(QuantityQualityBean bean, final DataCallback<String> callback) {
        OkGo.<String>post(UrlPath.quantity_quality_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(QuantityQualityBean bean, final DataCallback<String> callback) {
        OkGo.<String>post(UrlPath.quantity_quality_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));
                    }
                });
    }


}
