Browse Source

二手商品-相关推荐-浏览量

chao 3 years ago
parent
commit
9b57eca3da

+ 28 - 0
src/main/java/com/caimei365/commodity/controller/SecondHandApi.java

@@ -132,4 +132,32 @@ public class SecondHandApi {
         return secondHandService.getBrandList();
     }
 
+
+    /**
+     * 计算二手商品浏览量
+     */
+    @ApiOperation("计算二手商品浏览量(旧:/product/updateSecondHandProductCount)")
+    @ApiImplicitParam(required = true, name = "productId", value = "商品Id")
+    @GetMapping("/views")
+    public ResponseJson<Integer> updateSecondHandViews(Integer productId){
+        if (null == productId) {
+            return ResponseJson.error("商品Id不能为空!", null);
+        }
+        return secondHandService.updateSecondHandViews(productId);
+    }
+
+    /**
+     * 二手商品-相关推荐
+     *
+     * @param productId 商品Id
+     */
+    @ApiOperation("二手商品-相关推荐(旧:/product/getSecondHandProductRecommend)")
+    @ApiImplicitParam(required = false, name = "productId", value = "商品Id")
+    @GetMapping("/recommend")
+    public ResponseJson<List<SecondListVo>> getSecondRecommends(Integer productId) {
+        if (null == productId) {
+            return ResponseJson.error("商品Id不能为空!", null);
+        }
+        return secondHandService.getSecondRecommends(productId);
+    }
 }

+ 9 - 1
src/main/java/com/caimei365/commodity/mapper/SecondHandMapper.java

@@ -81,9 +81,17 @@ public interface SecondHandMapper {
      * @param secondHandType 二手商品分类,1二手仪器,2临期产品,3其他
      */
     List<SecondListVo> getSecondListByLimit(Integer secondHandType);
-
     /**
      * 商品品牌列表
      */
     List<BrandVo> getBrandList();
+    /**
+     * 更新二手商品浏览量
+     */
+    int updateSecondHandViews(SecondDetailVo second);
+
+    /**
+     * 二手商品-相关推荐
+     */
+    List<SecondListVo> getSecondRecommends(Integer productId);
 }

+ 12 - 0
src/main/java/com/caimei365/commodity/service/SecondHandService.java

@@ -82,4 +82,16 @@ public interface SecondHandService {
      * 商品品牌列表
      */
     ResponseJson<List<BrandVo>> getBrandList();
+
+    /**
+     * 计算二手商品浏览量
+     * @param productId 商品Id
+     */
+    ResponseJson<Integer> updateSecondHandViews(Integer productId);
+
+    /**
+     * 二手商品-相关推荐
+     * @param productId 商品Id
+     */
+    ResponseJson<List<SecondListVo>> getSecondRecommends(Integer productId);
 }

+ 77 - 1
src/main/java/com/caimei365/commodity/service/impl/SecondHandServiceImpl.java

@@ -13,7 +13,6 @@ import com.caimei365.commodity.model.vo.*;
 import com.caimei365.commodity.service.SecondHandService;
 import com.caimei365.commodity.utils.ImageUtils;
 import com.github.pagehelper.PageHelper;
-import io.swagger.v3.oas.annotations.parameters.RequestBody;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
@@ -24,6 +23,7 @@ import org.springframework.util.CollectionUtils;
 import javax.annotation.Resource;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
@@ -472,6 +472,82 @@ public class SecondHandServiceImpl implements SecondHandService {
             return "时间未知";
         }
     }
+    /**
+     * 获取是否在固定天数以内
+     *
+     * @param addtime 需要判断的时间
+     * @param now     当前时间
+     * @param day     多少天内
+     * @return
+     */
+    public boolean isLatestWeek(Date addtime, Date now, int day) {
+        Calendar calendar = Calendar.getInstance();  //得到日历
+        calendar.setTime(now);//把当前时间赋给日历
+        calendar.add(Calendar.DAY_OF_MONTH, -day);  //设置为day天前
+        Date before7days = calendar.getTime();   //得到day天前的时间
+        if (before7days.getTime() < addtime.getTime()) {
+            return true;
+        } else {
+            return false;
+        }
+    }
 
