@ -6,19 +6,28 @@ import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode ;
import com.hai.common.exception.ErrorCode ;
import com.hai.common.exception.ErrorHelp ;
import com.hai.common.exception.ErrorHelp ;
import com.hai.common.exception.SysCode ;
import com.hai.common.exception.SysCode ;
import com.hai.common.utils.DateUtil ;
import com.hai.common.pay.util.XmlUtil ;
import com.hai.common.utils.HttpsUtils ;
import com.hai.common.pay.util.sdk.WXPayConstants ;
import com.hai.common.utils.ResponseMsgUtil ;
import com.hai.common.security.AESEncodeUtil ;
import com.hai.common.utils.* ;
import com.hai.config.* ;
import com.hai.config.* ;
import com.hai.dao.* ;
import com.hai.dao.* ;
import com.hai.entity.* ;
import com.hai.entity.* ;
import com.hai.entity.OutRechargeOrder ;
import com.hai.enum_type.* ;
import com.hai.enum_type.* ;
import com.hai.model.* ;
import com.hai.model.* ;
import com.hai.msg.entity.MsgTopic ;
import com.hai.msg.entity.MsgTopic ;
import com.hai.service.* ;
import com.hai.service.* ;
import com.hai.service.pay.impl.GoodsOrderServiceImpl ;
import io.netty.handler.ipfilter.UniqueIpFilter ;
import io.netty.handler.ipfilter.UniqueIpFilter ;
import org.apache.commons.collections4.MapUtils ;
import org.apache.commons.collections4.MapUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.http.HttpEntity ;
import org.apache.http.client.methods.CloseableHttpResponse ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.entity.StringEntity ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.util.EntityUtils ;
import org.eclipse.paho.client.mqttv3.MqttClient ;
import org.eclipse.paho.client.mqttv3.MqttClient ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
@ -28,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource ;
import javax.annotation.Resource ;
import java.math.BigDecimal ;
import java.math.BigDecimal ;
import java.math.RoundingMode ;
import java.time.OffsetDateTime ;
import java.time.OffsetDateTime ;
import java.util.* ;
import java.util.* ;
@ -45,6 +55,12 @@ public class HighOrderServiceImpl implements HighOrderService {
@Resource
@Resource
private HighDeviceService deviceService ;
private HighDeviceService deviceService ;
@Resource
private BsRequestRecordService bsRequestRecordService ;
@Resource
private HighUserPayPasswordService highUserPayPasswordService ;
@Resource
@Resource
private ThirdProductConfig thirdProductConfig ;
private ThirdProductConfig thirdProductConfig ;
@ -128,16 +144,24 @@ public class HighOrderServiceImpl implements HighOrderService {
private RedisTemplate redisTemplate ;
private RedisTemplate redisTemplate ;
@Resource
@Resource
private MqttProviderConfig mqttProviderConfig ;
private GoodsOrderServiceImpl goodsOrderService ;
@Resource
@Resource
private WebSocket webSocket ;
private MqttProviderConfig mqttProviderConfig ;
@Resource
@Resource
private BaiduVoiceService baiduVoiceService ;
private BaiduVoiceService baiduVoiceService ;
@Resource
@Resource
private BsConfigService bsConfigService ;
private ApiMerchantsService apiMerchantsService ;
@Resource
private ApiAmountRecordService apiAmountRecordService ;
@Resource
private ApiOrderRecordService apiOrderRecordService ;
@Override
@Override
@Transactional (
@Transactional (
@ -1772,26 +1796,166 @@ public class HighOrderServiceImpl implements HighOrderService {
}
}
}
}
public JSONObject insertThirdProduct ( JSONObject object ) throws Exception {
@Override
@Transactional (
isolation = Isolation . SERIALIZABLE ,
propagation = Propagation . REQUIRES_NEW )
public HighOrder insertThirdProduct ( JSONObject object ) throws Exception {
HighOrder highOrder = new HighOrder ( ) ;
HighOrder highOrder = new HighOrder ( ) ;
HighUser user = highUserService . findByUserId ( object . getLong ( "userId" ) ) ;
HighUser user = highUserService . findByUserId ( object . getLong ( "userId" ) ) ;
JSONObject jsonObject = null ;
JSONObject jsonObject = null ;
// 积分抵扣金额
BigDecimal integralPrice = new BigDecimal ( 0 ) ;
// 优惠券抵扣金额
BigDecimal discountPrice = new BigDecimal ( 0 ) ;
// 实际支付金额
BigDecimal orderPayPrice = new BigDecimal ( 0 ) ;
// 市场金额
BigDecimal marketPrice = new BigDecimal ( 0 ) ;
// 查询详单订单的实际
JSONObject productDetail = thirdProductConfig . getThirdPartyByDetail ( object . getInteger ( "platformId" ) , object . getInteger ( "productType" ) , object . getLong ( "companyId" ) ) ;
// 1 星巴克 2 肯德基
// 1 星巴克 2 肯德基
if ( object . getInteger ( "productType" ) = = 1 ) {
if ( object . getInteger ( "productType" ) = = 1 ) {
jsonObject = QianZhuConfig . starbucksOrders ( object . getLong ( "userId" ) , object . getString ( "storeCode" ) , object . getString ( "orderItems" ) , object . getString ( "customerMobile" ) ) ;
JSONObject userToken = QianZhuConfig . getTokenV2 ( user . getId ( ) . toString ( ) , user . getName ( ) , user . getPhone ( ) ) ;
if ( ! userToken . getBoolean ( "success" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
List < String > List = new ArrayList < > ( ) ;
JSONArray array = object . getJSONArray ( "orderItems" ) ;
for ( Object o : array ) {
JSONObject object1 = ( JSONObject ) o ;
Map < String , Object > contentMap = new LinkedHashMap < > ( ) ;
if ( ! object1 . getString ( "productId" ) . equals ( "" ) ) {
contentMap . put ( "productId" , object1 . getString ( "productId" ) ) ;
}
if ( ! object1 . getString ( "cupSize" ) . equals ( "" ) ) {
contentMap . put ( "cupSize" , object1 . getString ( "cupSize" ) ) ;
}
if ( ! object1 . getString ( "temperature" ) . equals ( "" ) ) {
contentMap . put ( "temperature" , object1 . getString ( "temperature" ) ) ;
}
if ( ! object1 . getString ( "cream" ) . equals ( "" ) ) {
contentMap . put ( "cream" , object1 . getString ( "cream" ) ) ;
}
if ( ! object1 . getString ( "espresso" ) . equals ( "" ) ) {
contentMap . put ( "espresso" , object1 . getString ( "espresso" ) ) ;
}
if ( ! object1 . getString ( "milk" ) . equals ( "" ) ) {
contentMap . put ( "milk" , object1 . getString ( "milk" ) ) ;
}
if ( ! object1 . getString ( "milkBubble" ) . equals ( "" ) ) {
contentMap . put ( "milkBubble" , object1 . getString ( "milkBubble" ) ) ;
}
if ( ! object1 . getString ( "num" ) . equals ( "" ) ) {
contentMap . put ( "num" , object1 . getString ( "num" ) ) ;
}
List . add ( JSON . toJSONString ( contentMap ) ) ;
}
// 下单
jsonObject = QianZhuConfig . starbucksOrders ( user . getId ( ) . toString ( ) , object . getString ( "storeCode" ) , List . toString ( ) . replace ( " " , "" ) , object . getString ( "customerMobile" ) ) ;
// 判断下单是否成功
if ( ! Objects . equals ( jsonObject . getString ( "code" ) , "200" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
if ( ! jsonObject . getBoolean ( "success" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
// 根据订单号查询订单详情
JSONObject orderObject = QianZhuConfig . starbucksOrderByOrderNo ( jsonObject . getJSONObject ( "data" ) . getString ( "orderNo" ) ) ;
// 判断下单是否成功
if ( ! orderObject . getBoolean ( "success" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
// 插入订单内容
highOrder . setOrderNo ( jsonObject . getJSONObject ( "data" ) . getString ( "orderNo" ) ) ;
highOrder . setRegionId ( object . getString ( "regionId" ) ) ;
highOrder . setCompanyId ( object . getLong ( "companyId" ) ) ;
highOrder . setCreateTime ( new Date ( ) ) ;
highOrder . setMemId ( user . getId ( ) ) ;
highOrder . setMemName ( user . getName ( ) ) ;
highOrder . setMemPhone ( user . getPhone ( ) ) ;
highOrder . setOrderStatus ( 1 ) ;
highOrder . setRemarks ( object . getString ( "userRemark" ) ) ;
highOrderMapper . insert ( highOrder ) ;
// 获取星巴克订单内容
JSONArray starbucksOrder = orderObject . getJSONObject ( "data" ) . getJSONArray ( "orderItems" ) ;
for ( Object starbucksObject : starbucksOrder ) {
JSONObject childObject = ( JSONObject ) starbucksObject ;
HighChildOrder childOrder = new HighChildOrder ( ) ;
childOrder . setOrderId ( highOrder . getId ( ) ) ;
childOrder . setChildOrdeStatus ( 1 ) ;
childOrder . setGoodsType ( 9 ) ;
childOrder . setStoreAddress ( childObject . getString ( "storeAddress" ) ) ;
childOrder . setStoreName ( childObject . getString ( "storeName" ) ) ;
childOrder . setGoodsName ( childObject . getString ( "productName" ) ) ;
childOrder . setGoodsImg ( childObject . getString ( "imgUrl" ) ) ;
childOrder . setSaleCount ( childObject . getInteger ( "quantity" ) ) ;
childOrder . setGoodsPrice ( childObject . getBigDecimal ( "marketPrice" ) ) ;
// String goodsSpecName = childObject.getString("cupSize") +
// "," + childObject.getString("temperature") +
// "," + childObject.getString("milk") +
// "," + childObject.getString("milkBubble") +
// "," + childObject.getString("espresso") +
// "," + childObject.getString("cream");
// childOrder.setGoodsSpecName(goodsSpecName);
// 计算利润
BigDecimal profitPrice = childObject . getBigDecimal ( "unitPrice" ) . multiply ( productDetail . getBigDecimal ( "priceDiscount" ) . divide ( new BigDecimal ( 100 ) ) ) ;
BigDecimal childPrice = childObject . getBigDecimal ( "unitPrice" ) . add ( profitPrice ) . setScale ( 2 , RoundingMode . HALF_UP ) ;
// 计算实际支付金额
orderPayPrice = orderPayPrice . add ( childPrice ) ;
// 计算市场价金额
marketPrice = marketPrice . add ( childObject . getBigDecimal ( "marketPrice" ) ) ;
childOrder . setGoodsActualPrice ( childPrice ) ;
childOrder . setTotalPrice ( childPrice . multiply ( childObject . getBigDecimal ( "quantity" ) ) ) ;
highChildOrderMapper . insert ( childOrder ) ;
}
} else if ( object . getInteger ( "productType" ) = = 2 ) {
} else if ( object . getInteger ( "productType" ) = = 2 ) {
if ( object . getInteger ( "eatType" ) = = null ) {
if ( object . getInteger ( "eatType" ) = = null ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "请选择就餐方式!" ) ;
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "请选择就餐方式!" ) ;
}
}
jsonObject = QianZhuConfig . createKfcOrder ( object . getInteger ( "eatType" ) , object . getString ( "storeCode" ) , object . getString ( "customerMobile" ) , object . getString ( "orderItems" ) , object . getString ( "userId" ) , object . getString ( "userRemark" ) ) ;
// 下单
jsonObject = QianZhuConfig . createKfcOrder ( object . getInteger ( "eatType" ) , object . getString ( "storeCode" ) , object . getString ( "customerMobile" ) , object . getString ( "orderItems" ) , object . getString ( "userId" ) ) ;
// 判断下单是否成功
if ( ! Objects . equals ( jsonObject . getString ( "code" ) , "10000" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
}
if ( jsonObject ! = null & & jsonObject . getBoolean ( "success" ) ) {
// 根据订单号查询订单详情
JSONObject orderObject = QianZhuConfig . getKfcOrderByOrderNo ( jsonObject . getJSONObject ( "data" ) . getString ( "orderNo" ) ) ;
// 判断下单是否成功
if ( ! orderObject . getBoolean ( "success" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
}
// 插入订单内容
highOrder . setOrderNo ( jsonObject . getJSONObject ( "data" ) . getString ( "orderNo" ) ) ;
highOrder . setOrderNo ( jsonObject . getJSONObject ( "data" ) . getString ( "orderNo" ) ) ;
highOrder . setRegionId ( object . getString ( "regionId" ) ) ;
highOrder . setRegionId ( object . getString ( "regionId" ) ) ;
highOrder . setCompanyId ( object . getLong ( "companyId" ) ) ;
highOrder . setCreateTime ( new Date ( ) ) ;
highOrder . setCreateTime ( new Date ( ) ) ;
highOrder . setMemId ( user . getId ( ) ) ;
highOrder . setMemId ( user . getId ( ) ) ;
highOrder . setMemName ( user . getName ( ) ) ;
highOrder . setMemName ( user . getName ( ) ) ;
@ -1801,11 +1965,498 @@ public class HighOrderServiceImpl implements HighOrderService {
highOrderMapper . insert ( highOrder ) ;
highOrderMapper . insert ( highOrder ) ;
return jsonObject . getJSONObject ( "data" ) ;
// 获取肯德基订单内容
JSONArray kfcOrder = orderObject . getJSONObject ( "data" ) . getJSONObject ( "kfcPlaceOrder" ) . getJSONArray ( "items" ) ;
for ( Object kfcObject : kfcOrder ) {
JSONObject childObject = ( JSONObject ) kfcObject ;
HighChildOrder childOrder = new HighChildOrder ( ) ;
childOrder . setOrderId ( highOrder . getId ( ) ) ;
childOrder . setChildOrdeStatus ( 1 ) ;
childOrder . setGoodsType ( 4 ) ;
childOrder . setStoreAddress ( orderObject . getJSONObject ( "data" ) . getJSONObject ( "kfcPlaceOrder" ) . getString ( "storeAddress" ) ) ;
childOrder . setStoreName ( orderObject . getJSONObject ( "data" ) . getJSONObject ( "kfcPlaceOrder" ) . getString ( "storeName" ) ) ;
childOrder . setGoodsName ( childObject . getString ( "productName" ) ) ;
childOrder . setGoodsImg ( childObject . getString ( "imageUrl" ) ) ;
childOrder . setSaleCount ( childObject . getInteger ( "quantity" ) ) ;
childOrder . setGoodsPrice ( childObject . getBigDecimal ( "originPrice" ) ) ;
// 计算利润
BigDecimal profitPrice = childObject . getBigDecimal ( "price" ) . multiply ( productDetail . getBigDecimal ( "priceDiscount" ) . divide ( new BigDecimal ( 100 ) ) ) ;
BigDecimal childPrice = childObject . getBigDecimal ( "price" ) . add ( profitPrice ) . setScale ( 2 , RoundingMode . HALF_UP ) ;
// 计算实际支付金额
orderPayPrice = orderPayPrice . add ( childPrice ) ;
// 计算市场价金额
marketPrice = marketPrice . add ( childObject . getBigDecimal ( "originPrice" ) ) ;
childOrder . setGoodsActualPrice ( childPrice ) ;
childOrder . setTotalPrice ( childPrice . multiply ( childObject . getBigDecimal ( "quantity" ) ) ) ;
highChildOrderMapper . insert ( childOrder ) ;
}
}
// 定义优惠券
HighDiscountUserRel highDiscountUserRel = null ;
// 判断是否有优惠券
if ( object . getLong ( "memDiscountId" ) ! = null ) {
// 优惠券信息
highDiscountUserRel = highDiscountUserRelService . getRelById ( object . getLong ( "memDiscountId" ) ) ;
highOrder . setMemDiscountId ( object . getLong ( "memDiscountId" ) ) ;
if ( highDiscountUserRel = = null | | highDiscountUserRel . getStatus ( ) ! = 1 ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "优惠券状态错误" ) ;
}
if ( ! highDiscountUserRel . getHighDiscount ( ) . getUseScope ( ) . equals ( DiscountUseScope . type1 . getType ( ) )
& & ! highDiscountUserRel . getHighDiscount ( ) . getUseScope ( ) . equals ( DiscountUseScope . type3 . getType ( ) ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "无法使用此优惠券" ) ;
}
}
// 使用优惠券
// 优惠券抵扣
if ( highDiscountUserRel ! = null ) {
highOrder . setMemDiscountName ( highDiscountUserRel . getHighDiscount ( ) . getDiscountName ( ) ) ;
// 卡卷类型 1:满减 2:抵扣 3:折扣
if ( highDiscountUserRel . getHighDiscount ( ) . getDiscountType ( ) = = 1 ) {
// 如果商品支付总额 小于 满减价格
if ( orderPayPrice . compareTo ( highDiscountUserRel . getHighDiscount ( ) . getDiscountCondition ( ) ) < 0 ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . UN_MEMBER_ERROR , "订单未达到" + highDiscountUserRel . getHighDiscount ( ) . getDiscountCondition ( ) + "元,无法使用此优惠券" ) ;
}
// 满减额度
discountPrice = highDiscountUserRel . getHighDiscount ( ) . getDiscountPrice ( ) ;
}
// 卡卷类型 1:满减 2:抵扣 3:折扣
if ( highDiscountUserRel . getHighDiscount ( ) . getDiscountType ( ) = = 2 ) {
// 满减额度
discountPrice = highDiscountUserRel . getHighDiscount ( ) . getDiscountPrice ( ) ;
}
// 卡卷类型 1:满减 2:抵扣 3:折扣
if ( highDiscountUserRel . getHighDiscount ( ) . getDiscountType ( ) = = 3 ) {
// 满减额度
discountPrice = orderPayPrice . multiply ( highDiscountUserRel . getHighDiscount ( ) . getDiscountPrice ( ) ) ;
}
}
// 使用积分
// 判断积分数量是否大于0
if ( object . getLong ( "integralNum" ) ! = null ) {
if ( object . getLong ( "integralNum" ) > 0 & & productDetail . getBigDecimal ( "integralDiscount" ) . compareTo ( new BigDecimal ( "0" ) ) > 0 ) {
// 判断用户积分是否够
if ( object . getLong ( "integralNum" ) > user . getGold ( ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "用户积分不足" ) ;
}
// 积分抵扣金额
integralPrice = object . getBigDecimal ( "integralNum" ) . divide ( new BigDecimal ( 100 ) . setScale ( 2 , RoundingMode . HALF_UP ) ) ;
// 最高可抵扣金额
BigDecimal maxIntegralDeductionPrice = orderPayPrice . multiply ( productDetail . getBigDecimal ( "integralDiscount" ) ) . divide ( new BigDecimal ( 100 ) ) . setScale ( 2 , RoundingMode . HALF_UP ) ;
// 判读积分是否大于限制额度
if ( maxIntegralDeductionPrice . compareTo ( integralPrice ) < 0 ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "订单最大抵扣积分金额:" + maxIntegralDeductionPrice ) ;
}
// 判断积分抵扣比例是否为100% 并且积分数量是否可以抵扣最后的支付金额
if ( productDetail . getBigDecimal ( "integralDiscount" ) . compareTo ( new BigDecimal ( 100 ) ) = = 0 & & integralPrice . compareTo ( orderPayPrice ) = = 0 ) {
// 查询用户支付密码
HighUserPayPassword userPayPassword = highUserPayPasswordService . getDetailByUser ( object . getLong ( "userId" ) ) ;
if ( userPayPassword = = null ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . NOT_SET_USER_PAY_PWD , "" ) ;
}
if ( StringUtils . isBlank ( object . getString ( "password" ) ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . NOT_ENTER_USER_PAY_PWD , "" ) ;
}
// 校验支付密码
if ( ! AESEncodeUtil . aesEncrypt ( object . getString ( "password" ) ) . equals ( userPayPassword . getPassword ( ) ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . USER_PAY_PWD_ERROR , "" ) ;
}
}
}
}
highOrder . setPayPrice ( orderPayPrice ) ;
highOrder . setDeductionPrice ( discountPrice ) ;
highOrder . setPayGold ( object . getInteger ( "integralNum" ) ) ;
// 使用优惠券 或者积分
if ( highOrder . getMemDiscountId ( ) ! = null ) {
highOrder . setTotalPrice ( marketPrice ) ;
highOrder . setPayPrice ( marketPrice . subtract ( discountPrice ) ) ;
HighDiscountUserRel discountUserRel = highDiscountUserRelService . getRelById ( highOrder . getMemDiscountId ( ) ) ;
discountUserRel . setUseTime ( new Date ( ) ) ; // 使用时间
discountUserRel . setStatus ( 2 ) ; //状态 0:已过期 1:未使用 2:已使用
highDiscountUserRelService . updateDiscountUserRel ( discountUserRel ) ;
HighDiscountAgentCode code = highDiscountAgentCodeService . getCodeById ( discountUserRel . getDiscountAgentCodeId ( ) ) ;
code . setStatus ( 3 ) ;
highDiscountAgentCodeService . updateCode ( code ) ;
} else {
highOrder . setTotalPrice ( orderPayPrice ) ;
highOrder . setPayPrice ( orderPayPrice . subtract ( discountPrice ) ) ;
}
if ( integralPrice . compareTo ( new BigDecimal ( 0 ) ) > 0 ) {
highOrder . setPayPrice ( highOrder . getPayPrice ( ) . subtract ( integralPrice ) ) ;
highUserService . goldHandle ( highOrder . getMemId ( ) , object . getInteger ( "integralNum" ) , 2 , 2 , highOrder . getId ( ) ) ;
}
// 判断积分支付是否扣完金额
if ( highOrder . getPayPrice ( ) . compareTo ( new BigDecimal ( 0 ) ) = = 0 ) {
highOrder . setOrderStatus ( 2 ) ;
highOrder . setPayModel ( 1 ) ;
highOrder . setPayType ( 3 ) ;
highOrder . setPayTime ( new Date ( ) ) ;
highOrder . setPaySerialNo ( "HF" + DateUtil . date2String ( new Date ( ) , "yyyyMMddHHmmss" ) + IDGenerator . nextId ( 5 ) ) ;
} else {
} else {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , jsonObject . getString ( "message" ) ) ;
highOrder . setOrderStatus ( 1 ) ;
}
}
highOrder . setPayRealPrice ( highOrder . getPayPrice ( ) ) ;
highOrder . setRemarks ( object . getString ( "customerMobile" ) ) ;
highOrderMapper . updateByPrimaryKey ( highOrder ) ;
if ( highOrder . getOrderStatus ( ) = = 2 ) {
HighOrder highOrderList = highOrderService . getOrderById ( highOrder . getId ( ) ) ;
for ( HighChildOrder childOrder : highOrderList . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 2 ) ;
}
// 1 星巴克 2 肯德基 3:第三方会员充值
if ( object . getInteger ( "productType" ) = = 1 ) {
QianZhuConfig . starbucksOrdersPay ( highOrder . getOrderNo ( ) ) ;
} else if ( object . getInteger ( "productType" ) = = 2 ) {
QianZhuConfig . payKfcOrder ( highOrder . getOrderNo ( ) ) ;
}
}
}
}
// 创建提交记录
BsRequestRecord requestRecord = new BsRequestRecord ( ) ;
requestRecord . setCreateTime ( new Date ( ) ) ;
requestRecord . setUpdateTime ( new Date ( ) ) ;
requestRecord . setOrderNo ( highOrder . getOrderNo ( ) ) ;
requestRecord . setRequestContent ( object . toJSONString ( ) ) ;
requestRecord . setOperatorId ( 0L ) ;
requestRecord . setOperatorName ( "系统生成" ) ;
requestRecord . setSourceId ( highOrder . getId ( ) . toString ( ) ) ;
requestRecord . setSourceType ( 1 ) ;
bsRequestRecordService . insertRequestRecord ( requestRecord ) ;
return highOrder ;
}
@Override
@Transactional ( propagation = Propagation . REQUIRES_NEW )
public void hltUnionCardPayByThirdProduct ( HighUserCard userCard , Long orderId ) throws Exception {
String goodsDesc = "" ;
String tranDesc = "" ;
String instCode = "11101527" ;
String businessType = "hisen_consume" ;
// 查询订单信息
HighOrder order = getOrderById ( orderId ) ;
if ( order = = null ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . NOT_FOUND_ORDER , "" ) ;
}
if ( order . getHighChildOrderList ( ) . get ( 0 ) . getGoodsType ( ) = = 4 ) {
goodsDesc = "购买肯德基产品" ;
tranDesc = goodsDesc + order . getPayRealPrice ( ) + "元" ;
} else if ( order . getHighChildOrderList ( ) . get ( 0 ) . getGoodsType ( ) = = 9 ) {
goodsDesc = "购买星巴克产品" ;
tranDesc = goodsDesc + order . getPayRealPrice ( ) + "元" ;
} else {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "暂时无法支付,请使用其他支付方式" ) ;
}
// 工会卡支付
JSONObject consumption = HuiLianTongUnionCardConfig . consumption ( order . getOrderNo ( ) , userCard . getCardNo ( ) , order . getPayRealPrice ( ) , businessType , instCode , goodsDesc , tranDesc ) ;
System . out . println ( "工会卡支付响应参数" + consumption . toJSONString ( ) ) ;
Map < String , Object > dataMap = new HashMap < > ( ) ;
dataMap . put ( "orderNo" , order . getOrderNo ( ) ) ;
dataMap . put ( "cardType" , "ghk" ) ;
dataMap . put ( "cardNo" , userCard . getCardNo ( ) ) ;
dataMap . put ( "checkPassword" , "N" ) ;
dataMap . put ( "tranAmount" , order . getPayRealPrice ( ) ) ;
dataMap . put ( "tranChannel" , "HiSen" ) ;
dataMap . put ( "businessType" , businessType ) ;
dataMap . put ( "instCode" , instCode ) ;
dataMap . put ( "goodsDesc" , goodsDesc ) ;
dataMap . put ( "tranDesc" , tranDesc ) ;
HighGasOrderPush payPush = new HighGasOrderPush ( ) ;
payPush . setType ( OrderPushType . type5 . getType ( ) ) ;
payPush . setOrderNo ( order . getOrderNo ( ) ) ;
payPush . setCreateTime ( new Date ( ) ) ;
payPush . setCode ( consumption . getString ( "respCode" ) ) ;
payPush . setRequestContent ( JSON . toJSONString ( dataMap ) ) ;
payPush . setReturnContent ( consumption . toJSONString ( ) ) ;
highGasOrderPushMapper . insert ( payPush ) ;
if ( ! consumption . getString ( "respCode" ) . equals ( "0000" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , consumption . getString ( "respMessage" ) ) ;
}
JSONObject consumptionResult = HuiLianTongUnionCardConfig . resolveResponse ( consumption . getString ( "data" ) ) ;
if ( ! consumptionResult . getBoolean ( "success" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , consumptionResult . getString ( "message" ) ) ;
}
order . setPaySerialNo ( consumptionResult . getString ( "orderId" ) ) ; // 支付流水号
order . setOrderStatus ( 2 ) ;
order . setMemCardNo ( userCard . getCardNo ( ) ) ;
order . setPayType ( 4 ) ;
order . setPayTime ( new Date ( ) ) ; // 支付时间
updateOrder ( order ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 2 ) ;
}
if ( order . getHighChildOrderList ( ) . get ( 0 ) . getGoodsType ( ) = = 4 ) {
QianZhuConfig . payKfcOrder ( order . getOrderNo ( ) ) ;
} else if ( order . getHighChildOrderList ( ) . get ( 0 ) . getGoodsType ( ) = = 9 ) {
QianZhuConfig . starbucksOrdersPay ( order . getOrderNo ( ) ) ;
} else {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "暂时无法支付,请使用其他支付方式" ) ;
}
}
@Override
public List < HighOrder > getThirdOrder ( ) {
return highOrderMapper . getThirdOrder ( ) ;
}
@Override
public void thirdOrderToRefund ( Long orderId ) throws Exception {
HighOrder order = highOrderService . getOrderById ( orderId ) ;
// 101.待支付 102.已支付 100.已完成 104.已取消 105.已退款
if ( order . getOrderStatus ( ) ! = 2 ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "无法退款,订单不处于已支付状态" ) ;
}
// 退还积分
if ( order . getPayGold ( ) > 0 ) {
highUserService . goldHandle ( order . getMemId ( ) , order . getPayGold ( ) , 1 , 3 , order . getId ( ) ) ;
if ( order . getPayRealPrice ( ) . compareTo ( new BigDecimal ( 0 ) ) = = 0 ) {
order . setOrderStatus ( 4 ) ;
order . setRefundTime ( new Date ( ) ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 4 ) ;
}
updateOrder ( order ) ;
}
}
// 微信退款
if ( order . getPayType ( ) = = 2 ) {
Map < String , String > param = new HashMap < > ( ) ;
param . put ( "appid" , "wx637bd6f7314daa46" ) ;
param . put ( "mch_id" , "1289663601" ) ;
param . put ( "sub_mch_id" , "1614670195" ) ;
param . put ( "nonce_str" , WxUtils . makeNonStr ( ) ) ;
param . put ( "transaction_id" , order . getPaySerialNo ( ) ) ;
param . put ( "out_refund_no" , "HFR" + new Date ( ) . getTime ( ) ) ;
param . put ( "total_fee" , String . valueOf ( order . getPayRealPrice ( ) . multiply ( new BigDecimal ( "100" ) ) . intValue ( ) ) ) ;
param . put ( "refund_fee" , String . valueOf ( order . getPayRealPrice ( ) . multiply ( new BigDecimal ( "100" ) ) . intValue ( ) ) ) ;
param . put ( "sign_type" , "HMAC-SHA256" ) ;
String signStr = WxUtils . generateSignature ( param , "Skufk5oi85wDFGl888i6wsRSTkdd5df5" , WXPayConstants . SignType . HMACSHA256 ) ;
param . put ( "sign" , signStr ) ;
String resultXmL = doRefundRequest ( param . get ( "mch_id" ) , WxUtils . mapToXml ( param ) ) ;
OrderRefundModel orderRefundModel = XmlUtil . getObjectFromXML ( resultXmL , OrderRefundModel . class ) ;
if ( orderRefundModel . getResult_code ( ) . equals ( "SUCCESS" ) ) {
order . setRefundTime ( new Date ( ) ) ;
order . setRefundNo ( orderRefundModel . getOut_refund_no ( ) ) ;
order . setRefundPrice ( new BigDecimal ( orderRefundModel . getRefund_fee ( ) ) . divide ( new BigDecimal ( "100" ) ) ) ;
order . setOrderStatus ( 4 ) ;
order . setRefundTime ( new Date ( ) ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 4 ) ;
}
updateOrder ( order ) ;
} else {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , "退款失败!错误代码:" + orderRefundModel . getErr_code ( ) + ",错误描述" + orderRefundModel . getErr_code_des ( ) ) ;
}
}
// 工会卡退款
if ( order . getPayType ( ) = = 3 ) {
JSONObject jsonObject = HuiLianTongUnionCardConfig . refund ( "HFR" + new Date ( ) . getTime ( ) , order . getOrderNo ( ) ) ;
if ( jsonObject = = null ) {
jsonObject = HuiLianTongUnionCardConfig . refund ( "HFR" + new Date ( ) . getTime ( ) , order . getOrderNo ( ) ) ;
}
JSONObject dataObject = HuiLianTongUnionCardConfig . resolveResponse ( jsonObject . getString ( "data" ) ) ;
if ( dataObject . getBoolean ( "success" ) | | Objects . equals ( dataObject . getString ( "message" ) , "原交易已撤销,不可再次操作" ) ) {
order . setRefundTime ( new Date ( ) ) ;
order . setRefundNo ( "HFR" + new Date ( ) . getTime ( ) ) ;
order . setRefundPrice ( order . getPayRealPrice ( ) ) ;
order . setOrderStatus ( 4 ) ;
order . setRefundTime ( new Date ( ) ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 4 ) ;
}
updateOrder ( order ) ;
} else {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , dataObject . getString ( "message" ) ) ;
}
// 商户预充值 帐户退款
if ( order . getPayType ( ) = = 6 ) {
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( "orderNo" , order . getOrderNo ( ) ) ;
map . put ( "amountType" , 1 ) ;
map . put ( "sourceType" , 2 ) ;
if ( apiAmountRecordService . getApiAmountRecordByList ( map ) . size ( ) = = 1 ) {
ApiMerchants apiMerchants = apiMerchantsService . findByMchId ( order . getMerchId ( ) ) ;
// 插入金额记录
// 变更前金额
BigDecimal beforeAmount = apiMerchants . getAmounts ( ) ;
// 计算金额
apiMerchants . setAmounts ( apiMerchants . getAmounts ( ) . add ( order . getPayPrice ( ) ) ) ;
// 变更后金额
BigDecimal afterAmount = apiMerchants . getAmounts ( ) ;
apiMerchantsService . updateApiMerchants ( apiMerchants ) ;
ApiAmountRecord apiAmountRecord = new ApiAmountRecord ( ) ;
apiAmountRecord . setCreateTime ( new Date ( ) ) ;
apiAmountRecord . setUpdateTime ( new Date ( ) ) ;
apiAmountRecord . setMchId ( order . getMerchId ( ) ) ;
apiAmountRecord . setStatus ( 100 ) ;
apiAmountRecord . setAmount ( order . getPayPrice ( ) ) ;
apiAmountRecord . setAfterAmount ( afterAmount ) ;
apiAmountRecord . setBeforeAmount ( beforeAmount ) ;
apiAmountRecord . setAmountType ( 1 ) ;
apiAmountRecord . setSourceType ( 2 ) ;
apiAmountRecord . setSourceOrderNo ( order . getOrderNo ( ) ) ;
apiAmountRecord . setSourceId ( order . getId ( ) ) ;
apiAmountRecord . setSourceContent ( apiMerchants . getMerchantName ( ) + "|订单退款" + order . getPayPrice ( ) ) ;
apiAmountRecordService . insertAmountRecord ( apiAmountRecord ) ;
Map < String , Object > orderMap = new HashMap < > ( ) ;
orderMap . put ( "orderId" , order . getId ( ) ) ;
// 查询是否用重复订单
ApiOrderRecord apiOrderRecord = apiOrderRecordService . queryOrderResult ( orderMap ) ;
apiOrderRecord . setStatus ( 100 ) ;
apiOrderRecord . setRequestTime ( new Date ( ) ) ;
apiOrderRecordService . updateOrderRecord ( apiOrderRecord ) ;
}
}
}
// 银联退款
if ( order . getPayType ( ) = = 4 ) {
// 订单退款
JSONObject refund = UnionPayConfig . zwrefund ( UnionPayConfig . MER_ID2 , UnionPayConfig . TERM_ID2 , order . getOrderNo ( ) , order . getPaySerialNo ( ) , order . getPayRealPrice ( ) . multiply ( new BigDecimal ( "100" ) ) . longValue ( ) ) ;
if ( ! refund . getString ( "resultcode" ) . equals ( "W6" ) ) {
throw ErrorHelp . genException ( SysCode . System , ErrorCode . COMMON_ERROR , refund . getString ( "returnmsg" ) ) ;
}
order . setRefundTime ( new Date ( ) ) ;
order . setRefundNo ( refund . getString ( "oriwtorderid" ) ) ;
order . setRefundPrice ( order . getPayRealPrice ( ) ) ;
order . setOrderStatus ( 4 ) ;
order . setRefundTime ( new Date ( ) ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 4 ) ;
}
updateOrder ( order ) ;
}
if ( order . getMemDiscountId ( ) ! = null ) {
HighDiscountUserRel rel = highDiscountUserRelService . getRelById ( order . getMemDiscountId ( ) ) ;
if ( rel ! = null ) {
rel . setStatus ( 1 ) ; // 状态 0:已过期 1:未使用 2:已使用
rel . setUseTime ( null ) ;
highDiscountUserRelService . updateDiscountUserRel ( rel ) ;
HighDiscountAgentCode code = highDiscountAgentCodeService . getCodeById ( rel . getDiscountAgentCodeId ( ) ) ;
code . setStatus ( 2 ) ;
highDiscountAgentCodeService . updateCode ( code ) ;
}
}
}
public String doRefundRequest ( String mchId , String data ) throws Exception {
//小程序退款需要调用双向证书的认证
CloseableHttpClient httpClient = goodsOrderService . readCertificate ( mchId ) ;
try {
HttpPost httpost = new HttpPost ( "https://api.mch.weixin.qq.com/secapi/pay/refund" ) ; // 设置响应头信息
httpost . addHeader ( "Connection" , "keep-alive" ) ;
httpost . addHeader ( "Accept" , "*/*" ) ;
httpost . addHeader ( "Content-Type" , "text/xml" ) ;
httpost . addHeader ( "Host" , "api.mch.weixin.qq.com" ) ;
httpost . addHeader ( "X-Requested-With" , "XMLHttpRequest" ) ;
httpost . addHeader ( "Cache-Control" , "max-age=0" ) ;
httpost . addHeader ( "User-Agent" , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) " ) ;
httpost . setEntity ( new StringEntity ( data , "UTF-8" ) ) ;
CloseableHttpResponse response = httpClient . execute ( httpost ) ;
try {
HttpEntity entity = response . getEntity ( ) ;
String jsonStr = EntityUtils . toString ( response . getEntity ( ) , "UTF-8" ) ;
EntityUtils . consume ( entity ) ;
return jsonStr ;
} finally {
response . close ( ) ;
}
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
} finally {
httpClient . close ( ) ;
}
}
@Override
@Transactional ( propagation = Propagation . REQUIRES_NEW )
public void thirdCancelOrder ( Long orderId ) {
HighOrder order = getOrderById ( orderId ) ;
if ( order ! = null & & order . getOrderStatus ( ) = = 1 ) {
order . setOrderStatus ( 5 ) ;
order . setCancelTime ( new Date ( ) ) ;
for ( HighChildOrder childOrder : order . getHighChildOrderList ( ) ) {
childOrder . setChildOrdeStatus ( 5 ) ;
}
if ( order . getMemDiscountId ( ) ! = null ) {
HighDiscountUserRel rel = highDiscountUserRelService . getRelById ( order . getMemDiscountId ( ) ) ;
if ( rel ! = null ) {
rel . setStatus ( 1 ) ; // 状态 0:已过期 1:未使用 2:已使用
rel . setUseTime ( null ) ;
highDiscountUserRelService . updateDiscountUserRel ( rel ) ;
HighDiscountAgentCode code = highDiscountAgentCodeService . getCodeById ( rel . getDiscountAgentCodeId ( ) ) ;
code . setStatus ( 2 ) ;
highDiscountAgentCodeService . updateCode ( code ) ;
}
}
if ( order . getPayGold ( ) ! = null ) {
// 积分返回
highUserService . goldHandle ( order . getMemId ( ) , order . getPayGold ( ) , 1 , 3 , order . getId ( ) ) ;
}
updateOrder ( order ) ;
}
}
}