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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package com.matrix.system.shopXcx.api.tools;
 
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.bean.CustomerDataDictionary;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.hive.service.SysVipInfoService;
import com.matrix.system.shopXcx.api.pojo.OrderItemDto;
import com.matrix.system.shopXcx.api.pojo.OrderPostageInfoPOJO;
import com.matrix.system.shopXcx.api.pojo.ShopOrderDto;
import com.matrix.system.shopXcx.api.service.WxShopMemberDayService;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
import com.matrix.system.shopXcx.dto.ProductAttrInfoDTO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author jyy
 */
@Service("wxShopOrderUtil")
public class WxShopOrderUtil {
    @Autowired
    private SMSTools smsTools;
 
    @Autowired
    private ShopOrderDao shopOrderDao;
    @Autowired
    private ShopProductDao shopProductDao;
    @Autowired
    private ShopOrderDetailsDao shopOrderDetailsDao;
    @Autowired
    private ShopSkuDao shopSkuDao;
    @Autowired
    private ShopDeliveryInfoDao shopDeliveryInfoDao;
    @Autowired
    private ShopProductAttrRefDao shopProductAttrRefDao;
    @Autowired
    private WxShopMemberDayService wxShopMemberDayService;
    @Autowired
    private SysVipInfoService sysVipInfoService;
 
 
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
 
 
    /**
     * 根据产品ids获取产品信息相关Map
     * @param productIds
     * @return
     */
    public Map<Integer, ShopProduct> getProductInfoMap(List<Integer> productIds) {
        Map<Integer, ShopProduct> productMap = new HashMap<>();
        if (CollectionUtils.isEmpty(productIds)) {
            return productMap;
        }
        List<ShopProduct> productList = shopProductDao.selectByIds(productIds);
        if (CollectionUtils.isNotEmpty(productList)) {
            for (ShopProduct shopProduct : productList) {
                productMap.put(shopProduct.getId(), shopProduct);
            }
        }
        return productMap;
    }
 
    /**
     * 根据产品ids获取产品属性相关Map
     * @param productIds
     * @return
     */
    public Map<Integer, List<String>> getProductAttrInfoMap(List<Integer> productIds) {
        Map<Integer, List<String>> attrInfoMap = new HashMap<>();
        if (CollectionUtils.isEmpty(productIds)) {
            return attrInfoMap;
        }
        List<ProductAttrInfoDTO> attrInfo = shopProductAttrRefDao.selectAttrIdsByProductIds(productIds);
        if (CollectionUtils.isNotEmpty(attrInfo)) {
            for (ProductAttrInfoDTO productAttrInfoDTO : attrInfo) {
                List<String> attrIdList = StringUtils.strToCollToString(productAttrInfoDTO.getAttrStr(), "/");
                attrInfoMap.put(productAttrInfoDTO.getpId(), attrIdList);
            }
        }
        return attrInfoMap;
    }
 
