Search in sources :

Example 1 with PmsSkuInfo

use of com.xatu.gmall.entity.PmsSkuInfo in project GMall by 18391713434.

the class GmallSearchServiceApplicationTests method contextLoads.

@Test
public void contextLoads() throws IOException {
    List<PmsSkuInfo> pmsSkuInfoList = new ArrayList<>();
    pmsSkuInfoList = skuService.selectAllSku("61");
    // 转换为es数据结构
    List<PmsSearchSkuInfo> pmsSearchSkuInfoList = new ArrayList<>();
    for (PmsSkuInfo pmsSkuInfo : pmsSkuInfoList) {
        PmsSearchSkuInfo pmsSearchSkuInfo = new PmsSearchSkuInfo();
        BeanUtils.copyProperties(pmsSkuInfo, pmsSearchSkuInfo);
        pmsSearchSkuInfo.setProductId(pmsSkuInfo.getSpuId());
        pmsSearchSkuInfoList.add(pmsSearchSkuInfo);
    }
    // 导入es
    for (PmsSearchSkuInfo pmsSearchSkuInfo : pmsSearchSkuInfoList) {
        String idStr = pmsSearchSkuInfo.getId().toString();
        Index put = new Index.Builder(pmsSearchSkuInfo).index("com.xatu.gmall").type("PmsSkuInfo").id(idStr).build();
        jestClient.execute(put);
    }
}
Also used : PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo) ArrayList(java.util.ArrayList) Index(io.searchbox.core.Index) PmsSearchSkuInfo(com.xatu.gmall.entity.PmsSearchSkuInfo) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with PmsSkuInfo

use of com.xatu.gmall.entity.PmsSkuInfo in project GMall by 18391713434.

the class ItemController method item.

@RequestMapping("{skuId}.html")
public String item(@PathVariable String skuId, ModelMap map) {
    // sku对象
    PmsSkuInfo pmsSkuInfo = skuService.getSkuById(skuId);
    map.put("skuInfo", pmsSkuInfo);
    // 销售属性列表
    List<PmsProductSaleAttr> pmsProductSaleAttrs = spuService.spuSaleAttrListCheckBySku(pmsSkuInfo.getSpuId(), pmsSkuInfo.getId());
    map.put("spuSaleAttrListCheckBySku", pmsProductSaleAttrs);
    // 查询当前sku的spu的其他sku的集合的hash表
    List<PmsSkuInfo> pmsSkuInfos = skuService.getSkuSaleAttrValueListBySpu(pmsSkuInfo.getSpuId());
    HashMap<String, Long> skuSaleAttrHash = new HashMap<>();
    for (PmsSkuInfo skuInfo : pmsSkuInfos) {
        String k = "";
        Long v = skuInfo.getId();
        List<PmsSkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        for (PmsSkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
            k += skuSaleAttrValue.getSaleAttrValueId() + "|";
        }
        skuSaleAttrHash.put(k, v);
    }
    // 将sku的销售属性hash表放到页面
    String skuSaleAttrHashJsonStr = JSON.toJSONString(skuSaleAttrHash);
    map.put("skuSaleAttrHashJsonStr", skuSaleAttrHashJsonStr);
    return "item";
}
Also used : PmsProductSaleAttr(com.xatu.gmall.entity.PmsProductSaleAttr) HashMap(java.util.HashMap) PmsSkuSaleAttrValue(com.xatu.gmall.entity.PmsSkuSaleAttrValue) PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PmsSkuInfo

use of com.xatu.gmall.entity.PmsSkuInfo in project GMall by 18391713434.

the class SkuServiceImpl method getSkuById.

