shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.ishop.merchant.controller;
 
import com.iplatform.base.Constants;
import com.iplatform.base.PlatformRuntimeException;
import com.iplatform.model.po.S_user_core;
import com.ishop.merchant.BaseController;
import com.ishop.merchant.ProductConstants;
import com.ishop.merchant.VariableConstants;
import com.ishop.merchant.pojo.BrandCategoryParam;
import com.ishop.merchant.pojo.CopyProductRequest;
import com.ishop.merchant.pojo.ProductParam;
import com.ishop.merchant.util.ImageUtils;
import com.ishop.merchant.util.ProductAttrUtils;
import com.ishop.merchant.util.VoUtils;
import com.ishop.model.po.EbMerchant;
import com.ishop.model.po.EbProduct;
import com.ishop.model.po.EbProductAttr;
import com.ishop.model.po.EbProductAttrValue;
import com.ishop.model.po.EbProductBrand;
import com.ishop.model.po.EbProductCoupon;
import com.ishop.model.po.EbProductDescription;
import com.ishop.model.vo.ProductAddVo;
import com.ishop.model.vo.ProductCategoryVo;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/merchant/product")
public class MerchantProductController extends BaseController {
 
    /**
     * 商品下架
     * @param id
     * @return
     * @date 2023-08-02
     */
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public ResponseValue productDown(Long id){
        if(id == null || id <= 0){
            return ResponseValue.error(Constants.ERROR_ARGUMENT);
        }
        EbProduct product = this.getProductCache().get(id);
        if(product == null){
            return ResponseValue.error("商品不存在");
        }
        if(product.getIsShow().intValue() == 0){
            return ResponseValue.success("商品已经下架");
        }
 
        // 设置商品不可见,为更新缓存准备
        product.setIsShow(0);
 
        this.getProductService().execProductDown(id);
        this.getProductCache().update(product);
        return ResponseValue.success();
    }
 
