上代码
/**
* 模版事务
*/
@Resource
private TransactionTemplate transactionTemplate;
/**
* 商户服务项目编辑
*/
@Override
public boolean merchantServeUpdate(MerchantServeOperateReq req) {
//复制商户服务的属性
BaseMerchantServe serve = BeanUtil.copyBean(req, BaseMerchantServe.class);
//将富文本的字符串转为stream 存储到oss中,将返回的链接存储在数据库
String richTextFull = req.getServeDetailIamge();
//请求阿里云,耗时操作
String ossUrl = richTextToOSSUrl(richTextFull);
//服务详情图
serve.setServeDetailIamge(ossUrl);
//服务主图
serve.setServeMasterImage(JSON.toJSONString(req.getServeMasterImage()));
Integer id = req.getId();
//只针对如下做事务操作,也就是锁的时间只有下面一些。
//上面请求阿里云的时间 不占用这一行的行锁时间.
//目的 防止操作这一行数据占用时间过长,其他线程无法操作。
transactionTemplate.execute(action -> {
// 更新服务表
baseMerchantServeMapper.updateById(serve);
//将之前的服务ID删除
baseMerchantProjectService.deleteByServerId(id);
//新增服务项目
List<MerchantServeProjectResp> projectFromFront = req.getMerchantServeProjectResps();
if (CollectionUtils.isEmpty(projectFromFront)) {
return Boolean.TRUE;
}
List<BaseMerchantProject> saveServerList = projectFromFront.stream()
.map(item -> {
BaseMerchantProject project = BeanUtil.copyBean(item, BaseMerchantProject.class);
project.setMerchantServeId(id);
project.setInsertBaseInfo();
return project;
}).collect(Collectors.toList());
baseMerchantProjectService.saveBatch(saveServerList);
return Boolean.TRUE;
});
return Boolean.TRUE;
}