Commit 834b90fb authored by 覃振观's avatar 覃振观 👶

23-12-12: 所有主键改为数字类型。产品编码使用单独字段显示。

parent 2deeb9f9
......@@ -54,12 +54,14 @@ function faceGrid(elId, data) {
}
function save() {
$('#submitForm').ajaxSubmit({
url: web_app.name + '/product/saveProductAndDetail.ajax',
param:{ rawData: JSON.stringify(rawData) },
success: function(data) {
debugger;
}
checkUpdata();
let upData = window.dataSet.upData;
if(!upData) { return; }
Public.ajax(web_app.name + "/product/saveProductAndDetail.ajax", {
data: JSON.stringify(upData)
}, function (data) {
debugger;
});
}
......
package com.huigou.topsun.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.topsun.util.Snowflake;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* AppConfig Bean配置
*
* @author qinzhenguan
* @createDate 2023/12/12 08:38
**/
@Configuration
public class AppConfig {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
@Bean
public Snowflake snowflake() { return new Snowflake(1); }
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ import javax.persistence.Persistence;
* JPAUtils 用于管理 JPA EntityManager 创建工厂
*
* @author qinzhenguan
* @create 2023/11/28 08:58
* @createDate 2023/11/28 08:58
**/
public class JPAUtils {
......
......@@ -34,11 +34,11 @@ public interface ProductApplication {
/**
* description 对比所有对象,并保存更改的数据。
* @param rawMap 原数据
* @param updateMap 提交的数据
*
* @param rawMap 原数据
* @return java.util.HashMap<java.lang.String, java.lang.Object>
* @author qinzhenguan
* @createDate 2023/12/8 17:19
*/
HashMap<String, Object> saveProductAndDetail(HashMap<String, Object> rawMap, HashMap<String, Object> updateMap);
HashMap<String, Object> saveOrUpdataOnAllDetail(HashMap<String, Object> newData);
}
......@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.topsun.product.application.ProductApplication;
import com.huigou.topsun.product.domain.*;
import com.huigou.topsun.product.repository.*;
import com.huigou.topsun.util.Snowflake;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
......@@ -19,12 +20,14 @@ import java.util.stream.Collectors;
/**
* @author 16508
* @description 针对表【product(产品)】的数据库操作Service实现
* @createDate 2023-11-22 10:24:31
* @@createDate 2023-11-22 10:24:31
*/
@Service
@RequiredArgsConstructor //通过构造方法注入 ProductApplication(Class a) { this.a = a; }
public class ProductApplicationImpl implements ProductApplication {
private final ObjectMapper objectMapper;
private final Snowflake snowflake;
private final ProductRepository productRepository;
private final ProductDetailRepository detailRepository;
// 类型
......@@ -48,25 +51,24 @@ public class ProductApplicationImpl implements ProductApplication {
// Page<Product> productPage = productRepository.findAll(pageRequest);
// mapList.getContent().stream().map(Product::toString).forEach(System.out::println);
ArrayList<Product> productPage = (ArrayList<Product>) productRepository.findAll();
ObjectMapper mapper = new ObjectMapper();
// convert = mapList.stream().collect(Collectors.toMap(index -> index, Function.identity(),
// (oldV, newV) -> oldV));
return mapper.convertValue(productPage, new TypeReference<ArrayList<Map<String, Object>>>() {});
return objectMapper.convertValue(productPage, new TypeReference<ArrayList<Map<String, Object>>>() {});
}
/**
* description 查询产品所有详细信息
* @param productId 产品ID
* @param product 产品ID
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author qinzhenguan
* createDate 2023/11/29 13:19
* @createDate 2023/11/29 13:19
*/
@Override
public Map<String, Object> queryDetailAll(Product product) {
Map<String, Object> resultMap = new HashMap<>(200);
String strProductId = product.getProductId();
ProductDetail productDetail = detailRepository.findByProductId(strProductId);
Long productId = product.getProductId();
ProductDetail productDetail = detailRepository.findByProductId(productId);
// EntityManager manager = JPAUtils.getEntityManger();
// manager.getTransaction().begin();
// manager.flush();
......@@ -80,12 +82,12 @@ public class ProductApplicationImpl implements ProductApplication {
// --------------------------------- 详情查询 ---------------------------------
ArrayList<ProductMaterial> material = (ArrayList<ProductMaterial>) materialRepository.findByProductId(strProductId);
ArrayList<ProductLoss> loss = (ArrayList<ProductLoss>) lossRepository.findByProductId(strProductId);
ArrayList<ProductMaterial> material = (ArrayList<ProductMaterial>) materialRepository.findByProductId(productId);
ArrayList<ProductLoss> loss = (ArrayList<ProductLoss>) lossRepository.findByProductId(productId);
Set<Object> entitys = new HashSet<>();
entitys.add(typesetConfRepository.findByProductId(strProductId));
entitys.add(lookedRepository.findByProductId(strProductId));
entitys.add(publishedConfRepository.findByProductId(strProductId));
entitys.add(typesetConfRepository.findByProductId(productId));
entitys.add(lookedRepository.findByProductId(productId));
entitys.add(publishedConfRepository.findByProductId(productId));
entitys.add(productDetail);
entitys.add(product);
......@@ -104,7 +106,7 @@ public class ProductApplicationImpl implements ProductApplication {
listMaps.put("fronColors", fronColors == null ? new ArrayList<>() : fronColors);
} else {
ProductFaceColor fc = new ProductFaceColor();
fc.setProductFaceColorId("111");
fc.setProductFaceColorId(111L);
ArrayList<ProductFaceColor> fronColors = new ArrayList<>();
fronColors.add(fc);
listMaps.put("fronColors", fronColors);
......@@ -117,11 +119,11 @@ public class ProductApplicationImpl implements ProductApplication {
listMaps.put("backColors", backColors == null ? new ArrayList<>() : backColors);
}
}
ObjectMapper mapper = new ObjectMapper();
listMaps.put("selectedBrand", mapper.convertValue(brands, List.class));
listMaps.put("selectedFactory", mapper.convertValue(factorys, List.class));
listMaps.put("selectedColor", mapper.convertValue(colors, List.class));
listMaps.put("selectedCategory", mapper.convertValue(categorys, List.class));
listMaps.put("selectedBrand", objectMapper.convertValue(brands, List.class));
listMaps.put("selectedFactory", objectMapper.convertValue(factorys, List.class));
listMaps.put("selectedColor", objectMapper.convertValue(colors, List.class));
listMaps.put("selectedCategory", objectMapper.convertValue(categorys, List.class));
resultMap.put("listMaps", listMaps);
// resultMap.put("selectedBrand", convertToMap(Brand.class, "brandName", "brandId", brands));
// resultMap.put("selectedFactory", convertToMap(Factory.class, "factoryName", "factoryId", factorys));
......@@ -132,7 +134,13 @@ public class ProductApplicationImpl implements ProductApplication {
}
@Override
public HashMap<String, Object> saveProductAndDetail(HashMap<String, Object> rawMap, HashMap<String, Object> updateMap) {
public HashMap<String, Object> saveOrUpdataOnAllDetail(HashMap<String, Object> newData) {
Product product = objectMapper.convertValue(newData.get("Product"), Product.class);
ProductDetail productDetail = objectMapper.convertValue(newData.get("ProductDetail"), ProductDetail.class);
ProductLooked productLooked = objectMapper.convertValue(newData.get("ProductLooked"), ProductLooked.class);
if(product != null) {
product.getProductId();
}
return null;
}
......@@ -142,16 +150,16 @@ public class ProductApplicationImpl implements ProductApplication {
* @param entitys 所有相关的 实体 (如果需要 update,保留这些实体。提交时候用于比较更新值。)
* @return java.util.HashMap<java.lang.String, java.lang.Object>
* @author qinzhenguan
* createDate 2023/12/5 14:09
* @createDate 2023/12/5 14:09
*/
public Map<String, Object> assembleResultOfForm(Set<Object> entitys) {
Map<String, Object> map = new HashMap<>(1024);
ObjectMapper mapper = new ObjectMapper();
for(Object item : entitys) {
if(item == null) {
continue;
}
map.put(item.getClass().getSimpleName(), mapper.convertValue(item,
map.put(item.getClass().getSimpleName(), objectMapper.convertValue(item,
new TypeReference<Map<? extends String, ?>>() {}) );
// 这里要获取所有的属性名, 保存对应关系, 属性: 所属实体类;
//map.putAll(mapper.convertValue(item, new TypeReference<Map<? extends String, ?>>() {}));
......@@ -168,7 +176,7 @@ public class ProductApplicationImpl implements ProductApplication {
* @param list 需要转换的 List
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @author qinzhenguan
* createDate 2023/12/1 16:09
* @createDate 2023/12/1 16:09
*/
public Map<Object, Object> convertToMap(Class<?> clazz, String displayProperty, String valueProperty,
List<?> list) {
......@@ -176,8 +184,8 @@ public class ProductApplicationImpl implements ProductApplication {
set.add(displayProperty);
set.add(valueProperty);
if(checkClassesAndProperties(clazz, set, list.size())) {
ObjectMapper mapper = new ObjectMapper();
LinkedList<Map<String, Object>> mapList = mapper.convertValue(list, new TypeReference<LinkedList<Map<String, Object>>>() {});
LinkedList<Map<String, Object>> mapList = objectMapper.convertValue(list, new TypeReference<LinkedList<Map<String, Object>>>() {});
return mapList.stream().collect(Collectors.toMap(map -> map.get(displayProperty), map -> map.get(valueProperty),
(oldV, newV) -> oldV));
}
......@@ -191,7 +199,7 @@ public class ProductApplicationImpl implements ProductApplication {
* @param listSize 需要检查的 ListSize
* @return java.lang.Boolean true of false
* @author qinzhenguan
* createDate 2023/12/1 15:58
* @createDate 2023/12/1 15:58
*/
public Boolean checkClassesAndProperties(Class<?> clazz, Set<String> set, int listSize) {
if(listSize == 0) {
......@@ -224,7 +232,7 @@ public class ProductApplicationImpl implements ProductApplication {
* @param list 需要转换的 List
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @author qinzhenguan
* createDate 2023/11/29 11:36
* @createDate 2023/11/29 11:36
*/
public Map<Object, Object> convertToMap(Class<?> clazz, String propertyName, List<?> list) {
return convertToMap(clazz, propertyName, list, false);
......@@ -238,7 +246,7 @@ public class ProductApplicationImpl implements ProductApplication {
* @param isBean 是否需要转换为实体 (当前应用不需要返回实体)
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @author qinzhenguan
* createDate 2023/11/29 11:34
* @createDate 2023/11/29 11:34
*/
public Map<Object, Object> convertToMap(Class<?> clazz, String propertyName, List<?> list, boolean isBean) {
Map<String, Object> checkMap = checkClassesAndProperties(clazz, propertyName, list.size());
......@@ -247,8 +255,8 @@ public class ProductApplicationImpl implements ProductApplication {
return null;
}
Method method = (Method) checkMap.get("method");
ObjectMapper mapper = new ObjectMapper();
LinkedList<Map<String, Object>> mapList = mapper.convertValue(list, new TypeReference<LinkedList<Map<String, Object>>>() {});
LinkedList<Map<String, Object>> mapList = objectMapper.convertValue(list, new TypeReference<LinkedList<Map<String, Object>>>() {});
Map<Object, Object> resultMap = new HashMap<>(10);
Map<Object, Object> convert = new HashMap<>(1024);
if(isBean) {
......@@ -277,11 +285,11 @@ public class ProductApplicationImpl implements ProductApplication {
/**
* description 检查传入参数是否合法 并返回构造方法
*
* @param clazz Bean
* @param clazz javaBean
* @param propertyName property
* @return java.util.HashMap<java.lang.String, java.lang.Object> { "isOk" : 是否通过检查, "method":获取得到的 method }
* @author qinzhenguan
* createDate 2023/11/29 11:05
* @createDate 2023/11/29 11:05
*/
public HashMap<String, Object> checkClassesAndProperties(Class<?> clazz, String propertyName, int listSize) {
HashMap<String, Object> map = new HashMap<>(33);
......
package com.huigou.topsun.product.controller;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
......@@ -31,6 +30,9 @@ public class ProductController extends CommonController {
@Autowired
private ProductApplication productService;
@Autowired
private ObjectMapper objectMapper;
@Override
protected String getPagePath() {
return "/biz/topsun/product/";
......@@ -56,16 +58,15 @@ public class ProductController extends CommonController {
map4.put("六六六", "666");
map4.put("七七七", "777");
map4.put("八八八", "888");
this.putAttribute("selectedCategory", map4);
return forward("product");
}
public String forwardProductDetail() {
ObjectMapper mapper = new ObjectMapper();
ObjectMapper objectMapper = new ObjectMapper();
SDO sdo = this.getSDO();
HashMap<String, Object> param;
try {
param = mapper.readValue(sdo.getString("data"), new TypeReference<HashMap<String, Object>>() {});
param = objectMapper.readValue(sdo.getString("data"), new TypeReference<HashMap<String, Object>>() {});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
......@@ -73,12 +74,11 @@ public class ProductController extends CommonController {
}
public String loadProductDetail() {
ObjectMapper mapper = new ObjectMapper();
SDO sdo = this.getSDO();
Product product ;
try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
product = mapper.readValue(sdo.getString("data") + "", Product.class);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
product = objectMapper.readValue(sdo.getString("data") + "", Product.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
......@@ -87,15 +87,15 @@ public class ProductController extends CommonController {
return toResult(resultMap);
}
public String saveProductAndDetail() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
public String saveOrUpdataOnAllDetail() throws JsonProcessingException {
SDO sdo = this.getSDO();
String updateJson = sdo.toString();
HashMap<String,Object> updateMap = mapper.readValue(updateJson, new TypeReference<HashMap<String,Object>>() {});
HashMap<String,Object> rawMap = mapper.readValue(updateMap.get("rawData") + "", new TypeReference<HashMap<String,Object>>() {});
updateMap.remove("rawData");
HashMap<String,Object> resultMap = productService.saveProductAndDetail(rawMap, updateMap);
HashMap<String, Object> newData;
try {
newData = objectMapper.readValue(sdo.getString("data"), new TypeReference<HashMap<String, Object>>() {});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
HashMap<String,Object> resultMap = productService.saveOrUpdataOnAllDetail(newData);
return success(resultMap);
}
......
......@@ -18,7 +18,7 @@ public class Brand implements Serializable {
*/
@Id
@Column(name = "brand_id")
private String brandId;
private Long brandId;
/**
* 品牌名称
......
......@@ -18,7 +18,7 @@ public class Color implements Serializable {
*/
@Id
@Column(name = "color_id")
private String colorId;
private Long colorId;
/**
* 颜色名称
......
......@@ -18,7 +18,7 @@ public class Factory implements Serializable {
*/
@Id
@Column(name = "factory_id")
private String factoryId;
private Long factoryId;
/**
* 工厂名称
......
......@@ -21,8 +21,8 @@ public class Product implements Serializable {
* 产品ID
*/
@Id
@Column(name = "product_id", nullable = false, length = 32)
private String productId;
@Column(name = "product_id", nullable = false)
private Long productId;
/**
* 产品名称
......@@ -170,11 +170,11 @@ public class Product implements Serializable {
.toString();
}
public String getProductId() {
public Long getProductId() {
return this.productId;
}
public void setProductId(String productId) {
public void setProductId(Long productId) {
this.productId = productId;
}
......
......@@ -18,7 +18,7 @@ public class ProductCategory implements Serializable {
*/
@Id
@Column(name = "product_category_id")
private String productCategoryId;
private Long productCategoryId;
/**
* 产品类别名称
......
......@@ -22,13 +22,13 @@ public class ProductDetail implements Serializable {
*/
@Id
@Column(name = "product_detail_id", nullable = false, length = 32)
private String productDetailId;
private Long productDetailId;
/**
* 产品ID
*/
@Column(name = "product_id", nullable = true, length = 32)
private String productId;
private Long productId;
/**
* 工厂名称
......@@ -139,19 +139,19 @@ public class ProductDetail implements Serializable {
.toString();
}
public String getProductDetailId() {
public Long getProductDetailId() {
return this.productDetailId;
}
public void setProductDetailId(String productDetailId) {
public void setProductDetailId(Long productDetailId) {
this.productDetailId = productDetailId;
}
public String getProductId() {
public Long getProductId() {
return this.productId;
}
public void setProductId(String productId) {
public void setProductId(Long productId) {
this.productId = productId;
}
......
......@@ -18,7 +18,7 @@ public class ProductFace implements Serializable {
*/
@Id
@Column(name = "product_face_id")
private String productFaceId;
private Long productFaceId;
/**
* 产品版面图片
......
......@@ -18,19 +18,19 @@ public class ProductFaceColor implements Serializable {
*/
@Id
@Column(name = "product_face_color_id")
private String productFaceColorId;
private Long productFaceColorId;
/**
* 产品版面ID
*/
@Column(name = "product_face_id")
private String productFaceId;
private Long productFaceId;
/**
* 颜色ID
*/
@Column(name = "color_id")
private String colorId;
private Long colorId;
/**
* 覆盖率(单位%)
......
......@@ -22,13 +22,13 @@ public class ProductLooked implements Serializable {
*/
@Id
@Column(name = "product_looked_id", nullable = false, length = 32)
private String productLookedId;
private Long productLookedId;
/**
* 产品ID
*/
@Column(name = "product_id", nullable = true, length = 32)
private String productId;
private Long productId;
/**
* 长;单位cm
......@@ -169,19 +169,19 @@ public class ProductLooked implements Serializable {
.toString();
}
public String getProductLookedId() {
public Long getProductLookedId() {
return this.productLookedId;
}
public void setProductLookedId(String productLookedId) {
public void setProductLookedId(Long productLookedId) {
this.productLookedId = productLookedId;
}
public String getProductId() {
public Long getProductId() {
return this.productId;
}
public void setProductId(String productId) {
public void setProductId(Long productId) {
this.productId = productId;
}
......
......@@ -18,13 +18,13 @@ public class ProductLoss implements Serializable {
*/
@Id
@Column(name = "product_loss_id")
private String productLossId;
private Long productLossId;
/**
* 产品ID
*/
@Column(name = "product_id")
private String productId;
private Long productId;
/**
* 产品最大loss数量
......
......@@ -18,13 +18,13 @@ public class ProductMaterial implements Serializable {
*/
@Id
@Column(name = "product_material_id")
private String productMaterialId;
private Long productMaterialId;
/**
* 产品ID
*/
@Column(name = "product_id")
private String productId;
private Long productId;
/**
* 物料ID
......
......@@ -22,13 +22,13 @@ public class ProductProcess implements Serializable {
*/
@Id
@Column(name = "product_process_id", nullable = false, length = 32)
private String productProcessId;
private Long productProcessId;
/**
* 工艺id
*/
@Column(name = "product_technology_id", nullable = true, length = 32)
private String productTechnologyId;
private Long productTechnologyId;
/**
* 生成顺序
......@@ -92,19 +92,19 @@ public class ProductProcess implements Serializable {
.toString();
}
public String getProductProcessId() {
public Long getProductProcessId() {
return this.productProcessId;
}
public void setProductProcessId(String productProcessId) {
public void setProductProcessId(Long productProcessId) {
this.productProcessId = productProcessId;
}
public String getProductTechnologyId() {
public Long getProductTechnologyId() {
return this.productTechnologyId;
}
public void setProductTechnologyId(String productTechnologyId) {
public void setProductTechnologyId(Long productTechnologyId) {
this.productTechnologyId = productTechnologyId;
}
......
......@@ -18,13 +18,13 @@ public class ProductPublishedConf implements Serializable {
*/
@Id
@Column(name = "product_published_conf_id")
private String productPublishedConfId;
private Long productPublishedConfId;
/**
* 产品ID
*/
@Column(name = "product_id")
private String productId;
private Long productId;
/**
* 模数
......
......@@ -22,13 +22,13 @@ public class ProductRiskDescription implements Serializable {
*/
@Id
@Column(name = "product_risk_description_id", nullable = false, length = 32)
private String productRiskDescriptionId;
private Long productRiskDescriptionId;
/**
* 产品id
*/
@Column(name = "product_id", nullable = true, length = 32)
private String productId;
private Long productId;
/**
* 序号
......@@ -67,19 +67,19 @@ public class ProductRiskDescription implements Serializable {
.toString();
}
public String getProductRiskDescriptionId() {
public Long getProductRiskDescriptionId() {
return this.productRiskDescriptionId;
}
public void setProductRiskDescriptionId(String productRiskDescriptionId) {
public void setProductRiskDescriptionId(Long productRiskDescriptionId) {
this.productRiskDescriptionId = productRiskDescriptionId;
}
public String getProductId() {
public Long getProductId() {
return this.productId;
}
public void setProductId(String productId) {
public void setProductId(Long productId) {
this.productId = productId;
}
......
......@@ -21,14 +21,14 @@ public class ProductTechnology implements Serializable {
* 产品工艺id
*/
@Id
@Column(name = "product_technology_id", nullable = false, length = 32)
private String productTechnologyId;
@Column(name = "product_technology_id", nullable = false)
private Long productTechnologyId;
/**
* 产品id
*/
@Column(name = "product_id", nullable = true, length = 32)
private String productId;
@Column(name = "product_id", nullable = true)
private Long productId;
/**
* 产品工艺制程
......@@ -211,19 +211,19 @@ public class ProductTechnology implements Serializable {
.toString();
}
public String getProductTechnologyId() {
public Long getProductTechnologyId() {
return this.productTechnologyId;
}
public void setProductTechnologyId(String productTechnologyId) {
public void setProductTechnologyId(Long productTechnologyId) {
this.productTechnologyId = productTechnologyId;
}
public String getProductId() {
public Long getProductId() {
return this.productId;
}
public void setProductId(String productId) {
public void setProductId(Long productId) {
this.productId = productId;
}
......
......@@ -18,13 +18,13 @@ public class ProductTypesetConf implements Serializable {
*/
@Id
@Column(name = "product_typeset_conf_id")
private String productTypesetConfId;
private Long productTypesetConfId;
/**
* 产品ID
*/
@Column(name = "product_id")
private String productId;
private Long productId;
/**
* 印张长度(单位cm)
......
......@@ -9,5 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductDetailRepository extends JpaRepository<ProductDetail,String> {
ProductDetail findByProductId(String productId);
ProductDetail findByProductId(Long productId);
}
......@@ -10,8 +10,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductLookedRepository extends JpaRepository<ProductLooked,String> {
ProductLooked findByProductId(String productId);
ProductLooked findByProductIdEquals(String productId);
ProductLooked findByProductId(Long productId);
ProductLooked findByProductIdEquals(Long productId);
}
......@@ -11,6 +11,6 @@ import java.util.List;
* @Description:
*/
public interface ProductLossRepository extends JpaRepository<ProductLoss,String> {
List<ProductLoss> findByProductId(String productId);
List<ProductLoss> findByProductId(Long productId);
}
......@@ -11,5 +11,5 @@ import java.util.List;
* @Description:
*/
public interface ProductMaterialRepository extends JpaRepository<ProductMaterial,String> {
List<ProductMaterial> findByProductId(String productId);
List<ProductMaterial> findByProductId(Long productId);
}
......@@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductPublishedConfRepository extends JpaRepository<ProductPublishedConf,String> {
ProductPublishedConf findByProductId(String productId);
ProductPublishedConf findByProductId(Long productId);
ProductPublishedConf getProductPublishedConfByProductId(String productId);
}
......@@ -9,5 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductTypesetConfRepository extends JpaRepository<ProductTypesetConf,String> {
ProductTypesetConf findByProductId(String productId);
ProductTypesetConf findByProductId(Long productId);
}
package com.huigou.topsun.util;
import java.util.concurrent.ThreadLocalRandom;
/**
* |00000000|00000000|00000000|00000000|00000000|00000000|00000000|00000000|
* |--|xxxxxxxxxxxxxx 41bit 时间戳(毫秒) xxxxxxxxxxxxxxx|--|--------|--------|
* |---------------------------------------4bit机器id|xxxx|--------|--------|
* |------------------------------------------16bit自增序列|xxxxxxxx|xxxxxxxx|
*/
public class Snowflake {
// 开始时间
private static final long OFFSET = 1686211627000L;
private static long WORKER_ID ;
/** 机器ID位 **/
private static final long WORKER_ID_BITS = 4L;
/** 自增序列位 **/
private static final long SEQUENCE_ID_BITS = 16L;
private static final long WORKER_SHIFT_BITS = SEQUENCE_ID_BITS;
private static final long OFFSET_SHIFT_BITS = WORKER_ID_BITS + WORKER_SHIFT_BITS;
private static long SEQUENCE_MAX = (1 << SEQUENCE_ID_BITS) - 1;
private static long lastTimestamp = 0L;
private static long sequence = 0L;
public Snowflake(long workerId) {
WORKER_ID = workerId;
}
public static synchronized long nextId() {
long timestamp = timeGen();
if(timestamp < lastTimestamp) {
System.out.println("时钟回拨");
// 可以加入 ‘时钟序列’ 位,并缓存。
}
if(timestamp == lastTimestamp) {
sequence = ++sequence & SEQUENCE_MAX;
// 到达最大序列时等待
if(sequence == 0L) {
timestamp = tilNexMillis(lastTimestamp);
}
//timestamp = sequence != 0 ? timestamp : tilNexMillis(lastTimestamp);
} else {
// 初始序列 随机 1 - 3
sequence = ThreadLocalRandom.current().nextLong(1, 3);
}
lastTimestamp = timestamp;
// 时间戳 | 机器id | 序列号
return (timestamp - OFFSET) << OFFSET_SHIFT_BITS
| (WORKER_ID << WORKER_SHIFT_BITS)
| sequence;
}
protected static long tilNexMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
protected static long timeGen() {
return System.currentTimeMillis();
}
}
package com.huigou.topsun.util;
import org.junit.Test;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
public class TestSnowflake {
public List<Long> list = new CopyOnWriteArrayList<>();
@Test
public void run() throws InterruptedException {
generateTest();
new Snowflake(1);
final Long id = Snowflake.nextId();
Date date = new Date();
date.setTime( (id>>18) + 1686211627000L );
System.out.println(id);
}
public void generateTest()throws InterruptedException {
TreadA t1 = new TreadA();
TreadB t2 = new TreadB();
t1.start();
t2.start();
while (list.size() < 60000) {
Thread.sleep(500);
}
System.out.println(list.size());
Map<Object, Long> map = list.stream().collect(Collectors.groupingBy(l -> l, Collectors.counting()));
Map<Object, Long> sortedByCount = map.entrySet().stream()
.sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
List<Object> countList = map.entrySet().stream().filter(f -> f.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
if(countList.size() > 0) {
System.out.println("x");
}
}
protected static long timeGen() {
return System.currentTimeMillis() / 1000;
}
public class TreadA extends Thread {
public void run() {
super.run();
//long timestamp = timeGen();
for(int i = 0; i < 30000; i++) {
new Snowflake(1);
long id = Snowflake.nextId();
list.add(id);
}
}
}
public class TreadB extends Thread {
public void run() {
super.run();
for(int i = 0; i < 30000; i++) {
new Snowflake(1);
long id = Snowflake.nextId();
list.add(id);
}
}
}
}
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