/**
* @return
* @Date 14:05 2022/4/11
* @Param
**/
@Transactional(rollbackFor = Exception.class)
public ExecuteResult updateConnectList(List<YsOrderClothes> orderClothesList) {
List<Long> collect = orderClothesList.stream().map(YsOrderClothes::getId).collect(Collectors.toList());
//每批次执行数量
int batchCount = 1000;
//获取list中总数量
int sourListSize = collect.size();
//需要分多少批
int subCount = sourListSize % batchCount == 0 ? sourListSize / batchCount : sourListSize / batchCount + 1;
int startIndext = 0;
int stopIndext = 0;
for (int i = 0; i < subCount; i++) {
stopIndext = (i == subCount - 1) ? getStopIndext(batchCount, sourListSize, stopIndext) : stopIndext + batchCount;
List<Long> list = new ArrayList<>(collect.subList(startIndext, stopIndext));
if (CollUtil.isNotEmpty(list)){
boolean update = orderClothesService.lambdaUpdate().set(YsOrderClothes::getJjIng, 1).in(YsOrderClothes::getId, list).update();
}
startIndext = stopIndext;
}
/**
* 起线程执行
*/
orderClothesConnect.threadUpdateConnect(orderClothesList,sysUser, factoryId);
return ExecuteResult.ok();
}
/**
* 计算stop值
* @param batchCount 一共分多少次
* @param sourListSize 总数
* @param stopIndext 停止节点
* @return
*/
private static int getStopIndext(int batchCount, int sourListSize, int stopIndext) {
if (sourListSize == batchCount) {
return batchCount +stopIndext;
}else if(sourListSize % batchCount == 0){
return stopIndext + batchCount;
}else {
return sourListSize % batchCount + stopIndext;
}
}
/**
* @program: laundry-service
* @ClassName Othe
* @description:
* @author: XZY
* @create: 2022-11-04 16:30
* @Version 1.0
**/
@Service
@Slf4j
public class OrderClothesConnect {
@Autowired
private YsOrderClothesService ysOrderClothesService;
@Async("threadPoolTaskExecutor")
public void threadUpdateConnect(List<YsOrderClothes> orderClothesList, SysUser sysUser, Long factoryId){
log.info("多线程开始执行{}, threadId:{}, threadName:{} ","updateConnectList", Thread.currentThread().getId(), Thread.currentThread().getName());
ysOrderClothesService.extracted(orderClothesList,sysUser, factoryId);
log.info("多线程执行完毕{}, threadId:{}, threadName:{}", "updateConnectList", Thread.currentThread().getId(), Thread.currentThread().getName());
}
}
/**
* @param orderClothesList
* @param sysUser
* @param factoryId
*/
@Transactional(rollbackFor = Exception.class)
public void extracted(List<YsOrderClothes> orderClothesList,SysUser sysUser, Long factoryId) {
//业务逻辑
}