12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067 |
- package com.chinacreator.videoalliance.order.service;
- import java.net.URLEncoder;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import com.chinacreator.common.exception.BusinessException;
- import com.chinacreator.common.util.DESUtil;
- import com.chinacreator.common.util.MD5;
- import com.chinacreator.common.util.URLUtil;
- import com.chinacreator.common.util.UsermobUtil;
- import com.chinacreator.videoalliance.common.bean.AreaInfo;
- import com.chinacreator.videoalliance.common.dao.DictionaryDao;
- import com.chinacreator.videoalliance.common.util.AreaUtil;
- import com.chinacreator.videoalliance.common.util.ConfigUtil;
- import com.chinacreator.videoalliance.order.bean.*;
- import com.chinacreator.videoalliance.order.dao.BackBusiOrderDao;
- import com.chinacreator.videoalliance.order.dao.BackShareOrderDao;
- import com.chinacreator.videoalliance.order.dao.BlackWhiteDao;
- import com.chinacreator.videoalliance.order.dao.OrderDao;
- import com.chinacreator.videoalliance.order.dao.SPDao;
- import com.chinacreator.videoalliance.order.util.JsonUtil;
- import org.apache.commons.lang.math.NumberUtils;
- import org.apache.commons.lang.time.DateFormatUtils;
- import org.apache.commons.lang.time.DateUtils;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.util.StringUtils;
- /**
- * 后向产品服务类
- * @author xu.zhou
- * @date 20190705
- */
- @Component
- public class BackBusiOrderService {
- private static Logger log = Logger.getLogger("orderError");
- private static Logger logger = Logger.getLogger(BackBusiOrderService.class);
- private static Logger busilog = Logger.getLogger("youtuorder");
- @Autowired
- private BlackWhiteDao blackWhiteDao;
- @Autowired
- private SPDao spDao;
- @Autowired
- private DictionaryDao dictionaryDao;
- @Autowired
- private BackBusiOrderDao backBusiOrderDao;
- @Autowired
- private ShareOrderService shareOrderService;
- @Autowired
- private BackShareOrderDao backShareOrderDao;
- @Autowired
- private OrderDao orderDao;
- /**
- * 验证及解密手机号码
- * @param orderInfo
- * @throws Exception
- */
- private void checkUserid(OrderInfo orderInfo) throws Exception {
- String userid = orderInfo.getUserid();
- if ((StringUtils.isEmpty(userid)) || ("null".equals(userid)))
- throw new BusinessException("9051", "手机号码无效", new String[0]);
- if (!isValid(userid)) {
- if(userid.length() >10){
- userid = ConfigUtil.decrypt(userid, orderInfo.getCpid());
- userid = ConfigUtil.getUserid(userid, orderInfo.getCpid());
- }
- }
- if (isValid(userid)) {
- if(userid.length() < 11){//QQ号
- if(!org.apache.commons.lang.StringUtils.equals("TX20_twback_old", orderInfo.getChannel())&&!org.apache.commons.lang.StringUtils.equals("TX20_twback", orderInfo.getChannel())){
- throw new BusinessException("9051", "手机号码无效", new String[0]);
- }
- }
- }else{
- throw new BusinessException("9051", "手机号码无效", new String[0]);
- }
- orderInfo.setUserid(userid);
- }
- /**
- * 支持QQ号与手机号
- * zzb 2020/02/18
- * @throws BusinessException
- */
- private boolean isValid(String usermob) throws BusinessException{
- if (usermob == null) {
- return false;
- }
- usermob = usermob.replaceAll("[\\s-]", "");
- if(!usermob.matches("^[0-9]*$")){
- return false;
- }
- return true;
- }
- /**
- * 验证签名是否有效
- * @param orderInfo
- * @param deuserid 加密的手机号码
- * @throws Exception
- */
- public void checkSignChannel(OrderInfo orderInfo, String deuserid) throws Exception {
- String sign = orderInfo.getSignature();
- if (StringUtils.isEmpty(sign)) {
- throw new BusinessException("9054", "signature校验失败");
- }
- Map cpSpBean = backBusiOrderDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
- String pwd = cpSpBean.get("NETPWD").toString();
- //Md5(orderid+cpid+spid+userid+type+timestamp+pwd)
- String localSign = MD5.MD5Encode(orderInfo.getOrderid() + orderInfo.getCpid()
- + orderInfo.getSpid() + deuserid + orderInfo.getTimestamp() + pwd);
- if (!localSign.equals(orderInfo.getSignature())) {
- throw new BusinessException("9054", "signature校验失败");
- }
- }
- /**
- * 订购前验证
- * @param orderInfo
- * @throws Exception
- */
- public void checkOrder(OrderInfo orderInfo, String deuserid) throws Exception {
- checkCpSpInfo(orderInfo);
- checkSignChannel(orderInfo,deuserid);
- checkUserid(orderInfo);
- String userid = orderInfo.getUserid();
- //判断用户黑名单
- if (this.blackWhiteDao.isBlackUser(userid, orderInfo.getCpid())) {
- throw new BusinessException("9010", "用户为黑名单用户", new String[0]);
- }
- //获取地市信息
- AreaInfo areaInfo = getAreaInfo(userid);
- orderInfo.setProvince(areaInfo.getProvince());
- orderInfo.setArea(areaInfo.getArea());
- if(orderInfo.getProvince() != null && orderDao.isOpen(orderInfo.getSpid(),orderInfo.getCpid(),orderInfo.getProvince())){ // status 为2 表示省份尚未开通
- if(!this.blackWhiteDao.isWhiteUserBySpid(userid, orderInfo.getCpid(), orderInfo.getSpid())){//判断用户是否白名单,是则流程继续
- throw new BusinessException("9011", "省份尚未开通此业务,敬请期待!");
- }
- }
- }
- /**
- * 验证业务参数是否合法
- * @param orderInfo
- * @throws Exception
- */
- private void checkCpSpInfo(OrderInfo orderInfo)throws Exception {
- List<HashMap> confList = backBusiOrderDao.getBackBusiConf(orderInfo.getCpid(), orderInfo.getSpid());
- if(confList == null || confList.size() == 0){
- throw new BusinessException("9058", "无业务配置信息", new String[0]);
- }
- HashMap confHm = confList.get(0);
- //判断是否配置netdays参数
- if(("2".equals(confHm.get("BUSITYPE")) || "3".equals(confHm.get("BUSITYPE")))
- && (confHm.get("NETDAYS") == null || "".equals(confHm.get("NETDAYS")))){
- throw new BusinessException("9058", "无业务配置信息,NETDAYS", new String[0]);
- }
- getSPInfo(orderInfo); //设置SP其他信息
- }
- /**
- * 获取手机号码的地市信息
- * @param userid
- * @return
- * @throws Exception
- */
- private AreaInfo getAreaInfo(String userid) throws Exception {
- AreaInfo areaInfo = AreaUtil.getAreaInfoByUserid(userid);
- if (areaInfo == null) {
- areaInfo = new AreaInfo();
- }
- return areaInfo;
- }
- /**
- * 设置SP相关信息
- * @param orderInfo
- * @throws Exception
- */
- private void getSPInfo(OrderInfo orderInfo) throws Exception {
- SPInfo spInfo = this.spDao.findById(orderInfo.getSpid());
- if (spInfo != null) {
- orderInfo.setSpid(spInfo.getSpid());
- orderInfo.setMutex(spInfo.getMutex());
- orderInfo.setRelationSp(spInfo.getRelationSp());
- orderInfo.setPaytype(spInfo.getPaytype());
- orderInfo.setErrorhandle(spInfo.getErrorhandle());
- }
- }
- /**
- * 填充BackBusiOrderRec
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private BackBusiOrderRec getOrderRecBean(OrderInfo orderInfo) throws Exception{
- BackBusiOrderRec bean = new BackBusiOrderRec();
- bean.setLogid(orderInfo.getReorderid());
- bean.setId(backBusiOrderDao.generateID());
- bean.setArea(orderInfo.getArea());
- bean.setCpid(orderInfo.getCpid());
- bean.setSpid(orderInfo.getSpid());
- bean.setOrderid(orderInfo.getOrderid());
- bean.setProvince(orderInfo.getProvince());
- bean.setUserid(orderInfo.getUserid());
- bean.setChannel(orderInfo.getOrderchannel());
- bean.setVipstatus("4"); //默认设置为不赠送
- bean.setType(orderInfo.getType()+"");
- if("1".equals(bean.getType())){//退订
- bean.setVipstatus(""); //设置为空
- }
- return bean;
- }
- /**
- * 是否赠送会员重试
- * @param orderInfo
- * @return
- */
- public boolean hasVipRetry(OrderInfo orderInfo)throws Exception{
- boolean result = false;
- List<BackBusiOrderRec> list = backBusiOrderDao.findOrderRecByOrderid(orderInfo.getOrderid());
- if(list != null && list.size() > 0){
- BackBusiOrderRec orderBean = list.get(0);
- //判断是否符合重试条件
- if(orderBean.getOrderid().equals(orderInfo.getOrderid())
- && orderBean.getUserid().equals(orderInfo.getUserid())
- && orderBean.getCpid().equals(orderInfo.getCpid())
- && orderBean.getSpid().equals(orderInfo.getSpid())
- && "5".equals(orderBean.getVipstatus())
- && !"2".equals(orderBean.getBusitype()) //不是纯免流,
- ){ //符合重试条件
- result = true;
- }
- //拓维渠道为TX20_twback办理会员+免流产品业务,会员是自己领取,不触发会员重试
- if(result && "3".equals(orderBean.getBusitype()) && "TX20_twback".equals(orderInfo.getOrderchannel())){
- result = false;
- }
- }
- return result;
- }
- /**
- * 重试会员赠送
- * @param orderInfo
- * @return
- * @throws Exception
- */
- public String vipRetryBusi(OrderInfo orderInfo)throws Exception{
- String errorcode = "-1";
- String errorinfo = "";
- String result = "-1";
- BackBusiOrderRec orderBean = backBusiOrderDao.findOrderRecByOrderid(orderInfo.getOrderid()).get(0);
- try {
- //判断重试时间是否大于订购记录时间2分钟以上,防止并发请求
- if(!backBusiOrderDao.getMinNum(orderBean.getInserttime(),2)){
- throw new BusinessException("9073", "重试时间与订购时间相差小于2分钟");
- }
- //非首次重试
- if(orderBean.getRetrytime() != null && !"".equals(orderBean.getRetrytime())){
- if(!backBusiOrderDao.getMinNum(orderBean.getInserttime(),2)){
- throw new BusinessException("9073", "重试频繁,请稍候再试");
- }
- }
- orderInfo.setType(0); //设置为订购
- //判断业务互斥
- this.hasMutual(orderInfo); //已办理互斥业务
- //获取业务结束时间
- String newEndtime = this.getEndTime(orderInfo);
- //赠送会员状态,0成功,1待赠送,2赠送中,3失败,4不赠送,5返回9070,7重试失败
- orderBean.setVipstatus("2");
- //办理结果编码,0成功,1待处理,2处理中,其他为异常
- orderBean.setResultcode("2");
- orderBean.setResultinfo("重试中");
- backBusiOrderDao.updBusiStatus(orderBean.getVipstatus(),orderBean.getResultcode(),orderBean.getResultcode(),orderBean.getId()); //添加订购记录
- String sendVipRes = sendVip(orderInfo); //赠送会员
- if("9070".equals(sendVipRes)){
- orderBean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- //小毛会更新vipstatus值,不以接口返回的为准
- sendVipRes = backBusiOrderDao.getVipstatus(orderInfo.getOrderid());
- if("0".equals(sendVipRes)){//赠送成功
- if("3".equals(orderBean.getBusitype())){
- String currtime = backBusiOrderDao.getCurrentTime();
- orderInfo.setEndtime(newEndtime);
- orderInfo.setOrdertime(currtime);
- orderInfo.setEffecttime(currtime);
- orderBean.setVipstatus("0");
- backBusiOrderDao.order(orderInfo); //新增或更新订购关系
- }
- }else{
- orderBean.setVipstatus("7");
- throw new BusinessException("9002","赠送会员未成功", new String[0]);
- }
- result = "0";
- errorcode = "0";
- errorinfo = "重试成功";
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e);
- if ((e instanceof BusinessException)) {
- errorcode = ((BusinessException) e).getCode();
- errorinfo = ((BusinessException) e).getMessage();
- }else{
- errorcode = "8000";
- errorinfo ="系统忙,"+e.getMessage();
- }
- if(errorinfo!=null&&errorinfo.length()>2000){
- errorinfo = errorinfo.substring(0, 2000);
- }
- throw e;
- } finally {
- if(!"9073".equals(errorcode)){
- orderBean.setResultcode(errorcode);
- orderBean.setResultinfo(errorinfo);
- orderBean.setRetrychannel("1");
- orderBean.setRetrycount(Integer.parseInt(orderBean.getRetrycount())+1+"");
- backBusiOrderDao.updOrderRecByVipRetry(orderBean); //更新重试记录
- saveLog(orderInfo, errorcode, errorinfo);
- }
- }
- return result;
- }
- /**
- * 获取endtime
- * @param orderInfo
- * @return
- */
- private String getEndTime(OrderInfo orderInfo)throws Exception{
- String endtime = "";
- String currEndtime = "";
- Map reMap = this.hasEffect(orderInfo);
- if((Boolean)reMap.get("hasEffect")){ //存在有效订购关系
- Map currOrderInfo = (Map)reMap.get("currOrderInfo");
- currEndtime = (String)currOrderInfo.get("ENDTIME");
- }
- //[{PWD=kijkfds, NETDAYS=31, CHANNEL=test_01, NETDAYS=2, SPID=1168, BUSITYPE=3, CPID=youtu}]
- HashMap confHm = backBusiOrderDao.getBackBusiConf(orderInfo.getCpid(), orderInfo.getSpid()).get(0);
- //String busiType = confHm.get("BUSITYPE")+"";
- //免流或免流+会员两种产品要以netdays参数来确定endtime
- if("2".equals(confHm.get("BUSITYPE")) || "3".equals(confHm.get("BUSITYPE"))){
- if(currEndtime != null && !"".equals(currEndtime)){
- endtime = backBusiOrderDao.endtimeParamDay(confHm.get("NETDAYS")+"", currEndtime);
- }else{
- endtime = backBusiOrderDao.currParamDay(confHm.get("NETDAYS")+"");
- }
- }
- return endtime;
- }
- /**
- * 业务办理
- * @param orderInfo
- * @return
- * @throws Exception
- */
- public String orderBusi(OrderInfo orderInfo)throws Exception{
- //只有SPID为1168,TYPE为1,reorderid不为空时才走退订流程,只有天猫平台的优酷后台可以退订
- if("1168".equals(orderInfo.getSpid()) && orderInfo.getType() == 1 && !StringUtils.isEmpty(orderInfo.getReorderid()) && "youtu_pc01".equals(orderInfo.getOrderchannel())){
- orderInfo.setStatus(1);
- //return this.cancel(orderInfo);
- throw new BusinessException("8000", "暂不提供退订操作", new String[0]);
- }else{
- orderInfo.setType(0); ////设置为订购
- return this.order(orderInfo);
- }
- }
- /**
- * 判断结束时间是否大于当前时间
- * @param endTime
- * @param currTime
- * @return
- * @throws Exception
- */
- private boolean compareDate(String endTime, String currTime)throws Exception{
- boolean result = false;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
- Date dateCurrTime = sdf.parse(currTime);
- Date dateEndTime = sdf.parse(endTime);
- //dateEndTime小于dateCurrTime返回-1,dateEndTime大于dateCurrTime返回1,相等返回0
- if(dateEndTime.compareTo(dateCurrTime)>0){
- result = true;
- }
- return result;
- }
- /**
- * 订购业务
- * @param orderInfo
- * @return
- */
- public String cancel(OrderInfo orderInfo)throws Exception{
- String errorcode = "-1";
- String errorinfo = "";
- String result = "-1";
- BackBusiOrderRec bean = getOrderRecBean(orderInfo);
- try{
- //判断订购时的订单是否有效
- List<BackBusiOrderRec> list = backBusiOrderDao.findOrderRecByOrderid(orderInfo.getReorderid());
- if(list != null && list.size() > 0){
- boolean secceed = false; //是否有办理成功的订购操作
- for(BackBusiOrderRec tmpBean : list){
- if("0".equals(tmpBean.getResultcode()) && "0".equals(tmpBean.getType())){
- secceed = true;
- break;
- }
- }
- //不是办理成功的订购的操作
- if(!secceed){
- throw new BusinessException("9017", "订购订单号无有效订购关系", new String[0]);
- }
- }else{
- throw new BusinessException("9018", "订购订单号无记录", new String[0]);
- }
- this.hasDupliOrderid(orderInfo);//判断是否重复订单ID
- //{SPID=1168, ID=201907231006328670420, ENDTIME=20200903235959, STATUS=0, ORDERTIME=20190729184037, CPID=youtu, USERID=18673197465}
- Map currOrder = backBusiOrderDao.findYoutuOrderRal(bean);
- if(currOrder == null || currOrder.size() == 0){
- throw new BusinessException("9016", "退订失败,无该用户订购关系或者已失效", new String[0]);
- }
- String newEndtime = backBusiOrderDao.endtimeMinus31Day(currOrder.get("ENDTIME")+"");
- String currtime = backBusiOrderDao.getCurrentTime();
- //判断结束时间是否大于当前时间
- if(!this.compareDate(newEndtime, currtime)){
- newEndtime = currtime; //结束时间小于或等于当前时间
- }
- orderInfo.setEndtime(newEndtime);
- //退订
- backBusiOrderDao.cancel(orderInfo);
- result = "0";
- errorcode = "0";
- errorinfo = "成功";
- }catch(Exception e){
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e);
- if ((e instanceof BusinessException)) {
- errorcode = ((BusinessException) e).getCode();
- errorinfo = ((BusinessException) e).getMessage();
- }else{
- errorcode = "8000";
- errorinfo ="系统忙,"+e.getMessage();
- }
- if(errorinfo!=null&&errorinfo.length()>2000){
- errorinfo = errorinfo.substring(0, 2000);
- }
- throw e;
- }finally{
- bean.setResultcode(errorcode);
- bean.setResultinfo(errorinfo);
- if(bean.getResultinfo().indexOf("重复订单") == -1){
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- }
- saveLog(orderInfo, errorcode, errorinfo);
- }
- return result;
- }
- /**
- * 订购业务
- * @param orderInfo
- * @return
- */
- public String order(OrderInfo orderInfo)throws Exception{
- String errorcode = "-1";
- String errorinfo = "";
- String result = "-1";
- String shareErcode = "0";
- String shareErnfo = "成功";
- BackBusiOrderRec bean = getOrderRecBean(orderInfo);
- try {
- orderInfo.setType(0); //设置为订购
- List<HashMap> confList = backBusiOrderDao.getBackBusiConf(orderInfo.getCpid(), orderInfo.getSpid());
- //[{PWD=kijkfds, CHANNEL=test_01, VIPTYPE=2, VIPPOOL=vip_yout, SPID=1168, BUSITYPE=3, CPID=youtu}]
- HashMap confHm = confList.get(0);
- String busiType = confHm.get("BUSITYPE")+"";
- //是否为复合产品,0是,为空或为1时不是
- String hasfh = confHm.get("HASFH") == null ? "": confHm.get("HASFH")+"";
- //调能力平台标识:0调能力平台并以结果落订购关系,1调能力平台其结果不影响订购关系,为空时不调能力平台
- String hasshare = confHm.get("HASSHARE") == null ? "": confHm.get("HASSHARE")+"";
- bean.setBusitype(busiType);
- this.hasDupliOrderid(orderInfo);//判断是否重复订单ID
- this.hasMutual(orderInfo); //已办理互斥业务
- String newEndtime = ""; //业务结束时间
- String starttime = ""; //业务开始时间
- Map<String, String> resMap = this.getBusiTime(orderInfo);
- newEndtime = resMap.get("endtime");
- starttime = resMap.get("starttime");
- //赠送会员状态,0成功,1待赠送,2赠送中,3失败,4不赠送,5赠送超时9070
- bean.setVipstatus("2");
- //办理结果编码,0成功,1待处理,2处理中,其他为异常
- bean.setResultcode("2");
- bean.setResultinfo("处理中");
- if("1".equals(busiType)){ //会员
- bean.setStarttime("");
- bean.setEndtime("");
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- String sendVipRes = sendVip(orderInfo); //赠送会员
- if("9070".equals(sendVipRes)){
- bean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- if("0".equals(hasfh)){//是复合产品
- this.fhsendvipRes(sendVipRes, bean);
- }else{
- //小毛会更新vipstatus值,不以接口返回的为准,小毛只有赠送成功或明确赠送失败的才会把vipstatus改为0或3
- sendVipRes = backBusiOrderDao.getVipstatus(orderInfo.getOrderid());
- if("3".equals(sendVipRes)){//赠送失败
- bean.setVipstatus("3");
- throw new BusinessException("9002","赠送会员未成功", new String[0]);
- }else if("0".equals(sendVipRes)){ //赠送成功
- bean.setVipstatus("0");
- }else{ //其他当作9070处理
- bean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- }
- }else if("2".equals(busiType)){ //免流
- /****
- if("200".equals(orderInfo.getSpid())){
- List<OrderInfo> orderlist = orderDao.findByUser(orderInfo.getUserid(),orderInfo.getCpid());
- if(orderlist.size()>0 && orderInfo != null){
- List<String> spidlist = new ArrayList<String>();
- for(OrderInfo oi : orderlist){
- String spid = oi.getSpid();
- spidlist.add(spid);
- }
- if(spidlist.contains("979")){
- }else{
- throw new BusinessException("8051","您未订购相应的产品,不能办理此加油包");
- }
- }else{
- throw new BusinessException("8051","您未订购相应的产品,不能办理此加油包");
- }
- }
- if("201".equals(orderInfo.getSpid())){
- List<OrderInfo> orderlist = orderDao.findByUser(orderInfo.getUserid(),orderInfo.getCpid());
- if(orderlist.size()>0 && orderInfo != null){
- List<String> spidlist = new ArrayList<String>();
- for(OrderInfo oi : orderlist){
- String spid = oi.getSpid();
- spidlist.add(spid);
- }
- if(spidlist.contains("1018") || spidlist.contains("1017")){
- }else{
- throw new BusinessException("8051","您未订购相应的产品,不能办理此加油包");
- }
- }else{
- throw new BusinessException("8051","您未订购相应的产品,不能办理此加油包");
- }
- }
- ****/
- bean.setVipstatus("4"); //设置为不赠送
- bean.setStarttime(starttime);
- bean.setEndtime(newEndtime);
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- orderInfo.setEndtime(newEndtime);
- //设置订购时间
- this.setOrderTime(orderInfo);
- //调能力平台
- this.shareOrder(orderInfo, hasshare);
- backBusiOrderDao.order(orderInfo); //新增或更新订购关系
- }else if("3".equals(busiType)){ //会员+免流
- bean.setEndtime(newEndtime);
- bean.setStarttime(starttime);
- if("TX20_twback".equals(orderInfo.getOrderchannel())){//拓维订购会员+免流业务
- bean.setResultcode("1");
- bean.setResultinfo("处理中");
- bean.setVipendtime(newEndtime); //设置领取会员的最后时间,默认为订单的endtime
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- orderInfo.setEndtime(newEndtime);
- //设置订购时间
- this.setOrderTime(orderInfo);
- bean.setVipstatus("1"); //设置为待赠送,由拓维发短信给用户,由用户自己去领取
- //调能力平台
- this.shareOrder(orderInfo, hasshare);
- backBusiOrderDao.order(orderInfo); //新增或更新订购关系
- //更新其他未领取会员的最后领取时间
- backBusiOrderDao.updVipEndtime(bean.getUserid(),newEndtime);
- }else{
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- String sendVipRes = sendVip(orderInfo); //赠送会员
- if("9070".equals(sendVipRes)){
- bean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- if("0".equals(hasfh)){//是复合产品
- orderInfo.setEndtime(newEndtime);
- //设置订购时间
- this.setOrderTime(orderInfo);
- //复合产品赠送会员小毛不会更新后向订购记录表td_backbusi_order_rec的vipstatus记录。
- this.fhsendvipRes(sendVipRes, bean);
- //调能力平台
- this.shareOrder(orderInfo, hasshare);
- backBusiOrderDao.order(orderInfo); //新增或更新订购关系
- }else{
- //小毛会更新vipstatus值,不以接口返回的为准,小毛只有赠送成功或明确赠送失败的才会把vipstatus改为0或3
- sendVipRes = backBusiOrderDao.getVipstatus(orderInfo.getOrderid());
- if("0".equals(sendVipRes)){//赠送成功
- orderInfo.setEndtime(newEndtime);
- //设置订购时间
- this.setOrderTime(orderInfo);
- bean.setVipstatus("0");
- //调能力平台
- this.shareOrder(orderInfo, hasshare);
- backBusiOrderDao.order(orderInfo); //新增或更新订购关系
- }else if("3".equals(sendVipRes)){//赠送失败
- bean.setVipstatus("3");
- throw new BusinessException("9002","赠送会员未成功", new String[0]);
- }else{ //其他当作9070处理
- bean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- }
- }
- }
- result = "0";
- errorcode = "0";
- errorinfo = "成功";
- if("部分成功".equals(bean.getResultinfo())){
- errorinfo = "部分成功";
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e);
- if ((e instanceof BusinessException)) {
- errorcode = ((BusinessException) e).getCode();
- errorinfo = ((BusinessException) e).getMessage();
- }else{
- errorcode = "8000";
- errorinfo ="系统忙,"+e.getMessage();
- }
- if(errorinfo != null && errorinfo.length()>2000){
- errorinfo = errorinfo.substring(0, 2000);
- }
- throw e;
- } finally {
- bean.setResultcode(errorcode);
- bean.setResultinfo(errorinfo);
- if(bean.getResultinfo().indexOf("重复订单") == -1){
- backBusiOrderDao.addOrderRec(bean); //添加订购记录
- //上海权益
- if("shanghaiqy".equals(orderInfo.getOrderchannel())){
- backBusiOrderDao.updShInPaInfo(bean);
- }
- }
- saveLog(orderInfo, errorcode, errorinfo);
- //youtu_pc01是天猫优酷,没有提供回调接口,不进行会员赠送重试
- if("9070".equals(bean.getResultcode()) && !"youtu_pc01".equals(bean.getChannel())){//赠送会员超时
- //if("TX20_twback".equals(orderInfo.getOrderchannel()) && "3".equals(bean.getBusitype())){
- //拓维订购会员+免流产品时,由客户自己领取会员
- //}else{
- inserVipRetryMq(bean); //推送到队列进行重试
- //}
- }
- }
- return result;
- }
- /**
- * 设置当前时间
- * @param orderInfo
- * @throws Exception
- */
- private void setOrderTime(OrderInfo orderInfo) throws Exception{
- String currtime = this.backBusiOrderDao.getCurrentTime();
- orderInfo.setOrdertime(currtime);
- orderInfo.setEffecttime(currtime);
- }
- /**
- * 调能力平台
- * @param orderInfo
- * @param hasshare //调能力平台标识:0调能力平台并以结果落订购关系,1调能力平台其结果不影响订购关系,为空时不调能力平台,
- * @throws Exception
- */
- private void shareOrder(OrderInfo orderInfo, String hasshare) throws Exception{
- if("0".equals(hasshare) || "1".equals(hasshare)){
- String shareErcode = "0";
- String shareErnfo = "成功";
- if("0".equals(hasshare)){//以能力平台调用结果落订购关系的要设置渠道为SHARE,此渠道记录在订购关系表和订购日志表中
- orderInfo.setChannel("SHARE");
- }else{
- orderInfo.setChannel("");
- }
- try {
- shareOrderService.order(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
- } catch (BusinessException e) {
- e.printStackTrace();
- shareErcode = e.getCode();
- shareErnfo = e.getMessage();
- if("0".equals(hasshare)){
- throw new BusinessException("8888","调用能力共享平台订购接口失败");
- }
- } finally {
- saveBackShareLog(orderInfo,shareErcode,shareErnfo);
- }
- }
- }
- /**
- * 复合产品赠送会员结果解析
- * @param sendVipRes
- * @param bean
- */
- private void fhsendvipRes(String sendVipRes, BackBusiOrderRec bean) throws Exception{
- //0:成功, -1:部分成功 , 1:失败, 其他可以作为超时处理。
- if("0".equals(sendVipRes)){
- bean.setVipstatus("0");
- }else if("-1".equals(sendVipRes)){//部分成功
- bean.setVipstatus("0");
- bean.setResultinfo("部分成功");
- }else if("1".equals(sendVipRes)){//失败
- bean.setVipstatus("3");
- throw new BusinessException("9002","赠送会员未成功", new String[0]);
- }else{
- bean.setVipstatus("5"); //赠送超时
- throw new BusinessException("9070","赠送会员超时", new String[0]);
- }
- }
- /**
- * 赠送会员
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private String sendVip(OrderInfo orderInfo){
- String resultcode = "3"; //预设失败
- /* int retry = 3; //重试次数
- for(int i=1; i <= retry; i++){//最多调三次
- resultcode = this.invokeSendVip(orderInfo);
- if("0".equals(resultcode)){//如果赠送成功就结束重试
- break;
- }
- }*/
- resultcode = this.invokeSendVip(orderInfo);
- return resultcode;
- }
- /**
- * 调接口送会员
- * @param orderInfo
- * @return
- */
- private String invokeSendVip(OrderInfo orderInfo){
- String resultcode = "3";
- try {
- //http://114.255.201.228:86/activity/youkuHX
- String vipurl = this.dictionaryDao.getValue("backBusiVipUrl");
- String timestamp = (System.currentTimeMillis() / 1000) + "";
- String userid = orderInfo.getUserid();
- String orderid = orderInfo.getOrderid();
- String channel = orderInfo.getOrderchannel();
- String cpid = orderInfo.getCpid();
- String spid = orderInfo.getSpid();
- String pwd = "";
- List<HashMap> confList = backBusiOrderDao.getBackBusiConf(orderInfo.getCpid(), orderInfo.getSpid());
- pwd = confList.get(0).get("PWD").toString();
- if("0".equals(confList.get(0).get("HASFH"))){ //是复合产品
- vipurl = this.dictionaryDao.getValue("backBusiGroupVipUrl");
- }
- userid = DESUtil.encode(userid, pwd);
- //MD5(orderid+userid+goodscode+pwd+timestamp)转换为十六进制ASCII 码字符串,共32 个字符,全小写 userid= Des(手机号码,pwd)
- //MD5(orderid+userid+timestamp+pwd)转换为十六进制ASCII 码字符串,共32 个字符,全小写
- String signature = MD5.MD5Encode(orderid + userid + timestamp + pwd);
- signature = signature.toLowerCase();
- vipurl = vipurl + "?userid=" + URLEncoder.encode(userid, "utf-8")+ "&orderid="+ orderid + "&cpid=" + cpid + "&spid=" + spid + "×tamp="
- + timestamp + "&signature=" + signature+ "&apptype=2";
- busilog.info("vipurl: "+vipurl);
- //http://114.255.201.228:86/activity/eshop/vip?userid=iafPbU9aRLghY%2FEVMXFeag%3D%3D&orderid=201906231206498662914&goodscode=pointshop130×tamp=1561445765&signature=47fe0e3900b29ef88fd0889b7c0e4cc6&apptype=5
- String result = URLUtil.get(vipurl,30*1000); //调赠送会员接口,超时时间设置为10秒
- busilog.info("赠送会员结果=> userid: " +userid+", orderid: "+orderInfo.getOrderid()+" , result: "+result);
- Map<?,?> map = JsonUtil.jsonToMap(result);
- resultcode = (String)map.get("resultcode");
- if(resultcode.equals("0")){
- busilog.info("赠送成功");
- }
- } catch (Exception e) {
- e.printStackTrace();
- busilog.error("userid: "+orderInfo.getUserid()+"赠送会员失败,"+e);
- }
- return resultcode;
- }
- /**
- * 查询本地订购关系表当前是否有已生效的订购关系
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private Map hasEffect(OrderInfo orderInfo) throws Exception{
- Map reMap = new HashMap();
- boolean hasEffect = false;
- Map currOrderInfo = null;
- String currentTime = backBusiOrderDao.getCurrentTime();
- //查询用户本地订购关系表未失效的订购数据
- currOrderInfo = backBusiOrderDao.findByUserAndSpid(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
- if(currOrderInfo != null){//本地有订购关系
- if(Long.parseLong(currOrderInfo.get("ENDTIME").toString()) >= Long.parseLong(currentTime)){ //结束时间大于或等于当前时间,订购关系有效
- hasEffect = true;
- }
- }
- reMap.put("hasEffect", hasEffect);
- reMap.put("currOrderInfo", currOrderInfo);
- return reMap;
- }
- /**
- * 判断业务是否互斥
- * @param orderInfo
- * @throws Exception
- */
- private void hasMutual(OrderInfo orderInfo) throws Exception{
- boolean result = false;
- List<OrderInfo> listOrderInfo = backBusiOrderDao.findByUser(orderInfo.getUserid());
- // if(listOrderInfo != null && listOrderInfo.size() > 0){
- //
- // //快手阿里产品特殊处理
- // if("190".equals(orderInfo.getSpid())||"191".equals(orderInfo.getSpid())||"192".equals(orderInfo.getSpid())
- // ||"1029".equals(orderInfo.getSpid()) ||"1030".equals(orderInfo.getSpid())){
- // for(OrderInfo oldOrderInfo : listOrderInfo){
- // if(oldOrderInfo.getSpid().equals(orderInfo.getSpid())){
- // throw new BusinessException("9005","本月已订购,请勿重复订购", new String[0]);
- // }
- //
- // }
- // }
- // }
- if (listOrderInfo != null && listOrderInfo.size() > 0
- && !StringUtils.isEmpty(orderInfo.getMutex())) {
- String[] mutexSpids = orderInfo.getMutex().trim().split(",");
- for (String mutexSpid : mutexSpids) {
- for (OrderInfo oldOrderInfo : listOrderInfo) {
- if (!oldOrderInfo.getSpid().equals(orderInfo.getSpid()) && oldOrderInfo.getSpid().equals(mutexSpid)) {
- result = true;
- break;
- }
- }
- }
- }
- if(result){
- throw new BusinessException("9003","已办理互斥业务", new String[0]);
- }
- }
- /**
- * 判断是否有相同订单
- * @param orderInfo
- * @return true是,false否
- */
- private void hasDupliOrderid(OrderInfo orderInfo) throws Exception{
- List<BackBusiOrderRec> list = backBusiOrderDao.findOrderRecByOrderid(orderInfo.getOrderid());
- if(list != null && list.size() > 0){
- throw new BusinessException("9001", "重复订单", new String[0]);
- }
- }
- public String getEndTime(String endTimeStr) throws ParseException {
- Calendar calendar = Calendar.getInstance();
- Date date = DateUtils.parseDate(endTimeStr.substring(0, 8), new String[] { "yyyyMMdd" });
- calendar.setTime(date);
- String day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + "";
- String year = calendar.get(Calendar.YEAR) + "";
- String month = calendar.get(Calendar.MONTH) + 1 < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1)
- : calendar.get(Calendar.MONTH) + 1 + "";
- return year + month + day + "235959";
- }
- /**
- * 写订购日志
- * @param orderInfo
- * @param errorcode
- * @param errorinfo
- */
- public void saveLog(OrderInfo orderInfo, String errorcode, String errorinfo) {
- try {
- OrderLog orderLog = new OrderLog();
- orderLog.setApptype(orderInfo.getApptype());
- orderLog.setArea(orderInfo.getArea());
- orderLog.setChannel(orderInfo.getOrderchannel());
- orderLog.setOrderstatus(orderInfo.getOrderstatus());
- orderLog.setStatus(orderInfo.getStatus());
- orderLog.setCpid(orderInfo.getCpid());
- orderLog.setIsexperience(orderInfo.getIsexperience());
- orderLog.setOrdertype(orderInfo.getOrdertype() + "");
- orderLog.setProvince(orderInfo.getProvince());
- orderLog.setArea(orderInfo.getArea());
- orderLog.setSpid(orderInfo.getSpid());
- orderLog.setUserid(orderInfo.getUserid());
- orderLog.setErrorcode(errorcode);
- orderLog.setErrorinfo(errorinfo);
- if(!orderInfo.getTimes().isEmpty()){
- orderLog.setTimes((System.currentTimeMillis()-NumberUtils.toLong(orderInfo.getTimes()))+"");
- }
- this.backBusiOrderDao.addOrderLog(orderLog);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+",写订购日志出现异常,"+e.getMessage());
- }
- }
- /**
- * 写后向订购能力平台日志
- * @param orderInfo
- * @param errorcode
- * @param errorinfo
- */
- private void saveBackShareLog(OrderInfo orderInfo,String errorcode,String errorinfo){
- BackShareOrderBean bso = new BackShareOrderBean();
- bso.setUserid(orderInfo.getUserid());
- bso.setErrorcode(errorcode);
- bso.setErrorinfo(errorinfo);
- bso.setCpid(orderInfo.getCpid());
- bso.setSpid(orderInfo.getSpid());
- try {
- backShareOrderDao.addShareOrderLog(bso);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+",写后向订购能力平台日志出现异常,"+e.getMessage());
- }
- }
- /**
- * 赠送会员超时异步重试
- * @param bean
- */
- public void inserVipRetryMq(BackBusiOrderRec bean){
- try{
- Map<String, String> map = new HashMap<String, String>();
- map.put("userid", bean.getUserid());
- map.put("id", bean.getId());
- map.put("orderid", bean.getOrderid());
- map.put("type", "vipretry");
- //System.out.println("==============赠送会员失败,"+map);
- String mqReciveUrl = dictionaryDao.getValue("mqReciveUrl");
- //开发环境
- //URLUtil.post("http://172.16.33.16:8082/mq-service/recive.do", JsonUtil.objectToJson(map));
- //测试环境
- //URLUtil.post("http://10.199.99.177:86/mq-service/recive.do", JsonUtil.objectToJson(map));
- //生产环境
- //URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JsonUtil.objectToJson(map));
- URLUtil.post(mqReciveUrl, JsonUtil.objectToJson(map));
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /**
- * 获取开始时间和结束时间
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private Map<String, String> getBusiTime(OrderInfo orderInfo)throws Exception{
- Map<String, String> resMap = new HashMap<String, String>();
- String starttime = ""; //业务开始时间
- String endtime = ""; //业务结束时间
- String currEndtime = "";
- Map reMap = this.hasEffect(orderInfo);
- HashMap confHm = backBusiOrderDao.getBackBusiConf(orderInfo.getCpid(), orderInfo.getSpid()).get(0);
- if((Boolean)reMap.get("hasEffect")){ //存在有效订购关系
- if("1".equals(confHm.get("ENDTYPE"))){
- throw new BusinessException("9005","本月已订购,请勿重复订购");
- }
- Map currOrderInfo = (Map)reMap.get("currOrderInfo");
- currEndtime = (String)currOrderInfo.get("ENDTIME");
- }
- //[{PWD=kijkfds, NETDAYS=31, CHANNEL=test_01, NETDAYS=2, SPID=1168, BUSITYPE=3, CPID=youtu}]
- //String busiType = confHm.get("BUSITYPE")+"";
- //免流或免流+会员两种产品要以netdays参数来确定endtime
- if("2".equals(confHm.get("BUSITYPE")) || "3".equals(confHm.get("BUSITYPE"))){
- if(currEndtime != null && !"".equals(currEndtime)){
- if("3".equals(confHm.get("ENDTYPE"))){
- endtime = backBusiOrderDao.endtimeTheMonthLastDay();
- }else if("2".equals(confHm.get("ENDTYPE"))){
- endtime = backBusiOrderDao.endtimeNextMonthLastDay(currEndtime);
- }else{
- endtime = backBusiOrderDao.endtimeParamDay(confHm.get("NETDAYS")+"", currEndtime);
- }
- }else{
- if("1".equals(confHm.get("ENDTYPE")) || "2".equals(confHm.get("ENDTYPE"))|| "3".equals(confHm.get("ENDTYPE"))){
- endtime = backBusiOrderDao.endtimeTheMonthLastDay(); //特殊产品的结束时间只到本月底
- }else {
- endtime = backBusiOrderDao.currParamDay(confHm.get("NETDAYS")+"");
- }
- }
- if("1".equals(confHm.get("ENDTYPE")) || "2".equals(confHm.get("ENDTYPE")) || "3".equals(confHm.get("ENDTYPE"))){
- starttime = backBusiOrderDao.getKsStartTime(); //特殊产品开始时间就是当天
- }else{
- starttime = backBusiOrderDao.getStartTime(endtime,confHm.get("NETDAYS")+"");
- }
- }
- resMap.put("starttime", starttime);
- resMap.put("endtime", endtime);
- return resMap;
- }
- }
|