批量创建房屋改为多线程方式 --renpc

This commit is contained in:
Renpc-kilig 2023-11-20 11:52:00 +08:00
parent 451f13aff7
commit 2f4995963f
3 changed files with 87 additions and 22 deletions

View File

@ -4,6 +4,7 @@ import com.cm.common.base.AbstractService;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.bos.UserInfoBO;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.DateUtil;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.population.dao.house.IHouseDao;
@ -30,9 +31,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @ClassName: HouseServiceImpl
@ -398,6 +405,10 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
@Override
public void saveAuto(String token, AutoHouseDTO autoHouseDTO) {
UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
List<HouseVO> houseVOList = new ArrayList<>();
String buildingId = autoHouseDTO.getBuildingId();
BuildingDTO buildingDTO = buildingService.get(buildingId);
@ -411,6 +422,7 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
for (int unit = 1; unit <= unitNum; unit++) {
for (int floor = 1; floor <= floorsNum; floor++) {
for (int house = 1; house <= houseCount; house++) {
HouseVO houseVO = new HouseVO();
// 基本信息街道社区小区楼栋
houseVO.setStreet(buildingDTO.getStreet());
@ -425,13 +437,65 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
// 房屋信息单元楼层房号
houseVO.setAffiliationUnit("" + unit);
houseVO.setAffiliationFloors(String.valueOf(floor));
for (int house = 1; house <= houseCount; house++) {
houseVO.setHouseNum(floor + "0" + house);
save(token, houseVO);
}
}
System.out.println(floor + "0" + house);
houseVOList.add(houseVO);
}
}
}
int size = houseVOList.size();
int numThreads = size / 5 + (size % 5 == 0 ? 0 : 1); // 计算需要的线程数
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < size; i += 5) {
int end = Math.min(i + 5, size);
List<HouseVO> subList = houseVOList.subList(i, end);
executorService.submit(new saveHouse(subList, userInfoBO));
}
executorService.shutdown();
}
}
public class saveHouse implements Runnable {
private List<HouseVO> dataList;
private UserInfoBO userInfoBO;
public saveHouse(List<HouseVO> dataList, UserInfoBO userInfoBO) {
this.dataList = dataList;
this.userInfoBO = userInfoBO;
}
@Override
public void run() {
for (HouseVO houseVO : dataList) {
String houseId = UUIDUtil.getUUID();
Map<String, Object> params = null;
try {
params = HashMapUtil.beanToMap(houseVO);
} catch (Exception e) {
e.printStackTrace();
}
params.put("houseId", houseId);
if (userInfoBO != null) {
this.setSave(userInfoBO.getUserId(), params);
} else {
this.setSave("1", params);
}
houseDao.save(params);
}
}
private void setSave(String userId, Map<String, Object> params) {
String currentDate = DateUtil.getTime();
params.put("creator", userId);
params.put("gmtCreate", currentDate);
params.put("modifier", userId);
params.put("gmtModified", currentDate);
params.put("isDelete", 0);
}
}
}

View File

@ -32,11 +32,11 @@ public class AreaUtils {
@PostConstruct
public void init() {
PopulationInfoStaticService = PopulationInfoService;
initArea0();
/*initArea0();
initArea1();
initArea2();
initArea3();
initArea4();
initArea4();*/
aree.put(0, area0);
aree.put(1, area1);
aree.put(2, area2);

View File

@ -795,6 +795,7 @@
hp.population_info_id = #{populationInfoId}
)
</if>
ORDER BY house_num ASC
</select>
<!-- 房院管理列表 -->