xiangpei
2025-05-09 641b4a3b1572f3a75ce0bd7d645e34252237cf9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package cn.lili.test.elasticsearch;
 
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.promotion.service.PromotionService;
import cn.lili.modules.search.entity.dos.EsGoodsAttribute;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import cn.lili.modules.search.repository.EsGoodsIndexRepository;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.service.EsGoodsSearchService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.test.context.junit.jupiter.SpringExtension;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author paulG
 * @since 2020/10/14
 **/
@ExtendWith(SpringExtension.class)
@SpringBootTest
class EsTest {
 
    @Autowired
    private EsGoodsIndexService esGoodsIndexService;
 
    @Autowired
    private EsGoodsIndexRepository goodsIndexRepository;
 
    @Autowired
    private EsGoodsSearchService goodsSearchService;
 
    @Autowired
    private GoodsSkuService goodsSkuService;
 
    @Autowired
    private GoodsService goodsService;
 
    @Autowired
    private Cache cache;
 
    @Autowired
    private PromotionService promotionService;
 
 
    public static void main(String[] args) {
//        PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
//        String safeHTML = policy.sanitize("+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-");
//        System.out.println(safeHTML);
//        System.out.println(Sanitizers.FORMATTING.and(Sanitizers.FORMATTING).sanitize("+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-"));
//        System.out.println(HtmlUtil.unescape(safeHTML));
//        System.out.println(HtmlUtil.filter("+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-"));
//        Date dt1 = new Date(2021, 12, 10);
//        Date dt2 = new Date(2021, 12, 14);
        for (int i = 0; i < 1000; i++) {
 
            Goods goods = new Goods();
            goods.setGoodsName("测试商品" + i);
//            goods.setAuthFlag();
        }
 
//
 
    }
 
    @Test
    void cleanInvalidPromotion() {
        this.esGoodsIndexService.cleanInvalidPromotion();
        Assertions.assertTrue(true);
    }
 
    @Test
    void searchGoods() {
        EsGoodsSearchDTO goodsSearchDTO = new EsGoodsSearchDTO();
//       goodsSearchDTO.setKeyword("黄");
//        goodsSearchDTO.setProp("IETF_HTTP/3");
//       goodsSearchDTO.setPrice("100_20000");
//       goodsSearchDTO.setStoreCatId(1L);
//       goodsSearchDTO.setBrandId(123L);
//       goodsSearchDTO.setCategoryId(2L);
//       goodsSearchDTO.setNameIds(Arrays.asList("1344113311566553088", "1344113367694729216"));
        PageVO pageVo = new PageVO();
        pageVo.setPageNumber(0);
        pageVo.setPageSize(100);
        pageVo.setOrder("desc");
        pageVo.setNotConvert(true);
        SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(goodsSearchDTO, pageVo);
        Assertions.assertNotNull(esGoodsIndices);
        esGoodsIndices.getContent().forEach(System.out::println);
//       esGoodsIndices.getContent().forEach(i -> {
//           if (i.getPromotionMap() != null){
//               String s = i.getPromotionMap().keySet().parallelStream().filter(j -> j.contains(PromotionTypeEnum.FULL_DISCOUNT.name())).findFirst().orElse(null);
//               if (s != null) {
//                   FullDiscount basePromotion = (FullDiscount) i.getPromotionMap().get(s);
//                   System.out.println(basePromotion);
//               }
//           }
//       });
 
    }
 
    @Test
    void aggregationSearch() {
        EsGoodsSearchDTO goodsSearchDTO = new EsGoodsSearchDTO();
        //goodsSearchDTO.setKeyword("电脑");
        //goodsSearchDTO.setProp("颜色_故宫文创@版本_小新Pro13s");
//       goodsSearchDTO.setCategoryId("2");
//       goodsSearchDTO.setPrice("100_20000");
        PageVO pageVo = new PageVO();
        pageVo.setPageNumber(0);
        pageVo.setPageSize(10);
        pageVo.setOrder("desc");
        EsGoodsRelatedInfo selector = goodsSearchService.getSelector(goodsSearchDTO, pageVo);
        Assertions.assertNotNull(selector);
        System.out.println(JSONUtil.toJsonStr(selector));
 
    }
 
