packageco.dongchen.shop.common.model;importcom.google.common.collect.Lists;importorg.springframework.web.multipart.MultipartFile;importjava.util.List;publicclassProduct{privateLongid;privateIntegercreatedAt;privateIntegerupdatedAt;privateStringsku;privateStringname;privateIntegerstock;privateIntegerprice;privateBooleanisEnabled;privateStringimages;privateStringdescription;privateLonguserId;// NEWLY ADDED CODE 1 BEGIN
privateList<Long>ids;// NEWLY ADDED CODE 1 END
privateStringmainImage;privateList<String>imageList=Lists.newArrayList();privateBooleanisFavourite;privateList<MultipartFile>imageFiles;publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicIntegergetCreatedAt(){returncreatedAt;}publicvoidsetCreatedAt(IntegercreatedAt){this.createdAt=createdAt;}publicIntegergetUpdatedAt(){returnupdatedAt;}publicvoidsetUpdatedAt(IntegerupdatedAt){this.updatedAt=updatedAt;}publicStringgetSku(){returnsku;}publicvoidsetSku(Stringsku){this.sku=sku;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicIntegergetStock(){returnstock;}publicvoidsetStock(Integerstock){this.stock=stock;}publicIntegergetPrice(){returnprice;}publicvoidsetPrice(Integerprice){this.price=price;}publicBooleangetEnabled(){returnisEnabled;}publicvoidsetEnabled(Booleanenabled){isEnabled=enabled;}publicStringgetImages(){returnimages;}publicvoidsetImages(Stringimages){this.images=images;}publicStringgetDescription(){returndescription;}publicvoidsetDescription(Stringdescription){this.description=description;}publicLonggetUserId(){returnuserId;}publicvoidsetUserId(LonguserId){this.userId=userId;}// NEWLY ADDED CODE 2 BEGIN
publicList<Long>getIds(){returnids;}publicvoidsetIds(List<Long>ids){this.ids=ids;}// NEWLY ADDED CODE 2 END
publicStringgetMainImage(){returnmainImage;}publicvoidsetMainImage(StringmainImage){this.mainImage=mainImage;}publicList<String>getImageList(){returnimageList;}publicvoidsetImageList(List<String>imageList){this.imageList=imageList;}publicBooleangetFavourite(){returnisFavourite;}publicvoidsetFavourite(Booleanfavourite){isFavourite=favourite;}publicList<MultipartFile>getImageFiles(){returnimageFiles;}publicvoidsetImageFiles(List<MultipartFile>imageFiles){this.imageFiles=imageFiles;}}
Update product.xml
Copy and paste the following content into the product.xml file
under the service/src/main/resources/mapper directory:
1
2
3
4
5
6
<iftest="productCondition.ids != null and productCondition.ids.size() > 0 ">
and p.id in
<foreachcollection="productCondition.ids"item="item"index="index"open="("separator=","close=")">
#{item}
</foreach></if>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="co.dongchen.shop.mapper.ProductMapper"><sqlid="productFields">
p.id, p.created_at, p.updated_at, p.sku, p.name, p.stock, p.price, p.is_enabled, p.images, p.description, p.user_id
</sql><sqlid="whereCondition"><where><iftest="productCondition.id != null and productCondition.id != 0">
and p.id = #{productCondition.id}
</if><iftest="productCondition.isEnabled != null and productCondition.isEnabled == true">
and p.is_enabled = 1
</if><iftest="productCondition.isEnabled != null and productCondition.isEnabled == false">
and p.is_enabled = 0
</if><iftest="productCondition.name != null and productCondition.name != ''"><bindname="likeName"value="'%' + productCondition.name + '%'"/>
and p.name like #{likeName}
</if><!-- NEWLY ADDED CODE BEGIN --><iftest="productCondition.ids != null and productCondition.ids.size() > 0 ">
and p.id in
<foreachcollection="productCondition.ids"item="item"index="index"open="("separator=","close=")">
#{item}
</foreach></if><!-- NEWLY ADDED CODE END --></where></sql><selectid="queryPagedProducts"resultType="product">
select <includerefid="productFields"/>
from product p
<includerefid="whereCondition"/><iftest="pageHelperCondition.offSet != null and pageHelperCondition.limit != null">
limit #{pageHelperCondition.offSet},#{pageHelperCondition.limit}
</if><iftest="pageHelperCondition.offSet == null and pageHelperCondition.limit != null">
limit #{pageHelperCondition.limit}
</if></select><selectid="queryPagedCount"resultType="long">
select count(id)
from product p
<includerefid="whereCondition"/></select></mapper>
Update ProductService.java
Change the following method’s accessibility from private
to public in the ProductService.java file under the
service/src/main/co.dongchen.shop/service directory:
packageco.dongchen.shop.service;importco.dongchen.shop.common.model.Product;importco.dongchen.shop.common.util.PageHelper;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importredis.clients.jedis.Jedis;importjava.util.List;importjava.util.Set;importjava.util.stream.Collectors;@ServicepublicclassTopProductService{privatestaticfinalStringTOP_PRODUCT_KEY="TOP_PRODUCT_KEY";@AutowiredprivateProductServiceproductService;publicvoidincrease(Longid){Jedisjedis=newJedis("127.0.0.1");// Adds the score value of the member named the product id in the TOP_PRODUCT_KEY sorted set to one and returns the new score value of the member.
jedis.zincrby(TOP_PRODUCT_KEY,1.0D,String.valueOf(id));// Removes members after the fifth of the top five ranked members in the TOP_PRODUCT_KEY sorted set and returns the number of removed members.
jedis.zremrangeByRank(TOP_PRODUCT_KEY,5,-1);jedis.close();}privateList<Long>getTopIds(){Jedisjedis=newJedis("127.0.0.1");// Retrieve all members of the sorted set named TOP_PRODUCT_KEY.
Set<String>ids=jedis.zrevrange(TOP_PRODUCT_KEY,0,-1);jedis.close();returnids.stream().map(Long::parseLong).collect(Collectors.toList());}publicList<Product>getTopProducts(intsize){ProductproductCondition=newProduct();List<Long>topIds=getTopIds();// Retrieve minimum available products from the beginning.
topIds=topIds.subList(0,Math.min(topIds.size(),size));if(topIds.isEmpty()){returnLists.newArrayList();}productCondition.setIds(getTopIds());finalList<Long>orderedIds=topIds;List<Product>products=productService.queryWithImgs(productCondition,PageHelper.init(size,1));Ordering<Product>productOrdering=Ordering.natural().onResultOf(ps->{returnorderedIds.indexOf(ps.getId());});returnproductOrdering.sortedCopy(products);}}
Update Product Controller
Add the following code into ProductController.java file under
the controller/src/main/java/co.dongchen.shop/controller
directory: