package com.izouma.nineth.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.izouma.nineth.annotations.Searchable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * 用户资金账户
 */
@Data
@Entity
@Table(indexes = {
        @Index(columnList = "userId", unique = true),
})
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("用户积分")
public class UserIntegral extends BaseEntity{

    @ApiModelProperty("用户ID")
    private Long userId;

    @Searchable
    @ApiModelProperty("用户手机号")
    private String phone;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("可用积分")
    private Long availableIntegral;

    @ApiModelProperty("冻结积分")
    private Long freezeIntegral;

    @ApiModelProperty("消耗积分")
    private Long consumeIntegral;

    @ApiModelProperty("月消耗积分")
    private Long monthConsumeIntegral;

    @ApiModelProperty("月统计起始时间")
    private LocalDateTime monthStartTime;

    @ApiModelProperty("最后变动时间")
    private LocalDateTime lastChangeTime;

    @JsonProperty(access=JsonProperty.Access.READ_ONLY)
    public Long getTotalIntegral(){
        Long totalIntegral = 0L;
        if(this.getAvailableIntegral() != null){
            totalIntegral += this.getAvailableIntegral();
        }
        if(this.getFreezeIntegral() != null){
            totalIntegral += this.getFreezeIntegral();
        }
        return totalIntegral;
    }


}