    /**
     * 处理产品规格信息
     * @param shopOrderDto
     */
    public List<ShopOrderDetails> processProductSkuInfo(ShopOrder shopOrder, ShopOrderDto shopOrderDto,
                                                        ShopCoupon shopCoupon) {
        List<ShopOrderDetails> resList = new ArrayList<>();
        List<OrderItemDto> skuList = shopOrderDto.getOrderItemList();
        if (CollectionUtils.isEmpty(skuList)) {
            return resList;
        }
        List<Integer> ids = new ArrayList<>();
        //设置购买数量
        int total = 0;
        BigDecimal commodityMoney = new BigDecimal("0");
        for (OrderItemDto orderItemDto : skuList) {
            ids.add(orderItemDto.getSkuId());
            total += orderItemDto.getCount();
        }
        shopOrder.setPurchaseQuantity(total);
        List<ShopSku> contrastList = shopSkuDao.selectByIds(ids);
 
        List<Integer> productIds = new ArrayList<>();
 
        if (CollectionUtils.isNotEmpty(contrastList)) {
            for (ShopSku shopSku : contrastList) {
                productIds.add(shopSku.getPId());
            }
        }
 
        //获取产品信息相关Map
        Map<Integer, ShopProduct> productInfoMap = getProductInfoMap(productIds);
 
        //获取产品属性相关Map
        Map<Integer, List<String>> attrInfoMap = getProductAttrInfoMap(productIds);
 
        BigDecimal couponTotalMoney = new BigDecimal("0");
        Double discountRate = wxShopMemberDayService.getMemberDay();
        for (OrderItemDto orderItemDto : skuList) {
            boolean isExist = false;
            for (ShopSku shopSku : contrastList) {
                if (shopSku.getId().equals(orderItemDto.getSkuId())) {
                    isExist = true;
                    ShopOrderDetails shopOrderDetails = new ShopOrderDetails();
                    shopOrderDetails.setCreateBy(AppConstance.SYSTEM_USER);
                    shopOrderDetails.setUpdateBy(AppConstance.SYSTEM_USER);
                    shopOrderDetails.setpId(shopSku.getPId());
                    shopOrderDetails.setsId(shopSku.getId());
                    shopOrderDetails.setPrice(shopSku.getPrice());
                    shopOrderDetails.setCount(orderItemDto.getCount());
                    BigDecimal price = new BigDecimal(shopSku.getPrice().toString());
                    BigDecimal count = new BigDecimal(orderItemDto.getCount().toString());
                    shopOrderDetails.setsTitle(shopSku.getName());
                    resList.add(shopOrderDetails);
 
                    if (shopCoupon != null) {
                        BigDecimal skuTotalMoney = price.multiply(count);
                        shopOrderDetails.setTotalPrice(skuTotalMoney);
                        commodityMoney = commodityMoney.add(skuTotalMoney);
                        //如果该优惠券优惠产品包含该产品
                        String discountProductIds = "," + shopCoupon.getProductIds() + ",";
                        Integer productId = shopSku.getPId();
                        if (discountProductIds.indexOf("," + productId + ",") != -1) {
                            couponTotalMoney = couponTotalMoney.add(skuTotalMoney);
                            break;
                        }
 
                        //如果该优惠券优惠属性包含该产品属性
                        List<String> attrIds = attrInfoMap.get(productId);
                        if (CollectionUtils.isNotEmpty(attrIds)) {
                            String discountAttrIds = "," + shopCoupon.getAttrIds() + ",";
                            for (String attrId : attrIds) {
                                if (discountAttrIds.indexOf("," + attrId + ",") != -1) {
                                    couponTotalMoney = couponTotalMoney.add(skuTotalMoney);
                                    break;
                                }
                            }
                        }
                    } else {
                        ShopProduct shopProduct = productInfoMap.get(shopSku.getPId());
                        //今日有打折活动并且该商品不是特价商品
                        if (shopProduct != null && !discountRate.equals(1D) &&
                                !AppConstance.PRODUCT_SPECIAL_PRICE.equals(shopProduct.getIsSpecialPrice())) {
                            BigDecimal skuTotalMoney = price.multiply(count);
                            BigDecimal realMoney = skuTotalMoney.multiply(new BigDecimal(discountRate.toString()));
                            shopOrderDetails.setDiscountAmount(skuTotalMoney.subtract(realMoney));
                            String discountRateStr =  new BigDecimal("10").
                                    multiply(new BigDecimal(discountRate.toString())).toString();
                            shopOrderDetails.setDiscountExplain("会员日" + discountRateStr + "折");
                            shopOrderDetails.setTotalPrice(realMoney);
                            commodityMoney = commodityMoney.add(realMoney);
                            continue;
                        }
                        BigDecimal skuTotalMoney = price.multiply(count);
                        shopOrderDetails.setTotalPrice(skuTotalMoney);
                        commodityMoney = commodityMoney.add(skuTotalMoney);
                    }
                    break;
                }
            }
            //如果其中一个规格没有找到规格信息
            if (!isExist) {
                return new ArrayList<>();
            }
        }
 
        //如果达到优惠条件
        BigDecimal discountMoney = new BigDecimal("0");
        if (shopCoupon != null && couponTotalMoney.compareTo(shopCoupon.getMinAmount()) >= 0) {
            discountMoney = shopCoupon.getOffsetAmount();
            shopOrder.setDiscountExplain(shopCoupon.getCName());
            shopOrder.setDiscountAmount(discountMoney);
        } else {
            shopOrder.setDiscountAmount(BigDecimal.ZERO);
            shopOrder.setDiscountExplain("无");
        }
        shopOrder.setCommodityPrice(commodityMoney);
        shopOrder.setOrderMoney(commodityMoney.subtract(discountMoney));
        return resList;
    }
 
    /**
     * 新增发货信息
     */
    public int insertSendPackageInfo(ShopOrder shopOrder, String shippingMethod, ShopReceiveAddress receiveAddress) {
        ShopDeliveryInfo shopDeliveryInfo = new ShopDeliveryInfo();
        shopDeliveryInfo.setCreateBy(AppConstance.SYSTEM_USER);
        shopDeliveryInfo.setUpdateBy(AppConstance.SYSTEM_USER);
        shopDeliveryInfo.setOrderId(shopOrder.getId());
        shopDeliveryInfo.setOrderNo(shopOrder.getOrderNo());
        shopDeliveryInfo.setUserId(shopOrder.getUserId());
        shopDeliveryInfo.setDeliveryWay(shippingMethod);
        shopDeliveryInfo.setShopId(shopOrder.getStoreId().longValue());
        StringBuffer receiveAddrStr = new StringBuffer();
        if (receiveAddress != null) {
            receiveAddrStr.append(receiveAddress.getAddrProvince());
            receiveAddrStr.append(receiveAddress.getAddrCity());
            receiveAddrStr.append(receiveAddress.getAddrArea());
            receiveAddrStr.append(receiveAddress.getAddrDetailaddr());
        }
        shopDeliveryInfo.setReceiveAddress(receiveAddrStr.toString());
        int res = shopDeliveryInfoDao.insert(shopDeliveryInfo);
        return res;
    }
 
    /**
     * 校验商品限购
     * @param detailsList
     * @return
     */
    public void verifyProductLimitInfo(List<ShopOrderDetails> detailsList, String userId) {
        Map<Integer, Integer> productBuyMap = new HashMap<>();
        for (ShopOrderDetails shopOrderDetails : detailsList) {
            Integer productId = shopOrderDetails.getpId();
            Integer buyCount = shopOrderDetails.getCount();
            Integer buyTotal = productBuyMap.get(productId);
            if (buyTotal == null) {
                buyTotal = 0;
            }
            buyTotal += buyCount;
            productBuyMap.put(productId, buyTotal);
        }
        for (Integer key : productBuyMap.keySet()) {
            ShopProduct shopProduct = shopProductDao.selectById(key);
            //限购数量
            Integer buyLimit = shopProduct.getBuyLimit();
            if (buyLimit == null || buyLimit <= 0) {
                continue;
            }
            //限购周期
            Integer limitCycle = shopProduct.getLimitCycle();
            if (limitCycle == null || limitCycle <= 0) {
                continue;
            }
            Integer curBuyCount = productBuyMap.get(key);
            if (curBuyCount == null) {
                curBuyCount = 0;
            }
            Integer cycleByCount = shopOrderDetailsDao.selectBuyCountByProductId(userId, limitCycle - 1, key);
            if (cycleByCount + curBuyCount > buyLimit ) {
                throw new GlobleException(shopProduct.getTitle() + "产品" + limitCycle + "天内限购" + buyLimit + "个");
            }
        }
    }
 
    /**
     * 更新产品库存和规格销量
     */
    public int updateProductAndSkuInfo(List<ShopOrderDetails> detailsList) {
        int res = 0;
        for (ShopOrderDetails shopOrderDetails : detailsList) {
            ShopProduct shopProduct = shopProductDao.selectById(shopOrderDetails.getpId());
            if (shopProduct == null) {
                throw new GlobleException("没有找到商品信息!");
            }
            if (shopProduct.getDsVolume() == null) {
                shopProduct.setDsVolume(0);
            }
            shopProduct.setDsVolume(shopProduct.getDsVolume() + shopOrderDetails.getCount());
            if (shopProduct.getRsVolume() == null) {
                shopProduct.setRsVolume(0);
            }
            shopProduct.setRsVolume(shopProduct.getRsVolume() + shopOrderDetails.getCount());
            shopProductDao.updateByModel(shopProduct);
            ShopSku shopSku = shopSkuDao.selectById(shopOrderDetails.getsId());
            if (shopSku == null) {
                throw new GlobleException("没有找到规格信息!");
            }
            if (shopSku.getStock() == null) {
                shopSku.setStock(0);
            }
            //如果购买商品库存不足
            if (shopSku.getStock() - shopOrderDetails.getCount() < 0) {
                throw new GlobleException(shopProduct.getTitle() + "[" + shopSku.getName() + "]您来晚了商品已经卖完了");
            }
            shopSku.setStock(shopSku.getStock() - shopOrderDetails.getCount());
            shopSku.setSealCount(shopSku.getSealCount()+shopOrderDetails.getCount());
 
            res = shopSkuDao.updateByModel(shopSku);
        }
        return res;
    }
 