    /**
     * 商品上架
     * @param id
     * @return
     * @date 2023-08-02
     */
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public ResponseValue productUp(Long id){
        if(id == null || id <= 0){
            return ResponseValue.error(Constants.ERROR_ARGUMENT);
        }
        EbProduct product = this.getProductCache().get(id);
        if(product == null){
            return ResponseValue.error("商品不存在");
        }
        if (product.getIsAudit().intValue() == 1) {
            return ResponseValue.error("商品审核中无法上架");
        }
        if (!product.getAuditStatus().equals(ProductConstants.AUDIT_STATUS_EXEMPTION) && !product.getAuditStatus().equals(ProductConstants.AUDIT_STATUS_SUCCESS)) {
            return ResponseValue.error("商品状态异常无法上架");
        }
        if(product.getIsShow().intValue() == 1){
            return ResponseValue.success("商品已经上架");
        }
 
        int merId = this.getCurrentUser().getMer_id().intValue();
        EbMerchant merchant = this.getMerchantCache().get(merId);
        if (merchant.getIsSwitch().intValue() == 0) {
            return ResponseValue.error("打开商户开关后方能上架商品");
        }
 
        // 设置商品可见,为更新缓存准备
        product.setIsShow(1);
 
        // 获取商品skuid
        List<Integer> skuIdList = null;
        List<EbProductAttrValue> productAttrValueList = this.getProductAttrValueService().queryProductAttrValueList(ProductConstants.PRODUCT_TYPE_NORMAL, product.getId());
        if(!StringUtils.isEmptyList(productAttrValueList)){
            skuIdList = new ArrayList<>(productAttrValueList.size());
            for(EbProductAttrValue e : productAttrValueList){
                skuIdList.add(e.getId());
            }
        }
        this.getProductService().execProductUp(product.getId(), skuIdList);
        this.getProductCache().update(product);
        return ResponseValue.success();
    }
 
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseValue update(@RequestBody ProductAddVo request){
        if(request == null || request.getId() == null || request.getId() <= 0){
            return ResponseValue.error(Constants.ERROR_ARGUMENT);
        }
        this.checkProductRequest(request);
 
        EbProduct product = this.getProductCache().get(request.getId());
        if(product == null){
            return ResponseValue.error("商品不存在");
        }
        if (product.getIsRecycle().intValue() == 1 || product.getIsDel().intValue() == 1) {
            return ResponseValue.error("商品已删除");
        }
        if (product.getIsShow().intValue() == 1) {
            return ResponseValue.error("请先下架商品,再进行修改");
        }
        if (product.getIsAudit().intValue() == 1) {
            return ResponseValue.error("审核中的商品无法修改");
        }
 
        //
        String cdnUrl = this.getCdnUrl();
        product.setImage(this.clearCdnPrefix(request.getImage()));
        product.setSliderImage(ImageUtils.clearCdnMultiImageUrl(request.getSliderImage(), cdnUrl));
        product.setUpdateTime(DateUtils.getDateTimeNumber());
        product.setName(request.getName());
        product.setUnitName(request.getUnitName());
        product.setIntro(request.getIntro());
        product.setKeyword(request.getKeyword());
        product.setCateId(request.getCateId());
        product.setBrandId(request.getBrandId());
        product.setCategoryId(request.getCategoryId());
        product.setGuaranteeIds(request.getGuaranteeIds());
        product.setTempId(request.getTempId());
        product.setSort(request.getSort());
        product.setSpecType(request.getSpecType()? 1:0);
        product.setIsSub(request.getIsSub()? 1:0);
        product.setAuditStatus(ProductConstants.AUDIT_STATUS_EXEMPTION);
        product.setIsAudit(0);
        // 计算价格
        ProductAttrUtils.combineProductAttrValue(request, product);
 
        List<EbProductAttr> addAttrList = new ArrayList<>(4);
        List<EbProductAttr> updateAttrList = new ArrayList<>(4);
        for(EbProductAttr e : request.getAttr()){
            if(e.getId() == null || e.getId() <= 0){
                e.setType(ProductConstants.PRODUCT_TYPE_NORMAL);
                e.setProductId(request.getId());
                addAttrList.add(e);
            } else {
                e.setIsDel(0);
                updateAttrList.add(e);
            }
        }
 
        // 2023-09-10 规格中是否包含价格信息,如果包含说明需要用户选择价格的
//        String[] priceAttr = ProductAttrUtils.hasPriceAttr(request.getAttr());
 
        List<EbProductAttrValue> addAttrValueList = new ArrayList<>(4);
        List<EbProductAttrValue> updateAttrValueList = new ArrayList<>(4);
        for(EbProductAttrValue attrValue : request.getAttrValue()){
            attrValue.setSku(ProductAttrUtils.getSku(attrValue.getAttrValue()));
            attrValue.setImage(this.clearCdnPrefix(attrValue.getImage()));
            attrValue.setProductId(product.getId());
            attrValue.setVersion(0);
            if(attrValue.getId() == null || attrValue.getId() <= 0){
                // add
                attrValue.setQuota(0);
                attrValue.setQuotaShow(0);
                attrValue.setType(ProductConstants.PRODUCT_TYPE_NORMAL);
                addAttrValueList.add(attrValue);
            } else {
                // update
                attrValue.setIsDel(0);
                updateAttrValueList.add(attrValue);
            }
 
            // 2023-09-10 规格中是否包含价格信息
//            if(priceAttr != null){
//                double moneyPrice = ProductAttrUtils.acquirePriceAttrMoney(priceAttr, attrValue.getSku());
//                if(moneyPrice != -1){
//                    attrValue.setPrice(moneyPrice);
//                    logger.debug("找到了自定义金额:{}", moneyPrice);
//                }
//            }
        }
 
        EbProductDescription spd = this.createProductDescription(request.getId(), request.getContent());
        List<EbProductCoupon> couponList = this.createProductCouponList(request.getCouponIds(), request.getId());
 
        EbMerchant merchant = this.getMerchantCache().get(product.getMerId());
        if (merchant.getProductSwitch().intValue() == 0 && product.getAuditStatus().equals(ProductConstants.AUDIT_STATUS_EXEMPTION)) {
            product.setAuditStatus(ProductConstants.AUDIT_STATUS_EXEMPTION);
        } else {
            product.setAuditStatus(ProductConstants.AUDIT_STATUS_WAIT);
        }
 
        this.getProductService().execUpdateProduct(product
                , addAttrList, updateAttrList, addAttrValueList, updateAttrValueList, spd, couponList);
        this.getProductCache().update(product);
        this.getProductAttrCache().removeList(ProductAttrUtils.combineKey(ProductConstants.PRODUCT_TYPE_NORMAL, product.getId()));
        logger.info("商户更新商品:" + product.getId());
        return ResponseValue.success();
    }
 
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseValue save(@RequestBody ProductAddVo request){
        if(request == null){
            return ResponseValue.error(Constants.ERROR_ARGUMENT);
        }
//        if (!request.getSpecType() && request.getAttrValue().size() > 1) {
//            throw new PlatformRuntimeException("单规格商品属性值不能大于1");
//        }
//        if (request.getIsSub()) {
//            request.getAttrValue().forEach(av -> {
//                double brokerageRatio = av.getBrokerage() + av.getBrokerageTwo();
//                if (brokerageRatio > VariableConstants.retailStoreBrokerageRatio) {
//                    throw new PlatformRuntimeException("一二级返佣比例之和范围为 0~{}" + VariableConstants.retailStoreBrokerageRatio);
//                }
//            });
//        }
        this.checkProductRequest(request);
 
        int merchantId = this.getCurrentUser().getMer_id().intValue();
 
        EbProduct product = VoUtils.acquireProduct(request, merchantId);
        product.setImage(this.clearCdnPrefix(request.getImage()));
        if(StringUtils.isNotEmpty(request.getSliderImage())){
            // 2023-08-01 多个图片,应该先分别裁剪前缀。
//            product.setSliderImage(this.clearCdnPrefix(request.getSliderImage()));
            product.setSliderImage(ImageUtils.clearCdnMultiImageUrl(request.getSliderImage(), this.getCdnUrl()));
        }
        if(StringUtils.isNotEmpty(request.getFlatPattern())){
            product.setFlatPattern(this.clearCdnPrefix(request.getFlatPattern()));
        }
 
        long productId = NumberGenerator.getLongSequenceNumber();
 
        //计算价格
//        List<EbProductAttrValue> attrValueAddRequestList = request.getAttrValue();
//        EbProductAttrValue minAttrValue = attrValueAddRequestList.stream().min(Comparator.comparing(EbProductAttrValue::getPrice)).get();
//        product.setPrice(minAttrValue.getPrice());
//        product.setOtPrice(minAttrValue.getOtPrice());
//        product.setCost(minAttrValue.getCost());
//        product.setStock(attrValueAddRequestList.stream().mapToInt(EbProductAttrValue::getStock).sum());
        ProductAttrUtils.combineProductAttrValue(request, product);
        product.setCreateTime(DateUtils.getDateTimeNumber());
        product.setUpdateTime(product.getCreateTime());
        product.setId(productId);
 
        List<EbProductAttr> addRequestList = request.getAttr();
        List<EbProductAttr> attrList = addRequestList.stream().map(e -> {
//            e.setId(id);
            e.setType(ProductConstants.PRODUCT_TYPE_NORMAL);
            e.setProductId(productId);
            return e;
        }).collect(Collectors.toList());
 
//        // 2023-09-10 规格中是否包含价格信息,如果包含说明需要用户选择价格的
//        String[] priceAttr = ProductAttrUtils.hasPriceAttr(attrList);
 
        for(EbProductAttrValue attrValue : request.getAttrValue()){
//            attrValue.setId(id);
            attrValue.setSku(ProductAttrUtils.getSku(attrValue.getAttrValue()));
            attrValue.setQuota(0);
            attrValue.setQuotaShow(0);
            attrValue.setType(ProductConstants.PRODUCT_TYPE_NORMAL);
            attrValue.setImage(this.clearCdnPrefix(attrValue.getImage()));
            attrValue.setProductId(productId);
 
            // 2023-09-10 规格中是否包含价格信息
//            if(priceAttr != null){
//                double moneyPrice = ProductAttrUtils.acquirePriceAttrMoney(priceAttr, attrValue.getSku());
//                if(moneyPrice != -1){
//                    attrValue.setPrice(moneyPrice);
//                    logger.debug("找到了自定义金额:{}", moneyPrice);
//                }
//            }
        }
 
        EbProductDescription spd = this.createProductDescription(productId, request.getContent());
 
        List<EbProductCoupon> couponList = null;
        if(!StringUtils.isEmptyList(request.getCouponIds())){
            couponList = this.createProductCouponList(request.getCouponIds(), productId);
        }
 
        this.getProductService().execInsertProduct(product, attrList, request.getAttrValue(), spd, couponList);
        // 2023-08-01 加入商品缓存
        this.getProductCache().save(product);
        logger.info("商户新增商品:" + productId);
        return ResponseValue.success();
    }
 
