package easydo.technology.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;

import easydo.technology.base.PageInfo;

/**
 * 转换
 * Created by jinjin on 2020-09-22.
 */
public class ConvertUtil {
    public static <T, S> T convert(final S s, Class<T> clz) {
        return s == null ? null : JSONObject.parseObject(JSONObject.toJSONString(s), clz);
    }

    public static <T, S> List<T> convertList(List<S> s, Class<T> clz) {
        List<T> res = new ArrayList<>();
        for (S si : s)
            res.add(JSONObject.parseObject(JSONObject.toJSONString(si), clz));
        return res;
    }

    public static <T, S> Set<T> convertSet(Set<S> s, Class<T> clz) {
        Set<T> res = new HashSet<>();
        for (S si : s)
            res.add(JSONObject.parseObject(JSONObject.toJSONString(si), clz));
        return res;
    }

    public static <T, S> PageInfo<T> convertPage(IPage<S> page, Class<T> clz) {
        if (page == null) {
            return null;
        }
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setTotalElements(page.getTotal());
        pageInfo.setContent(convertList(page.getRecords(), clz));
        return pageInfo;
    }

}
