Commit 2bd5a004 authored by suhuiguang's avatar suhuiguang

1.代码优化增加抽象类

parent bb4feabb
package com.yeejoin.amos.boot.module.jg.biz.service.impl;
package com.yeejoin.amos.boot.module.jg.biz.context;
import com.yeejoin.amos.boot.module.jg.api.service.IEquipUsedCheck;
import org.springframework.beans.BeansException;
......
package com.yeejoin.amos.boot.module.jg.biz.service.impl;
import com.yeejoin.amos.boot.module.jg.api.dto.FlowingEquipRedisKeyDTO;
import com.yeejoin.amos.boot.module.jg.api.service.IEquipUsedCheck;
import com.yeejoin.amos.boot.module.jg.biz.context.FlowingEquipRedisContext;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author Administrator
*/
@Slf4j
public abstract class BaseEquipUsedCheckService implements IEquipUsedCheck {
/**
* 并发校验(页面同时提交或者页面同时打开多个时,某些设备都是为使用状态)校验设备流程在用状态及更新不在用为再使用状态
*
* @param record 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void equipRepeatUsedCheck(String record, String companyCode) {
RLock lock = getRedisClient().getLock(this.getRepeatUsedCheckLockKey(companyCode, getApplyBizType(), record));
try {
boolean isLocked = lock.tryLock(0, 180, TimeUnit.SECONDS);
if (!isLocked) {
log.error("设备:{}在被其他业务使用,加锁失败", record);
throw new BadRequest("存在其他业务在使用设备,请刷新后重试!");
}
RBucket<Set<String>> RBucket = getRedisClient().getBucket(this.getFlowingEquipRedisKey(companyCode, getApplyBizType()));
Set<String> equipListOfUsed = RBucket.get();
if (equipListOfUsed != null && equipListOfUsed.contains(record)) {
throw new BadRequest("存在设备正在同时被其他流程使用,请刷新后重试!");
}
if (equipListOfUsed == null || equipListOfUsed.isEmpty()) {
equipListOfUsed = new TreeSet<>();
}
equipListOfUsed.add(record);
getRedisClient().getBucket(this.getFlowingEquipRedisKey(companyCode, getApplyBizType())).set(equipListOfUsed);
FlowingEquipRedisContext.setRedisKeyInfo(new FlowingEquipRedisKeyDTO(this.getFlowingEquipRedisKey(companyCode, getApplyBizType()), Collections.singletonList(record)));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
private String getRepeatUsedCheckLockKey(String companyCode, String bizType, String record) {
return String.format("%s-%s-%s-%s", "JG_REPEAT_CHECK", companyCode, bizType, record);
}
String getFlowingEquipRedisKey(String companyCode, String type) {
return String.format("%s:%s:%s", "EQUIP_IN_FLOWING", companyCode, type);
}
/**
* 删除流程中的数据,释放redis空间(异常处理及驳回到发起节点、撤回到发起节点、完成时时进行)
*
* @param records 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void delDataForCheckEquipRepeatUsed(List<String> records, String companyCode) {
RBucket<Set<String>> rBucket = getRedisClient().getBucket(this.getFlowingEquipRedisKey(companyCode, getApplyBizType()));
Set<String> equipListOfUsed = rBucket.get();
equipListOfUsed = equipListOfUsed.stream().filter(record -> !records.contains(record)).collect(Collectors.toSet());
rBucket.set(equipListOfUsed);
}
/**
* 删除流程中的数据,释放redis空间(异常处理及驳回到发起节点、撤回到发起节点、完成时时进行)
*
* @param records 设备唯一标识
* @param redisKey key
*/
@Override
public void delDataForCheckWithKey(List<String> records, String redisKey) {
delRedisData(records, redisKey, getRedisClient());
}
@Override
public Set<String> getEquipInFlow(String companyCode) {
RBucket<Set<String>> rBucket = getRedisClient().getBucket(this.getFlowingEquipRedisKey(companyCode, getApplyBizType()));
return rBucket.get();
}
private void delRedisData(List<String> records, String redisKey, RedissonClient redissonClient) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(redisKey);
Set<String> equipListOfUsed = rBucket.get();
equipListOfUsed = equipListOfUsed.stream().filter(record -> !records.contains(record)).collect(Collectors.toSet());
rBucket.set(equipListOfUsed);
}
public abstract RedissonClient getRedisClient();
public abstract String getApplyBizType();
@Override
public String applyBizType() {
return getApplyBizType();
}
}
......@@ -16,6 +16,7 @@ import com.yeejoin.amos.boot.module.jg.api.enums.CompanyTypeEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.ConstructionEnum;
import com.yeejoin.amos.boot.module.jg.api.mapper.CommonMapper;
import com.yeejoin.amos.boot.module.jg.api.mapper.JgUseRegistrationMapper;
import com.yeejoin.amos.boot.module.jg.biz.context.EquipUsedCheckStrategyContext;
import com.yeejoin.amos.boot.module.jg.biz.dao.ESEquipmentCategory;
import com.yeejoin.amos.boot.module.jg.biz.service.*;
import com.yeejoin.amos.boot.module.ymt.api.dto.ESEquipmentCategoryDto;
......
package com.yeejoin.amos.boot.module.jg.biz.service.impl;
import com.yeejoin.amos.boot.module.jg.api.dto.CompanyEquipCountDto;
import com.yeejoin.amos.boot.module.jg.api.dto.FlowingEquipRedisKeyDTO;
import com.yeejoin.amos.boot.module.jg.api.mapper.JgInstallationNoticeMapper;
import com.yeejoin.amos.boot.module.jg.api.service.IEquipUsedCheck;
import com.yeejoin.amos.boot.module.jg.biz.context.FlowingEquipRedisContext;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -21,7 +17,7 @@ import java.util.stream.Collectors;
*/
@Component
@Slf4j
public class InstallNoticeEquipUsedCheckImpl implements IEquipUsedCheck {
public class InstallNoticeEquipUsedCheckImpl extends BaseEquipUsedCheckService {
private RedissonClient redissonClient;
......@@ -35,90 +31,22 @@ public class InstallNoticeEquipUsedCheckImpl implements IEquipUsedCheck {
}
/**
* 并发校验(页面同时提交或者页面同时打开多个时,某些设备都是为使用状态)校验设备流程在用状态及更新不在用为再使用状态
*
* @param record 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void equipRepeatUsedCheck(String record, String companyCode) {
RLock lock = redissonClient.getLock(this.getRepeatUsedCheckLockKey(companyCode, bizType, record));
try {
boolean isLocked = lock.tryLock(0, 180, TimeUnit.SECONDS);
if (!isLocked) {
log.error("设备:{}在被其他业务使用,加锁失败", record);
throw new BadRequest("存在其他业务在使用设备,请刷新后重试!");
}
RBucket<Set<String>> RBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
Set<String> equipListOfUsed = RBucket.get();
if (equipListOfUsed != null && equipListOfUsed.contains(record)) {
throw new BadRequest("存在设备正在同时被其他流程使用,请刷新后重试!");
}
if (equipListOfUsed == null || equipListOfUsed.isEmpty()) {
equipListOfUsed = new TreeSet<>();
}
equipListOfUsed.add(record);
redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType)).set(equipListOfUsed);
FlowingEquipRedisContext.setRedisKeyInfo(new FlowingEquipRedisKeyDTO(this.getFlowingEquipRedisKey(companyCode, bizType), Collections.singletonList(record)));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
private String getRepeatUsedCheckLockKey(String companyCode, String bizType, String record) {
return String.format("%s-%s-%s-%s", "JG_REPEAT_CHECK", companyCode, bizType, record);
}
private String getFlowingEquipRedisKey(String companyCode, String type) {
return String.format("%s:%s:%s", "EQUIP_IN_FLOWING", companyCode, type);
}
/**
* 删除流程中的数据,释放redis空间(异常处理及驳回到发起节点、撤回到发起节点、完成时时进行)
*
* @param records 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void delDataForCheckEquipRepeatUsed(List<String> records, String companyCode) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
Set<String> equipListOfUsed = rBucket.get();
equipListOfUsed = equipListOfUsed.stream().filter(record -> !records.contains(record)).collect(Collectors.toSet());
rBucket.set(equipListOfUsed);
public RedissonClient getRedisClient() {
return redissonClient;
}
/**
* 删除流程中的数据,释放redis空间(异常处理及驳回到发起节点、撤回到发起节点、完成时时进行)
*
* @param records 设备唯一标识
* @param redisKey key
*/
@Override
public void delDataForCheckWithKey(List<String> records, String redisKey) {
UseRegisterEquipUsedCheckImpl.delRedisData(records, redisKey, redissonClient);
}
@Override
public Set<String> getEquipInFlow(String companyCode) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
return rBucket.get();
}
@Override
public String applyBizType() {
public String getApplyBizType() {
return bizType;
}
@Override
public void init() {
List<CompanyEquipCountDto> companyEquipCountDtos = installationNoticeMapper.queryForFlowingEquipList();
companyEquipCountDtos.forEach(c -> {
RBucket<Set<String>> rBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(c.getCompanyCode(), bizType));
RBucket<Set<String>> rBucket = redissonClient.getBucket(getFlowingEquipRedisKey(c.getCompanyCode(), bizType));
rBucket.set(Arrays.stream(c.getRecords().split(",")).collect(Collectors.toSet()));
});
}
......
......@@ -26,6 +26,7 @@ import com.yeejoin.amos.boot.module.jg.api.mapper.JgInstallationNoticeMapper;
import com.yeejoin.amos.boot.module.jg.api.mapper.JgUseRegistrationMapper;
import com.yeejoin.amos.boot.module.jg.api.service.IJgInstallationNoticeService;
import com.yeejoin.amos.boot.module.jg.api.vo.SortVo;
import com.yeejoin.amos.boot.module.jg.biz.context.EquipUsedCheckStrategyContext;
import com.yeejoin.amos.boot.module.jg.biz.context.FlowingEquipRedisContext;
import com.yeejoin.amos.boot.module.jg.biz.feign.TzsServiceFeignClient;
import com.yeejoin.amos.boot.module.jg.biz.service.ICmWorkflowService;
......
......@@ -29,6 +29,7 @@ import com.yeejoin.amos.boot.module.jg.api.mapper.JgUseRegistrationEqMapper;
import com.yeejoin.amos.boot.module.jg.api.mapper.JgUseRegistrationMapper;
import com.yeejoin.amos.boot.module.jg.api.service.IJgUseRegistrationService;
import com.yeejoin.amos.boot.module.jg.api.vo.SortVo;
import com.yeejoin.amos.boot.module.jg.biz.context.EquipUsedCheckStrategyContext;
import com.yeejoin.amos.boot.module.jg.biz.context.FlowingEquipRedisContext;
import com.yeejoin.amos.boot.module.jg.biz.feign.TzsServiceFeignClient;
import com.yeejoin.amos.boot.module.jg.biz.service.ICommonService;
......
......@@ -2,16 +2,14 @@ package com.yeejoin.amos.boot.module.jg.biz.service.impl;
import com.yeejoin.amos.boot.module.jg.api.dto.CompanyEquipCountDto;
import com.yeejoin.amos.boot.module.jg.api.mapper.JgUseRegistrationMapper;
import com.yeejoin.amos.boot.module.jg.api.service.IEquipUsedCheck;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -19,7 +17,7 @@ import java.util.stream.Collectors;
*/
@Component
@Slf4j
public class UseRegisterEquipUsedCheckImpl implements IEquipUsedCheck {
public class UseRegisterEquipUsedCheckImpl extends BaseEquipUsedCheckService {
private RedissonClient redissonClient;
......@@ -32,73 +30,18 @@ public class UseRegisterEquipUsedCheckImpl implements IEquipUsedCheck {
this.useRegistrationMapper = useRegistrationMapper;
}
/**
* 并发校验(页面同时提交或者页面同时打开多个时,某些设备都是为使用状态)校验设备流程在用状态及更新不在用为再使用状态
*
* @param record 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void equipRepeatUsedCheck(String record, String companyCode) {
RLock lock = redissonClient.getLock(this.getRepeatUsedCheckLockKey(companyCode, bizType, record));
try {
boolean isLocked = lock.tryLock(0, 180, TimeUnit.SECONDS);
if (!isLocked) {
log.error("设备:{}在被其他业务使用,加锁失败", record);
throw new BadRequest("存在其他业务在使用设备,请刷新后重试!");
}
RBucket<Set<String>> RBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
Set<String> equipListOfUsed = RBucket.get();
if (equipListOfUsed != null && equipListOfUsed.contains(record)) {
throw new BadRequest("存在设备正在同时被其他流程使用,请刷新后重试!");
}
if (equipListOfUsed == null || equipListOfUsed.isEmpty()) {
equipListOfUsed = new TreeSet<>();
}
equipListOfUsed.add(record);
redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType)).set(equipListOfUsed);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
private String getRepeatUsedCheckLockKey(String companyCode, String bizType, String record) {
return String.format("%s-%s-%s-%s", "JG_REPEAT_CHECK", companyCode, bizType, record);
}
private String getFlowingEquipRedisKey(String companyCode, String type) {
return String.format("%s:%s:%s", "EQUIP_IN_FLOWING", companyCode, type);
}
/**
* 删除流程中的数据,释放redis空间(异常处理及驳回到发起节点、撤回到发起节点、完成时时进行)
*
* @param records 设备唯一标识
* @param companyCode 登录人的公司代码
*/
@Override
public void delDataForCheckEquipRepeatUsed(List<String> records, String companyCode) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
Set<String> equipListOfUsed = rBucket.get();
equipListOfUsed = equipListOfUsed.stream().filter(record -> !records.contains(record)).collect(Collectors.toSet());
rBucket.set(equipListOfUsed);
}
@Override
public Set<String> getEquipInFlow(String companyCode) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(this.getFlowingEquipRedisKey(companyCode, bizType));
return rBucket.get();
public RedissonClient getRedisClient() {
return redissonClient;
}
@Override
public String applyBizType() {
public String getApplyBizType() {
return bizType;
}
@Override
public void init() {
List<CompanyEquipCountDto> companyEquipCountDtos = useRegistrationMapper.queryForFlowingEquipList();
......@@ -108,15 +51,4 @@ public class UseRegisterEquipUsedCheckImpl implements IEquipUsedCheck {
});
}
@Override
public void delDataForCheckWithKey(List<String> records, String redisKey) {
delRedisData(records, redisKey, redissonClient);
}
static void delRedisData(List<String> records, String redisKey, RedissonClient redissonClient) {
RBucket<Set<String>> rBucket = redissonClient.getBucket(redisKey);
Set<String> equipListOfUsed = rBucket.get();
equipListOfUsed = equipListOfUsed.stream().filter(record -> !records.contains(record)).collect(Collectors.toSet());
rBucket.set(equipListOfUsed);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment