package com.brilliance.isc.common.lock;

import lombok.*;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class LockInfo {

    private String objType;

    private String objInr;

    private String usrId;

    private String trnName;

    private String trnLabel;

    /**
     * 正常的业务交易是可重入的
     * 后台服务则不允许重入
     */
    private boolean reentrant = true;

    public String genLockKey(){
        return "bizlock#"+objType+"#"+objInr;
    }

    public String genLockValue(){
        return usrId+"#"+trnName+"#"+reentrant;
    }

    public String getUsrId() {
        return usrId;
    }

    public LockInfo(String key, String value){
       this(key);
        String[] valArr = value.split("#");
        usrId = valArr[0];
        trnName = valArr[1];
        if(valArr.length == 3){
            reentrant = Boolean.parseBoolean(valArr[2]);
        }
    }

    public LockInfo(String key) {
        String[] keyArr = key.split("#");
        objType = keyArr[1];
        objInr = keyArr[2];
    }
}
