bocloud.sms/bocloud.sms.service/src/main/java/com/bocloud/sms/service/GlobalManConfigServiceImpl....

104 lines
4.2 KiB
Java
Raw Normal View History

2024-08-20 09:57:30 +00:00
package com.bocloud.sms.service;
import com.bocloud.sms.entity.GlobalManConfig;
import com.bocloud.sms.interfaces.GlobalManConfigService;
import com.bocloud.sms.repository.GlobalManConfigRepository;
import com.bocloud.sms.service.utils.OrderExceptionQuartz;
import com.megatron.common.model.*;
import com.megatron.common.utils.GridHelper;
import com.megatron.framework.lock.AutoCloseLock;
import com.megatron.framework.lock.LockFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author wpj
* @since 2022/9/8
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class GlobalManConfigServiceImpl implements GlobalManConfigService {
private final GlobalManConfigRepository globalManConfigRepository;
private final LockFactory lockFactory;
@Override
public GeneralResult<GridBean<GlobalManConfig>> list(Pager pager, RequestContext context) {
int total = this.globalManConfigRepository.count(pager.getParams());
List<GlobalManConfig> beans = this.globalManConfigRepository.list(pager);
GridBean<GlobalManConfig> gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, beans);
return new GeneralResult<>(true, gridBean, "查询成功");
}
@Override
public Result create(GlobalManConfig globalManConfig, RequestContext context) {
if (globalManConfigRepository.existCron(globalManConfig.getCron())) {
throw new RuntimeException("数据已存在");
}
globalManConfig.setGmtCreate(new Date());
this.globalManConfigRepository.save(globalManConfig);
return new Result(true, "添加成功");
}
@Override
public Result modify(GlobalManConfig globalManConfig, RequestContext context) {
String path = GlobalManConfig.class.getSimpleName() + "_" + globalManConfig.getId();
try (AutoCloseLock lock = lockFactory.getACLock(path)) {
Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
GlobalManConfig globalConfig = this.globalManConfigRepository.query(globalManConfig.getId());
if (null == globalConfig) {
throw new RuntimeException("数据不存在");
}
BeanUtils.copyProperties(globalManConfig, globalConfig, "gmt_crate");
boolean flag = globalManConfigRepository.update(globalConfig);
return flag ? new Result(true, "修改成功") : new Result(false, "修改失败");
} catch (Exception e) {
log.error("update data fail:", e);
throw new RuntimeException("修改失败", e);
}
}
@Override
public Result remove(Long id, RequestContext context) {
String path = GlobalManConfig.class.getSimpleName() + "_" + id;
try (AutoCloseLock lock = lockFactory.getACLock(path)) {
Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
GlobalManConfig globalManConfig = this.globalManConfigRepository.query(id);
if (null == globalManConfig) {
throw new RuntimeException("数据不存在");
}
globalManConfigRepository.delete(GlobalManConfig.class, id);
return new Result(true, "删除成功");
} catch (Exception e) {
log.error("remove data fail:", e);
throw new RuntimeException("删除数据失败", e);
}
}
@Override
public Result allow(RequestContext context) {
try {
List<GlobalManConfig> globalManConfigList = this.globalManConfigRepository.listAll();
for (GlobalManConfig globalManConfig : globalManConfigList) {
Boolean aBoolean = OrderExceptionQuartz.filterWithCronTime(globalManConfig.getCron(), new Date());
if (aBoolean) {
return new Result(true, "全局运维启动");
}
}
} catch (ParseException e) {
log.error("对比cron时间失败:", e);
}
return new Result(true, "全局运维关闭");
}
}