+    /**
+     * 计算二手商品浏览量
+     *
+     * @param productId 商品Id
+     */
+    @Override
+    public ResponseJson<Integer> updateSecondHandViews(Integer productId) {
+        SecondDetailVo second = secondHandMapper.getSecondHandDetail(productId);
+        Integer viewingNum = 0;
+        if (null != second) {
+            viewingNum = second.getViewingNum();
+            /* 更新新浏览量规则
+             * 7天内,每被浏览一次,浏览数添加一个20以内的随机数
+             * 14天内,每被浏览一次,浏览数添加一个10以内的随机数
+             * 30天内,每被浏览一次,浏览数添加一个5以内的随机数
+             * 30天以外,每被浏览一次,浏览数添加1
+             */
+            Date onLine = second.getOnLineDate();
+            int random = 1;
+            if (null != onLine) {
+                boolean latestWeek = isLatestWeek(onLine, new Date(), 7);
+                boolean latestHalfMonth = isLatestWeek(onLine, new Date(), 14);
+                boolean latestMonth = isLatestWeek(onLine, new Date(), 30);
+                if (latestWeek) {
+                    random = (int) (Math.random() * 20 + 1);
+                } else if (latestHalfMonth) {
+                    random = (int) (Math.random() * 10 + 1);
+                } else if (latestMonth) {
+                    random = (int) (Math.random() * 5 + 1);
+                }
+            }
+            if (null != viewingNum) {
+                viewingNum = viewingNum + random;
+                second.setViewingNum(viewingNum);
+            } else {
+                viewingNum = random;
+                second.setViewingNum(random);
+            }
+            secondHandMapper.updateSecondHandViews(second);
+            return ResponseJson.success(viewingNum);
+        } else {
+            return ResponseJson.error("二手商品异常!", null);
+        }
+    }
 
+    /**
+     * 二手商品-相关推荐
+     *
+     * @param productId 商品Id
+     */
+    @Override
+    public ResponseJson<List<SecondListVo>> getSecondRecommends(Integer productId) {
+        List<SecondListVo> recommendList = secondHandMapper.getSecondRecommends(productId);
+        for (SecondListVo product : recommendList) {
+            product.setImage(ImageUtils.getImageURL("product", product.getImage(), 0, domain));
+        }
+        return ResponseJson.success(recommendList);
+    }
 }

+ 20 - 0
src/main/resources/mapper/SecondHandMapper.xml

@@ -34,6 +34,10 @@
             #{visibility}, #{addTime}, #{updateTime}, #{onlineTime}, #{offlineTime}
         )
     </insert>
+    <update id="updateSecondHandViews">
+        UPDATE cm_second_hand_detail SET viewingNum = #{viewingNum}
+        WHERE productID = #{productId}
+    </update>
     <select id="getSeconHandList" resultType="com.caimei365.commodity.model.vo.SecondListVo">
         select
             p.productID as productId,
@@ -182,4 +186,20 @@
 		where status = '1' and delFlag = '0' and description is not null and description != '' and id not in (161)
 		order by sort
     </select>
+    <select id="getSecondRecommends" resultType="com.caimei365.commodity.model.vo.SecondListVo">
+        select
+        p.productID as productId,
+        p.actStatus,
+        p.`name` as name,
+        p.mainImage as image,
+        p.price1 as price,
+        p.brandID as brandId,
+        cb.name as "brandName"
+        from product p
+        left join cm_second_hand_recommend cshr on p.productID = cshr.recommendProductID
+        left join cm_brand cb on cb.id = p.brandID
+        where p.productCategory = 2 and p.validFlag = 2 and cshr.delFlag = 0
+        and cshr.secondHandProductID = #{productId}
+        order by cshr.sort desc
+    </select>
 </mapper>