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 getProductInfoMap(List productIds) { Map productMap = new HashMap<>(); if (CollectionUtils.isEmpty(productIds)) { return productMap; } List 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> getProductAttrInfoMap(List productIds) { Map> attrInfoMap = new HashMap<>(); if (CollectionUtils.isEmpty(productIds)) { return attrInfoMap; } List attrInfo = shopProductAttrRefDao.selectAttrIdsByProductIds(productIds); if (CollectionUtils.isNotEmpty(attrInfo)) { for (ProductAttrInfoDTO productAttrInfoDTO : attrInfo) { List attrIdList = StringUtils.strToCollToString(productAttrInfoDTO.getAttrStr(), "/"); attrInfoMap.put(productAttrInfoDTO.getpId(), attrIdList); } } return attrInfoMap; } /** * 处理产品规格信息 * @param shopOrderDto */ public List processProductSkuInfo(ShopOrder shopOrder, ShopOrderDto shopOrderDto, ShopCoupon shopCoupon) { List resList = new ArrayList<>(); List skuList = shopOrderDto.getOrderItemList(); if (CollectionUtils.isEmpty(skuList)) { return resList; } List 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 contrastList = shopSkuDao.selectByIds(ids); List productIds = new ArrayList<>(); if (CollectionUtils.isNotEmpty(contrastList)) { for (ShopSku shopSku : contrastList) { productIds.add(shopSku.getPId()); } } //获取产品信息相关Map Map productInfoMap = getProductInfoMap(productIds); //获取产品属性相关Map Map> 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 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()); shopDeliveryInfo.setCompanyId(shopOrder.getCompanyId()); 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 detailsList, String userId) { Map 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 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 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; } }