    private List<EbProductCoupon> createProductCouponList(List<Integer> couponIds, long productId){
        List<EbProductCoupon> couponList = null;
        if(!StringUtils.isEmptyList(couponIds)){
            couponList = new ArrayList<>(couponIds.size());
            EbProductCoupon productCoupon = null;
            for (Integer couponId : couponIds) {
                productCoupon = new EbProductCoupon();
                productCoupon.setId(NumberGenerator.getLongSequenceNumber());
                productCoupon.setProductId(productId);
                productCoupon.setCouponId(couponId);
                productCoupon.setAddTime(VariableConstants.getNowTime());
                couponList.add(productCoupon);
            }
        }
        return couponList;
    }
 
    private EbProductDescription createProductDescription(long productId, String content){
        EbProductDescription spd = new EbProductDescription();
        spd.setDescription(StringUtils.isNotEmpty(content) ? this.clearCdnPrefix(content) : StringUtils.EMPTY_STRING);
        spd.setType(ProductConstants.PRODUCT_TYPE_NORMAL);
        spd.setProductId(productId);
        spd.setId(NumberGenerator.getLongSequenceNumber());
        return spd;
    }
 
    private void checkProductRequest(ProductAddVo request){
        if (!request.getSpecType() && request.getAttrValue().size() > 1) {
            throw new PlatformRuntimeException("单规格商品属性值不能大于1");
        }
        if (request.getIsSub()) {
            request.getAttrValue().forEach(av -> {
                double brokerageRatio = av.getBrokerage() + av.getBrokerageTwo();
                if (brokerageRatio > VariableConstants.retailStoreBrokerageRatio) {
                    throw new PlatformRuntimeException("一二级返佣比例之和范围为 0~{}" + VariableConstants.retailStoreBrokerageRatio);
                }
            });
        }
    }
 
