package cn.autoform.web.controller.resumable.redis.impl;
|
|
import cn.autoform.fw.exception.IEMRuntimeException;
|
import cn.autoform.web.controller.resumable.redis.CacheTool;
|
import cn.autoform.web.controller.resumable.redis.FormLock;
|
import cn.autoform.web.controller.resumable.redis.lock.Lock;
|
import cn.autoform.web.controller.resumable.redis.lock.LocksEntity;
|
import lombok.Setter;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.io.Serializable;
|
|
/**
|
* 表单锁实现类
|
* @author wangyuxin
|
*
|
*/
|
@Service
|
public class FormLockImpl implements FormLock {
|
|
@Resource
|
@Setter
|
private CacheTool<Serializable> jedisCluster;
|
|
@Value("${form.lock.time}")
|
@Setter
|
private long lockTime;
|
|
/**
|
* 表单锁的缓存key
|
*/
|
public static final String LOCK_FORMAT = "form_%s_%d_%d_%d";
|
|
/*
|
* 获取表单锁
|
*/
|
@Override
|
public String getLock(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) {
|
String cacheKey;
|
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class);
|
if(locks == null) {
|
locks = new LocksEntity();
|
}
|
Lock lock = locks.addLock();
|
jedisCluster.add(cacheKey, locks);
|
return lock.getLockInfo();
|
}
|
|
@Override
|
public String getKey(String formId, Integer dataRowNum, Integer tenantId, Integer companyId, String lockInfo) {
|
System.out.println("时间是多少:"+lockTime);
|
String cacheKey;
|
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class);
|
if(locks == null) {
|
throw new IEMRuntimeException("FORM_UPDATE");
|
}
|
Lock lock = locks.addKey(lockInfo, System.currentTimeMillis() + lockTime);
|
jedisCluster.add(cacheKey, locks);
|
return lock.getKey().getKeyInfo();
|
}
|
|
@Override
|
public void unLock(String formId, Integer dataRowNum, Integer tenantId, Integer companyId, String lock,
|
String key) {
|
String cacheKey;
|
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class);
|
// System.out.println(JSONTool.toJson(locks));
|
if(locks == null
|
|| locks.getLock() == null
|
|| !locks.getLock().getLockInfo().equals(lock)
|
|| !locks.getLock().getKey().getKeyInfo().equals(key)) {
|
throw new IEMRuntimeException("FORM_UPDATE");
|
}
|
jedisCluster.delete(cacheKey);
|
}
|
|
public String getKey(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) {
|
return String.format(LOCK_FORMAT, formId, dataRowNum, tenantId, companyId);
|
}
|
|
@Override
|
public Integer getLockSize(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) {
|
LocksEntity locks = jedisCluster.get(getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class);
|
if(locks == null) {
|
return 0;
|
}
|
return locks.getLocks().size();
|
}
|
|
}
|