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

import cn.estsh.i3plus.pojo.base.annotation.AnnoOutputColumn;
import cn.estsh.i3plus.pojo.base.annotation.DynamicField;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.WmsEnumUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @Description :
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-04 15:58
 * @Modify:
 **/
@Data
@MappedSuperclass
@EntityListeners(BaseBeanListener.class)    //对象状态监听
//@Entity
//以子类table为准
//@javax.persistence.Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
//@JsonInclude(value = JsonInclude.Include.NON_EMPTY) //""或null属性不参加序列转换
public abstract class BaseBean implements Serializable {

    private static final long serialVersionUID = 1L;

    //此处使用hibernate的主键策略方式
    //手动设置,使用iplus-platform-common中的idtool生成
    //将Long类型系列化成String避免精度丢失
    @Id
    @GeneratedValue(generator = "IDGenerator")
    @GenericGenerator(name = "IDGenerator", strategy = "assigned")
    @Column(name="ID")
    @ApiParam(value = "主键",example = "0")
    @JsonSerialize(using = ToStringSerializer.class)
    @AnnoOutputColumn(hidden = true)
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.NUMBER, isRequire = 2)
    public Long id;

    //get单独处理
    public Long getId() {
        if(id != null) {
            return id.longValue();
        }else{
            return id;
        }
    }

    /**
     * MonogoDB _id映射
     * @param id
     */
    @JSONField(name="_id")
    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="ORGANIZE_CODE")
    @ApiParam(value ="组织代码")
//    @AnnoOutputColumn(hidden = true)
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.TEXT, isQuery = 2, isRequire = 2)
    public String organizeCode;

    @Column(name="IS_VALID", columnDefinition = "int default 1")
    @ApiParam(value = "有效性",example = "1")
    @AnnoOutputColumn(refClass = WmsEnumUtil.TRUE_OR_FALSE.class,refForeignKey = "value",value = "description", hidden = true)
    public Integer isValid;    //CommonEnumUtil.IS_VAILD;

    @Column(name="IS_DELETED", columnDefinition = "int default 2")
    @ApiParam(value = "是否已删除",example = "2")
    @AnnoOutputColumn(hidden = true)
    //逻辑删除，软删除
    public Integer isDeleted;    //CommonEnumUtil.TRUE_OR_FALSEA

    @Column(name="CREATE_USER",updatable = false)
    @ApiParam(value = "创建用户")
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.LIST, getValWay = CommonEnumUtil.DYNAMIC_FIELD_GET_WAY.URL,
            dataSrc = "/impp/operate/sys-user/query-user-by-pager",
            searchColumnName = "userName", listColumnName = "userLoginName,userName", explicitColumnName = "userName")
    public String createUser;

    @Column(name="CREATE_DATE_TIME",updatable = false)
    @ApiParam(value = "创建日期")
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.DATETIME, isRequire = 2)
    public String createDatetime;

    @Column(name="MODIFY_USER")
    @ApiParam(value = "修改人")
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.LIST, getValWay = CommonEnumUtil.DYNAMIC_FIELD_GET_WAY.URL,
            dataSrc = "/impp/operate/sys-user/query-user-by-pager",
            searchColumnName = "userName", listColumnName = "userLoginName,userName", explicitColumnName = "userName")
    public String modifyUser;

    @Column(name="MODIFY_DATE_TIME")
    @ApiParam(value = "修改日期")
    @DynamicField(webFieldType = CommonEnumUtil.FIELD_TYPE.DATETIME, isRequire = 2)
    public String modifyDatetime;

    @Transient
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(value="创建日期查询用，查询起始日期",example = "2018-01-01 01:00:00")
    @AnnoOutputColumn(hidden = true)
    public String createDateTimeStart;

    @Transient
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(value="创建日期查询用，查询结束日期",example = "2018-12-31 23:59:59")
    @AnnoOutputColumn(hidden = true)
    public String createDateTimeEnd;

    @Transient
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(value="修改日期查询用，查询起始日期",example = "2018-01-01 01:00:00")
    @AnnoOutputColumn(hidden = true)
    public String modifyDateTimeStart;

    @Transient
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(value = "修改日期查询用，查询结束日期",example = "2018-12-31 23:59:59")
    @AnnoOutputColumn(hidden = true)
    public String modifyDateTimeEnd;

    @Transient
    @ApiParam(value = "排序属性")
    @AnnoOutputColumn(hidden = true)
    public String orderByParam = "";

    @Transient
    @ApiParam(value = "排序类型，1 正序，2 倒序",example = "1")
    @AnnoOutputColumn(hidden = true)
    //CommonEnumUtil.ASC_OR_DESC  1 asc,2 desc
    public transient Integer ascOrDesc = 1;

    public int getIsValidVal() {
        return this.isValid == null ? 0 : this.isValid;
    }

    public int getIsDeletedVal() {
        return this.isDeleted == null ? 0 : this.isDeleted;
    }

//    @Transient
//    @ApiParam(value = "多列排序")
//    @AnnoOutputColumn(hidden = true)
//    public LinkedHashMap<String,Integer> sortParamMap;

    //排序方式
    public String orderBy(){
        String result = "";
        if (orderByParam!=null&&orderByParam.trim().length()>0) {
            result = " order by " + orderByParam;
            if(ascOrDesc == CommonEnumUtil.ASC_OR_DESC.ASC.getValue()) {
                result += " asc";
            }else{
                result += " desc";
            }
        }
        return result;
    }

}