    @Test
    void init() {
        LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name());
        queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
        List<GoodsSku> list = goodsSkuService.list(queryWrapper);
        List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
        for (GoodsSku goodsSku : list) {
            EsGoodsIndex index = new EsGoodsIndex(goodsSku);
            esGoodsIndices.add(index);
            cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
        }
        esGoodsIndexService.initIndex(esGoodsIndices, true);
        Assertions.assertTrue(true);
    }
 
    @Test
    void addIndex() {
        List<EsGoodsAttribute> esGoodsAttributeList = new ArrayList<>();
        EsGoodsAttribute attribute = new EsGoodsAttribute();
        attribute.setType(0);
        attribute.setName("颜色");
        attribute.setValue("16.1英寸 6核R5 16G 512G 高色域");
        esGoodsAttributeList.add(attribute);
        attribute = new EsGoodsAttribute();
        attribute.setType(0);
        attribute.setName("版本");
        attribute.setValue("RedmiBook 18英寸 深空灰");
        esGoodsAttributeList.add(attribute);
        EsGoodsIndex goodsIndex = initGoodsIndexData("122", "0|2", "140", "142", "A142", "RedmiBook 18 锐龙版 超轻薄全面屏(6核R5-4500U 16G 512G 100% sRGB高色域)灰 手提 笔记本电脑 小米 红米 ", "131", "小米自营旗舰店", 10000D);
        goodsIndex.setAttrList(esGoodsAttributeList);
 
        //GoodsSku goodsSkuByIdFromCache = goodsSkuService.getGoodsSkuByIdFromCache("121");
        //EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSkuByIdFromCache);
 
 
        esGoodsIndexService.addIndex(goodsIndex);
 
        Assertions.assertTrue(true);
    }
 
    @Test
    void searchAll() {
        Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
        Assertions.assertNotNull(all);
        all.forEach(System.out::println);
    }
 
    @Test
    void updateIndex() {
        EsGoodsIndex byId = esGoodsIndexService.findById("121");
        esGoodsIndexService.updateIndex(byId);
        Assertions.assertTrue(true);
    }
 
    @Test
    void deleteIndex() {
        esGoodsIndexService.deleteIndex(null);
        Assertions.assertTrue(true);
    }
 
    @Test
    void cleanPromotion() {
        esGoodsIndexService.cleanInvalidPromotion();
        Assertions.assertTrue(true);
    }
 
 
    private EsGoodsIndex initGoodsIndexData(String brandId, String categoryPath, String goodsId, String id, String sn, String goodsName, String storeId, String storeName, Double price) {
        EsGoodsIndex goodsIndex = new EsGoodsIndex();
        goodsIndex.setBuyCount(99);
        goodsIndex.setCommentNum(99);
        goodsIndex.setGrade(100D);
        goodsIndex.setHighPraiseNum(100);
        goodsIndex.setIntro("I'd like a cup of tea, please");
        goodsIndex.setAuthFlag("1");
        goodsIndex.setMarketEnable("1");
        goodsIndex.setMobileIntro("I want something cold to drink");
        goodsIndex.setPoint(0);
        goodsIndex.setSelfOperated(true);
        goodsIndex.setThumbnail("picture");
        goodsIndex.setStoreCategoryPath("1");
 
        goodsIndex.setId(id);
        goodsIndex.setBrandId(brandId);
        goodsIndex.setGoodsId(goodsId);
        goodsIndex.setCategoryPath(categoryPath);
        goodsIndex.setGoodsName(goodsName);
        goodsIndex.setPrice(price);
        goodsIndex.setSn(sn);
        goodsIndex.setStoreId(storeId);
        goodsIndex.setStoreName(storeName);
        return goodsIndex;
    }
 
 
}