@@ -515,15 +515,14 @@ public class WxCouponServiceImpl implements WxCouponService {
}
//平台券先不设置商户
List<JSONObject> merchantParamList = null;
if (record.checkCreateWithMerchants( )) {
Map<TenantEntity,String> tenantMerchantMap = null;
if (EnumCouponType.checkCreateWithMerchants(record.getType() )) {
try {
checkCouponMerchantReceiverAndUpdate(record );
tenantMerchantMap = checkCouponMerchantReceiver(record,true );
}catch(Exception e) {
return new ResultData(Result.ERROR,e.getMessage());
}
merchantParamList = JSONObject.parseArray(record.getMerchantParams(), JSONObject.class);
}else if (record.checkCreateWithMalls()) {
}else if (EnumCouponType.checkCreateWithMalls(record.getType())) {
String[] mallTenantIds = record.getMallTenantIds();
if (null == mallTenantIds || mallTenantIds.length <= 0 ) {
return new ResultData(Result.ERROR,"请选择商场");
@@ -533,21 +532,26 @@ public class WxCouponServiceImpl implements WxCouponService {
final IdWorker idWorker = IdWorker.get();
if (record.getId() == null) {
record.setId(idWorker.nextId());
if (record.checkCreateWithMerchants()) {
merchantParamList.forEach(merchantParam -> {
WxCouponMerchant cm = new WxCouponMerchant();
WxCouponMerchantDto wxCouponMerchantDto = parseMerchantParam(merchantParam);
cm.setId(idWorker.nextId());
cm.updateTenantInfo(record);
cm.setMerchantId(wxCouponMerchantDto.getMerchantId());
cm.setParameter(wxCouponMerchantDto.getParameter());
cm.setProductId(record.getId());
cm.setCreateDate(new Date());
cm.setUpdateDate(new Date());
cm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
wxCouponMerchantMapper.insert(cm);
});
}else if (record.checkCreateWithMalls()) {
if (EnumCouponType.checkCreateWithMerchants(record.getType())) {
for (Iterator<TenantEntity> it = tenantMerchantMap.keySet().iterator();it.hasNext();) {
TenantEntity mallTenant = it.next();
String merchantParamListString = tenantMerchantMap.get(mallTenant);
List<JSONObject> merchantParamList = JSONArray.parseArray(merchantParamListString, JSONObject.class);
merchantParamList.forEach(merchantParam -> {
WxCouponMerchant cm = new WxCouponMerchant();
WxCouponMerchantDto wxCouponMerchantDto = parseMerchantParam(merchantParam);
cm.setId(idWorker.nextId());
cm.updateTenantInfo(mallTenant);
cm.setMerchantId(wxCouponMerchantDto.getMerchantId());
cm.setParameter(wxCouponMerchantDto.getParameter());
cm.setProductId(record.getId());
cm.setCreateDate(new Date());
cm.setUpdateDate(new Date());
cm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
wxCouponMerchantMapper.insert(cm);
});
}
}else if (EnumCouponType.checkCreateWithMalls(record.getType())) {
for (String mt : record.getMallTenantIds()) {
WxCouponMall mall = new WxCouponMall();
mall.updateTenantInfo(record);
@@ -586,9 +590,14 @@ public class WxCouponServiceImpl implements WxCouponService {
record.setInventory(oldCoupon.getInventory() + record.getRemainInventory() - oldCoupon.getRemainInventory());
}
}
if (record.checkCreateWithMerchants()) {
updateCouponMerchants(record,merchantParamList,idWorker);
}else if (record.checkCreateWithMalls()) {
if (EnumCouponType.checkCreateWithMerchants(record.getType())) {
try {
updateCouponMerchants(record,idWorker);
} catch (Exception e) {
logger.error("updateCouponMerchants error.",e);
return new ResultData(Result.ERROR,"更新商户出错。"+e.getMessage());
}
}else if (EnumCouponType.checkCreateWithMalls(record.getType())) {
WxCouponMall couponMall = new WxCouponMall();
couponMall.updateTenantInfo(record);
couponMall.setProductId(record.getId());
@@ -614,69 +623,107 @@ public class WxCouponServiceImpl implements WxCouponService {
return new ResultData(record.getId());
}
private void updateCouponMerchants(WxCoupon record,List<JSONObject> merchantParamList,final IdWorker idWorker) {
WxCouponMerchant cmParam = new WxCouponMerchant();
cmParam.updateTenantInfo(record);
cmParam.setProductId(record.getId());
List<WxCouponMerchant> oldList = wxCouponMerchantMapper.findList(cmParam);
merchantParamList.stream().forEach(merchantParam -> {
WxCouponMerchant cm = new WxCouponMerchant();
WxCouponMerchantDto wxCouponMerchantDto = parseMerchantParam(merchantParam);
cm.updateTenantInfo(record);
cm.setMerchantId(wxCouponMerchantDto.getMerchantId());
cm.setProductId(record.getId());
WxCouponMerchant rcm = wxCouponMerchantMapper.selectOne(new QueryWrapper<>(cm));
if (rcm != null) {
rcm.setParameter(wxCouponMerchantDto.getParameter());
rcm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
rcm.setUpdateDate(new Date());
wxCouponMerchantMapper.updateById(rcm);
}else {
cm.setId(idWorker.nextId());
cm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
cm.setParameter(wxCouponMerchantDto.getParameter());
cm.setCreateDate(new Date());
cm.setUpdateDate(new Date());
wxCouponMerchantMapper.insert(cm);
}
oldList.removeIf(
old->old.getProductId().equals(record.getId()) &&
old.getMerchantId().equals(wxCouponMerchantDto.getMerchantId()));
});
oldList.stream().forEach(old->{
old.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_INVALID.getCode());
old.setUpdateDate(new Date());
wxCouponMerchantMapper.updateById(old);
});
private void updateCouponMerchants(WxCoupon record,final IdWorker idWorker) throws Exception {
Map<TenantEntity,String> tenantMerchantParmMap = checkCouponMerchantReceiver(record,false);
for (Iterator<TenantEntity> it = tenantMerchantParmMap.keySet().iterator();it.hasNext();) {
TenantEntity mallTenantEntity = it.next();
WxCouponMerchant cmParam = new WxCouponMerchant();
cmParam.updateTenantInfo(mallTenantEntity);
cmParam.setProductId(record.getId());
List<WxCouponMerchant> oldList = wxCouponMerchantMapper.findList(cmParam);
List<JSONObject> merchantParamList = JSONArray.parseArray(tenantMerchantParmMap.get(mallTenantEntity),JSONObject.class);
merchantParamList.stream().forEach(merchantParam -> {
WxCouponMerchant cm = new WxCouponMerchant();
WxCouponMerchantDto wxCouponMerchantDto = parseMerchantParam(merchantParam);
cm.updateTenantInfo(mallTenantEntity);
cm.setMerchantId(wxCouponMerchantDto.getMerchantId());
cm.setProductId(record.getId());
WxCouponMerchant rcm = wxCouponMerchantMapper.selectOne(new QueryWrapper<>(cm));
if (rcm != null) {
rcm.setParameter(wxCouponMerchantDto.getParameter());
rcm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
rcm.setUpdateDate(new Date());
wxCouponMerchantMapper.updateById(rcm);
}else {
cm.setId(idWorker.nextId());
cm.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_VALID.getCode());
cm.setParameter(wxCouponMerchantDto.getParameter());
cm.setCreateDate(new Date());
cm.setUpdateDate(new Date());
wxCouponMerchantMapper.insert(cm);
}
oldList.removeIf(
old->old.getProductId().equals(record.getId()) &&
old.getMerchantId().equals(wxCouponMerchantDto.getMerchantId()));
});
oldList.stream().forEach(old->{
old.setStatus(EnumCouponMerchantStatus.COUPON_MERCHANT_STATUS_INVALID.getCode());
old.setUpdateDate(new Date());
wxCouponMerchantMapper.updateById(old);
});
}
}
private void checkCouponMerchantReceiverAndUpdate(WxCoupon wxCoupon) throws Exception{
try {
List<WxMerchant> merchantList = checkCouponMerchantReceiver(wxCoupon);
if(merchantList.size() > 1){
wxCoupon.setBusiness(EnumBusiness.BUSINESS_ID6.getCode());
}else{
wxCoupon.setBusiness(merchantList.get(0).getBusinessId());
wxCoupon.setSubBusiness(merchantList.get(0).getSubBusinessId());
//返回商场商户,key:商场tenant value:merchantParams
private Map<TenantEntity,String> checkCouponMerchantReceiver(WxCoupon record,boolean check) throws Exception{
/*
* 父券对应子商场商户
*/
if (EnumCouponType.isParentCoupon(record.getType())) {
if (StringUtils.isBlank(record.getChildMerchantParams())){
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND.getCode(),"所属商户为空");
}
List<JSONObject> tenantMerchantParamList = JSONObject.parseArray(record.getChildMerchantParams(), JSONObject.class);
if(tenantMerchantParamList == null || tenantMerchantParamList.isEmpty()){
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND.getCode(),"所属商户为空");
}
}catch (Exception e) {
throw e;
}
Map<TenantEntity,String> retMap = new HashMap<TenantEntity,String>();
for (JSONObject o:tenantMerchantParamList) {
String mallTenantId = o.getString("tenantId");
String mallParentTenantId = record.getTenantId();
String mallMerchantParams = o.getString("mallMerchantParams");
TenantEntity mallTenantEntity = new TenantEntity();
mallTenantEntity.setTenantId(mallTenantId);
mallTenantEntity.setParentTenantId(mallParentTenantId);
if (check) {
List<WxMerchant> merchantList = checkCouponOneMallMerchantReceiver(mallTenantEntity,mallMerchantParams,record);
record.setBusiness(EnumBusiness.BUSINESS_ID6.getCode());
}
retMap.put(mallTenantEntity, mallMerchantParams);
}
return retMap;
}else {
if (check) {
List<WxMerchant> merchantList = checkCouponOneMallMerchantReceiver(record,record.getMerchantParams(),record);
if(merchantList.size() > 1){
record.setBusiness(EnumBusiness.BUSINESS_ID6.getCode());
}else{
record.setBusiness(merchantList.get(0).getBusinessId());
record.setSubBusiness(merchantList.get(0).getSubBusinessId());
}
}
Map<TenantEntity,String> retMap = new HashMap<TenantEntity,String>();
TenantEntity tenantEntity = new TenantEntity();
tenantEntity.updateTenantInfo(record);
retMap.put(tenantEntity, record.getMerchantParams());
return retMap;
}
}
private List<WxMerchant> checkCouponMerchantReceiver(WxCoupon record) throws Exception{
List<JSONObject> merchantParamList = JSONObject.parseArray(record.getMerchantParams(), JSONObject.class);
if(merchantParamList == null || merchantParamList.isEmpty()){
private List<WxMerchant> checkCouponOneMallMerchantReceiver(TenantEntity mallTenantEntity,String merchantParams,WxCoupon record) throws Exception {
List<JSONObject> merchantParamList = JSONObject.parseArray(merchantParams, JSONObject.class);
if(merchantParamList == null || merchantParamList.isEmpty()){
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND.getCode(),"所属商户为空");
}
List<Long> merchantIds = new ArrayList<>();
for (JSONObject o:merchantParamList) {
merchantIds.add(o.getLong("id"));
}
WxMerchant merchantQ = new WxMerchant();
merchantQ.updateTenantInfo(record);
merchantQ.updateTenantInfo(mallTenantEntity );
merchantQ.setIds(merchantIds);
List<WxMerchant> merchantList = wxMerchantService.findList(merchantQ);
if(merchantList.size() != merchantParamList.size()){
@@ -691,7 +738,7 @@ public class WxCouponServiceImpl implements WxCouponService {
if(record.getSalePrice() != null && record.getSalePrice() > 0) {
EnumAppPlat plat = EnumCouponType.getAppPlat(record.getType());
EnumPayWay payWay = EnumAppPlat.getPayWay(plat);
WxAppinfo cAppInfo = wxAppinfoService.getCAppInfo(record ,plat);
WxAppinfo cAppInfo = wxAppinfoService.getCAppInfo(mallTenantEntity ,plat);
if (cAppInfo == null) {
throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND);
}
@@ -720,21 +767,7 @@ public class WxCouponServiceImpl implements WxCouponService {
PayShareAdapterService payShareServie = payServiceFactory.getPayShareAdapterService(payWay.getCode(),payAccount.getPayVersion());
if (EnumPayMchType.DIRECT.equals(payMchType)) {
WxMerchant makeMerchant = null;
if(merchantList.size() == 1){
record.setMerchantType(EnumCouponMerchantType.ONE_MERCHANT.getCode());
makeMerchant = merchantList.get(0);
}else{
List<WxMerchant> adminMerchant = merchantList.stream().filter(m -> EnumMerchantAdmin.PUBLIC_ADMIN.getCode().equals(m.getIsAdmin())).collect(toList());
if(adminMerchant == null || adminMerchant.isEmpty()){
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "多门店券需包含一个商管商户");
}else if(adminMerchant.size() > 1){
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "多门店券只能包含一个商管商户");
}
record.setMerchantType(EnumCouponMerchantType.MULTIPLE_MERCHANT.getCode());
makeMerchant = adminMerchant.get(0);
}
WxMerchant makeMerchant = getDirectPayAccountMerchant(merchantList,record);
record.setMakeMerchantId(makeMerchant.getId());
WxProfitSharingReceiver receiver = payShareServie.getReceiver(payAccount, makeMerchant.getId(), null, payMchType.getCode());
@@ -773,6 +806,37 @@ public class WxCouponServiceImpl implements WxCouponService {
return merchantList;
}
//直联模式收款商户
private WxMerchant getDirectPayAccountMerchant(List<WxMerchant> merchantList,WxCoupon coupon) throws Exception{
WxMerchant makeMerchant = null;
//平台券,集团商管商户
if (EnumCouponType.COUPON_DOUYIN_PLAT.getCode() == coupon.getType()) {
//TODO 查询集团商管商户
//品牌券,查询集团品牌商户
}else if (EnumCouponType.COUPON_DOUYIN_BRAND.getCode() == coupon.getType()) {
//商场券
}else {
if(coupon.getMerchantType() == EnumCouponMerchantType.ONE_MERCHANT.getCode()){
if (merchantList.size() == 1) {
makeMerchant = merchantList.get(0);
}else {
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "单商户券只能有一个商户");
}
}else {
List<WxMerchant> adminMerchant = merchantList.stream().filter(m -> EnumMerchantAdmin.PUBLIC_ADMIN.getCode().equals(m.getIsAdmin())).collect(toList());
if(adminMerchant == null || adminMerchant.isEmpty()){
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "多门店券需包含一个商管商户");
}else if(adminMerchant.size() > 1){
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "多门店券只能包含一个商管商户");
}
makeMerchant = adminMerchant.get(0);
}
}
return makeMerchant;
}
//总分模式下不能分账的商户
private boolean toTalPayMchTypeMerchantIsBad(PayShareAdapterService payShareServie,WxPayAccount payAccount,
String merchantName,Long merchantId,EnumPayMchType payMchType,EnumAppPlat plat) throws Exception {
@@ -1158,7 +1222,9 @@ public class WxCouponServiceImpl implements WxCouponService {
public WxCoupon findById(WxCoupon wxCoupon) {
WxCoupon query = new WxCoupon();
query.setId(wxCoupon.getId());
query.setType(wxCoupon.getType());
if (null != wxCoupon.getType()) {
query.setType(wxCoupon.getType());
}
query.setTenantId(wxCoupon.getTenantId());
List<WxCoupon> list = wxCouponMapper.findCouponList(query);
if(CollectionUtils.isNotEmpty(list)){
@@ -1455,12 +1521,29 @@ public class WxCouponServiceImpl implements WxCouponService {
}
@Override
public void setCouponMerchants(WxCoupon wxCoupon) throws Exception {
public void setParent CouponMall Merchants(TenantEntity mallTenantEntity, WxCoupon wxCoupon) throws Exception {
try {
checkCouponMerchantReceiverAndUpdate(wxCoupon);
List<JSONObject> merchantParamList = JSONObject.parseArray(wxCoupon.getMerchantParams(), JSONObject.class);
WxCoupon coupon = wxCouponMapper.selectById(wxCoupon.getId(), mallTenantEntity.getParentTenantId());
if (null == coupon) {
throw new MallinkException(Result.ERROR,"券未查询到");
}
if (null == wxCoupon.getMerchantIds()) {
throw new MallinkException(Result.ERROR,"请选择商户");
}
//
StringBuffer merchantParams = new StringBuffer("{\"tenantId\":").append(mallTenantEntity.getTenantId()).append(",\"mallMerchantParams\":").append("[");
Long[] mids = wxCoupon.getMerchantIds();
for (int i = 0 ;i < mids.length; i++ ){
merchantParams.append("{\"id\":").append(mids[i]).append("}");
if (i < mids.length-1 ) {
merchantParams.append(",");
}
}
merchantParams.append("]");
coupon.setChildMerchantParams(merchantParams.toString());
checkCouponMerchantReceiver(coupon,true);
final IdWorker idWorker = IdWorker.get();
updateCouponMerchants(wxCoupon,merchantParamList,idWorker);
updateCouponMerchants(coupon ,idWorker);
}catch(Exception e) {
throw e;
}