Commit 2a24fb92 authored by 覃振观's avatar 覃振观 👶

Product 查询

parent 27d11442
package com.huigou.topsun.config;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* JPAUtils 用于管理 JPA EntityManager 创建工厂
*
* @author qinzhenguan
* @create 2023/11/28 08:58
**/
public class JPAUtils {
public static EntityManagerFactory emf = createEntityManagerFactory();
private static EntityManagerFactory createEntityManagerFactory() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mysql-jpa");
return emf;
}
public static EntityManager getEntityManger(){
EntityManager entityManager = emf.createEntityManager();
return entityManager;
}
}
\ No newline at end of file
package com.huigou.topsun.product.application;
import com.huigou.topsun.product.domain.Brand;
import java.util.ArrayList;
import java.util.Map;
/**
* @author 16508
* @description 针对表【brand(品牌)】的数据库操作Service
......@@ -8,4 +13,8 @@ package com.huigou.topsun.product.application;
*/
public interface BrandApplication {
Map<String, Object> querySelectableInFactoryName();
ArrayList<Brand> findAll();
}
package com.huigou.topsun.product.application;
import com.huigou.topsun.product.domain.Product;
import org.springframework.data.domain.Page;
import java.util.Map;
/**
* @author 16508
* @description 针对表【product(产品)】的数据库操作Service
* @createDate 2023-11-22 10:24:31
*/
* @author 16508
* @description 针对表【product(产品)】的数据库操作Service
* @createDate 2023-11-22 10:24:31
*/
public interface ProductApplication {
/**
* description 查询产品列表
* @param page 当前页
* @param size 页大小
* @return org.springframework.data.domain.Page<com.huigou.topsun.product.domain.Product>
* @author qinzhenguan
* @create: 2023/11/29 9:34
*/
Page<Product> findProductPage(int page, int size);
/**
* description 获取产品所有信息
* @param productId 产品ID
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author qinzhenguan
* @create: 2023/11/29 9:33
*/
Map<String, Object> queryDetailAll(int productId);
}
package com.huigou.topsun.product.application.impl;
import com.huigou.topsun.product.application.BrandApplication;
import com.huigou.topsun.product.domain.Brand;
import com.huigou.topsun.product.repository.BrandRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 16508
* @description 针对表【brand(品牌)】的数据库操作Service实现
......@@ -11,6 +19,25 @@ import org.springframework.stereotype.Service;
@Service
public class BrandApplicationImpl implements BrandApplication {
@Autowired
private BrandRepository brandRepository;
@Override
public Map<String, Object> querySelectableInFactoryName() {
ArrayList<Brand> brands = this.findAll();
Map<String, Brand> map = brands.stream().collect(Collectors.toMap(Brand::getBrandName, (b) -> b));
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("map", map);
resultMap.put("list", brands);
return resultMap;
}
@Override
public ArrayList<Brand> findAll() {
return (ArrayList<Brand>) brandRepository.findAll();
}
}
......
package com.huigou.topsun.product.application.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.topsun.config.JPAUtils;
import com.huigou.topsun.product.application.BrandApplication;
import com.huigou.topsun.product.application.ProductApplication;
import com.huigou.topsun.product.domain.*;
import com.huigou.topsun.product.repository.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author 16508
* @description 针对表【product(产品)】的数据库操作Service实现
* @createDate 2023-11-22 10:24:31
*/
* @author 16508
* @description 针对表【product(产品)】的数据库操作Service实现
* @createDate 2023-11-22 10:24:31
*/
@Service
@RequiredArgsConstructor//通过构造方法注入 ProductApplication(Class a) { this.a = a; }
public class ProductApplicationImpl implements ProductApplication {
}
private final ProductRepository productRepository;
private final ProductDetailRepository detailRepository;
// 类型
private final BrandRepository brandRepository;
private final BrandApplication brandService;
private final FactoryRepository factoryRepository;
private final ColorRepository colorRepository;
private final ProductCategoryRepository categoryRepository;
// 详细信息
private final ProductTypesetConfRepository typesetConfRepository;
private final ProductLookedRepository lookedRepository;
private final ProductPublishedConfRepository publishedConfRepository;
private final ProductLossRepository lossRepository;
private final ProductMaterialRepository materialRepository;
private final ProductFaceRepository faceRepository;
private final ProductFaceColorRepository faceColorRepository;
@Override
public Page<Product> findProductPage(int page, int size) {
PageRequest pageRequest = new PageRequest(page, size);
Page<Product> productPage = productRepository.findAll(pageRequest);
productPage.getContent().stream().map(Product::toString).forEach(System.out::println);
return productPage;
}
/**
* description 查询产品所有详细信息
* @param productId 产品ID
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author qinzhenguan
* @create: 2023/11/29 13:19
*/
@Override
public Map<String, Object> queryDetailAll(int productId) {
Map<String, Object> resultMap = new HashMap<>();
ProductDetail productDetail = detailRepository.findByProductIdEquals((long)productId);
resultMap.put("detai", productDetail);
EntityManager manager = JPAUtils.getEntityManger();
manager.getTransaction().begin();
// --------------------------------- 类别查询 ---------------------------------
ArrayList<Brand> brands = (ArrayList<Brand>)brandRepository.findAll();
ArrayList<Factory> factorys = (ArrayList<Factory>)factoryRepository.findAll();
ArrayList<Color> colors = (ArrayList<Color>)colorRepository.findAll();
ArrayList<ProductCategory> categorys = (ArrayList<ProductCategory>)categoryRepository.findAll();
// --------------------------------- 详情查询 ---------------------------------
ProductTypesetConf typesetConf = typesetConfRepository.findByProductIdEquals(Long.toString(productId));
System.err.println(Long.toString(productId));
ProductLooked looked = lookedRepository.findByProductId(Long.toString(productId));
ProductPublishedConf publishedConf = publishedConfRepository.findByProductIdEquals(Long.toString(productId));
ProductLoss loss = lossRepository.findByProductIdEquals(Long.toString(productId));
ProductMaterial material = materialRepository.findByProductIdEquals(Long.toString(productId));
resultMap.put("typesetConf", typesetConf);
resultMap.put("looked", looked);
resultMap.put("publishedConf", publishedConf);
resultMap.put("loss", loss);
resultMap.put("material", material);
// --------------------------------- 版面查询 ---------------------------------
if(productDetail != null) {
if(productDetail.getBackProductFaceId() != null) {
ProductFace frontFace = faceRepository.findByProductFaceIdEquals(productDetail.getBackProductFaceId());
ProductFaceColor fronColor = faceColorRepository.findByProductFaceIdEquals(productDetail.getBackProductFaceId());
resultMap.put("frontFace", frontFace);
resultMap.put("fronColor", fronColor);
}
if(productDetail.getBackProductFaceId() != null) {
ProductFace backFace = faceRepository.findByProductFaceIdEquals(productDetail.getBackProductFaceId());
ProductFaceColor backColor = faceColorRepository.findByProductFaceIdEquals(productDetail.getBackProductFaceId());
resultMap.put("backFace", backFace);
resultMap.put("backColor", backColor);
}
}
manager.flush();
manager.close();
Map<Object, Object> selectedBrand = convertToMap(Brand.class, "brandName", brands);
Map<Object, Object> selectedFactory = convertToMap(Factory.class, "factoryName", factorys);
Map<Object, Object> selectedColor = convertToMap(Color.class, "colorId", colors);
Map<Object, Object> selectedProductCategory = convertToMap(ProductCategory.class, "productCategoryId", categorys);
resultMap.put("selectedBrand", selectedBrand);
resultMap.put("selectedFactory", selectedFactory);
resultMap.put("selectedColor", selectedColor);
resultMap.put("selectedProductCategory", selectedProductCategory);
return resultMap;
}
/**
* description 转换成 Map 不需要转换为实体
* @param clazz Bean
* @param propertyName 连接的 property
* @param list 需要转换的 List
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @author qinzhenguan
* @create: 2023/11/29 11:36
*/
public Map<Object, Object> convertToMap(Class<?> clazz, String propertyName, List<?> list) {
return convertToMap(clazz, propertyName, list, false);
}
/**
* description 转换成 Map
* @param clazz Bean
* @param propertyName 连接的 property
* @param list 需要转换的 List
* @param isBean 是否需要转换为实体
* @return java.util.Map<java.lang.Object, java.lang.Object>
* @author qinzhenguan
* @create: 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());
if(!(Boolean) checkMap.get("isOk")) {
return null;
}
Method method = (Method) checkMap.get("method");
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> mapList = mapper.convertValue(list, List.class);
Map<Object, Object> resultMap = new HashMap<>();
Map<Object, Object> convert = new HashMap<>();
if(isBean) {
// --------------------------------- 实体 ---------------------------------
try {
for (int i = 0; i < mapList.size(); i++) {
Object o = clazz.newInstance();
BeanUtils.populate(o, mapList.get(i));
Object key = method.invoke(o);
convert.put(key, o);
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
// --------------------------------- 非实体 ---------------------------------
convert = mapList.stream().collect(Collectors.toMap(map -> map.get(propertyName), Function.identity(),
(oldV, newV) -> oldV));
//此处仍需处理 Key == null;
}
resultMap.put("map", convert);
resultMap.put("list", list);
return resultMap;
}
/**
* description 检查传入参数是否合法
*
* @param clazz Bean
* @param propertyName property
* @return java.util.HashMap<java.lang.String, java.lang.Object> { "isOk" : 是否通过检查, "method":获取得到的 method }
* @author qinzhenguan
* @create: 2023/11/29 11:05
*/
public HashMap<String, Object> checkClassesAndProperties(Class<?> clazz, String propertyName, int listSize) {
HashMap<String, Object> map = new HashMap<>();
if(listSize == 0) {
System.err.println("[checkClassesAndProperties] : List< "+clazz.getName()+" > 需要转换的列表没有元素。请检查~!");
map.put("isOk", false);
return map;
}
Field[] fields = clazz.getDeclaredFields();
Field field = Arrays.stream(fields).filter(f -> propertyName.equals(f.getName())).findFirst().orElse(null);
if(field == null) {
System.err.println("[checkClassesAndProperties] : " + clazz.getName() + " 无法找到 : " + propertyName + " " +
"该属性。请检查~!");
map.put("isOk", false);
return map;
}
Method[] methods = clazz.getDeclaredMethods();
Method method = Arrays.stream(methods)
.filter(m -> m.getName().toLowerCase().contains("get" + propertyName.toLowerCase())).findFirst().orElse(null);
if (method == null) {
System.err.println("[checkClassesAndProperties] : " + propertyName + " 该属性无法找到构造方法。请检查~!");
map.put("isOk", false);
return map;
}
map.put("isOk", true);
map.put("method", method);
return map;
}
}
package com.huigou.topsun.product.controller;
import com.huigou.topsun.product.domain.Product;
import com.huigou.topsun.product.application.ProductApplication;
import org.springframework.data.domain.Page;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.SDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.Map;
/**
* ProductController
*
* @author qinzhenguan
* @date 2023/11/27 11:19
**/
@Controller
@ControllerMapping("/product")
public class ProductController extends CommonController {
@Autowired
private ProductApplication productService;
/**
* description
* @author qinzhenguan
* @date 2023/11/27 11:49
* @return null
*/
public String findProduct() {//需要特定返回值
SDO sdo = this.getSDO();
int page = sdo.getInteger("page");
int size = sdo.getInteger("size");
Page<Product> productPage = productService.findProductPage(page, size);
return toResult(productPage);
}
public String queryDetailAll() {
SDO sdo = this.getSDO();
int productId = sdo.getInteger("productId");
Map<String, Object> map = productService.queryDetailAll(productId);
return toResult(map);
}
}
\ No newline at end of file
......@@ -54,13 +54,13 @@ public class ProductDetail implements Serializable {
* 产品背面ID
*/
@Column(name = "back_product_face_id")
private Long backProductFaceId;
private String backProductFaceId;
/**
* 产品正面ID
*/
@Column(name = "right_product_face_id")
private Long rightProductFaceId;
private String rightProductFaceId;
/**
* 产品承印物
......
......@@ -4,7 +4,6 @@ import java.io.Serializable;
import javax.persistence.*;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
/**
* 产品外观
......@@ -18,13 +17,7 @@ public class ProductLooked implements Serializable {
* 产品外观ID
*/
@Id
@GeneratedValue(
generator = "system-uuid"
)
@GenericGenerator(
name = "system-uuid",
strategy = "guid"
)
// @GeneratedValue(strategy = GenerationType.IDENTITY) // 使用自增策略
@Column(name = "product_looked_id")
private String productLookedId;
......
......@@ -35,7 +35,7 @@ public class ProductLoss implements Serializable {
/**
* loss比率(单位%)
*/
@Column(name = "product_loss_ rate")
@Column(name = "product_loss_rate")
private Double productLossRate;
/**
......
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductDetailRepository extends JpaRepository<ProductDetail,String> {
ProductDetail findByProductIdEquals(Long productId);
}
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductFaceColorRepository extends JpaRepository<ProductFaceColor,String> {
ProductFaceColor findByProductFaceIdEquals(String productFaceId);
}
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductFaceRepository extends JpaRepository<ProductFace,String> {
ProductFace findByProductFaceIdEquals(String productFaceId);
}
......@@ -4,11 +4,15 @@ import com.huigou.topsun.product.domain.ProductLooked;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author qinzhenguan
* @Auther: xin.lu
* @Date: 2023/11/22/10:42
* @Description:
*/
public interface ProductLookedRepository extends JpaRepository<ProductLooked,String> {
ProductLooked findByProductId(String productId);
ProductLooked findByProductIdEquals(String productId);
ProductLooked getProductLookedByProductId(String productId);
}
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductLossRepository extends JpaRepository<ProductLoss,String> {
ProductLoss findByProductIdEquals(String productId);
}
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductMaterialRepository extends JpaRepository<ProductMaterial,String> {
ProductMaterial findByProductIdEquals(String productId);
}
......@@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductPublishedConfRepository extends JpaRepository<ProductPublishedConf,String> {
ProductPublishedConf findByProductIdEquals(String productId);
ProductPublishedConf getProductPublishedConfByProductId(String productId);
}
......@@ -2,11 +2,13 @@ package com.huigou.topsun.product.repository;
import com.huigou.topsun.product.domain.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @Auther: xin.lu
* @author: xin.lu
* @Date: 2023/11/22/10:40
* @Description:
*/
@Repository
public interface ProductRepository extends JpaRepository<Product,String> {
}
......@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @Description:
*/
public interface ProductTypesetConfRepository extends JpaRepository<ProductTypesetConf,String> {
ProductTypesetConf findByProductIdEquals(String productId);
}
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