    /**
     * 拷贝(采集)商品方法。可以是第三方各种平台。
     * @param request
     * @return
     * @date 2023-06-21
     */
    @RequestMapping(value = "/copy/product", method = RequestMethod.POST)
    public ResponseValue copyProduct(@RequestBody CopyProductRequest request){
 
        return ResponseValue.success();
    }
 
    /**
     * 商户端关联展示品牌分类关联数据。
     * @param param
     * @return
     * @date 2023-06-15
     */
    @RequestMapping(value = "/brand/list", method = RequestMethod.GET)
    public ResponseValue pageBrandList(BrandCategoryParam param){
        if(param == null || param.getCid() == null){
            return ResponseValue.error(Constants.ERROR_ARGUMENT);
        }
        GenericPager<EbProductBrand> pager = this.getProductBrandService().queryPageBrandListByCategory(param.getCid(), param.getBrandName());
        return ResponseValue.success(pager);
    }
 
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseValue list(ProductParam productParam){
        long merchantId = this.getCurrentUser().getMer_id();
        if(merchantId <= 0){
            return ResponseValue.error("您不是商户,无法使用该功能");
        }
        productParam.setMerId((int)merchantId);
        GenericPager<EbProduct> pager = this.getProductService().queryPageMerchantProductList(productParam);
        if(!StringUtils.isEmptyList(pager.getDatas())){
            for(EbProduct e : pager.getDatas()){
                e.setParameterInt("collectCount", 0);
                if(StringUtils.isNotEmpty(e.getImage())){
                    e.setImage(this.getCdnUrl() + e.getImage());
                }
            }
        }
        return ResponseValue.success(pager);
    }
 
    @RequestMapping(value = "/tabs/headers", method = RequestMethod.GET)
    public ResponseValue getTabsHeader(){
        logger.warn("暂时给出商品测试标签页");
        return ResponseValue.success(this.getProductService().queryTabsHeader());
    }
 
    @RequestMapping(value = "/cache/tree", method = RequestMethod.GET)
    public ResponseValue getMerchantCategoryCacheTree(){
        List<ProductCategoryVo> list = this.getProductCategoryCache().getTree(1);
        return ResponseValue.success(list);
    }
}