package cn.estsh.i3plus.pojo.mdm.bean.base;

import cn.estsh.i3plus.pojo.base.annotation.AnnoOutputColumn;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MdmEnumUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

/**
 * @Description : MDM基础bean
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2020-05-12 17:30
 * @Modify:
 **/
@Data
@MappedSuperclass
@ApiModel("MDM基础bean")
public abstract class BaseMdmBean extends BaseBean {

    private static final long serialVersionUID = 3957595289035947441L;

    @Transient
    @ApiModelProperty("是否主数据")
    @AnnoOutputColumn(refClass = CommonEnumUtil.TRUE_OR_FALSE.class,hidden = true)
    public static Integer isMdmMaster = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();

    @Column(name = "MDM_VERSION")
    @ApiModelProperty("mdm数据版本")
    @JsonSerialize(using = ToStringSerializer.class)
    @AnnoOutputColumn(hidden = true)
    private Long mdmVersion;

    public long getMdmVersionVal() {
        return mdmVersion == null ? 0 : mdmVersion;
    }

    // 版本递增
    public long nextMdmVersion() {
        mdmVersion = getMdmVersionVal() + 1;
        return mdmVersion;
    }

    @Column(name = "IS_MDM_PUBLISHED")
    @ApiModelProperty("是否发布")
    @AnnoOutputColumn(refClass = CommonEnumUtil.TRUE_OR_FALSE.class,hidden = true)
    private Integer isMdmPublished;

    @Column(name = "MDM_OPERATE_TYPE")
    @ApiModelProperty("操作类型")
    @AnnoOutputColumn(refClass = MdmEnumUtil.MDM_OPERATE_TYPE.class,hidden = true)
    private Integer mdmOperateType;

    public Integer getMdmOperateTypeVal() {
        return mdmOperateType == null ? MdmEnumUtil.MDM_OPERATE_TYPE.INSERT.getValue() : mdmOperateType;
    }

    public String getMdmOperateTypeTxt(){
        return MdmEnumUtil.MDM_OPERATE_TYPE.valueOfDescription(getMdmOperateTypeVal());
    }

    @Column(name = "MDM_REF_ID")
    @ApiModelProperty("关联id")
    @JsonSerialize(using = ToStringSerializer.class)
    @AnnoOutputColumn(hidden = true)
    private Long mdmRefId;

    //排序方式
    @Override
    public String orderBy() {
        String result = "";
        if (orderByParam != null && orderByParam.trim().length() > 0) {
            result = " order by ";
            if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == isMdmMaster) {
                result += "master.";
            } else {
                result += "busi.";
            }

            result += orderByParam;

            if (ascOrDesc == CommonEnumUtil.ASC_OR_DESC.ASC.getValue()) {
                result += " asc";
            } else {
                result += " desc";
            }
        }
        return result;
    }
}
