diff --git a/mall-common/src/main/java/com/suisung/mall/common/modules/store/ShopStoreActivityItem.java b/mall-common/src/main/java/com/suisung/mall/common/modules/store/ShopStoreActivityItem.java index d251842d..a1e70711 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/modules/store/ShopStoreActivityItem.java +++ b/mall-common/src/main/java/com/suisung/mall/common/modules/store/ShopStoreActivityItem.java @@ -1,6 +1,7 @@ package com.suisung.mall.common.modules.store; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModel; @@ -81,5 +82,6 @@ public class ShopStoreActivityItem implements Serializable { @ApiModelProperty(value = "积分商品已售数量") private Integer activity_points_product_sale_num; - + @TableField(exist = false) + private String product_group; } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java index e787c5a0..0831a718 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductBaseServiceImpl.java @@ -6223,7 +6223,7 @@ public class ShopProductBaseServiceImpl extends BaseServiceImpl shopProductItems= shopProductItemService.list(queryWrapper); Map countMap=shopProductItems.stream().collect(Collectors.toMap(ShopProductItem::getProduct_id,ShopProductItem::getItem_quantity)); items.forEach(shopProductIndex -> { - shopProductIndex.put("item_quantity",MapUtil.getLong(countMap,shopProductIndex.get("product_id"))); + shopProductIndex.put("itemQuantity",MapUtil.getLong(countMap,shopProductIndex.get("product_id"))); }); return items; } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductItemServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductItemServiceImpl.java index 7fdc3d39..7833e341 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductItemServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/product/service/impl/ShopProductItemServiceImpl.java @@ -2251,15 +2251,21 @@ public class ShopProductItemServiceImpl extends BaseServiceImpl { queryWrapper.or(q->q.eq("store_id",store_id).eq("item_src_id",productNumber)); }); queryWrapper.eq("store_id",store_id); queryWrapper.groupBy("product_id"); List shopProductItems= this.list(queryWrapper); - Map map=shopProductItems.stream().collect(Collectors.toMap(ShopProductItem::getProduct_id,shopProductItem->shopProductItem.getMergedItemId() - +"_"+shopProductItem.getMergedUnitPrices())); +// Map map=shopProductItems.stream().collect(Collectors.toMap(ShopProductItem::getProduct_id,shopProductItem->shopProductItem.getMergedItemId() +// +"_"+shopProductItem.getMergedUnitPrices())); + Map map=new HashMap(); + shopProductItems.forEach(item -> { + map.put(String.valueOf(item.getProduct_id()),item.getMergedItemId()+"_"+item.getMergedUnitPrices()); + map.put(item.getProduct_id()+":category_id",String.valueOf(item.getCategory_id())); + }); + return map; } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStoreActivityItemService.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStoreActivityItemService.java index 853403b6..4046963b 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStoreActivityItemService.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStoreActivityItemService.java @@ -20,4 +20,5 @@ public interface ShopStoreActivityItemService extends IBaseService getNormalItem(List item_ids); + Map getExistingActvieShopItem(List activityIds,Integer storeId); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreActivityItemServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreActivityItemServiceImpl.java index a392ce4c..bc251fa8 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreActivityItemServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreActivityItemServiceImpl.java @@ -196,4 +196,21 @@ public class ShopStoreActivityItemServiceImpl extends BaseServiceImpl getExistingActvieShopItem( List activityIds,Integer storeId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.select("CONCAT_WS('-', activity_id, product_id,item_id) AS product_group,activity_item_id"); + activityIds.forEach(activityId -> { + queryWrapper.or(q->q.eq("activity_id", activityId).eq("store_id", storeId)); + }); + Map result = new HashMap<>(); + List shopStoreActivityItems=list(queryWrapper); + shopStoreActivityItems.forEach(shopStoreActivityItem -> { + result.put(shopStoreActivityItem.getProduct_group(),shopStoreActivityItem.getActivity_item_id()); + }); + + return result; + } + } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveModel.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveModel.java index 8b5e793a..a35bcc4e 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveModel.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveModel.java @@ -45,4 +45,7 @@ public class ActiveModel implements Serializable { @ApiModelProperty(value = "活动id") private String flowNo; + @ApiModelProperty(value = "购买下限") + private BigDecimal activity_item_min_quantity; + } \ No newline at end of file diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveShopInfo.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveShopInfo.java index cef3a35f..93026e5c 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveShopInfo.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/dto/ActiveShopInfo.java @@ -44,4 +44,7 @@ public class ActiveShopInfo { @ApiModelProperty(value = "活动id") private String flowNo; + + @ApiModelProperty(value = "购买下限") + private BigDecimal activityItemMinQuantity; } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncBaseThirdSxAbstract.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncBaseThirdSxAbstract.java index 58206863..1e0c578d 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncBaseThirdSxAbstract.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncBaseThirdSxAbstract.java @@ -978,9 +978,6 @@ public abstract class SyncBaseThirdSxAbstract{ .map(object->{ final JSONObject jsonObj= (JSONObject) object; ShopStoreActivityBase shopStoreActivityBase=new ShopStoreActivityBase(); - shopStoreActivityBase.setActivity_name(jsonObj.getStr("activityName")); - shopStoreActivityBase.setActivity_state(jsonObj.getInt("activityState")); - shopStoreActivityBase.setActivity_starttime(jsonObj.getDate("activityStarttime")); shopStoreActivityBase.setFlow_no(jsonObj.getStr("flowNo")); return shopStoreActivityBase; }) @@ -992,7 +989,8 @@ public abstract class SyncBaseThirdSxAbstract{ Map resultMap=new HashMap<>(); existing.forEach(shopStoreActivityBase -> { - resultMap.put(shopStoreActivityBase.getActivity_name()+"_"+shopStoreActivityBase.getActivity_starttime().getTime()+"_"+shopStoreActivityBase.getActivity_state()+"_"+shopStoreActivityBase.getFlow_no(),shopStoreActivityBase.getActivity_id()+"__"+(StringUtils.isEmpty(shopStoreActivityBase.getActivity_rule())?"0":shopStoreActivityBase.getActivity_rule())); + resultMap.put(shopStoreActivityBase.getFlow_no(), + shopStoreActivityBase.getActivity_id()+"__"+(StringUtils.isEmpty(shopStoreActivityBase.getActivity_rule())?"0":shopStoreActivityBase.getActivity_rule())); }); return resultMap; @@ -1014,9 +1012,7 @@ public abstract class SyncBaseThirdSxAbstract{ QueryWrapper query = new QueryWrapper<>(); query.select("activity_name", "activity_state","activity_starttime","activity_rule","activity_endtime","activity_id","flow_no"); shopStoreActivityBases.forEach(shopStoreActivityBase -> { - query.or(q -> q.eq("activity_name", shopStoreActivityBase.getActivity_name()).eq("activity_state",shopStoreActivityBase.getActivity_state()) - .eq("activity_starttime",shopStoreActivityBase.getActivity_starttime()) - .eq("flow_no",shopStoreActivityBase.getFlow_no()) + query.or(q -> q.eq("flow_no",shopStoreActivityBase.getFlow_no()) .eq("store_id", storeId)); }); diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java index 7fa25913..72952622 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java @@ -37,6 +37,7 @@ import com.suisung.mall.common.modules.base.ShopBaseProductCategory; import com.suisung.mall.common.modules.sixun.SxSyncGoods; import com.suisung.mall.common.modules.sixun.SxSyncVip; import com.suisung.mall.common.modules.store.ShopStoreActivityBase; +import com.suisung.mall.common.modules.store.ShopStoreActivityItem; import com.suisung.mall.common.modules.sync.StoreDbConfig; import com.suisung.mall.common.modules.sync.SyncApp; import com.suisung.mall.common.modules.sync.SyncConfig; @@ -45,10 +46,10 @@ import com.suisung.mall.common.pojo.req.SyncThirdMemberReq; import com.suisung.mall.common.pojo.res.ThirdApiRes; import com.suisung.mall.common.utils.I18nUtil; import com.suisung.mall.common.utils.StringUtils; -import com.suisung.mall.core.web.service.RedisService; import com.suisung.mall.shop.base.service.ShopBaseProductBrandService; import com.suisung.mall.shop.base.service.ShopBaseProductCategoryService; import com.suisung.mall.shop.base.service.ShopBaseProductSpecService; +import com.suisung.mall.shop.chain.controller.admin.ShopChainItemController; import com.suisung.mall.shop.number.service.ShopNumberSeqService; import com.suisung.mall.shop.page.service.OssService; import com.suisung.mall.shop.product.service.ShopProductBaseService; @@ -64,6 +65,7 @@ import com.suisung.mall.shop.sixun.service.SxSyncVipService; import com.suisung.mall.shop.sixun.utils.CommonUtil; import com.suisung.mall.shop.sixun.utils.FileUtils; import com.suisung.mall.shop.store.service.ShopStoreActivityBaseService; +import com.suisung.mall.shop.store.service.ShopStoreActivityItemService; import com.suisung.mall.shop.sync.Utils.ActiveShopJsonUtils; import com.suisung.mall.shop.sync.Utils.BigDecimalFormatter; import com.suisung.mall.shop.sync.Utils.ThreadFileUtils; @@ -88,7 +90,6 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @@ -173,6 +174,11 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements @Autowired private ShopBaseProductBrandService productBrandService; + @Autowired + private ShopStoreActivityItemService shopStoreActivityItemService; + @Autowired + private ShopChainItemController item; + /** * 批量保存商品的分类 * @@ -185,9 +191,9 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); } //用gson保证与客户端的顺序一致 - List sxCategoryModelList=categoryListJSON.toList(SxCategoryModel.class); + List sxCategoryModelList = categoryListJSON.toList(SxCategoryModel.class); Gson gson = new Gson(); - String jsonStr=gson.toJson(sxCategoryModelList); + String jsonStr = gson.toJson(sxCategoryModelList); // 验签、appid,必要参数判断 SyncApp syncApp = syncAppService.checkAppSign(appKey, sign, jsonStr); if (syncApp == null) { @@ -223,9 +229,9 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); } //用gson保证与客户端的顺序一致 - List brandModels=brandListJSON.toList(BrandModel.class); + List brandModels = brandListJSON.toList(BrandModel.class); Gson gson = new Gson(); - String jsonStr=gson.toJson(brandModels); + String jsonStr = gson.toJson(brandModels); // 验签、appid,必要参数判断 SyncApp syncApp = syncAppService.checkAppSign(appKey, sign, jsonStr); if (syncApp == null) { @@ -286,14 +292,14 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements */ @Override @GlobalTransactional - // @Transactional + // @Transactional public ThirdApiRes saveOrUpdateMemberBatch(String appKey, String sign, List memberList) { if (StrUtil.isBlank(appKey) || StrUtil.isBlank(sign) || CollUtil.isEmpty(memberList)) { return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); } //用gson保证与客户端的顺序一致 Gson gson = new Gson(); - String jsonStr=gson.toJson(memberList); + String jsonStr = gson.toJson(memberList); // 验签、appid,必要参数判断 SyncApp syncApp = syncAppService.checkAppSign(appKey, sign, jsonStr); if (syncApp == null) { @@ -305,14 +311,14 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements return new ThirdApiRes().fail(1004, I18nUtil._("单次同步记录最多" + limitCnt + "条!")); } shopNumberSeqService.clearKeyStoreAccountBaseId(); - int count =0; + int count = 0; try { - count= baseBatchSaveOrUpdateMemberBatch(memberList, storeId); - }catch (RuntimeException e) { - throw new ApiException(_("保存失败"+e.getMessage())); + count = baseBatchSaveOrUpdateMemberBatch(memberList, storeId); + } catch (RuntimeException e) { + throw new ApiException(_("保存失败" + e.getMessage())); } - // shopNumberSeqService.clearKeyStoreAccountBaseId(); + // shopNumberSeqService.clearKeyStoreAccountBaseId(); Map resp = new HashMap<>(); resp.put("count", count); @@ -504,7 +510,7 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements String jsonStr = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); //验证签名,签名不正确删除文件 SyncApp syncApp = syncAppService.checkAppSign(appKey, sign, jsonStr); - if(syncApp == null) { + if (syncApp == null) { Files.delete(path);//删除文件 return new ThirdApiRes().fail(500, "文件上传失败:签名验证失败"); } @@ -573,14 +579,14 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements List failMessage = new ArrayList<>(); shopBaseProductCategoryService.getCategoryListByStoreId(storeId); // getBrandMapByStoreId() - Map brandMaps= productBrandService.getBrandMapByStoreId(storeId); + Map brandMaps = productBrandService.getBrandMapByStoreId(storeId); QueryWrapper storeDbConfigQueryWrapper = new QueryWrapper<>(); storeDbConfigQueryWrapper.eq("store_id", storeId); StoreDbConfig storeDbConfig = storeDbConfigService.getOne(storeDbConfigQueryWrapper); for (int i = 0; i < newFolders.size(); i++) { final int taskId = i; - final String isNegativeAllowed=storeDbConfig.getIsNegativeAllowed(); - String priorityMode=storeDbConfig.getPriorityMode(); + final String isNegativeAllowed = storeDbConfig.getIsNegativeAllowed(); + String priorityMode = storeDbConfig.getPriorityMode(); threadNum.incrementAndGet(); futures.add(executor.submit(() -> { int count = 0;//失败重试机制,当失败重试一次,再次失败则记录到数据库中 @@ -590,7 +596,7 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements String fileName = "good_" + (taskId + 1) + ".txt"; JSONArray jsonArray = new ThreadFileUtils().processFolder(taskName, newFolders.get(taskId)); try { - baseSaveOrUpdateGoodsBatch(jsonArray, storeId,isNegativeAllowed,priorityMode,brandMaps); + baseSaveOrUpdateGoodsBatch(jsonArray, storeId, isNegativeAllowed, priorityMode, brandMaps); success.getAndIncrement(); threadNum.decrementAndGet(); return "成功" + taskId; @@ -660,7 +666,7 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements } @Override - public void syncShopImages(Integer storeId){ + public void syncShopImages(Integer storeId) { syncShopImageService.syncMapingShopImages(storeId); } @@ -899,77 +905,82 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements @Override - public ThirdApiRes fileUploadToOss(String appKey, String sign, String syncType,Date refreshDate, List folders) { - if (StrUtil.isBlank(appKey) || StrUtil.isBlank(sign) ) { + public ThirdApiRes fileUploadToOss(String appKey, String sign, String syncType, Date refreshDate, List folders) { + if (StrUtil.isBlank(appKey) || StrUtil.isBlank(sign)) { return new ThirdApiRes().fail(1003, I18nUtil._("缺少必要参数!")); } // 验签、appid,必要参数判断 SyncApp syncApp = syncAppService.getOne(new LambdaQueryWrapper() - .select(SyncApp::getApp_key, SyncApp::getApp_secret,SyncApp::getStore_id) + .select(SyncApp::getApp_key, SyncApp::getApp_secret, SyncApp::getStore_id) .eq(SyncApp::getApp_key, appKey) - .eq(SyncApp::getApp_secret,sign)); + .eq(SyncApp::getApp_secret, sign)); if (syncApp == null) { return new ThirdApiRes().fail(1001, I18nUtil._("签名有误!")); } String storeId = syncApp.getStore_id(); - if(null==syncApp.getStore_id()|| syncApp.getStore_id().isEmpty()){ + if (null == syncApp.getStore_id() || syncApp.getStore_id().isEmpty()) { logger.info("商店id为空"); - return new ThirdApiRes().fail(250,"商店id为空"); + return new ThirdApiRes().fail(250, "商店id为空"); } - if(folders==null||folders.isEmpty()){ + if (folders == null || folders.isEmpty()) { logger.info("没有商品数据"); - return new ThirdApiRes().fail(250,"没有商品数据"); + return new ThirdApiRes().fail(250, "没有商品数据"); } - String newfolder=new FileUtils().getSyncTypeFlag(syncType,clientPath)+storeId+FileUtils.pathSeparator+folders.get(0)+FileUtils.pathSeparator; + String newfolder = new FileUtils().getSyncTypeFlag(syncType, clientPath) + storeId + FileUtils.pathSeparator + folders.get(0) + FileUtils.pathSeparator; upLoadZipToOss(newfolder);//上传文件到cos return new ThirdApiRes().success("上传成功"); } @Override - @Async public void syncActives(String appKey, String sign, JSONArray activeJsonArray) { if (StrUtil.isBlank(appKey) || StrUtil.isBlank(sign) || ObjectUtil.isEmpty(activeJsonArray)) { logger.error("缺少必要参数!"); return; } // 验签、appid,必要参数判断 - List activeModelList= activeJsonArray.toList(ActiveModel.class); - Gson gson=new GsonBuilder() + List activeModelList = activeJsonArray.toList(ActiveModel.class); + Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") // 设置全局 Date 格式 .create(); - String gsonStr= gson.toJson(activeModelList); - SyncApp syncAppO = syncAppService.checkAppSign(appKey,sign,gsonStr); + String gsonStr = gson.toJson(activeModelList); + SyncApp syncAppO = syncAppService.checkAppSign(appKey, sign, gsonStr); if (syncAppO == null) { logger.error("签名有误!"); return; } String storeId = syncAppO.getStore_id(); //活动逻辑 - batchSaveShopStoreActivityBase(activeJsonArray, Integer.valueOf(storeId)); + new Thread(new Runnable() { + @Override + public void run() { + batchSaveShopStoreActivityBase(activeJsonArray, Integer.valueOf(storeId)); + } + }).start(); } /** * 批量新增或更新活动数据 + * * @param jsonArray */ - private void batchSaveShopStoreActivityBase(JSONArray jsonArray,Integer storeId) { - List addshopStoreActivityBaseList=new ArrayList<>(); - List updateShopStoreActivityBaseList=new ArrayList<>(); - Map stringIntegerMap= checkExistingActive(jsonArray,storeId); - AccountUserBase accountUserBase=new AccountUserBase(); + private void batchSaveShopStoreActivityBase(JSONArray jsonArray, Integer storeId) { + List addshopStoreActivityBaseList = new ArrayList<>(); + List updateShopStoreActivityBaseList = new ArrayList<>(); + Map stringIntegerMap = checkExistingActive(jsonArray, storeId); + AccountUserBase accountUserBase = new AccountUserBase(); accountUserBase.setUser_is_admin(1); accountUserBase.setStore_ids("1"); - AccountUserBase accountUserBases=accountService.findOneAccountUserBase(accountUserBase); - Integer userId= accountUserBases.getUser_id(); - jsonArray.stream().parallel().forEach(object->{ - final JSONObject jsonObj= (JSONObject) object; - String activityName=jsonObj.getStr("activityName"); - Integer activityTypeId=jsonObj.getInt("activityTypeId"); - if(null==activityTypeId){ + AccountUserBase accountUserBases = accountService.findOneAccountUserBase(accountUserBase); + Integer userId = accountUserBases.getUser_id(); + jsonArray.stream().parallel().forEach(object -> { + final JSONObject jsonObj = (JSONObject) object; + String activityName = jsonObj.getStr("activityName"); + Integer activityTypeId = jsonObj.getInt("activityTypeId"); + if (null == activityTypeId) { return; } - ShopStoreActivityBase shopStoreActivityBase=new ShopStoreActivityBase(); + ShopStoreActivityBase shopStoreActivityBase = new ShopStoreActivityBase(); if (activityTypeId == 1) { activityTypeId = StateCode.ACTIVITY_TYPE_LIMITED_DISCOUNT; //限时秒杀 } @@ -977,22 +988,22 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements activityTypeId = StateCode.ACTIVITY_TYPE_ONE_PIECE_DISCOUNT; //折扣 } if (activityTypeId == 3) { - JSONArray activeMaxDesList=jsonObj.getJSONArray("activeMaxDesList"); + JSONArray activeMaxDesList = jsonObj.getJSONArray("activeMaxDesList"); activityTypeId = StateCode.ACTIVITY_TYPE_REDUCTION; //满减 - String rules= ActiveShopJsonUtils.getRules(activeMaxDesList); + String rules = ActiveShopJsonUtils.getRules(activeMaxDesList); shopStoreActivityBase.setActivity_rule(rules); } - Date activityStarttime=jsonObj.getDate("activityStarttime"); - Date activityEndtime=jsonObj.getDate("activityEndtime"); - Integer activityState=jsonObj.getInt("activityState"); - String flowNo=jsonObj.getStr("flowNo"); - // Date activityReleasetime= jsonObj.getDate("activityReleasetime"); - BigDecimal discount=jsonObj.getBigDecimal("discount"); - String key=activityName+"_"+activityStarttime.getTime()+"_"+activityState+"_"+flowNo; + Date activityStarttime = jsonObj.getDate("activityStarttime"); + Date activityEndtime = jsonObj.getDate("activityEndtime"); + Integer activityState = jsonObj.getInt("activityState"); + String flowNo = jsonObj.getStr("flowNo"); + // Date activityReleasetime= jsonObj.getDate("activityReleasetime"); + BigDecimal discount = jsonObj.getBigDecimal("discount"); shopStoreActivityBase.setActivity_state(activityState); shopStoreActivityBase.setActivity_name(activityName); shopStoreActivityBase.setActivity_type(1);//免费参与 - // shopStoreActivityBase.setActivity_releasetime(activityReleasetime); + shopStoreActivityBase.setActivity_title(activityName); + // shopStoreActivityBase.setActivity_releasetime(activityReleasetime); shopStoreActivityBase.setStore_id(storeId); shopStoreActivityBase.setActivity_on_is_off(0); shopStoreActivityBase.setActivity_type_id(activityTypeId); @@ -1002,26 +1013,25 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements shopStoreActivityBase.setSubsite_id(0); shopStoreActivityBase.setUser_id(userId); shopStoreActivityBase.setFlow_no(flowNo); - if(stringIntegerMap.containsKey(key)){//更新 - String keyVal= MapUtil.getStr(stringIntegerMap,key); - shopStoreActivityBase.setActivity_id(MapUtil.getInt(stringIntegerMap,keyVal.split("_")[0])); - synchronized(updateShopStoreActivityBaseList){ + if (stringIntegerMap.containsKey(flowNo)) {//更新 + String keyVal = MapUtil.getStr(stringIntegerMap, flowNo); + shopStoreActivityBase.setActivity_id(MapUtil.getInt(stringIntegerMap, keyVal.split("_")[0])); + synchronized (updateShopStoreActivityBaseList) { updateShopStoreActivityBaseList.add(shopStoreActivityBase); } - }else { - synchronized(addshopStoreActivityBaseList) { + } else { + synchronized (addshopStoreActivityBaseList) { addshopStoreActivityBaseList.add(shopStoreActivityBase); } } - }); - if(CollectionUtil.isNotEmpty(addshopStoreActivityBaseList)){ - shopStoreActivityBaseService.saveBatch(addshopStoreActivityBaseList,addshopStoreActivityBaseList.size()); + if (CollectionUtil.isNotEmpty(addshopStoreActivityBaseList)) { + shopStoreActivityBaseService.saveBatch(addshopStoreActivityBaseList, addshopStoreActivityBaseList.size()); } - if(CollectionUtil.isNotEmpty(updateShopStoreActivityBaseList)){ + if (CollectionUtil.isNotEmpty(updateShopStoreActivityBaseList)) { shopStoreActivityBaseService.updateBatchById(updateShopStoreActivityBaseList); } - logger.info("同步活动数据结束:共{}条数据更新,{}条数据新增",updateShopStoreActivityBaseList.size(),addshopStoreActivityBaseList.size()); + logger.info("同步活动数据结束:共{}条数据更新,{}条数据新增", updateShopStoreActivityBaseList.size(), addshopStoreActivityBaseList.size()); } @@ -1029,135 +1039,191 @@ public class SyncThirdDataServiceImpl extends SyncBaseThirdSxAbstract implements * 将商品添加到活动 * 1、找出匹配的活动 * 2、新增商品到活动列表 + * * @param appKey * @param sign * @param activeJsonArray */ @Override - @Async public void syncActiveShops(String appKey, String sign, JSONArray activeJsonArray) { if (StrUtil.isBlank(appKey) || StrUtil.isBlank(sign) || ObjectUtil.isEmpty(activeJsonArray)) { logger.error("缺少必要参数!"); return; } // 验签、appid,必要参数判断 - List activeShopInfos= activeJsonArray.toList(ActiveShopInfo.class); - Gson gson=new GsonBuilder() + List activeShopInfos = activeJsonArray.toList(ActiveShopInfo.class); + Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") // 设置全局 Date 格式 - .registerTypeAdapter(BigDecimal.class,new BigDecimalTypeAdapter()) + .registerTypeAdapter(BigDecimal.class, new BigDecimalTypeAdapter()) .create(); - String gsonStr= gson.toJson(activeShopInfos); - SyncApp syncAppO = syncAppService.checkAppSign(appKey,sign,gsonStr); + String gsonStr = gson.toJson(activeShopInfos); + SyncApp syncAppO = syncAppService.checkAppSign(appKey, sign, gsonStr); if (syncAppO == null) { logger.error("签名有误!"); return; } String storeId = syncAppO.getStore_id(); //把商品添加到活动的逻辑 - batchAddActiveShopBase(activeJsonArray, Integer.valueOf(storeId)); - + new Thread(new Runnable() { + @Override + public void run() { + batchAddActiveShopBase(activeJsonArray, Integer.valueOf(storeId)); + } + }).start(); } /** * 批量新增或更新活动商品数据 + * * @param jsonArray */ - private void batchAddActiveShopBase(JSONArray jsonArray,Integer storeId) { - List updateShopStoreActivityBaseList=new ArrayList<>(); - Map stringIntegerMap= checkExistingActive(jsonArray,storeId); - List productNumbers=new ArrayList<>(); - jsonArray.stream().parallel().forEach(object->{ - final JSONObject jsonObj= (JSONObject) object; + private void batchAddActiveShopBase(JSONArray jsonArray, Integer storeId) { + List updateShopStoreActivityBaseList = new ArrayList<>(); + Map stringIntegerMap = checkExistingActive(jsonArray, storeId); + List productNumbers = new ArrayList<>(); + jsonArray.stream().parallel().forEach(object -> { + final JSONObject jsonObj = (JSONObject) object; synchronized (productNumbers) { - String itemNo=jsonObj.getStr("itemNo"); + String itemNo = jsonObj.getStr("itemNo"); productNumbers.add(itemNo); } }); - Map shopProduckKeyMap=shopProductBaseService.getProductBasicIdByStore(storeId,productNumbers);//获取货架号对应的商品id - Map shopItemKeyMap=shopProductItemService.getProductItemIdByStore(storeId,productNumbers);//获取商品id对应的item的id + Map shopProduckKeyMap = shopProductBaseService.getProductBasicIdByStore(storeId, productNumbers);//获取货架号对应的商品id + Map shopItemKeyMap = shopProductItemService.getProductItemIdByStore(storeId, productNumbers);//获取商品id对应的item的id - Map rulesChe=new HashMap();//存储rule格式activeId:rules,如果不存在则从数据库取,存在则从缓存取数据 - jsonArray.forEach(object->{ - final JSONObject jsonObj= (JSONObject) object; - String activityName=jsonObj.getStr("activityName"); - Integer activityTypeId=jsonObj.getInt("activityTypeId"); - if(null==activityTypeId){ + List activityIds = new ArrayList<>(); + if (!stringIntegerMap.isEmpty()) { + stringIntegerMap.forEach((k, v) -> { + String activityId = v.split("__")[0]; + activityIds.add(activityId); + }); + } + + Map shopStoreActvityItems = shopStoreActivityItemService.getExistingActvieShopItem(activityIds, storeId); + //activity_id, product_id,item_id + List addShopStoreActivityItems = new ArrayList<>(); + List updteShopStoreActivityItems = new ArrayList<>(); + Map rulesChe = new HashMap();//存储rule格式activeId:rules,如果不存在则从数据库取,存在则从缓存取数据 + jsonArray.forEach(object -> { + final JSONObject jsonObj = (JSONObject) object; + String activityName = jsonObj.getStr("activityName"); + Integer activityTypeId = jsonObj.getInt("activityTypeId"); + if (null == activityTypeId) { return; } - String ruleType=""; - List newItems=new ArrayList<>(); - Long productId=null; - String itemNo=jsonObj.getStr("itemNo"); - BigDecimal discount=jsonObj.getBigDecimal("discount"); - if(shopProduckKeyMap.containsKey(itemNo)){ - productId= (Long) shopProduckKeyMap.get(itemNo); + String ruleType = ""; + List newItems = new ArrayList<>(); + Long productId = null; + String itemNo = jsonObj.getStr("itemNo"); + BigDecimal discount = jsonObj.getBigDecimal("discount"); + if (shopProduckKeyMap.containsKey(itemNo)) { + productId = (Long) shopProduckKeyMap.get(itemNo); } - if(null==productId){ + if (null == productId) { return; } - Date activityStarttime=jsonObj.getDate("activityStarttime"); - Integer activityState=jsonObj.getInt("activityState"); - String flowNo=jsonObj.getStr("flowNo"); - - String key=activityName+"_"+activityStarttime.getTime()+"_"+activityState+"_"+flowNo; - - if(stringIntegerMap.containsKey(key)){//更新 - String keyVal=MapUtil.getStr(stringIntegerMap,key); - String activityId=keyVal.split("_")[0]; - String rules=""; - if(null!=rulesChe.get(activityId)){ - rules=rulesChe.get(activityId); - }else { - rules=keyVal.split("__")[1].equals("0")?null:keyVal.split("_")[1]; + Date activityStarttime = jsonObj.getDate("activityStarttime"); + Integer activityState = jsonObj.getInt("activityState"); + String flowNo = jsonObj.getStr("flowNo"); + if (stringIntegerMap.containsKey(flowNo)) {//更新 + String keyVal = MapUtil.getStr(stringIntegerMap, flowNo); + String activityId = keyVal.split("__")[0]; + String rules = ""; + if (null != rulesChe.get(activityId)) { + rules = rulesChe.get(activityId); + } else { + rules = keyVal.split("__")[1].equals("0") ? null : keyVal.split("_")[1]; } - String itemIds= shopItemKeyMap.get(productId); + String itemIds = shopItemKeyMap.get(String.valueOf(productId)); // if(null!=itemIds){ // itemIds= (String) shopItemKeyMap.get(String.valueOf(productId)); // } assert itemIds != null; + BigDecimal oldPrice; + BigDecimal specPrice; + BigDecimal newDiscout = new BigDecimal(1); if (activityTypeId == 1) {//限时秒杀 - ruleType=ActiveShopJsonUtils.SECKILL; - BigDecimal oldPrice=jsonObj.getBigDecimal("oldPrice"); - BigDecimal specPrice=jsonObj.getBigDecimal("specPrice"); - if(oldPrice.compareTo(BigDecimal.ZERO)>0&&specPrice.compareTo(BigDecimal.ZERO)>0){ - BigDecimal newDiscout=specPrice.divide(oldPrice,4, RoundingMode.HALF_UP); - newItems.addAll(getFulReduItemList(itemIds,ruleType,newDiscout));//获取满减规则的itemid + ruleType = ActiveShopJsonUtils.SECKILL; + oldPrice = jsonObj.getBigDecimal("oldPrice"); + specPrice = jsonObj.getBigDecimal("specPrice"); + if (oldPrice.compareTo(BigDecimal.ZERO) > 0 && specPrice.compareTo(BigDecimal.ZERO) > 0) { + newDiscout = specPrice.divide(oldPrice, 4, RoundingMode.HALF_UP); + newItems.addAll(getFulReduItemList(itemIds, ruleType, newDiscout));//获取满减规则的itemid } } if (activityTypeId == 2) {//折扣 - if(discount.compareTo(BigDecimal.ZERO)<=0){ + if (discount.compareTo(BigDecimal.ZERO) <= 0) { logger.error("商品折扣要大于0"); return; } - ruleType=ActiveShopJsonUtils.DISCOUNT; - newItems.addAll(getFulReduItemList(itemIds,ruleType,discount));//获取满减规则的itemid + ruleType = ActiveShopJsonUtils.DISCOUNT; + newItems.addAll(getFulReduItemList(itemIds, ruleType, discount));//获取满减规则的itemid } if (activityTypeId == 3) {//满减 - ruleType=ActiveShopJsonUtils.FULLREDUCE; - newItems.addAll(getFulReduItemList(itemIds,ruleType,null));//获取满减规则的itemid + ruleType = ActiveShopJsonUtils.FULLREDUCE; + newItems.addAll(getFulReduItemList(itemIds, ruleType, null));//获取满减规则的itemid } - String newRules= ActiveShopJsonUtils.buildPromotionRule(ruleType,rules,newItems); - rulesChe.put(activityId,newRules); + String newRules = ActiveShopJsonUtils.buildPromotionRule(ruleType, rules, newItems); + rulesChe.put(activityId, newRules); + String[] itemIdsArray = itemIds.split("_")[0].split(","); + String category_id = shopItemKeyMap.get(productId + ":category_id"); + String unitprices = itemIds.split("_")[1]; + String[] priceArray = unitprices.split(","); + for (int i = 0; i < itemIdsArray.length; i++) { + ShopStoreActivityItem shopStoreActivityItem = new ShopStoreActivityItem(); + shopStoreActivityItem.setActivity_id(Integer.valueOf(activityId)); + shopStoreActivityItem.setActivity_type_id(activityTypeId); + shopStoreActivityItem.setActivity_item_state(activityState); + shopStoreActivityItem.setActivity_item_starttime(activityStarttime); + shopStoreActivityItem.setActivity_item_endtime(jsonObj.getDate("activityEndtime")); + shopStoreActivityItem.setProduct_id(productId); + shopStoreActivityItem.setItem_id(Long.valueOf(itemIdsArray[i])); + shopStoreActivityItem.setActivity_item_min_quantity(jsonObj.getBigDecimal("activityItemMinQuantity").intValue()); + shopStoreActivityItem.setCategory_id(Integer.valueOf(category_id)); + shopStoreActivityItem.setStore_id(storeId); + + String activityItemKey = activityId + "-" + productId + "-" + itemIdsArray[i]; + Long shopActivyItemId = shopStoreActvityItems.get(activityItemKey); + if (newDiscout.compareTo(BigDecimal.ZERO) < 1) { + continue; + } + BigDecimal price = new BigDecimal(priceArray[i]).multiply(newDiscout).setScale(2, RoundingMode.HALF_UP); + shopStoreActivityItem.setActivity_item_price(price); + if (ObjectUtil.isNotEmpty(shopActivyItemId)) { + shopStoreActivityItem.setActivity_item_id(shopActivyItemId); + updteShopStoreActivityItems.add(shopStoreActivityItem); + } else { + addShopStoreActivityItems.add(shopStoreActivityItem); + } + } + } + }); + if (!rulesChe.isEmpty()) { + rulesChe.forEach((k, v) -> { + if (StringUtils.isNotEmpty(v)) { + ShopStoreActivityBase shopStoreActivityBase = new ShopStoreActivityBase(); + shopStoreActivityBase.setActivity_id(Integer.valueOf(k)); + shopStoreActivityBase.setActivity_rule(v); + updateShopStoreActivityBaseList.add(shopStoreActivityBase); + } + }); + } + if (CollectionUtil.isNotEmpty(updateShopStoreActivityBaseList)) { + shopStoreActivityBaseService.updateBatchById(updateShopStoreActivityBaseList); } - }); + if (CollectionUtil.isNotEmpty(addShopStoreActivityItems)) { + shopStoreActivityItemService.saveBatch(addShopStoreActivityItems, addShopStoreActivityItems.size()); + } - if(!rulesChe.isEmpty()){ - rulesChe.forEach((k,v)->{ - if(StringUtils.isNotEmpty(v)){ - ShopStoreActivityBase shopStoreActivityBase=new ShopStoreActivityBase(); - shopStoreActivityBase.setActivity_id(Integer.valueOf(k)); - shopStoreActivityBase.setActivity_rule(v); - updateShopStoreActivityBaseList.add(shopStoreActivityBase); + if(CollectionUtil.isNotEmpty(updteShopStoreActivityItems)){ + shopStoreActivityItemService.updateBatchById(updteShopStoreActivityItems,updteShopStoreActivityItems.size()); } - }); - } - if(CollectionUtil.isNotEmpty(updateShopStoreActivityBaseList)){ - shopStoreActivityBaseService.updateBatchById(updateShopStoreActivityBaseList); - } + logger.info("同步活动商品数据结束:更新共{}条活动数据,商品数据为{}条",updateShopStoreActivityBaseList.size(),jsonArray.size()); + logger.info("同步活动商品数据结束:新增数据{}条,更新{}条活动数据,商品数据为{}条",addShopStoreActivityItems.size(),updteShopStoreActivityItems.size(),jsonArray.size()); } /**