public PmsSkuInfo getSkuById(String skuId) {
    /*PmsSkuInfo pmsSkuInfo = skuInfoMapper.selectById(skuId);
        List<PmsSkuImage> skuImageList = skuImageMapper.selectList(new QueryWrapper<PmsSkuImage>().eq("sku_id", skuId));
        pmsSkuInfo.setSkuImageList(skuImageList);*/
    PmsSkuInfo pmsSkuInfo = null;
    // 链接缓存
    Jedis jedis = redisUtil.getJedis();
    // 查询缓存
    String skuKey = "sku:" + skuId + ":info";
    String skuJson = jedis.get(skuKey);
    try {
        if (StringUtils.isNotBlank(skuJson)) {
            pmsSkuInfo = JSON.parseObject(skuJson, PmsSkuInfo.class);
        } else {
            // 如果缓存没有,查询mysql
            // 设置分布式锁
            // String OK = jedis.set("sku:" + skuId + ":lock",token,"nx","px",10*1000); //拿到10秒控制权限
            String token = UUID.randomUUID().toString();
            // 拿到10秒控制权限
            String OK = jedis.set("sku:" + skuId + ":lock", token, "nx", "px", 10 * 1000);
            if (StringUtils.isNotBlank(OK) && OK.equals("OK")) {
                // 设置成功,有权在10秒的过期时间内访问数据库
                pmsSkuInfo = skuInfoMapper.selectById(skuId);
                List<PmsSkuImage> skuImageList = skuImageMapper.selectList(new QueryWrapper<PmsSkuImage>().eq("sku_id", skuId));
                pmsSkuInfo.setSkuImageList(skuImageList);
                // mysql查询结果存入redis
                if (pmsSkuInfo != null) {
                    jedis.set("sku:" + skuId + ":info", JSON.toJSONString(pmsSkuInfo));
                } else {
                    // 数据库中不存在该sku
                    // 为了防止缓存穿透,null或者空字符串设置给redis
                    jedis.setex("sku:" + skuId + ":info", 60 * 3, JSON.toJSONString(""));
                }
                // 在访问mysql之后,将分布式锁释放
                // jedis.del("sku:" + skuId + ":lock");
                String lockToken = jedis.get("sku:" + skuId + ":lock");
                if (StringUtils.isNotBlank(lockToken) && lockToken.equals(token)) {
                    // 用token确认删除的是自己的锁
                    // jedis.eval("lua"):可以使用lua脚本,在查询key的同时删除该key,防止高并发下的意外发生
                    jedis.del("sku:" + skuId + ":lock");
                }
            } else {
                // 设置失败自旋(该线程睡眠几秒后,重新尝试访问)
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return getSkuById(skuId);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        jedis.close();
    }
    return pmsSkuInfo;
// //Redisson锁是包装的JUC内的锁策略,是Java代码层面的分布式锁
// RLock lock = redisson.getLock("anyLock");//声明锁
// // 加锁以后10秒钟自动解锁
// // 无需调用unlock方法手动解锁
// PmsSkuInfo pmsSkuInfo = null;
// //链接缓存
// Jedis jedis = RedisUtil.getJedis();
// //查询缓存
// String skuKey = "sku:"+skuId+":info";
// String skuJson = jedis.get(skuKey);
// try{
// if(StringUtils.isNotBlank(skuJson)){
// pmsSkuInfo = JSON.parseObject(skuJson, PmsSkuInfo.class);
// }else{
// //如果缓存没有,查询mysql
// //设置分布式锁
// // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
// boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
// if (res) {
// try {
// pmsSkuInfo = skuInfoMapper.selectById(skuId);
// List<PmsSkuImage> skuImageList = skuImageMapper.selectList(new QueryWrapper<PmsSkuImage>().eq("sku_id", skuId));
// pmsSkuInfo.setSkuImageList(skuImageList);
// //mysql查询结果存入redis
// if(pmsSkuInfo!=null){
// jedis.set("sku:"+skuId+":info",JSON.toJSONString(pmsSkuInfo));
// }else{
// //数据库中不存在该sku
// //为了防止缓存穿透,null或者空字符串设置给redis
// jedis.setex("sku:"+skuId+":info",60*3, JSON.toJSONString(""));
// }
// }catch (Exception e){
// e.printStackTrace();
// } finally {
// lock.unlock();
// }
// }else{
// return getSkuById(skuId); //如果得不到锁,重新访问
// }
// 
// }
// }catch (Exception e){
// e.printStackTrace();
// }finally {
// jedis.close();
// }
// return pmsSkuInfo;
}
Also used : Jedis(redis.clients.jedis.Jedis) PmsSkuImage(com.xatu.gmall.entity.PmsSkuImage) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo)

Example 4 with PmsSkuInfo

use of com.xatu.gmall.entity.PmsSkuInfo in project GMall by 18391713434.

the class SkuServiceImpl method selectAllSku.

public List<PmsSkuInfo> selectAllSku(String catalog3Id) {
    List<PmsSkuInfo> pmsSkuInfos = skuInfoMapper.selectList(new QueryWrapper<PmsSkuInfo>());
    for (PmsSkuInfo pmsSkuInfo : pmsSkuInfos) {
        Long skuId = pmsSkuInfo.getId();
        List<PmsSkuAttrValue> pmsSkuAttrValueList = skuAttrValueMapper.selectList(new QueryWrapper<PmsSkuAttrValue>().eq("sku_id", skuId));
        pmsSkuInfo.setSkuAttrValueList(pmsSkuAttrValueList);
    }
    return pmsSkuInfos;
}
Also used : PmsSkuAttrValue(com.xatu.gmall.entity.PmsSkuAttrValue) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo)

