package com.topsts.gas.user.utils;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.PassInstanceDetail;
import com.alipay.api.request.AlipayMarketingPassInstanceQueryRequest;
import com.alipay.api.request.AlipayMarketingVoucherStockUseRequest;
import com.alipay.api.response.AlipayMarketingPassInstanceQueryResponse;
import com.alipay.api.response.AlipayMarketingVoucherStockUseResponse;
import com.mydt.common.DateUtils;
import com.mydt.common.StringUtils;
import com.topsts.gas.common.em.Available;
import com.topsts.gas.coupon.domain.UserCoupon;
import com.topsts.gas.coupon.mapper.UserCouponMapper;
import com.topsts.gas.pay.domain.AlipayAccountInfo;
public class CouponSynchronizationUtils {
private final static Logger logger = LoggerFactory.getLogger(CouponSynchronizationUtils.class);
public static AlipayClient alipayclient(AlipayAccountInfo alipayAccountInfo) {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", alipayAccountInfo.getAppId(), alipayAccountInfo.getAppPrivateKey(), "json", "utf-8", alipayAccountInfo.getAlipayAppPublicKey(), "RSA2");
return alipayClient;
}
/**
* @describe 用for循环实现查询卡券同步到数据库
* @author
[email protected]
* @param alipayclient
* @param user_id
* @param userCouponMapper
*/
public static void couponListForQuery(AlipayClient alipayclient, Long user_id, UserCouponMapper userCouponMapper){
AlipayMarketingPassInstanceQueryRequest request = new AlipayMarketingPassInstanceQueryRequest();
int page_num = 1;
int page_size = 10;
forLoop(alipayclient, request, user_id, page_num, page_size, userCouponMapper);
}
/**
* @describe 查询出按page_size分页的总页数,根据总页数循环调用childLoop同步数据库
* @author
[email protected]
* @param alipayclient
* @param request
* @param user_id
* @param page_num
* @param page_size
* @param userCouponMapper
*/
public static void forLoop(AlipayClient alipayclient, AlipayMarketingPassInstanceQueryRequest request, Long user_id, int page_num, int page_size, UserCouponMapper userCouponMapper){
request = CouponSynchronizationUtils.requestVO(request, user_id, page_num, page_size);
AlipayMarketingPassInstanceQueryResponse response = null;
try {
response = alipayclient.execute(request);
if(response.isSuccess()){
Long total_page = response.getTotalPage();
page_num = 0;
for (Long i = total_page; i > 0; i--) {
page_num += 1;
childLoop(alipayclient, request, user_id, page_num, page_size, userCouponMapper);
}
}
if (!response.isSuccess()) {
logger.error("调用alipay.marketing.pass.instance.query接口失败:" + StringUtils.toStr(response));
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("验证签名失败可能是秘钥不对请检查:" + StringUtils.toStr(e));
}
}
/**
* @describe 同步卡券到数据库,forLoop调用
* @author
[email protected]
* @param alipayclient
* @param request
* @param user_id
* @param page_num
* @param page_size
* @param userCouponMapper
*/
public static void childLoop(AlipayClient alipayclient, AlipayMarketingPassInstanceQueryRequest request, Long user_id, int page_num, int page_size, UserCouponMapper userCouponMapper){
request = CouponSynchronizationUtils.requestVO(request, user_id, page_num, page_size);
AlipayMarketingPassInstanceQueryResponse response = null;
try {
response = alipayclient.execute(request);
if(response.isSuccess()){
insetCoupon(response.getInstanceList(),userCouponMapper);
}
if (!response.isSuccess()) {
logger.error("调用alipay.marketing.pass.instance.query接口失败:" + StringUtils.toStr(response));
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("验证签名失败可能是秘钥不对请检查:" + StringUtils.toStr(e));
}
}
/**
* @describe 设置请求参数
* @author
[email protected]
* @param request
* @param user_id
* @param page_num
* @param page_size
* @return
*/
public static AlipayMarketingPassInstanceQueryRequest requestVO(AlipayMarketingPassInstanceQueryRequest request, Long user_id, int page_num, int page_size){
request.setBizContent("{" +
"\"user_id\":\""+user_id+"\"," +
"\"page_num\":"+page_num+"," +
"\"page_size\":"+page_size+"" +
" }");
return request;
}
/**
* @describe 用递归实现查询卡券同步到数据库
* @author
[email protected]
* @param alipayclient
* @param user_id
* @param userCouponMapper
*/
public static void couponListRecursionQuery(AlipayClient alipayclient, Long user_id, UserCouponMapper userCouponMapper){
AlipayMarketingPassInstanceQueryRequest request = new AlipayMarketingPassInstanceQueryRequest();
int page_num = 1;
int page_size = 10;
recursionLoop(alipayclient, request, user_id, page_num, page_size, userCouponMapper);
}
/**
* @describe 递归方法
* @author
[email protected]
* @param alipayclient
* @param request 同步卡券
* @param user_id 支付宝过来用户id
* @param page_num 页码
* @param page_size 每页记录条数,必须为大于0的整数,最大值为20;
* @param userCouponMapper
*/
public static void recursionLoop(AlipayClient alipayclient, AlipayMarketingPassInstanceQueryRequest request, Long user_id, int page_num, int page_size, UserCouponMapper userCouponMapper){
request = CouponSynchronizationUtils.requestVO(request, user_id, page_num, page_size);
AlipayMarketingPassInstanceQueryResponse response = null;
try {
response = alipayclient.execute(request);
if(response.isSuccess()){
if(response.getInstanceList().size() == 10){
logger.info("判断递归次数如果大于10即跳出递归,不在查询:"+page_num);
if(page_num == 11){
return;
}
insetCoupon(response.getInstanceList(),userCouponMapper);
page_num += 1;
recursionLoop(alipayclient, request, user_id, page_num, page_size, userCouponMapper);
}
if(response.getInstanceList().size() < 10) {
logger.info("判断返回的数据是不是小于指定的每页显示条数10条:" + response.getInstanceList().size());
insetCoupon(response.getInstanceList(), userCouponMapper);
}
}
if (!response.isSuccess()) {
logger.error("调用alipay.marketing.pass.instance.query接口失败:" + StringUtils.toStr(response));
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("验证签名失败可能是秘钥不对请检查:" + StringUtils.toStr(e));
}
}
/**
* @describe 同步卡券数据库
* @author
[email protected]
* @param instanceList
* @param userCouponMapper
*/
public static void insetCoupon(List
instanceList, UserCouponMapper userCouponMapper){
for (PassInstanceDetail passInstanceDetail : instanceList) {
logger.info("返回出来的数据alipay.marketing.pass.instance.query:" + passInstanceDetail);
UserCoupon userCoupon = userCouponMapper.selectByPrimaryKey(Long.valueOf(passInstanceDetail.getPassId()));
if (userCoupon == null) {
userCoupon = new UserCoupon();
userCoupon.setId(Long.valueOf(passInstanceDetail.getSerialNumber()));
userCoupon.setCreateTime(DateUtils.getDateFromYYYY_MM_dd_HH_mm_ss(passInstanceDetail.getCreateTime()));
userCoupon.setLimitEndTime(DateUtils.getDateFromYYYY_MM_dd_HH_mm_ss(passInstanceDetail.getStartDate()));
userCoupon.setLimitStartTime(DateUtils.getDateFromYYYY_MM_dd_HH_mm_ss(passInstanceDetail.getEndDate()));
userCoupon.setUseTime(DateUtils.getDateFromYYYY_MM_dd_HH_mm_ss(passInstanceDetail.getModifyTime()));
userCoupon.setTitle(passInstanceDetail.getLogoText());
userCoupon.setDescription(passInstanceDetail.getProduct());
userCoupon.setAvailable(Available.YES.getValue());
userCouponMapper.insert(userCoupon);
}
}
}
/**
* @describe 外部商户券码券核销(alipay.marketing.voucher.stock.use)
* @author [email protected]
* @param alipayclient
* @param entity_no
* @param out_biz_no
*/
public static void voucherStockUse(AlipayClient alipayclient, String entity_no, String out_biz_no){
AlipayMarketingVoucherStockUseRequest request = new AlipayMarketingVoucherStockUseRequest();
request.setBizContent("{" +
"\"entity_no\":\""+out_biz_no+"\"," +
"\"out_biz_no\":\""+out_biz_no+"\"" +
"}");
AlipayMarketingVoucherStockUseResponse response;
try {
response = alipayclient.execute(request);
if(response.isSuccess()){
logger.info("调用alipay.marketing.voucher.stock.use接口成功:" + response.getVoucherId());
if(response.getVoucherId() != null){
logger.info("调用alipay.marketing.voucher.stock.use外部商户券码券核销成功:" + response.getVoucherId());
}
if(response.getVoucherId() == null){
logger.info("调用alipay.marketing.voucher.stock.use外部商户券码券核销失败:" + response);
}
}
if (!response.isSuccess()) {
logger.error("调用alipay.marketing.voucher.stock.use接口失败:" + StringUtils.toStr(response));
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("验证签名失败可能是秘钥不对请检查:" + StringUtils.toStr(e));
}
}
}