package com.chinacreator.videoalliance.order.service; 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.Base64; 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.dao.SysDao; 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.BlackWhiteDao; import com.chinacreator.videoalliance.order.dao.OrderDao; import com.chinacreator.videoalliance.order.dao.OrderLogDao; import com.chinacreator.videoalliance.order.dao.SPDao; @Component public class ExperienceOrderService { @Autowired private OrderLogDao orderLogDao; @Autowired private BlackWhiteDao blackWhiteDao; @Autowired private SPDao spDao; @Autowired private DictionaryDao dictionaryDao; @Autowired private SysDao sysDao; @Autowired private ProvinceControlDao provinceControlDao; @Autowired private OrderDao orderDao; 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 checkSpInfo(OrderInfo orderInfo) throws Exception { SPInfo spInfo = getSPInfo(orderInfo); if (spInfo.getCanorder() == 1) { throw new BusinessException("9012", "该产品不支持订购操作", new String[0]); } if (orderInfo.getOrdertype() == 1) { if (spInfo.getType() != 0) { throw new BusinessException("5305", "产品标识符无效", new String[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 if (orderInfo.getOrdertype() == 0) { if (spInfo.getType() == 0) throw new BusinessException("5305", "产品标识符无效", new String[0]); } else { throw new BusinessException("8002", "{0}参数无效", new String[] { "ordertype" }); } orderInfo.setSpid(spInfo.getSpid()); } public void checkOrder(OrderInfo orderInfo) throws Exception { checkUserid(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() }); } 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()); return spInfo; } private AreaInfo getAreaInfo(String userid) throws Exception { AreaInfo areaInfo = AreaUtil.getAreaInfoByUserid(userid); if (areaInfo == null) { areaInfo = new AreaInfo(); } return areaInfo; } public void order(OrderInfo orderInfo) throws Exception { String errorcode = "0"; String errorinfo = ""; try { if (!this.orderDao.hasValid(orderInfo.getUserid(), orderInfo.getCpid(), orderInfo.getSpid())) { if (!this.blackWhiteDao.isWhiteUser(orderInfo.getUserid(), orderInfo.getCpid())) { CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null); if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) { throw new BusinessException("9002", "该业务暂未开放,敬请期待。", new String[0]); } } String currentTime = this.sysDao.getCurrentTime(); orderInfo.setOrdertime(currentTime); if (orderInfo.getEffecttime() == null) { orderInfo.setEffecttime(currentTime); } orderInfo.setOrderstatus(7); orderInfo.setIsexperience(1); orderInfo.setShowstatus("0"); this.orderDao.order(orderInfo); }else{ throw new BusinessException("9002", "该业务还在有效期内,过期后请重新订购", new String[0]); } } catch (Exception e) { errorcode = "8000"; if ((e instanceof BusinessException)) { errorcode = ((BusinessException)e).getCode(); } throw e; } finally { saveLog(orderInfo, errorcode, errorinfo); } } public void saveLog(OrderInfo orderInfo, String errorcode, String errorinfo) { OrderLog orderLog = new OrderLog(); System.out.println("orderInfo==null=======>"+(orderInfo==null)); 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); this.orderLogDao.addOrderLog(orderLog); } }