package com.prolog.upcloud.base.wh.operate.core;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static com.prolog.upcloud.base.wh.operate.constant.MsgTemplate.NO_RESPONSE;

/**
 * 味精助手
 *
 * @author 鄢菁KingYan
 * @date 2022/07/08
 */
@Slf4j
public class RestMsgHelper {

    public static <T> T unpack(RestMessage<T> msg) {
        log.info("unpack==========>param:{}", JSONObject.toJSONString(msg));
        Assert.notNull(msg, NO_RESPONSE);
        Assert.isTrue(msg.isSuccess(), msg.getMessage());
        return msg.getData();
    }

    public static <E> List<E> unpackPage(Page<E> page) {
        log.info("unpack==========>param:{}", JSONObject.toJSONString(page));
        Assert.notNull(page, "拆解参数失败");
        return page.getList();
    }

    public static <T> List<T> unpackPageForObject(Object o, Class<T> clazz) {
        log.info("unpackPageForObject==========>param:{}", JSONObject.toJSONString(o));
        Assert.notNull(o, "拆解参数失败");
        return JSON.parseArray(JSON.toJSONString(o), clazz);
    }

}
