@ -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 < SxCategoryModel > sxCategoryModelList = categoryListJSON . toList ( SxCategoryModel . class ) ;
List < SxCategoryModel > 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 < BrandModel > brandModels = brandListJSON . toList ( BrandModel . class ) ;
List < BrandModel > 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 < SyncThirdMemberReq > 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 < String , Integer > 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 < String > failMessage = new ArrayList < > ( ) ;
shopBaseProductCategoryService . getCategoryListByStoreId ( storeId ) ;
/ / getBrandMapByStoreId ( )
Map < String , Integer > brandMaps = productBrandService . getBrandMapByStoreId ( storeId ) ;
Map < String , Integer > brandMaps = productBrandService . getBrandMapByStoreId ( storeId ) ;
QueryWrapper < StoreDbConfig > 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 < String > folders ) {
if ( StrUtil . isBlank ( appKey ) | | StrUtil . isBlank ( sign ) ) {
public ThirdApiRes fileUploadToOss ( String appKey , String sign , String syncType , Date refreshDate , List < String > folders ) {
if ( StrUtil . isBlank ( appKey ) | | StrUtil . isBlank ( sign ) ) {
return new ThirdApiRes ( ) . fail ( 1003 , I18nUtil . _ ( " 缺少必要参数! " ) ) ;
}
/ / 验签 、 appid , 必要参数判断
SyncApp syncApp = syncAppService . getOne ( new LambdaQueryWrapper < SyncApp > ( )
. 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 < ActiveModel > activeModelList = activeJsonArray . toList ( ActiveModel . class ) ;
Gson gson = new GsonBuilder ( )
List < ActiveModel > 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 < ShopStoreActivityBase > addshopStoreActivityBaseList = new ArrayList < > ( ) ;
List < ShopStoreActivityBase > updateShopStoreActivityBaseList = new ArrayList < > ( ) ;
Map < String , String > stringIntegerMap = checkExistingActive ( jsonArray , storeId ) ;
AccountUserBase accountUserBase = new AccountUserBase ( ) ;
private void batchSaveShopStoreActivityBase ( JSONArray jsonArray , Integer storeId ) {
List < ShopStoreActivityBase > addshopStoreActivityBaseList = new ArrayList < > ( ) ;
List < ShopStoreActivityBase > updateShopStoreActivityBaseList = new ArrayList < > ( ) ;
Map < String , String > 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 < ActiveShopInfo > activeShopInfos = activeJsonArray . toList ( ActiveShopInfo . class ) ;
Gson gson = new GsonBuilder ( )
List < ActiveShopInfo > 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 < ShopStoreActivityBase > updateShopStoreActivityBaseList = new ArrayList < > ( ) ;
Map < String , String > stringIntegerMap = checkExistingActive ( jsonArray , storeId ) ;
List < String > productNumbers = new ArrayList < > ( ) ;
jsonArray . stream ( ) . parallel ( ) . forEach ( object - > {
final JSONObject jsonObj = ( JSONObject ) object ;
private void batchAddActiveShopBase ( JSONArray jsonArray , Integer storeId ) {
List < ShopStoreActivityBase > updateShopStoreActivityBaseList = new ArrayList < > ( ) ;
Map < String , String > stringIntegerMap = checkExistingActive ( jsonArray , storeId ) ;
List < String > 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 < String , Long > shopProduckKeyMap = shopProductBaseService . getProductBasicIdByStore ( storeId , productNumbers ) ; / / 获取货架号对应的商品id
Map < String , String > shopItemKeyMap = shopProductItemService . getProductItemIdByStore ( storeId , productNumbers ) ; / / 获取商品id对应的item的id
Map < String , Long > shopProduckKeyMap = shopProductBaseService . getProductBasicIdByStore ( storeId , productNumbers ) ; / / 获取货架号对应的商品id
Map < String , String > shopItemKeyMap = shopProductItemService . getProductItemIdByStore ( storeId , productNumbers ) ; / / 获取商品id对应的item的id
Map < String , String > 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 < String > activityIds = new ArrayList < > ( ) ;
if ( ! stringIntegerMap . isEmpty ( ) ) {
stringIntegerMap . forEach ( ( k , v ) - > {
String activityId = v . split ( " __ " ) [ 0 ] ;
activityIds . add ( activityId ) ;
} ) ;
}
Map < String , Long > shopStoreActvityItems = shopStoreActivityItemService . getExistingActvieShopItem ( activityIds , storeId ) ;
/ / activity_id , product_id , item_id
List < ShopStoreActivityItem > addShopStoreActivityItems = new ArrayList < > ( ) ;
List < ShopStoreActivityItem > updteShopStoreActivityItems = new ArrayList < > ( ) ;
Map < String , String > 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 < org . json . JSONObject > 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 < org . json . JSONObject > 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 ( ) ) ;
}
/ * *