Commit 95961974 authored by haoxuyang's avatar haoxuyang

去掉无用的 javascript ,处理首页查询商品,更新历史搜索记录导致行锁的问题

parent c745b0a1
...@@ -13,7 +13,6 @@ import com.ruoyi.mall.domain.MallShopShop; ...@@ -13,7 +13,6 @@ import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallMemberUserService; import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IMallShopShopService; import com.ruoyi.mall.service.IMallShopShopService;
import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.ISysUserService;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
......
package com.ruoyi.mall.mapper; package com.ruoyi.mall.mapper;
import java.util.List; import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.mall.domain.MallMemberSearchHistory; import com.ruoyi.mall.domain.MallMemberSearchHistory;
import com.ruoyi.mall.domain.MallMemberSearchHistoryDto; import com.ruoyi.mall.domain.MallMemberSearchHistoryDto;
...@@ -28,6 +29,11 @@ public interface MallMemberSearchHistoryMapper ...@@ -28,6 +29,11 @@ public interface MallMemberSearchHistoryMapper
*/ */
public List<MallMemberSearchHistoryDto> selectMallMemberSearchHistoryList(MallMemberSearchHistory mallMemberSearchHistory); public List<MallMemberSearchHistoryDto> selectMallMemberSearchHistoryList(MallMemberSearchHistory mallMemberSearchHistory);
/**
* 按用户+搜索词精确匹配最近一条
*/
MallMemberSearchHistory selectByUserIdAndGoodName(@Param("userId") Long userId, @Param("goodName") String goodName);
/** /**
* 新增mall * 新增mall
* *
......
...@@ -36,6 +36,11 @@ public interface IMallMemberSearchHistoryService ...@@ -36,6 +36,11 @@ public interface IMallMemberSearchHistoryService
*/ */
public int insertMallMemberSearchHistory(MallMemberSearchHistory mallMemberSearchHistory); public int insertMallMemberSearchHistory(MallMemberSearchHistory mallMemberSearchHistory);
/**
* 记录或刷新用户搜索词(独立短事务,避免拖住商品列表)
*/
void saveOrTouchSearchHistory(Long userId, String goodName);
/** /**
* 修改mall * 修改mall
* *
......
...@@ -7,7 +7,6 @@ import com.alibaba.fastjson.JSONObject; ...@@ -7,7 +7,6 @@ import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult; import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.MallSalesman; import com.ruoyi.mall.domain.MallSalesman;
import netscape.javascript.JSObject;
/** /**
* 业务员Service接口 * 业务员Service接口
......
...@@ -7,7 +7,6 @@ import com.ruoyi.common.core.domain.model.LoginUser; ...@@ -7,7 +7,6 @@ import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.mall.domain.*; import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.VO.SeckillGoodsVO; import com.ruoyi.mall.domain.VO.SeckillGoodsVO;
import com.ruoyi.mall.domain.vo.GoodsProductVO; import com.ruoyi.mall.domain.vo.GoodsProductVO;
...@@ -17,8 +16,9 @@ import com.ruoyi.mall.domain.vo.MemberCollectVO; ...@@ -17,8 +16,9 @@ import com.ruoyi.mall.domain.vo.MemberCollectVO;
import com.ruoyi.mall.mapper.*; import com.ruoyi.mall.mapper.*;
import com.ruoyi.mall.service.*; import com.ruoyi.mall.service.*;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -27,9 +27,9 @@ import java.math.BigDecimal; ...@@ -27,9 +27,9 @@ import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CountDownLatch;
/** /**
* 商品Service业务层处理 * 商品Service业务层处理
...@@ -39,6 +39,9 @@ import java.util.concurrent.CountDownLatch; ...@@ -39,6 +39,9 @@ import java.util.concurrent.CountDownLatch;
*/ */
@Service @Service
public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService { public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService {
private static final Logger log = LoggerFactory.getLogger(MallGoodsGoodsServiceImpl.class);
@Resource @Resource
private MallGoodsGoodsMapper mallGoodsGoodsMapper; private MallGoodsGoodsMapper mallGoodsGoodsMapper;
@Resource @Resource
...@@ -65,8 +68,6 @@ public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService { ...@@ -65,8 +68,6 @@ public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService {
IMallGoodsProductService mallGoodsProductService; IMallGoodsProductService mallGoodsProductService;
@Resource @Resource
private MallShopSupplierMapper mallShopSupplierMapper; private MallShopSupplierMapper mallShopSupplierMapper;
@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
/** /**
* 查询商品 * 查询商品
...@@ -185,87 +186,63 @@ public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService { ...@@ -185,87 +186,63 @@ public class MallGoodsGoodsServiceImpl implements IMallGoodsGoodsService {
* @param mallGoodsGoods 商品 * @param mallGoodsGoods 商品
* @return 商品 * @return 商品
*/ */
@Transactional
@Override @Override
public PageInfo<IndexGoodsVO> selectMallGoodsGoodsListByPage(Integer pageNum, Integer pageSize, MallGoodsGoodsDTO mallGoodsGoods, String userId) { public PageInfo<IndexGoodsVO> selectMallGoodsGoodsListByPage(Integer pageNum, Integer pageSize, MallGoodsGoodsDTO mallGoodsGoods, String userId) {
if (!StringUtils.isEmpty(mallGoodsGoods.getGoodsName()) && !StringUtils.isEmpty(userId)) { saveSearchHistoryQuietly(mallGoodsGoods, userId);
MallMemberSearchHistory search = new MallMemberSearchHistory();
search.setGoodName(mallGoodsGoods.getGoodsName());
search.setUserId(Long.parseLong(userId));
List<MallMemberSearchHistoryDto> list = searchHistoryService.selectMallMemberSearchHistoryList(search);
if (list.size() > 0) {
MallMemberSearchHistoryDto mallMemberSearchHistory = list.get(0);
MallMemberSearchHistory result = new MallMemberSearchHistory();
BeanUtils.copyProperties( mallMemberSearchHistory,result);
result.setCreateAt(DateUtils.getTime());
searchHistoryService.updateMallMemberSearchHistory(result);
} else {
MallMemberSearchHistory searchHistory = new MallMemberSearchHistory();
searchHistory.setCreateAt(DateUtils.getTime());
searchHistory.setGoodName(mallGoodsGoods.getGoodsName());
searchHistory.setUserId(Long.parseLong(userId));
searchHistoryService.insertMallMemberSearchHistory(searchHistory);
}
}
PageHelper.startPage(pageNum, pageSize); PageHelper.startPage(pageNum, pageSize);
// List<IndexGoodsVO> list = mallGoodsGoodsMapper.selectMallGoodsGoodsListVO(mallGoodsGoods);
List<IndexGoodsVO> list = mallGoodsGoodsMapper.selectMallGoodsGoodsListDTO(mallGoodsGoods); List<IndexGoodsVO> list = mallGoodsGoodsMapper.selectMallGoodsGoodsListDTO(mallGoodsGoods);
if (!list.isEmpty()) { if (!list.isEmpty()) {
MallMemberUser mallMemberUser = new MallMemberUser(); MallMemberUser mallMemberUser = null;
// 用户信息
if (StringUtils.isNotBlank(userId)) { if (StringUtils.isNotBlank(userId)) {
mallMemberUser = userService.selectMallMemberUserById(Long.parseLong(userId)); mallMemberUser = userService.selectMallMemberUserById(Long.parseLong(userId));
} }
for (IndexGoodsVO indexGoodsVO : list) {
fillGoodsSpecAndPrice(indexGoodsVO, mallMemberUser);
}
}
return new PageInfo<>(list);
}
try { private void saveSearchHistoryQuietly(MallGoodsGoodsDTO mallGoodsGoods, String userId) {
CountDownLatch countDownLatch = new CountDownLatch(list.size()); if (mallGoodsGoods == null || StringUtils.isEmpty(mallGoodsGoods.getGoodsName()) || StringUtils.isEmpty(userId)) {
for (IndexGoodsVO indexGoodsVO : list) { return;
MallMemberUser finalMallMemberUser = mallMemberUser; }
threadPoolTaskExecutor.execute(() -> { try {
searchHistoryService.saveOrTouchSearchHistory(Long.parseLong(userId), mallGoodsGoods.getGoodsName());
Long goodsId = indexGoodsVO.getId(); } catch (Exception e) {
//规格列表 log.warn("保存搜索历史失败, userId={}, keyword={}", userId, mallGoodsGoods.getGoodsName(), e);
//indexGoodsVO.setProList(mallGoodsProductMapper.selectMallGoodsProductByGoodsIdVO(goodsId)); }
List<GoodsProductVO> proList = mallGoodsProductMapper.selectMallGoodsProductByGoodsIdVO(goodsId); }
indexGoodsVO.setProList(proList);
// VIP价
if (finalMallMemberUser != null && "1".equals(finalMallMemberUser.getVip())) {
if (proList.size() > 1) {
indexGoodsVO.setVipPriceRangeHigh(proList.get(0).getPriceVip().toString());
indexGoodsVO.setVipPriceRangeLow(proList.get(proList.size() - 1).getPriceVip().toString());
} else {
indexGoodsVO.setVipPriceRangeHigh(proList.get(0).getPriceVip().toString());
indexGoodsVO.setVipPriceRangeLow(indexGoodsVO.getPriceRangeHigh());
}
} else {
indexGoodsVO.setPriceVip(null);
}
if (proList.size() >= 1) {
indexGoodsVO.setPriceRangeHigh(proList.get(0).getPrice().toString());
indexGoodsVO.setPriceRangeLow(proList.get(proList.size() - 1).getPrice().toString());
if(indexGoodsVO.getKilograms()!=null){
BigDecimal kilograms = new BigDecimal(indexGoodsVO.getKilograms());
BigDecimal pricePerKg = proList.get(0).getPrice()
.divide(kilograms, 2, RoundingMode.HALF_UP)
.divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
BigDecimal bigDecimal = pricePerKg.setScale(2, RoundingMode.HALF_UP);
indexGoodsVO.setKilogramsPrice(bigDecimal);
}
} else {
indexGoodsVO.setPriceRangeHigh(proList.get(0).getPrice().toString());
indexGoodsVO.setPriceRangeLow(indexGoodsVO.getPriceRangeHigh());
}
countDownLatch.countDown(); private void fillGoodsSpecAndPrice(IndexGoodsVO indexGoodsVO, MallMemberUser mallMemberUser) {
}); List<GoodsProductVO> proList = mallGoodsProductMapper.selectMallGoodsProductByGoodsIdVO(indexGoodsVO.getId());
} if (proList == null) {
countDownLatch.await(); proList = Collections.emptyList();
} catch (Exception e) { }
e.printStackTrace(); indexGoodsVO.setProList(proList);
if (proList.isEmpty()) {
return;
}
if (mallMemberUser != null && "1".equals(mallMemberUser.getVip())) {
if (proList.size() > 1) {
indexGoodsVO.setVipPriceRangeHigh(proList.get(0).getPriceVip().toString());
indexGoodsVO.setVipPriceRangeLow(proList.get(proList.size() - 1).getPriceVip().toString());
} else {
indexGoodsVO.setVipPriceRangeHigh(proList.get(0).getPriceVip().toString());
indexGoodsVO.setVipPriceRangeLow(indexGoodsVO.getPriceRangeHigh());
} }
} else {
indexGoodsVO.setPriceVip(null);
}
indexGoodsVO.setPriceRangeHigh(proList.get(0).getPrice().toString());
indexGoodsVO.setPriceRangeLow(proList.get(proList.size() - 1).getPrice().toString());
if (indexGoodsVO.getKilograms() != null && indexGoodsVO.getKilograms()>0) {
BigDecimal kilograms = new BigDecimal(indexGoodsVO.getKilograms());
BigDecimal pricePerKg = proList.get(0).getPrice()
.divide(kilograms, 2, RoundingMode.HALF_UP)
.divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
indexGoodsVO.setKilogramsPrice(pricePerKg.setScale(2, RoundingMode.HALF_UP));
} }
PageInfo<IndexGoodsVO> pageInfo = new PageInfo<>(list);
return pageInfo;
} }
......
package com.ruoyi.mall.service.impl; package com.ruoyi.mall.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallMemberSearchHistory; import com.ruoyi.mall.domain.MallMemberSearchHistory;
import com.ruoyi.mall.domain.MallMemberSearchHistoryDto; import com.ruoyi.mall.domain.MallMemberSearchHistoryDto;
import com.ruoyi.mall.mapper.MallMemberSearchHistoryMapper; import com.ruoyi.mall.mapper.MallMemberSearchHistoryMapper;
import com.ruoyi.mall.service.IMallMemberSearchHistoryService; import com.ruoyi.mall.service.IMallMemberSearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
...@@ -53,6 +56,22 @@ public class MallMemberSearchHistoryServiceImpl implements IMallMemberSearchHist ...@@ -53,6 +56,22 @@ public class MallMemberSearchHistoryServiceImpl implements IMallMemberSearchHist
return mallMemberSearchHistoryMapper.insertMallMemberSearchHistory(mallMemberSearchHistory); return mallMemberSearchHistoryMapper.insertMallMemberSearchHistory(mallMemberSearchHistory);
} }
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void saveOrTouchSearchHistory(Long userId, String goodName) {
MallMemberSearchHistory exist = mallMemberSearchHistoryMapper.selectByUserIdAndGoodName(userId, goodName);
if (exist != null) {
exist.setCreateAt(DateUtils.getTime());
mallMemberSearchHistoryMapper.updateMallMemberSearchHistory(exist);
return;
}
MallMemberSearchHistory searchHistory = new MallMemberSearchHistory();
searchHistory.setUserId(userId);
searchHistory.setGoodName(goodName);
searchHistory.setCreateAt(DateUtils.getTime());
mallMemberSearchHistoryMapper.insertMallMemberSearchHistory(searchHistory);
}
/** /**
* 修改mall * 修改mall
* *
......
...@@ -30,7 +30,6 @@ import com.ruoyi.system.mapper.SysDictDataMapper; ...@@ -30,7 +30,6 @@ import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper; import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper; import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper; import com.ruoyi.system.mapper.SysUserRoleMapper;
import netscape.javascript.JSObject;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
......
...@@ -36,6 +36,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -36,6 +36,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where> </where>
order by create_at desc order by create_at desc
</select> </select>
<select id="selectByUserIdAndGoodName" resultMap="MallMemberSearchHistoryResult">
select id, user_id, good_name, create_at
from mall_member_search_history
where user_id = #{userId}
and good_name = #{goodName}
order by id desc
limit 1
</select>
<select id="selectMallMemberSearchHistoryById" parameterType="Long" resultMap="MallMemberSearchHistoryResult"> <select id="selectMallMemberSearchHistoryById" parameterType="Long" resultMap="MallMemberSearchHistoryResult">
<include refid="selectMallMemberSearchHistoryVo"/> <include refid="selectMallMemberSearchHistoryVo"/>
......
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