SendCouponHelper.java 4.32 KB
/**
 * 
 */
package com.yoho.unions.helper;

import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.promotion.CouponForm;
import com.yoho.service.model.request.YohoCoinCostReqBO;
import com.yoho.service.model.request.YohoCoinLogReqBO;
import com.yoho.service.model.request.YohoCoinRecordReq;
import com.yoho.service.model.response.CommonRspBO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author ping.huang 2016年8月27日
 */
@Component
public class SendCouponHelper {

	Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	ServiceCaller serviceCaller;

	/**
	 * 发送优惠券
	 *
	 * @param couponId
	 * @param uid
	 */
	public String sendCoupon(String couponId, int uid) throws ServiceException {
		log.info("start with sendCoupon. couponId is {}, uid is {}", couponId, uid);
		
		if (StringUtils.isEmpty(couponId) || "0".equals(couponId)) {
			return "0";
		}
		
		CouponForm form = new CouponForm();
		form.setCouponId(couponId);
		form.setUid(String.valueOf(uid));
		String couponCode = "";
		try {
			couponCode = serviceCaller.call("promotion.sendCoupon", form, String.class);
		} catch (Exception e) {
			log.error("sendCoupon failed, ex: {}", e.getMessage());
		}
		log.info("end with sendCoupon. couponCode is {}", couponCode);
		return couponCode;
	}

	/**
	 * 发送有货币
	 * 
	 * @param uid
	 * @param num
	 * @return
	 */
	public boolean sendYOHOBi(int uid, int num) {
		YohoCoinRecordReq req = buildYohoCoinCostReq(uid, num);
		final String serviceName = "users.addRecord";
		boolean result = false;
		try {
			log.info("BrandActivityServiceImpl sendYOHOBi req------ is {}", req);
			CommonRspBO rspBO = serviceCaller.call(serviceName, req, CommonRspBO.class);
			log.info("BrandActivityServiceImpl sendYOHOBi result----- is {}", rspBO);
			if (rspBO != null && rspBO.getCode() == 200) {
				result = true;
			}
		} catch (Exception e) {
			log.warn("in method sendYOHOBIEvent ,invoke {} occurs error,detail is {}", serviceName, e);
		}
		return result;
	}

	/**
	 * 发送有货币
	 *
	 * @param uid
	 * @param num
	 * @return
	 */
	public boolean sendYOHOBi(int uid, int num, int type) {
		YohoCoinRecordReq req = buildYohoCoinCostReq(uid, num, type);
		final String serviceName = "users.addRecord";
		boolean result = false;
		try {
			log.info("BrandActivityServiceImpl sendYOHOBi req------ is {}", req);
			CommonRspBO rspBO = serviceCaller.call(serviceName, req, CommonRspBO.class);
			if (rspBO != null && rspBO.getCode() == 200) {
				result = true;
			}
		} catch (Exception e) {
			log.warn("in method sendYOHOBIEvent ,invoke {} occurs error,detail is {}", serviceName, e);
		}
		return result;
	}

	// 构造参数
	private YohoCoinRecordReq buildYohoCoinCostReq(int uid, int num) {
		YohoCoinRecordReq req = new YohoCoinRecordReq();

		YohoCoinCostReqBO yohoCoinCostReq = new YohoCoinCostReqBO();
		yohoCoinCostReq.setUid(uid);
		yohoCoinCostReq.setNum(num);
		yohoCoinCostReq.setType(Byte.valueOf(String.valueOf(6)));// 抽奖送币
		yohoCoinCostReq.setParams("{}");
		yohoCoinCostReq.setOrderCode("0");
		req.setCost(yohoCoinCostReq);

		YohoCoinLogReqBO yohoCoinLogReqBO = new YohoCoinLogReqBO();
		yohoCoinLogReqBO.setUid(uid);
		yohoCoinLogReqBO.setChangeNum(Short.valueOf(String.valueOf(num)));
		yohoCoinLogReqBO.setChangeType(Byte.valueOf(String.valueOf(6)));
		yohoCoinLogReqBO.setChangeParams("{}");
		req.setHistory(yohoCoinLogReqBO);
		return req;
	}

	// 构造参数
	private YohoCoinRecordReq buildYohoCoinCostReq(int uid, int num, int type) {
		YohoCoinRecordReq req = new YohoCoinRecordReq();

		YohoCoinCostReqBO yohoCoinCostReq = new YohoCoinCostReqBO();
		yohoCoinCostReq.setUid(uid);
		yohoCoinCostReq.setNum(num);
		yohoCoinCostReq.setType(Byte.valueOf(String.valueOf(type)));// 抽奖送币
		yohoCoinCostReq.setParams("{}");
		yohoCoinCostReq.setOrderCode("0");
		req.setCost(yohoCoinCostReq);

		YohoCoinLogReqBO yohoCoinLogReqBO = new YohoCoinLogReqBO();
		yohoCoinLogReqBO.setUid(uid);
		yohoCoinLogReqBO.setChangeNum(Short.valueOf(String.valueOf(num)));
		yohoCoinLogReqBO.setChangeType(Byte.valueOf(String.valueOf(type)));
		yohoCoinLogReqBO.setChangeParams("{}");
		req.setHistory(yohoCoinLogReqBO);
		return req;
	}

}