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.TagRule;
import com.liangjian.dataplatform.member.entity.vo.datanodes.PDM;
import com.liangjian.dataplatform.member.entity.vo.tagrules.ConditionNode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/9 13:45
 *
 * 标签规则的Vo
 */
@ApiModel("标签规则")
public class TagRuleVo implements Serializable {
    private static final long serialVersionUID = 440935302795521828L;

    @ApiModelProperty("id")
    private String id;

    @ApiModelProperty("规则名称")
    private String name;

    @ApiModelProperty("规则用途")
    private String  desc;

    @ApiModelProperty("面向对象")
    private Integer object;

    @ApiModelProperty("面向对象范围")
    private List<String> objectRange;

    @ApiModelProperty("产出标签方式")
    private Integer outputType;

    @ApiModelProperty("产出标签的内容")
    private List<ConditionNode> outputEntity;

    @ApiModelProperty("条件相关表")
    private PDM pdm;

    @ApiModelProperty("作业id")
    private String jobId;

    @ApiModelProperty("作业状态")
    private String jobInfo;

    @ApiModelProperty("创建时间")
    private Date crtDate;

    @ApiModelProperty("更新时间")
    private Date lastUpdate;

    @ApiModelProperty("创建用户")
    private String crtUser;

    public static TagRuleVo transfer(TagRule tagRule) {
        TagRuleVo vo = new TagRuleVo();
        vo.setId(tagRule.getId());
        vo.setName(tagRule.getName());
        vo.setDesc(tagRule.getDesc());
        vo.setObject(tagRule.getObject());
        vo.setObjectRange(Arrays.asList(tagRule.getObjectRange().split(",")));
        vo.setOutputType(tagRule.getOutputType());

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectReader reader = objectMapper.readerFor(new TypeReference<List<ConditionNode>>() {});
//        List<ConditionNode> conditionNodes = GsonUtil.getInstance()
//                .fromJson(tagRule.getOutputEntity(), new TypeToken<List<ConditionNode>>(){}.getType());
        ObjectReader pdmReader = objectMapper.readerFor(PDM.class);
        try{
            List<ConditionNode> conditionNodes = reader.readValue(tagRule.getOutputEntity());
            vo.setOutputEntity(conditionNodes);

            if(tagRule.getRelativeTable() != null && !tagRule.getRelativeTable().isEmpty()) {
                PDM pdm = pdmReader.readValue(tagRule.getRelativeTable());
                vo.setPdm(pdm);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }


        if(tagRule.getJobId() != null && !tagRule.getJobId().isEmpty()) {
            vo.setJobId(tagRule.getJobId());
            try {
                String jobInfoStr = tagRule.getJobInfo().getInfoStr();
                vo.setJobInfo(jobInfoStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }

        vo.setCrtDate(tagRule.getCrtDate());
        vo.setLastUpdate(tagRule.getLastUpdate());
        vo.setCrtUser(tagRule.getCrtUser());
        return vo;
    }

    public static TagRule reTanslate(TagRuleVo vo) {
        TagRule po = new TagRule();
        po.setId(vo.getId());
        po.setName(vo.getName());
        po.setDesc(vo.getDesc());
        po.setObject(vo.getObject());
        po.setObjectRange(vo.getObjectRange().stream().reduce((i,j) -> i+","+j).orElse(""));
        po.setOutputType(vo.getOutputType());
//        po.setOutputEntity(GsonUtil.getInstance().toJson(vo.getOutputEntity()));
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(vo.getOutputEntity());
            po.setOutputEntity(json);

            if(vo.getPdm() != null) {
                String pdmJs = objectMapper.writeValueAsString(vo.getPdm());
                po.setRelativeTable(pdmJs);
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        po.setJobId(vo.getJobId());
        po.setCrtDate(vo.getCrtDate());
        po.setLastUpdate(vo.getLastUpdate());
        po.setCrtUser(vo.getCrtUser());
        return po;
    }

    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 String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getObject() {
        return object;
    }

    public void setObject(Integer object) {
        this.object = object;
    }

    public List<String> getObjectRange() {
        return objectRange;
    }

    public void setObjectRange(List<String> objectRange) {
        this.objectRange = objectRange;
    }

    public Integer getOutputType() {
        return outputType;
    }

    public void setOutputType(Integer outputType) {
        this.outputType = outputType;
    }

    public List<ConditionNode> getOutputEntity() {
        return outputEntity;
    }

    public void setOutputEntity(List<ConditionNode> outputEntity) {
        this.outputEntity = outputEntity;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getJobInfo() {
        return jobInfo;
    }

    public void setJobInfo(String jobInfo) {
        this.jobInfo = jobInfo;
    }

    public Date getCrtDate() {
        return crtDate;
    }

    public void setCrtDate(Date crtDate) {
        this.crtDate = crtDate;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public String getCrtUser() {
        return crtUser;
    }

    public void setCrtUser(String crtUser) {
        this.crtUser = crtUser;
    }

    public PDM getPdm() {
        return pdm;
    }

    public void setPdm(PDM pdm) {
        this.pdm = pdm;
    }
}
