Commit 80fe0f99 authored by chenzhao's avatar chenzhao

货位信息表同步 修改id

parent 99edb783
package com.yeejoin.amos.boot.biz.common.utils;
public class SnowFlakeGenerateIdWorker {
/**
* 开始时间截
*/
private final long twepoch = 1420041600000L;
/**
* 机器id所占的位数
*/
private final long workerIdBits = 5L;
/**
* 数据标识id所占的位数
*/
private final long datacenterIdBits = 5L;
/**
* 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
*/
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
/**
* 支持的最大数据标识id,结果是31
*/
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
/**
* 序列在id中占的位数
*/
private final long sequenceBits = 12L;
/**
* 机器ID向左移12位
*/
private final long workerIdShift = sequenceBits;
/**
* 数据标识id向左移17位(12+5)
*/
private final long datacenterIdShift = sequenceBits + workerIdBits;
/**
* 时间截向左移22位(5+5+12)
*/
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
/**
* 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
*/
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
/**
* 工作机器ID(0~31)
*/
private long workerId;
/**
* 数据中心ID(0~31)
*/
private long datacenterId;
/**
* 毫秒内序列(0~4095)
*/
private long sequence = 0L;
/**
* 上次生成ID的时间截
*/
private long lastTimestamp = -1L;
/**
* 构造函数
*
* @param workerId 工作ID (0~31)
* @param datacenterId 数据中心ID (0~31)
*/
public SnowFlakeGenerateIdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* 获得下一个ID (该方法是线程安全的)
*
* @return long
*/
public synchronized long nextId() {
long timestamp = timeGen();
timestamp = generateId(timestamp);
return ((timestamp - twepoch) << timestampLeftShift) //
| (datacenterId << datacenterIdShift) //
| (workerId << workerIdShift) //
| sequence;
}
private long generateId(long timestamp){
//如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
if(timestamp < lastTimestamp){
throw new RuntimeException(
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
//如果是同一时间生成的,则进行毫秒内序列
if(lastTimestamp == timestamp)
{
sequence = (sequence + 1) & sequenceMask;
//毫秒内序列溢出
if(sequence == 0)
//阻塞到下一个毫秒,获得新的时间戳
timestamp = tilNextMillis(lastTimestamp);
}
else//时间戳改变,毫秒内序列重置
{
sequence = 0L;
}
//上次生成ID的时间截
lastTimestamp = timestamp;
return timestamp;
}
/**
*获得下一个ID (string)
**/
public synchronized String generateNextId() {
long timestamp = timeGen();
timestamp = generateId(timestamp);
//移位并通过或运算拼到一起组成64位的ID
return String.valueOf(((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift)
| sequence);
}
/**
* 阻塞到下一个毫秒,直到获得新的时间戳
*
* @param lastTimestamp 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* 返回以毫秒为单位的当前时间
*
* @return 当前时间(毫秒)
*/
protected long timeGen() {
return System.currentTimeMillis();
}
}
...@@ -249,6 +249,7 @@ public class OrgPersonController extends BaseController { ...@@ -249,6 +249,7 @@ public class OrgPersonController extends BaseController {
@TycloudOperation( ApiLevel = UserType.AGENCY) @TycloudOperation( ApiLevel = UserType.AGENCY)
@RequestMapping(value = "/listAllByCurrentUserALL", method = RequestMethod.GET) @RequestMapping(value = "/listAllByCurrentUserALL", method = RequestMethod.GET)
@ApiOperation(httpMethod = "GET", value = "列表分页查询(表单用)", notes = "列表分页查询(表单用)") @ApiOperation(httpMethod = "GET", value = "列表分页查询(表单用)", notes = "列表分页查询(表单用)")
@PersonIdentify
public ResponseModel<Object> listAllByCurrentUserALL() { public ResponseModel<Object> listAllByCurrentUserALL() {
Map<String, Object> req = new HashMap<>(); Map<String, Object> req = new HashMap<>();
ReginParams reginParams = getSelectedOrgInfo(); ReginParams reginParams = getSelectedOrgInfo();
......
...@@ -11,6 +11,7 @@ import com.google.common.collect.Lists; ...@@ -11,6 +11,7 @@ import com.google.common.collect.Lists;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams; import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.dto.OrgMenuDto; import com.yeejoin.amos.boot.biz.common.dto.OrgMenuDto;
import com.yeejoin.amos.boot.biz.common.excel.ExcelUtil; import com.yeejoin.amos.boot.biz.common.excel.ExcelUtil;
import com.yeejoin.amos.boot.biz.common.utils.SnowFlakeGenerateIdWorker;
import com.yeejoin.amos.component.feign.model.FeignClientResult; import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.component.feign.utils.FeignUtil; import com.yeejoin.amos.component.feign.utils.FeignUtil;
import com.yeejoin.amos.feign.morphic.Morphic; import com.yeejoin.amos.feign.morphic.Morphic;
...@@ -82,6 +83,9 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i ...@@ -82,6 +83,9 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i
private IFormGroupService iFormGroupService; private IFormGroupService iFormGroupService;
@Autowired @Autowired
IWarehouseService iWarehouseService;
@Autowired
private IEqSourceFileService iEqSourceFileService; private IEqSourceFileService iEqSourceFileService;
@Autowired @Autowired
...@@ -361,10 +365,10 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i ...@@ -361,10 +365,10 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i
private void synWarehouse(Map<String, Object> formKeyMap) { private void synWarehouse(Map<String, Object> formKeyMap) {
String parentId = (String) formKeyMap.get("parentId"); String parentId = (String) formKeyMap.get("parentId");
if ("0".equals(parentId)) { String stuctureName = formInstanceMapper.getStuctureName(parentId);
formKeyMap.put("address", formKeyMap.get("name")); if (StringUtils.isEmpty(stuctureName)){
} else { formKeyMap.put("address",formKeyMap.get("name"));
String stuctureName = formInstanceMapper.getStuctureName(parentId); }else {
formKeyMap.put("address", stuctureName + "-" + formKeyMap.get("name")); formKeyMap.put("address", stuctureName + "-" + formKeyMap.get("name"));
} }
formInstanceMapper.saveStucture(formKeyMap); formInstanceMapper.saveStucture(formKeyMap);
...@@ -1668,6 +1672,12 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i ...@@ -1668,6 +1672,12 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i
String orgCode = null; String orgCode = null;
String companyName = null; String companyName = null;
String dutyUser = null; String dutyUser = null;
//代码执行前 需清空数据库原有数据 涉及 wl_warehouse、wl_warehouse_structure、wl_form_instance。
formInstanceMapper.delete(null);
iWarehouseStructureService.getBaseMapper().delete(null);
iWarehouseService.getBaseMapper().delete(null);
if (!ObjectUtils.isEmpty(reginParams) && !ObjectUtils.isEmpty(reginParams.getPersonIdentity())) { if (!ObjectUtils.isEmpty(reginParams) && !ObjectUtils.isEmpty(reginParams.getPersonIdentity())) {
if (!ObjectUtils.isEmpty(reginParams.getPersonIdentity().getBizOrgCode())) { if (!ObjectUtils.isEmpty(reginParams.getPersonIdentity().getBizOrgCode())) {
companyCode = reginParams.getPersonIdentity().getBizOrgCode(); companyCode = reginParams.getPersonIdentity().getBizOrgCode();
...@@ -1705,11 +1715,23 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i ...@@ -1705,11 +1715,23 @@ public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> i
} }
private void importBuildMessage(List<BuildingImportDto> collect, String orgCode, String bizOrgName, String bizOrgCode, String dutyUser) { private void importBuildMessage(List<BuildingImportDto> collect, String orgCode, String bizOrgName, String bizOrgCode, String dutyUser) {
// 处理建筑数据、入库 // 处理建筑数据、入库 并在wl_warehouse插入跟数据
//避免id重复且sourceId与id保持一致 通过雪花算法生成
SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker =
new SnowFlakeGenerateIdWorker(0L, 0L);
long id = snowFlakeGenerateIdWorker.nextId();
Warehouse warehouse = new Warehouse();
warehouse.setName(orgCode);
warehouse.setCompanyName(bizOrgName);
warehouse.setSourceCode("10000");
warehouse.setId(id);
warehouse.setSourceId(id);
iWarehouseService.getBaseMapper().insert(warehouse);
collect.forEach(item -> { collect.forEach(item -> {
HashMap<String, Object> buildingData = new HashMap<>(); HashMap<String, Object> buildingData = new HashMap<>();
// 建筑默认父级id 为顶级id // 建筑默认父级id 为顶级id
buildingData.put("parentId", 0); buildingData.put("parentId", id);
// 建筑类型 // 建筑类型
String groupCode = null; String groupCode = null;
if (!ObjectUtils.isEmpty(item.getBuildingType())) { if (!ObjectUtils.isEmpty(item.getBuildingType())) {
......
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