    /**
     * 根据下单信息获取邮费
     * @param shippingMethod 购买方式(1=物流,2=门店自提)
     * @param orderMoney 订单金额
     * @param province  接受包裹省名称
     * @return
     */
    public BigDecimal getPostageByOrderInfo(Integer shippingMethod, BigDecimal orderMoney, String province) {
        //如果没有提交配送方式或配送方式不是物流
        if (shippingMethod == null || shippingMethod != 1) {
            return new BigDecimal("0");
        }
        OrderPostageInfoPOJO orderPostageInfoPOJO = getOrderPostageInfo();
        //默认为外省用户
        boolean inProvinceFlag = false;
        //判断是否是省内用户
        if (province != null && province.indexOf("湖南") != -1) {
            inProvinceFlag = true;
        }
        if (!inProvinceFlag) {
            return new BigDecimal(orderPostageInfoPOJO.getOutProvincePostage().toString());
        }
        //是否达到满减条件
        boolean isOver = orderMoney.compareTo(new BigDecimal(orderPostageInfoPOJO.getProvinceMinAmountTree().toString())) >= 0;
        //如果是省内用户并且已经达到了免邮的费用
        if (isOver) {
            return new BigDecimal("0");
        }
        BigDecimal postage  = new BigDecimal(orderPostageInfoPOJO.getInProvincePostage().toString());
        return postage;
    }
 
    /**
     * 根据用户ID和订单ID获取所购买商品名称
     * @param openId 用户openId
     * @param orderId 订单ID
     * @return 所含商品名称(多个以","隔开)
     */
    public String getProductNames(Long userId, Integer orderId) {
        ShopOrder orderDetail = shopOrderDao.selectOrderInfoById(userId, orderId);
        List<ShopOrderDetails> details = orderDetail.getDetails();
        if (CollectionUtils.isEmpty(details)) {
            return "";
        }
        StringBuffer productNameBuffer = new StringBuffer();
        Integer maxLength = 30;
        for (int i = 0; i< details.size(); i++) {
            ShopOrderDetails shopOrderDetails = details.get(i);
            ShopProduct shopProduct = shopOrderDetails.getShopProduct();
            if (shopProduct == null) {
                continue;
            }
            if (i == 0 && shopProduct.getTitle().length() > maxLength) {
                productNameBuffer.append(shopProduct.getTitle().substring(0, maxLength) + "...");
                break;
            }
            if ((productNameBuffer.length() + shopProduct.getTitle().length()) > maxLength) {
                productNameBuffer.append("等");
                break;
            }
            productNameBuffer.append(shopProduct.getTitle() + ",");
        }
        String productNames = productNameBuffer.toString();
        if (productNames.endsWith(",")) {
            productNames = productNames.substring(0, productNames.length() - 1);
        }
        if (productNames.endsWith(",等")) {
            productNames = productNames.substring(0, productNames.length() - 2) + "等";
        }
        return productNames;
    }
 
 
 
 
    /**
     * 从数据字典表中获取邮费信息
     * @return
     */
    public OrderPostageInfoPOJO getOrderPostageInfo() {
        OrderPostageInfoPOJO res = new OrderPostageInfoPOJO();
        res.setInProvincePostage(0D);
        res.setOutProvincePostage(0D);
        res.setProvinceMinAmountTree(0D);
        //TODO  暂时设置邮费为0
        res.setInProvincePostage(0D);
        return res;
    }
 
    /**
     * 处理从数据字典中获取的邮费
     * @return
     */
    private Double processPostageValue(CustomerDataDictionary dataDictionary) {
        Double res = 0D;
        try {
            Double value = Double.valueOf(dataDictionary.getValue());
            if (value != null && value.compareTo(0D) == 1) {
                res = value;
            }
        } catch (Exception e) {
            LogUtil.error("没有获取到相关的邮费信息", e);
            return 0D;
        }
        return res;
    }
 
 
 
}