package com.chinacreator.videoalliance.order.service; import java.sql.SQLException; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.time.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.chinacreator.common.exception.BusinessException; 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.util.AreaUtil; import com.chinacreator.videoalliance.common.util.ConfigUtil; 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.OrderDao; import com.chinacreator.videoalliance.order.dao.OrderLogDao; import com.chinacreator.videoalliance.order.dao.OrderSysDao; import com.chinacreator.videoalliance.order.dao.SPDao; /** * vac订购关系查询服务类 * @author xu.zhou * @date 20190315 */ @Component public class VacQryService { private static final Logger logger = LoggerFactory.getLogger(VacQryService.class); @Autowired private OrderDao orderDao; @Autowired private SPDao spDao; @Autowired private DictionaryDao dictionaryDao; @Autowired private OrderLogDao orderLogDao; @Autowired private OrderSysDao sysDao; /** * 检查手机号码 * @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]); if (!UsermobUtil.isValid(userid)) { userid = ConfigUtil.decrypt(userid, orderInfo.getCpid()); userid = ConfigUtil.getUserid(userid, orderInfo.getCpid()); } if (!UsermobUtil.isValid(userid)) { throw new BusinessException("7002", "手机号码无效", new String[0]); } orderInfo.setUserid(userid); } 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]); } } private SPInfo checkSpInfo(OrderInfo orderInfo) throws Exception { SPInfo spInfo = getSPInfo(orderInfo); if (spInfo.getCanorder() == 1 && StringUtils.isEmpty(orderInfo.getOrderchannel())) { throw new BusinessException("9012", "该产品不支持订购操作", new String[0]); } orderInfo.setSpid(spInfo.getSpid()); return spInfo; } /** * 查询VAC订购关系 * @param orderInfo * @return * @throws Exception */ public String qryVacOrder(OrderInfo orderInfo) throws Exception { //检查手机号码 checkUserid(orderInfo); //查询CPID checkCpInfo(orderInfo); //查询SPID SPInfo spInfo = checkSpInfo(orderInfo); String userid = orderInfo.getUserid(); //查询当前是否有订购关系 hasExists(orderInfo); AreaInfo areaInfo = getAreaInfo(userid); orderInfo.setProvince(areaInfo.getProvince()); orderInfo.setArea(areaInfo.getArea()); String result = qryVac( orderInfo, spInfo); if("0".equals(result)){ recOrderRelations(orderInfo); } return result; } /** * 获取区域信息 * @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 */ private void hasExists(OrderInfo orderInfo) throws Exception { logger.info("++++++++++++++++++++++++查询是否有已存在的订购关系"); 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(mutexSpid)) { // throw new BusinessException("9011", "您已订购同类产品!", new String[0]); // } } } } } } /** * 查询VAC如果有订购关系存在,保存到本地订购关系表,及订购日志表 * @param orderInfo * @throws Exception */ private void recOrderRelations(OrderInfo orderInfo) throws Exception { //logger.info("++++++++++++++++++++++入库订购关系"); String errorcode = "0"; String errorinfo = ""; try { this.hasExists( orderInfo); String currentTime = this.sysDao.getCurrentTime(); if (orderInfo.getOrdertime() == null) { orderInfo.setOrdertime(currentTime); } if (orderInfo.getEffecttime() == null) { orderInfo.setEffecttime(currentTime); } this.orderDao.order(orderInfo); } catch (Exception e) { e.printStackTrace(); errorcode = "8000"; errorinfo ="系统忙"; if ((e instanceof BusinessException)) { errorcode = ((BusinessException) e).getCode(); errorinfo = ((BusinessException) e).getMessage(); if(errorinfo.length()>200){ errorinfo = errorinfo.substring(0, 200); } } 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); } /** * 调VAC接口查询订购信息 * @param orderInfo * @param spInfo * @param cpInfo * @return * @throws Exception */ private String qryVac(OrderInfo orderInfo, SPInfo spInfo)throws Exception{ String result = "-1"; logger.info("***********进入订购关系判断*************"); try { String url = dictionaryDao.getValue("vacQuery"); //外网地址 //url = "http://127.0.0.52:9999/vacquery/vacQuery.do"; logger.info("++++++++资费ID:"+spInfo.getVacproductid()+"+++++++++++调用url:"+url); url = url+"?userId="+orderInfo.getUserid(); String resultstr = URLUtil.get(url); //String resultstr = "{\"errorinfo\":\"\",\"orderItems\":[{\"coreId\":\"\",\"da\":\"18672686569\",\"daType\":0,\"effectiveTime\":\"20190312095737\",\"expireTime\":\"20501231235959\",\"fa\":\"\",\"faType\":0,\"id\":\"8000610900\",\"idType\":0,\"oa\":\"18672686569\",\"oaType\":0,\"resumeTime\":\"\",\"status\":0,\"subscribeTime\":\"20190312095747\",\"suspendTime\":\"\"},{\"coreId\":\"\",\"da\":\"18672686569\",\"daType\":0,\"effectiveTime\":\"20150701000000\",\"expireTime\":\"20501231235959\",\"fa\":\"\",\"faType\":0,\"id\":\"3100008300\",\"idType\":0,\"oa\":\"18672686569\",\"oaType\":0,\"resumeTime\":\"\",\"status\":0,\"subscribeTime\":\"20150701072816\",\"suspendTime\":\"\"},{\"coreId\":\"\",\"da\":\"18672686569\",\"daType\":0,\"effectiveTime\":\"20180601000000\",\"expireTime\":\"20501231000000\",\"fa\":\"\",\"faType\":0,\"id\":\"7000000200\",\"idType\":0,\"oa\":\"18672686569\",\"oaType\":0,\"resumeTime\":\"\",\"status\":0,\"subscribeTime\":\"20180527085644\",\"suspendTime\":\"\"},{\"coreId\":\"\",\"da\":\"18672686569\",\"daType\":0,\"effectiveTime\":\"20180601000000\",\"expireTime\":\"20501231000000\",\"fa\":\"\",\"faType\":0,\"id\":\"8200001100\",\"idType\":0,\"oa\":\"18672686569\",\"oaType\":0,\"resumeTime\":\"\",\"status\":0,\"subscribeTime\":\"20180527085644\",\"suspendTime\":\"\"}],\"resultCode\":0}"; logger.info("查询返回信息=====>"+resultstr); JSONObject json = JSONObject.fromObject(resultstr); if (json.getString("resultCode").equals("0")) { JSONArray jsons = json.getJSONArray("orderItems"); for (int i = 0; i < jsons.size(); i++){ //logger.debug("++++++++++++++++++"+jsons.getJSONObject(i).get("id")); //if("8000610900".equals(jsons.getJSONObject(i).get("id"))){ if(spInfo.getVacproductid().equals(jsons.getJSONObject(i).get("id"))){ orderInfo.setOrdertime(jsons.getJSONObject(i).get("effectiveTime").toString()); orderInfo.setEffecttime(jsons.getJSONObject(i).get("effectiveTime").toString()); orderInfo.setStatus(0); orderInfo.setOrderchannel("localQry"); orderInfo.setType(1);//app订购 orderInfo.setOrderstatus(3); //营业厅订购 result = "0"; } } } logger.info("***************"+(result.equals("1")?"vac无订购关系":"vac有订购关系")); } catch (Exception e) { throw new BusinessException("9060", "查询VAC订购关系出现异常!"+e.getMessage(), new String[0]); } return result; } }