package com.chinacreator.videoalliance.order.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.chinacreator.common.exception.BusinessException; import com.chinacreator.common.util.URLUtil; import com.chinacreator.videoalliance.common.dao.DictionaryDao; import com.chinacreator.videoalliance.order.bean.OrderInfo; import com.chinacreator.videoalliance.order.bean.SPInfo; import com.chinacreator.videoalliance.order.dao.CapBusiConfDao; import com.chinacreator.videoalliance.order.dao.SPDao; import com.chinacreator.videoalliance.order.util.CapUtil; import com.chinacreator.videoalliance.order.util.JsonUtil; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * CAP接口服务 * @author xu.zhou * @date 20190905 */ @Component public class CapOrderService { private static Logger log = Logger.getLogger(CapOrderService.class); @Autowired private SPDao spDao; @Autowired private CapBusiConfDao capBusiConfDao; @Autowired private DictionaryDao dictionaryDao; /** * cap订单同步 * @param orderInfo 订购信息 * @param accesstype //sp/cp的二次认证方式: 1:H5认证 2:SDK认证 3:短信认证 4:页面认证 * @return * @throws Exception */ public String orderSync(OrderInfo orderInfo, String accesstype) throws Exception{ String result = "-1"; String errorinfo = "cap订单同步失败"; try { SPInfo spInfo = getSPInfo(orderInfo.getSpid()); if(spInfo == null || (StringUtils.isEmpty(spInfo.getVacproductid()) && !"0".equals(spInfo.getHaslocal()))){ throw new BusinessException("7003", "产品配置错误"); } if (!"0".equals(spInfo.getHaslocal()) && spInfo.getFeetype() == 1) { String resJson = CapUtil.capSync(orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getUserid(), spInfo.getSpcode(), spInfo.getVacproductid(), "0", orderInfo.getSeqnumber(), accesstype, spInfo.getPrice(), orderInfo.getToken()); log.info("调cap订单同步接口返回结果:"+resJson); if (resJson != null) { Map map = JsonUtil.jsonToMap(resJson); result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1"; errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap订单同步失败"; } } } catch (Exception e) { e.printStackTrace(); log.info("cap订单同步失败====userid"+orderInfo.getUserid()+"====spid"+orderInfo.getSpid()+"==="+e.getMessage()); } return result; } /** * 订购 * @param orderInfo 订购信息 * @param accesstype //sp/cp的二次认证方式: 1:H5认证 2:SDK认证 3:短信认证 4:页面认证 * @return * @throws Exception */ public String order(OrderInfo orderInfo, String accesstype) throws Exception{ SPInfo spInfo = getSPInfo(orderInfo.getSpid()); String result = "-1"; String errorinfo = "cap订购失败"; if(spInfo == null || (StringUtils.isEmpty(spInfo.getVacproductid()) && !"0".equals(spInfo.getHaslocal()))){ throw new BusinessException("7003", "产品配置错误"); } //该短代码已放开20220302 if("0".equals(spInfo.getHaslocal())){ //不调CAP接口 只在本地生成订购关系 result = "0"; return result; } if (!"0".equals(spInfo.getHaslocal()) && spInfo.getFeetype() == 1) { String resJson = CapUtil.cap(orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getUserid(), spInfo.getSpcode(), spInfo.getVacproductid(), "0", orderInfo.getSeqnumber(), accesstype, spInfo.getPrice(), orderInfo.getToken()); log.info("调AP返回结果:"+resJson); if (resJson != null) { Map map = JsonUtil.jsonToMap(resJson); result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1"; errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap订购失败"; } } /** 业务返回编码 0000 处理成功 0007 订单同步失败 0009 参数异常 0008 非4G用户,鉴权失败 0001 订购鉴权失败/退订鉴权失败 0004 话单缓存失败 0003 查询用户归属省编码失败 0002 cbss产品订购异常 0006 cbss产品退订异常 */ if (!result.equals("0")) { if(!"cardbus".equals(orderInfo.getOrderchannel())){ insertPushTw(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid()); } if(result.equals("-1")){ throw new BusinessException(result, "cap订购失败:接口调用失败,服务暂不可用!", new String[0]); }else{ errorinfo = handelErrorinfo(result,errorinfo); throw new BusinessException(result, "cap订购失败:"+errorinfo, new String[0]); } } return result; } /** * 正向订购 * @param orderInfo 订购信息 * @param accesstype //sp/cp的二次认证方式: 1:H5认证 2:SDK认证 3:短信认证 4:页面认证 * @return * @throws Exception */ public Map orderCap(OrderInfo orderInfo, String accesstype) throws Exception{ SPInfo spInfo = getSPInfo(orderInfo.getSpid()); String result = "-1"; String errorinfo = "cap订购失败"; String time=""; HashMap resultMap = new HashMap(); if(spInfo == null || (StringUtils.isEmpty(spInfo.getVacproductid()) && !"0".equals(spInfo.getHaslocal()))){ throw new BusinessException("7003", "产品配置错误"); } if("0".equals(spInfo.getHaslocal())){ //不调CAP接口 只在本地生成订购关系 result = "0"; String times = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); resultMap.put("result",result); resultMap.put("orderTime",times); return resultMap; } if (!"0".equals(spInfo.getHaslocal()) && spInfo.getFeetype() == 1) { String resJson = CapUtil.cap(orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getUserid(), spInfo.getSpcode(), spInfo.getVacproductid(), "0", orderInfo.getSeqnumber(), accesstype, spInfo.getPrice(), orderInfo.getToken()); log.info("调CAP返回结果:"+resJson); if (resJson != null) { Map map = JsonUtil.jsonToMap(resJson); result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1"; errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap订购失败"; time= map.get("time") != null ? map.get("time").toString() : ""; } } /** 业务返回编码 0000 处理成功 0007 订单同步失败 0009 参数异常 0008 非4G用户,鉴权失败 0001 订购鉴权失败/退订鉴权失败 0004 话单缓存失败 0003 查询用户归属省编码失败 0002 cbss产品订购异常 0006 cbss产品退订异常 */ if (!result.equals("0")) { if(!"cardbus".equals(orderInfo.getOrderchannel())){ insertPushTw(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid()); } if(result.equals("-1")){ throw new BusinessException(result, "cap订购失败:接口调用失败,服务暂不可用!", new String[0]); }else if ("9068".equals(result)){ throw new BusinessException(result, errorinfo); }else { errorinfo = handelErrorinfo(result,errorinfo); throw new BusinessException(result, "cap订购失败:"+errorinfo, new String[0]); } } resultMap.put("result",result); resultMap.put("orderTime",time); return resultMap; } private String handelErrorinfo(String result,String errorinfo){ if(result.equals("0001") || result.equals("0002") || result.equals("0006") || result.equals("0007")){ // System.out.println("---符合---"); // errorinfo = errorinfo.replace("\\",""); JSONObject obj = JSON.parseObject(errorinfo); String CAP_MSG = obj.getString("CAP_MSG"); String OTHER_MSG = obj.getString("OTHER_MSG"); errorinfo = CAP_MSG +","+ OTHER_MSG; return errorinfo; } // System.out.println("---不符合---"); return errorinfo; } /** * 退订 * @param orderInfo * @return * @throws Exception */ public String cancel(OrderInfo orderInfo) throws Exception{ SPInfo spInfo = getSPInfo(orderInfo.getSpid()); String result = "-1"; String errorinfo = "cap退订失败"; if(spInfo == null || (StringUtils.isEmpty(spInfo.getVacproductid()) && !"0".equals(spInfo.getHaslocal()))){ throw new BusinessException("7003", "产品配置错误"); } /**** * 指定下线产品可以通过鉴权平台退订 * TB_SP_INFO表的计费点如果配置为XXX_delorder,代表这个业务已下线,不能订购,可以通过鉴权平台退订 * 验证通过后把计费点的“_delorder”去除,还原计费点,用于CAP接口订购,否则调CAP接口会报:"8005", "不在白名单产品列表不能订购" */ /*** if("kf".equals(orderInfo.getCancelchannel()) && spInfo.getVacproductid().indexOf("_delorder") != -1){ //还原计费点标识 spInfo.setVacproductid(spInfo.getVacproductid().replace("_delorder", "")); } ****/ //该段代码已放开20220302 if("0".equals(spInfo.getHaslocal())){ //不调CAP接口 result = "0"; return result; } if (!"0".equals(spInfo.getHaslocal()) && spInfo.getFeetype() == 1) { String resJson = CapUtil.cap(orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getUserid(), spInfo.getSpcode(), spInfo.getVacproductid(), "1", "", "", "", ""); log.info("调AP返回结果:"+resJson); if (resJson != null) { Map map = JsonUtil.jsonToMap(resJson); result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1"; errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap退订失败"; } } /** 业务返回编码 0000 处理成功 0007 订单同步失败 0009 参数异常 0008 非4G用户,鉴权失败 0001 订购鉴权失败/退订鉴权失败 0004 话单缓存失败 0003 查询用户归属省编码失败 0002 cbss产品订购异常 0006 cbss产品退订异常 */ if (!result.equals("0")) { if(result.equals("-1")){ throw new BusinessException(result, "cap退订失败:接口调用失败,服务暂不可用", new String[0]); }else{ errorinfo = handelErrorinfo(result,errorinfo); throw new BusinessException(result, "cap退订失败:"+errorinfo, new String[0]); } } return result; } /** * 退订 * @param orderInfo * @return * @throws Exception */ public Map cancelCap(OrderInfo orderInfo) throws Exception{ SPInfo spInfo = getSPInfo(orderInfo.getSpid()); String result = "-1"; String errorinfo = "cap退订失败"; String time=""; HashMap resultMap = new HashMap(); if(spInfo == null || (StringUtils.isEmpty(spInfo.getVacproductid()) && !"0".equals(spInfo.getHaslocal()))){ throw new BusinessException("7003", "产品配置错误"); } /**** * 指定下线产品可以通过鉴权平台退订 * TB_SP_INFO表的计费点如果配置为XXX_delorder,代表这个业务已下线,不能订购,可以通过鉴权平台退订 * 验证通过后把计费点的“_delorder”去除,还原计费点,用于CAP接口订购,否则调CAP接口会报:"8005", "不在白名单产品列表不能订购" */ /*** if("kf".equals(orderInfo.getCancelchannel()) && spInfo.getVacproductid().indexOf("_delorder") != -1){ //还原计费点标识 spInfo.setVacproductid(spInfo.getVacproductid().replace("_delorder", "")); } ****/ // if("0".equals(spInfo.getHaslocal())){ //不调CAP接口 // result = "0"; // return result; // } if("0".equals(spInfo.getHaslocal())){ //不调CAP接口 result = "0"; String times = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); resultMap.put("result",result); resultMap.put("orderTime",times); return resultMap; } if (!"0".equals(spInfo.getHaslocal()) && spInfo.getFeetype() == 1) { String resJson = CapUtil.cap(orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getUserid(), spInfo.getSpcode(), spInfo.getVacproductid(), "1", "", "", "", ""); log.info("调AP返回结果:"+resJson); if (resJson != null) { Map map = JsonUtil.jsonToMap(resJson); result = map.get("resultcode") != null ? map.get("resultcode").toString() : "-1"; errorinfo = map.get("errorinfo") != null ? map.get("errorinfo").toString() : "cap退订失败"; time= map.get("time") != null ? map.get("time").toString() : ""; } } /** 业务返回编码 0000 处理成功 0007 订单同步失败 0009 参数异常 0008 非4G用户,鉴权失败 0001 订购鉴权失败/退订鉴权失败 0004 话单缓存失败 0003 查询用户归属省编码失败 0002 cbss产品订购异常 0006 cbss产品退订异常 */ if (!result.equals("0")) { if(result.equals("-1")){ throw new BusinessException(result, "cap退订失败:接口调用失败,服务暂不可用", new String[0]); }else{ errorinfo = handelErrorinfo(result,errorinfo); throw new BusinessException(result, "cap退订失败:"+errorinfo, new String[0]); } } resultMap.put("result",result); resultMap.put("cancelTime",time); return resultMap; } /** * cap用户综合查询 * @param userid * @return */ public String capQuery(String userid) throws Exception { if(userid!=null && userid!=""){ String resJson = CapUtil.capQuery(userid); log.info("调cap用户综合查询返回结果:"+resJson); JSONObject obj = JSON.parseObject(resJson); String data = obj.getString("data"); return data; } return null; } /** * 卡部信控查询 * @param userid * @return */ public String kbxkQuery(String userid) throws Exception { if(userid!=null && userid!=""){ String resJson = CapUtil.kbxkQuery(userid); log.info("调卡部信控查询返回结果:"+resJson); JSONObject obj = JSON.parseObject(resJson); String data = obj.getString("data"); return data; } return null; } private SPInfo getSPInfo(String spid) throws Exception { return this.spDao.findById(spid); } public boolean hasCap(OrderInfo orderInfo) throws Exception{ return capBusiConfDao.hasCap(orderInfo.getCpid(), orderInfo.getSpid()); } /** * CAP订购失败推送拓维后向订购 * @param userid * @param cpid * @param spid */ public void insertPushTw(String userid,String cpid,String spid){ try { Map map = new HashMap(); map.put("userid",userid); map.put("kcSpid",spid); map.put("kcCpid",cpid); map.put("type", "twhx"); String mqReciveUrl = dictionaryDao.getValue("mqReciveUrl"); URLUtil.post(mqReciveUrl, JsonUtil.objectToJson(map)); //测试环境 //URLUtil.post("http://10.199.99.158:8030/mq-service/recive.do", JsonUtil.objectToJson(map)); //生产环境 //URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JsonUtil.objectToJson(map)); } catch (Exception e) { e.printStackTrace(); } } // public static void main(String args[]){ // CapOrderService c = new CapOrderService(); // String result = "0006"; // String errorinfo = "{\"CAP_MSG\":\"cbss产品退订异常\",\"OTHER_CODE\":\"8888\",\"OTHER_INTER\":\"cbss退订接口\",\"OTHER_MSG\":\"TCS_SPTradeOrder_VAC执行异常:\\n[INDETERMINATE]TradeCheckTradeSuperLimit.cpp:1476,12CRMException-300263: 特殊限制判断:用户当前为5G主套餐不能订购sp定向流量包!$$$20106$$$\"}"; // // // String re = c.handelErrorinfo(result,errorinfo); // System.out.println(re); // String ssss = "cbss产品退订异常,TCS_SPTradeOrder_VAC执行异常:\n" + // "[INDETERMINATE]TradeCheckTradeSuperLimit.cpp:1476,12CRMException-300263: 特殊限制判断:用户当前为5G主套餐不能订购sp定向流量包!$$$20106$$$"; // System.out.println(ssss.length()); // } }