Example 5 with PmsSkuInfo

use of com.xatu.gmall.entity.PmsSkuInfo in project GMall by 18391713434.

the class CartController method addToCart.

@LoginRequired(loginSuccess = false)
@RequestMapping("/addToCart")
public String addToCart(String skuId, int quantity, HttpServletRequest request, HttpServletResponse response) {
    // 调用商品服务查询商品信息
    PmsSkuInfo pmsSkuInfo = skuService.getSkuById(skuId);
    // 将商品信息封装成购物车信息
    OmsCartItem omsCartItem = new OmsCartItem();
    omsCartItem.setCreateDate(new Date());
    omsCartItem.setDeleteStatus(0);
    omsCartItem.setModifyDate(new Date());
    omsCartItem.setPrice(BigDecimal.valueOf(pmsSkuInfo.getPrice()));
    omsCartItem.setProductAttr("");
    omsCartItem.setProductBrand("");
    omsCartItem.setProductCategoryId(pmsSkuInfo.getCatalog3Id());
    omsCartItem.setProductId(Long.valueOf(pmsSkuInfo.getSpuId()));
    omsCartItem.setProductName(pmsSkuInfo.getSkuName());
    omsCartItem.setProductPic(pmsSkuInfo.getSkuDefaultImg());
    omsCartItem.setProductSkuCode("1111111111");
    omsCartItem.setProductSkuId(Long.valueOf(skuId));
    omsCartItem.setQuantity(quantity);
    // 判断用户是否登录
    String memberId = (String) request.getAttribute("memberId");
    String nickname = (String) request.getAttribute("nickname");
    if (StringUtils.isBlank(memberId)) {
        // 用户未登录
        List<OmsCartItem> omsCartItems = new ArrayList<>();
        // 判断用户本地是由有cookie信息
        String cartListCookie = CookieUtil.getCookieValue(request, "cartListCookie", true);
        if (cartListCookie != null) {
            // cookie不为空
            omsCartItems = JSON.parseArray(cartListCookie, OmsCartItem.class);
            boolean exist = if_cart_exist(omsCartItems, omsCartItem);
            // 判断当点添加的商品在我们的购物车是否存在
            if (exist) {
                // 之前添加过,更新操作
                for (OmsCartItem cartItem : omsCartItems) {
                    String productSkuId = cartItem.getProductSkuId().toString();
                    if (productSkuId.equals(omsCartItem.getProductSkuId())) {
                        cartItem.setQuantity(cartItem.getQuantity() + omsCartItem.getQuantity());
                        cartItem.setPrice(cartItem.getPrice().add(omsCartItem.getPrice()));
                    }
                }
            } else {
                // 之前没有添加过
                omsCartItems.add(omsCartItem);
            }
        } else {
            // cookie为空
            omsCartItems.add(omsCartItem);
        }
        CookieUtil.setCookie(request, response, "cartListCookie", JSON.toJSONString(omsCartItems), 60 * 60 * 72, true);
    } else {
        // 用户已登录
        // 在db中查询购物车数据
        OmsCartItem omsCartItemFromDB = cartService.getCartExistByUser(memberId, skuId);
        if (omsCartItemFromDB == null) {
            // 购物车中没有该商品
            omsCartItem.setMemberId(Long.valueOf(memberId));
            cartService.addCartIterm(omsCartItem);
        } else {
            // 购物车中有该商品 直接更新数据库
            omsCartItemFromDB.setQuantity(omsCartItem.getQuantity());
            cartService.updateCarItem(omsCartItemFromDB);
        }
        // 同步缓存
        cartService.flushCache(memberId);
    }
    return "redirect:/success.html";
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem) PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo) ArrayList(java.util.ArrayList) Date(java.util.Date) LoginRequired(com.xatu.gmall.annotations.LoginRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PmsSkuInfo (com.xatu.gmall.entity.PmsSkuInfo)5 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 ArrayList (java.util.ArrayList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 LoginRequired (com.xatu.gmall.annotations.LoginRequired)1 OmsCartItem (com.xatu.gmall.entity.OmsCartItem)1 PmsProductSaleAttr (com.xatu.gmall.entity.PmsProductSaleAttr)1 PmsSearchSkuInfo (com.xatu.gmall.entity.PmsSearchSkuInfo)1 PmsSkuAttrValue (com.xatu.gmall.entity.PmsSkuAttrValue)1 PmsSkuImage (com.xatu.gmall.entity.PmsSkuImage)1 PmsSkuSaleAttrValue (com.xatu.gmall.entity.PmsSkuSaleAttrValue)1 Index (io.searchbox.core.Index)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Jedis (redis.clients.jedis.Jedis)1