From 48e73a708b0760ec063e960e1eda6da99e8ee15b Mon Sep 17 00:00:00 2001 From: XinYi Song <2037158277@qq.com> Date: Wed, 15 Dec 2021 16:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=95=86=E5=AE=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/controller/ProductController.java | 51 +++++++++ .../java/com/xkrs/model/qo/ProductQo.java | 21 ++++ .../java/com/xkrs/service/ProductService.java | 20 ++++ .../xkrs/service/impl/ProductServiceImpl.java | 100 ++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 src/main/java/com/xkrs/controller/ProductController.java create mode 100644 src/main/java/com/xkrs/service/ProductService.java create mode 100644 src/main/java/com/xkrs/service/impl/ProductServiceImpl.java diff --git a/src/main/java/com/xkrs/controller/ProductController.java b/src/main/java/com/xkrs/controller/ProductController.java new file mode 100644 index 0000000..a41ba1a --- /dev/null +++ b/src/main/java/com/xkrs/controller/ProductController.java @@ -0,0 +1,51 @@ +package com.xkrs.controller; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.common.tool.TokenUtil; +import com.xkrs.dao.SysUserDao; +import com.xkrs.model.entity.SysUserEntity; +import com.xkrs.model.qo.ProductQo; +import com.xkrs.service.ProductService; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * @Author: XinYi Song + * @Date: 2021/12/15 15:42 + */ +@RestController +public class ProductController { + + @Resource + private ProductService productService; + + @Resource + private SysUserDao sysUserDao; + + /** + * 添加商品信息 + * @param productQo + * @param token + * @return + */ + @PostMapping("/addingProduct") + public String addingProduct(@RequestBody ProductQo productQo, @RequestHeader(value="Authorization") String token){ + // 获取区域信息 + Locale locale = LocaleContextHolder.getLocale(); + // 验证token + String tokenUserName = TokenUtil.getTokenUserName(token); + SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); + if(sysUserEntity == null){ + return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR,"您还没有注册登录,请先注册登录",locale); + } + return productService.addingProduct(productQo,sysUserEntity.getId()); + } +} diff --git a/src/main/java/com/xkrs/model/qo/ProductQo.java b/src/main/java/com/xkrs/model/qo/ProductQo.java index 4dc6111..d3ab239 100644 --- a/src/main/java/com/xkrs/model/qo/ProductQo.java +++ b/src/main/java/com/xkrs/model/qo/ProductQo.java @@ -4,6 +4,7 @@ import com.xkrs.model.validation.ProductQoInsert; import javax.validation.constraints.NotBlank; import java.math.BigDecimal; +import java.util.List; /** * @Author: XinYi Song @@ -34,6 +35,10 @@ public class ProductQo { @NotBlank(message = "{Product.productPrice.null}",groups={ProductQoInsert.class}) private BigDecimal productPrice; + private List productCover; + + private List productContent; + public String getProductName() { return productName; } @@ -65,4 +70,20 @@ public class ProductQo { public void setProductPrice(BigDecimal productPrice) { this.productPrice = productPrice; } + + public List getProductCover() { + return productCover; + } + + public void setProductCover(List productCover) { + this.productCover = productCover; + } + + public List getProductContent() { + return productContent; + } + + public void setProductContent(List productContent) { + this.productContent = productContent; + } } diff --git a/src/main/java/com/xkrs/service/ProductService.java b/src/main/java/com/xkrs/service/ProductService.java new file mode 100644 index 0000000..72d42ce --- /dev/null +++ b/src/main/java/com/xkrs/service/ProductService.java @@ -0,0 +1,20 @@ +package com.xkrs.service; + +import com.xkrs.model.qo.ProductQo; + +import java.util.List; + +/** + * @Author: XinYi Song + * @Date: 2021/12/15 15:38 + */ +public interface ProductService { + + /** + * 添加商品信息 + * @param productQo 商品信息 + * @param business 商家用户id + * @return + */ + String addingProduct(ProductQo productQo,Integer business); +} diff --git a/src/main/java/com/xkrs/service/impl/ProductServiceImpl.java b/src/main/java/com/xkrs/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..fc03422 --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/ProductServiceImpl.java @@ -0,0 +1,100 @@ +package com.xkrs.service.impl; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.BusinessDao; +import com.xkrs.dao.ProductContentPhotoDao; +import com.xkrs.dao.ProductCoverDao; +import com.xkrs.dao.ProductDao; +import com.xkrs.model.entity.BusinessEntity; +import com.xkrs.model.entity.ProductContentPhotoEntity; +import com.xkrs.model.entity.ProductCoverEntity; +import com.xkrs.model.entity.ProductEntity; +import com.xkrs.model.qo.ProductQo; +import com.xkrs.service.ProductService; +import com.xkrs.utils.DateTimeUtil; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * @Author: XinYi Song + * @Date: 2021/12/15 15:38 + */ +@Service +public class ProductServiceImpl implements ProductService { + + @Resource + private ProductDao productDao; + + @Resource + private BusinessDao businessDao; + + @Resource + private ProductCoverDao productCoverDao; + + @Resource + private ProductContentPhotoDao productContentPhotoDao; + + /** + * 添加商品信息 + * @param productQo 商品信息 + * @param business 商家用户id + * @return + */ + @Override + public String addingProduct(ProductQo productQo, Integer business) { + Locale locale = LocaleContextHolder.getLocale(); + // 商品封面图片 + List productCover = productQo.getProductCover(); + // 商品内容图片 + List productContent = productQo.getProductContent(); + BusinessEntity byBusinessId = businessDao.findByBusinessId(business); + if(byBusinessId == null){ + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"您还未入驻,请先入驻!",locale); + } + ProductEntity byProductName = productDao.findByProductName(productQo.getProductName()); + if(byProductName != null){ + return outputEncapsulationObject(PromptMessageEnum.FILE_EXISTS,"您已添加该商品,请勿重复添加!",locale); + } + if(productCover == null || productCover.size() == 0 || productContent == null || productContent.size() == 0){ + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"商品封面或商品内容图片不能未空!",locale); + } + ProductEntity productEntity = new ProductEntity(); + productEntity.setProductName(productQo.getProductName()); + productEntity.setProductDescription(productQo.getProductDescription()); + productEntity.setProductCategories(productQo.getProductCategories()); + productEntity.setProductPrice(productQo.getProductPrice()); + productEntity.setBusinessId(business); + productEntity.setShelfType("0"); + productEntity.setProductTime(DateTimeUtil.dateTimeToString(LocalDateTime.now())); + + List coverEntities = new ArrayList<>(); + for(String cover : productCover){ + ProductCoverEntity productCoverEntity = new ProductCoverEntity(); + productCoverEntity.setProductCoverPhoto(cover); + productCoverEntity.setProductId(productEntity.getId()); + coverEntities.add(productCoverEntity); + } + productCoverDao.saveAll(coverEntities); + + List contentPhotoEntities = new ArrayList<>(); + for(String content : productContent){ + ProductContentPhotoEntity productContentPhotoEntity = new ProductContentPhotoEntity(); + productContentPhotoEntity.setProductContentPhoto(content); + productContentPhotoEntity.setProductId(productEntity.getId()); + contentPhotoEntities.add(productContentPhotoEntity); + } + productContentPhotoDao.saveAll(contentPhotoEntities); + + productDao.save(productEntity); + + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"商品添加成功!",locale); + } +}