package com.liangjian.dataplatform.member.entity.vo;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.liangjian.dataplatform.member.entity.po.primary.LayoutConfig;
import io.swagger.annotations.ApiModel;

import java.util.Date;
import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/26 13:58
 */
@ApiModel("动态布局的记录结构")
public class LayoutConfigVo {
    private String id;

    private String name;

    private List<LayoutInfo> layoutInfo;

    private Date createDate;

    private Date updateDate;

    private String createUser;

    public LayoutConfigVo() {
    }

    public LayoutConfigVo(String id, String name, List<LayoutInfo> layoutInfo, Date createDate, Date updateDate, String createUser) {
        this.id = id;
        this.name = name;
        this.layoutInfo = layoutInfo;
        this.createDate = createDate;
        this.updateDate = updateDate;
        this.createUser = createUser;
    }

    public static LayoutConfigVo transfer(LayoutConfig layoutConfig) {
        String json = layoutConfig.getLayoutInfo();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectReader objectReader = objectMapper.readerFor(new TypeReference<List<LayoutInfo>>() {});
        try {
            List<LayoutInfo> layoutInfos = objectReader.readValue(json);
            return new LayoutConfigVo(
                    layoutConfig.getId(),
                    layoutConfig.getName(),
                    layoutInfos,
                    layoutConfig.getCreateDate(),
                    layoutConfig.getUpdateDate(),
                    layoutConfig.getCreateUser()
            );
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static LayoutConfig reTransfer(LayoutConfigVo vo) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(vo.getLayoutInfo());
            LayoutConfig po = new LayoutConfig();
            po.setId(vo.getId());
            po.setName(vo.getName());
            po.setLayoutInfo(json);
            po.setCreateDate(vo.getCreateDate());
            po.setUpdateDate(vo.getUpdateDate());
            po.setCreateUser(vo.getCreateUser());
            return po;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<LayoutInfo> getLayoutInfo() {
        return layoutInfo;
    }

    public void setLayoutInfo(List<LayoutInfo> layoutInfo) {
        this.layoutInfo = layoutInfo;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }
}
