package com.chinacreator.videoalliance.order.service; import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import com.chinacreator.common.exception.BusinessException; import com.chinacreator.common.util.Base64; 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.bean.CPInfo; import com.chinacreator.videoalliance.common.dao.DictionaryDao; import com.chinacreator.videoalliance.common.dao.ProvinceControlDao; import com.chinacreator.videoalliance.common.util.AreaUtil; import com.chinacreator.videoalliance.common.util.ConfigUtil; import com.chinacreator.videoalliance.order.bean.EcbProductInfo; import com.chinacreator.videoalliance.order.bean.EcbResponseBean; import com.chinacreator.videoalliance.order.bean.OrderInfo; import com.chinacreator.videoalliance.order.bean.OrderLog; import com.chinacreator.videoalliance.order.bean.SPInfo; import com.chinacreator.videoalliance.order.dao.*; import com.chinacreator.videoalliance.order.util.JsonUtil; import org.apache.commons.lang.math.NumberUtils; 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; @Component public class CSNewOrderService { private static Logger log = Logger.getLogger("orderError"); @Autowired private OrderDao orderDao; @Autowired private BlackWhiteDao blackWhiteDao; @Autowired private SPDao spDao; @Autowired private DictionaryDao dictionaryDao; @Autowired private EcbDiscntNewDao ecbDiscntNewDao; @Autowired private ProvinceControlDao provinceControlDao; @Autowired private OrderLogDao orderLogDao; @Autowired private OrderSysDao sysDao; @Autowired private EcbService ecbService; @Autowired private BackBusiOrderDao backBusiOrderDao; /** * 验证手机号码 * @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("7002", "手机号码为空", new String[0]); orderInfo.setUserid(ConfigUtil.decrypt(userid, orderInfo.getCpid())); if ((orderInfo.getUserid() != null) && (orderInfo.getUserid().length() > 11)) { orderInfo.setUserid(ConfigUtil.getUserid(orderInfo.getUserid(), orderInfo.getCpid())); } if (!UsermobUtil.isValid(orderInfo.getUserid())) { throw new BusinessException("7002", "手机号码无效", new String[0]); } } /** * 验证CP * @param orderInfo * @throws Exception */ private void checkCpInfo(OrderInfo orderInfo) throws Exception { CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null); if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) { throw new BusinessException("9002", "该业务暂未开放,敬请期待。", new String[0]); } } /** * 验证SP * @param orderInfo * @throws Exception */ private void checkSpInfo(OrderInfo orderInfo) throws Exception { SPInfo spInfo = getSPInfo(orderInfo); if (spInfo.getCanorder() == 1 && StringUtils.isEmpty(orderInfo.getOrderchannel())) { throw new BusinessException("9012", "该产品不支持订购操作", new String[0]); } if (orderInfo.getOrdertype() == 1) { if (spInfo.getType() != 0 && spInfo.getType() != 3) { throw new BusinessException("5305", "产品标识符无效", new String[0]); } if(spInfo.getType() == 0){ if (StringUtils.isEmpty(orderInfo.getVideoid())) { throw new BusinessException("8002", "{0}参数无效", new String[] { "videoid" }); } if (StringUtils.isEmpty(orderInfo.getVideoname())) throw new BusinessException("8002", "{0}参数无效", new String[] { "videoname" }); try { String videoname = new String(Base64.decodeBase64(orderInfo.getVideoname()), "UTF-8"); orderInfo.setVideoname(videoname); } catch (Exception e) { throw new BusinessException("8002", "{0}参数无效", new String[] { "videoname" }); } orderInfo.setEndtime(this.sysDao.getLastDay()); }else{ orderInfo.setOrdertype(3); } } else if (orderInfo.getOrdertype() == 0) { if (spInfo.getType() == 0){ throw new BusinessException("5305", "产品标识符无效", new String[0]); } } else if (orderInfo.getOrdertype() == 2) { if (spInfo.getType() != 0) { throw new BusinessException("5305", "产品标识符无效", new String[0]); } orderInfo.setCachesucceed(0); } else { throw new BusinessException("8002", "{0}参数无效", new String[] { "ordertype" }); } orderInfo.setSpid(spInfo.getSpid()); } /** * 订购验证 * @param orderInfo * @throws Exception */ public void checkOrder(OrderInfo orderInfo) throws Exception { checkSign(orderInfo); checkUserid(orderInfo); checkCpInfo(orderInfo); checkSpInfo(orderInfo); String userid = orderInfo.getUserid(); if (this.blackWhiteDao.isBlackUser(userid, orderInfo.getCpid())) { throw new BusinessException("9002", "用户为黑名单用户", new String[0]); } AreaInfo areaInfo = getAreaInfo(userid); orderInfo.setProvince(areaInfo.getProvince()); orderInfo.setArea(areaInfo.getArea()); CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), areaInfo.getProvince()); if (cpInfo == null) { throw new BusinessException("7005", "CP标识无效", new String[0]); } String allowOrder = this.dictionaryDao.getValue("allowOrder"); if (("1".equals(allowOrder)) || (areaInfo.getStatus() == 1)) { throw new BusinessException("9002", "该业务暂未开放,敬请期待。", new String[0]); } if ((areaInfo.getProvince() != null) && (this.provinceControlDao.isNotOpen(areaInfo.getProvince()))) throw new BusinessException("9003", "您号码的归属地{0}暂未开放该业务,敬请期待。", new String[] { areaInfo.getProvince() }); if(this.orderDao.isOpen(orderInfo.getSpid(),orderInfo.getCpid(),orderInfo.getProvince())){ // status 为2 表示省份尚未开通 throw new BusinessException("9003", "省份尚未开通此业务,敬请期待!"); } } /** * 验证签名是否有效 * @param orderInfo * @param deuserid 加密的手机号码 * @throws Exception */ public void checkSign(OrderInfo orderInfo) 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(cpid + spid + userid + type + channel + timestamp + pwd) String channel = orderInfo.getOrderchannel(); if(orderInfo.getType() == 1){ channel = orderInfo.getCancelchannel(); } String localSign = orderInfo.getCpid() + orderInfo.getSpid() + orderInfo.getUserid() + orderInfo.getType() + channel + orderInfo.getTimestamp() + pwd; System.out.println("localSign=>"+localSign); localSign = MD5.MD5Encode(localSign); if (!localSign.equals(orderInfo.getSignature())) { throw new BusinessException("9054", "signature校验失败"); } } /** * 获取省份区域信息 * @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 * @return * @throws Exception */ private SPInfo getSPInfo(OrderInfo orderInfo) throws Exception { SPInfo spInfo = null; if (StringUtils.isEmpty(orderInfo.getSpid())) spInfo = this.spDao.findDefaultByCP(orderInfo.getCpid()); else { spInfo = this.spDao.findById(orderInfo.getSpid()); } if (spInfo == null) { throw new BusinessException("5305", "产品标识符无效", new String[0]); } orderInfo.setSpid(spInfo.getSpid()); orderInfo.setMutex(spInfo.getMutex()); orderInfo.setRelationSp(spInfo.getRelationSp()); orderInfo.setPaytype(spInfo.getPaytype()); orderInfo.setErrorhandle(spInfo.getErrorhandle()); return spInfo; } /** * 订购 * @param orderInfo * @throws Exception */ public void order(OrderInfo orderInfo) throws Exception { String errorcode = "0"; String errorinfo = ""; String result = "0"; try { List listOrderInfo = orderDao.findByUser(orderInfo.getUserid()); if (listOrderInfo != null && listOrderInfo.size() > 0) { if (!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.getStatus() == 0) { throw new BusinessException("9010", "您已订购,请不要重复订购!", new String[0]); } if (oldOrderInfo.getSpid().equals(orderInfo.getSpid()) && oldOrderInfo.getStatus() == 1) { throw new BusinessException("9010", "您本月已退订,请下月再订购!", new String[0]); } if (oldOrderInfo.getSpid().equals(mutexSpid)) { throw new BusinessException("9011", "您已订购同类产品!", new String[0]); } } } } else { if (listOrderInfo != null && listOrderInfo.size() != 0) { for (OrderInfo oldOrderInfo : listOrderInfo) { if (oldOrderInfo.getSpid().equals(orderInfo.getSpid()) && oldOrderInfo.getStatus() == 0) { throw new BusinessException("9010", "您已订购,请不要重复订购!", new String[0]); } if (oldOrderInfo.getSpid().equals(orderInfo.getSpid()) && oldOrderInfo.getStatus() == 1) { throw new BusinessException("9010", "您本月已退订,请下月再订购!", new String[0]); } } } } } //不是白名单 if (!this.blackWhiteDao.isWhiteUser(orderInfo.getUserid(), orderInfo.getCpid())) { //不是白名单 if(orderInfo.getPaytype() == 1){ boolean hasinvoke = true; if("0".equals(dictionaryDao.getValue("csnewSwitch"))){ hasinvoke = false; String whiteUserList = dictionaryDao.getValue("csnewList"); if(whiteUserList != null && !"".equals(whiteUserList.trim())){ String [] array = whiteUserList.trim().split("\\|"); for(String tmpuser : array){ if(orderInfo.getUserid().equals(tmpuser)){ hasinvoke = true; break; } } } } if(!hasinvoke){ throw new BusinessException("9013", "非畅视白名单", new String[0]); } EcbProductInfo ecbProductInfo = this.queryPorduct(orderInfo.getUserid(), orderInfo.getCpid(),orderInfo.getSpid()); log.debug("HANDLEbean==>"+JsonUtil.objectToJson(ecbProductInfo)); /** * 用户当前订购资费的状态: 0:用户当前无订购可订购(跟cBSS前台生成规则一致) 1:用户当前有订购可退订 2:用户当前有订购不可退订 3:用户当前有产品可订购 */ if(!"3".equals(ecbProductInfo.getDiscntOrderState()) && !"0".equals(ecbProductInfo.getDiscntOrderState())){ throw new BusinessException("9013", "您不能订购此业务!", new String[0]); } result = ecbService.order(orderInfo.getUserid(), orderInfo.getCpid(),orderInfo.getSpid(),ecbProductInfo); ecbDiscntNewDao.order(ecbProductInfo,orderInfo); }else{ throw new BusinessException("9012", "该产品不支持订购操作", new String[0]); } } String currentTime = this.sysDao.getCurrentTime(); if (orderInfo.getOrdertime() == null) { orderInfo.setOrdertime(currentTime); } if (orderInfo.getEffecttime() == null) { orderInfo.setEffecttime(currentTime); } if (!result.equals("0") && orderInfo.getErrorhandle() == 0 ) { orderInfo.setOrderstatus(7); orderInfo.setIsexperience(1); orderInfo.setShowstatus("0"); } //添加订购关系 this.orderDao.order(orderInfo); } catch (Exception e) { log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e); e.printStackTrace(); if ((e instanceof BusinessException)) { errorcode = ((BusinessException) e).getCode(); errorinfo = ((BusinessException) e).getMessage(); if(errorinfo.length()>200){ errorinfo = errorinfo.substring(0, 200); } }else{ errorcode = "8000"; errorinfo ="系统忙,"+e.getMessage(); } if(!errorcode.equals("9999")){ throw e; } } finally { saveLog(orderInfo, errorcode, errorinfo); } } /** * 退订验证 * @param orderInfo * @throws Exception */ public void checkCancel(OrderInfo orderInfo) throws Exception { checkSign(orderInfo); checkUserid(orderInfo); CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null); if (cpInfo == null) { throw new BusinessException("7005", "CP标识无效", new String[0]); } SPInfo spInfo = getSPInfo(orderInfo); if (spInfo.getCancancelorder() == 1 && StringUtils.isEmpty(orderInfo.getCancelchannel())) { throw new BusinessException("9011", "该产品不支持退订操作", new String[0]); } if (this.orderDao.hasTiyanValid(orderInfo.getUserid(), orderInfo.getCpid(), orderInfo.getSpid())) throw new BusinessException("9015", "该产品体验期未结束不支持退订操作", new String[0]); } /** * 退订 * @param orderInfo * @throws Exception */ public void cancel(OrderInfo orderInfo) throws Exception { String errorcode = "0"; String errorinfo = ""; try { List listOrderInfo = orderDao.findByUser(orderInfo.getUserid()); if (listOrderInfo == null || listOrderInfo.size() == 0) { throw new BusinessException("9016", "退订失败,无该用户订购关系或者已失效", new String[0]); } boolean isHasOrder = false; for (OrderInfo oldOrderInfo : listOrderInfo) { if (orderInfo.getSpid().equals(oldOrderInfo.getSpid())) { orderInfo.setOrdertime(oldOrderInfo.getOrdertime()); if(!StringUtils.isEmpty(oldOrderInfo.getEndtime())){ orderInfo.setEndtime(oldOrderInfo.getEndtime()); } if (oldOrderInfo.getStatus() != 0) { throw new BusinessException("9017", "退订失败,该用户已退订该产品", new String[0]); } isHasOrder = true; break; } } if (!isHasOrder) { throw new BusinessException("9018", "退订失败,该用户没有订购该产品或者已失效", new String[0]); } //不是白名单 if (!this.blackWhiteDao.isWhiteUser(orderInfo.getUserid(), orderInfo.getCpid())) { if(orderInfo.getPaytype() == 1){ EcbProductInfo ecbProductInfo = this.queryPorduct(orderInfo.getUserid(), orderInfo.getCpid(),orderInfo.getSpid()); log.debug("HANDLEbean==>"+JsonUtil.objectToJson(ecbProductInfo)); /** * 用户当前订购资费的状态: 0:用户当前无订购可订购(跟cBSS前台生成规则一致) 1:用户当前有订购可退订 2:用户当前有订购不可退订 3:用户当前有产品可订购 */ if(!"1".equals(ecbProductInfo.getDiscntOrderState())){ throw new BusinessException("9013", "您不能退订此业务!", new String[0]); } errorcode = ecbService.cancelOrder(orderInfo.getUserid(), orderInfo.getCpid(),orderInfo.getSpid(),orderInfo.getOrdertime(),ecbProductInfo); ecbDiscntNewDao.cancel(ecbProductInfo,orderInfo); }else { throw new BusinessException("9012", "该产品不支持退订操作", new String[0]); } } String currentTime = this.sysDao.getCurrentTime(); if (orderInfo.getCanceltime() != null) { orderInfo.setEndtime(getEndTime(orderInfo.getCanceltime())); } else { orderInfo.setCanceltime(currentTime); } if (orderInfo.getEffecttime() == null) { orderInfo.setEffecttime(currentTime); } if (orderInfo.getEndtime() == null) { orderInfo.setEndtime(this.sysDao.getEndDayOfCurrentMonth()); } //退订 this.orderDao.cancelOrder(orderInfo); } catch (Exception e) { e.printStackTrace(); log.error("userid:"+orderInfo.getUserid()+"退订出现异常,"+e); if ((e instanceof BusinessException)) { errorcode = ((BusinessException) e).getCode(); errorinfo = ((BusinessException) e).getMessage(); if(errorinfo.length()>200){ errorinfo = errorinfo.substring(0, 200); } }else{ errorcode = "8000"; errorinfo ="系统忙,"+e.getMessage(); } if(!errorcode.equals("9999")){ throw e; } } finally { saveLog(orderInfo, errorcode, errorinfo); } } 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) { 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.orderLogDao.addOrderLog(orderLog); } /** * 查询ECB产品信息 * @param userid * @param cpid * @param spid * @return * @throws Exception */ public EcbProductInfo queryPorduct(String userid, String cpid, String spid) throws Exception { EcbProductInfo resEcbProductInfo = null; String ecbProductUrl = dictionaryDao.getValue("ecbProductUrl"); String result = URLUtil.get(ecbProductUrl+"?userid=" + userid + "&cpid=" + cpid + "&spid=" + spid, NumberUtils.toInt(this.dictionaryDao.getValue("ecbTimeout"))); //String result = "{\"transIdo\":\"20190830120302278\",\"errorInfo\":\"成功\",\"requeststr\":\"\",\"resultCode\":\"0\",\"code\":\"0000\",\"userProductInfo\":{\"productInfos\":[{\"productId\":\"89016266\",\"productName\":\"4G套餐-30元套餐(苏湘川甘新)\",\"packageCode\":\"51947015\",\"packageName\":\"畅视产品资费包\",\"discntCode\":\"8244270\",\"discntName\":\"标清畅视包月套餐【每月30元】\",\"discntFee\":\"3000\",\"discntStartDate\":\"20190830120302\",\"discntEndDate\":\"20501231235959\",\"discntOrderState\":\"0\",\"discntValue\":\"0\"},{\"productId\":\"89016266\",\"productName\":\"4G套餐-30元套餐(苏湘川甘新)\",\"packageCode\":\"51947015\",\"packageName\":\"畅视产品资费包\",\"discntCode\":\"8302733\",\"discntName\":\"畅视20元包月套餐\",\"discntFee\":\"2000\",\"discntStartDate\":\"20190830120302\",\"discntEndDate\":\"20191231235959\",\"discntOrderState\":\"0\",\"discntValue\":\"1\"},{\"productId\":\"89016266\",\"productName\":\"4G套餐-30元套餐(苏湘川甘新)\",\"packageCode\":\"51947015\",\"packageName\":\"畅视产品资费包\",\"discntCode\":\"61125230\",\"discntName\":\"畅视业务60元40G定向流量包\",\"discntFee\":\"6000\",\"discntStartDate\":\"20190830120302\",\"discntEndDate\":\"20501231120000\",\"discntOrderState\":\"0\",\"discntValue\":\"1\"}],\"userProductId\":\"89016266\",\"userProductName\":\"4G套餐-30元套餐(苏湘川甘新)\"},\"responsestr\":\"\"}"; log.info("result====" + userid + ":" + result); EcbResponseBean infos = (EcbResponseBean)JsonUtil.jsonToBean(result, EcbResponseBean.class); log.info("infos====" + userid + ":" + JsonUtil.objectToJson(infos)); if (!infos.getResultCode().equals("0")) { throw new BusinessException(infos.getResultCode(), "查询产品信息未成功,"+infos.getErrorInfo(), new String[0]); } if (infos.getUserProductInfo() == null) { throw new BusinessException("9036", "无产品信息", new String[0]); } if ((infos.getUserProductInfo().getProductInfos() == null) || (infos.getUserProductInfo().getProductInfos().size() == 0)) { throw new BusinessException("9034", "资费信息信息不全", new String[0]); } List infoList = new ArrayList(); List proinfos = infos.getUserProductInfo().getProductInfos(); for (EcbProductInfo ecbProductInfo : proinfos) { if ("51947015".equals(ecbProductInfo.getPackageCode()) && "61125230".equals(ecbProductInfo.getDiscntCode())){ infoList.add(ecbProductInfo); } } log.info("infoList====" + userid + ":" + JsonUtil.objectToJson(infoList)); if (infoList == null || infoList.size() == 0) { throw new BusinessException("9035", "未查询出用户资费信息", new String[0]); }else{ resEcbProductInfo = infoList.get(0); } return resEcbProductInfo; } }