123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- package com.chinacreator.videoalliance.order.service;
- import java.net.URLEncoder;
- import java.text.ParseException;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- 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.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.OrderInfo;
- import com.chinacreator.videoalliance.order.bean.OrderLog;
- import com.chinacreator.videoalliance.order.bean.SPInfo;
- import com.chinacreator.videoalliance.order.bean.TdYoutuOrderRecBean;
- 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.dao.YoutuDao;
- 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;
- @Component
- public class YoutuOrderService {
-
- private static Logger log = Logger.getLogger("orderError");
-
- private static Logger logger = Logger.getLogger(YoutuOrderService.class);
- @Autowired
- private OrderDao orderDao;
- @Autowired
- private BlackWhiteDao blackWhiteDao;
- @Autowired
- private SPDao spDao;
- @Autowired
- private DictionaryDao dictionaryDao;
- @Autowired
- private ProvinceControlDao provinceControlDao;
-
- @Autowired
- private YoutuDao youtuDao;
- /**
- * 验证及解密手机号码
- * @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 (!UsermobUtil.isValid(userid)) {
- userid = ConfigUtil.decrypt(userid, orderInfo.getCpid());
- userid = ConfigUtil.getUserid(userid, orderInfo.getCpid());
- }
- if (!UsermobUtil.isValid(userid)) {
- throw new BusinessException("9052", "手机号码无效", new String[0]);
- }
- orderInfo.setUserid(userid);
- }
-
- /**
- * CPID验证
- * @param orderInfo
- * @throws Exception
- */
- public void checkCpInfo(OrderInfo orderInfo) throws Exception {
- CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null);
- if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) {
- throw new BusinessException("9053", "该业务暂未开放,敬请期待。", new String[0]);
- }
- }
-
- /**
- * 验证签名是否有效
- * @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 = youtuDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
- String pwd = cpSpBean.get("NETPWD").toString();
- String localSign = MD5.MD5Encode(orderInfo.getOrderid() + orderInfo.getType() + orderInfo.getCpid()
- + orderInfo.getSpid() + deuserid + pwd + orderInfo.getTimestamp());
- if (!localSign.equals(orderInfo.getSignature())) {
- throw new BusinessException("9054", "signature校验失败");
- }
- }
-
- /**
- * 验证SPID
- * @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("9055", "该产品不支持订购操作", new String[0]);
- }
- orderInfo.setSpid(spInfo.getSpid());
- }
- /**
- * 订购前验证
- * @param orderInfo
- * @throws Exception
- */
- public void checkOrder(OrderInfo orderInfo, String deuserid) throws Exception {
- checkSignChannel(orderInfo,deuserid);
- checkUserid(orderInfo);
- checkCpInfo(orderInfo);
- checkSpInfo(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());
- CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), areaInfo.getProvince());
- if (cpInfo == null) {
- throw new BusinessException("9011", "CP标识无效", new String[0]);
- }
- String allowOrder = this.dictionaryDao.getValue("allowOrder");
- if (("1".equals(allowOrder)) || (areaInfo.getStatus() == 1)) {
- throw new BusinessException("9012", "该业务暂未开放,敬请期待。", new String[0]);
- }
- if ((areaInfo.getProvince() != null) && (this.provinceControlDao.isNotOpen(areaInfo.getProvince())))
- throw new BusinessException("9013", "您号码的归属地{0}暂未开放该业务,敬请期待。", new String[] { areaInfo.getProvince() });
- if(this.orderDao.isOpen(orderInfo.getSpid(),orderInfo.getCpid(),orderInfo.getProvince())){ // status 为2 表示省份尚未开通
- throw new BusinessException("9014", "省份尚未开通此业务,敬请期待!");
- }
- }
- /**
- * 获取手机号码的地市信息
- * @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;
- }
- 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("9015", "产品标识符无效", 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;
- }
-
- private TdYoutuOrderRecBean getOrderRecBean(OrderInfo orderInfo) throws Exception{
- TdYoutuOrderRecBean bean = new TdYoutuOrderRecBean();
- bean.setId(youtuDao.generateID());
- bean.setArea(orderInfo.getArea());
- bean.setBusitype(orderInfo.getType()+"");
- bean.setCpid(orderInfo.getCpid());
- bean.setSpid(orderInfo.getSpid());
- bean.setOrderid(orderInfo.getOrderid());
- bean.setProvince(orderInfo.getProvince());
- bean.setUserid(orderInfo.getUserid());
- bean.setVipstatus("1");
- return bean;
- }
-
- /**
- * 检查业务
- * @param orderInfo
- * @return
- */
- public String orderBusi(OrderInfo orderInfo)throws Exception{
- String errorcode = "-1";
- String errorinfo = "";
- String result = "-1";
- TdYoutuOrderRecBean bean = getOrderRecBean(orderInfo);
- try {
- orderInfo.setType(0); //设置为订购
- // if(!this.valiDate()){
- // throw new BusinessException("9007", "不在业务办理期限", new String[0]);
- // }
-
- if(this.hasDupliOrderid(orderInfo)){//判断是否重复订单ID
- throw new BusinessException("9001", "重复订单,不处理", new String[0]);
- }
-
- // if(this.hasOrder(orderInfo)){//订购关系表已存在订购记录
- // throw new BusinessException("9006", "重复订购,不处理", new String[0]);
- // }
-
- if(this.hasMutual(orderInfo)){//已办理互斥业务
- throw new BusinessException("9003","已办理互斥业务,办理失败", new String[0]);
- }
-
- String newEndtime = youtuDao.curr31Day();
- //查询本地订购关系表当前是否有已生效的订购关系
- Map reMap = this.hasEffect(orderInfo);
- if((Boolean)reMap.get("hasEffect")){ //存在有效订购关系
- Map currOrderInfo = (Map)reMap.get("currOrderInfo");
- newEndtime = youtuDao.endtime31Day(currOrderInfo.get("ENDTIME").toString());
- }
- bean.setResultcode("9555");
- bean.setResultinfo("正在赠送会员");
- youtuDao.addOrderRec(bean); //添加订购记录
- String sendVipRes = sendVip(orderInfo); //赠送会员
- //小毛会更新vipstatus值,不以接口返回的为准
- sendVipRes = youtuDao.getVipstatus(orderInfo.getOrderid());
- if("0".equals(sendVipRes)){//赠送成功
- String currtime = youtuDao.getCurrentTime();
- orderInfo.setEndtime(newEndtime);
- orderInfo.setOrdertime(currtime);
- orderInfo.setEffecttime(currtime);
- bean.setVipstatus("0");
- youtuDao.order(orderInfo);
- }else{
- throw new BusinessException("9002","赠送会员未成功", new String[0]);
- }
- result = "0";
- errorcode = "0";
- errorinfo = "成功";
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e);
- 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 {
- bean.setResultcode(errorcode);
- bean.setResultinfo(errorinfo);
- youtuDao.addOrderRec(bean); //添加订购记录
- saveLog(orderInfo, errorcode, errorinfo);
- }
- return result;
- }
-
- /**
- * 赠送会员
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private String sendVip(OrderInfo orderInfo)throws Exception{
- String resultcode = "-1";
- try {
- //{SPID=1167, MOBTYPE=0, SPCODE=88226, MUTEXTYPE=0, VACPRODUCTID=8000654300, MUTEX=21156, TYPE=1, NETPWD=ea044e50, CPID=youtu}
- Map cpSpBean = youtuDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
- //http://114.255.201.238:8090/video-activity/youkuHX
- String vipurl = this.dictionaryDao.getValue("youtuVipUrl") ;
- String cpid = orderInfo.getCpid();
- String spid = orderInfo.getSpid();
- String orderid = orderInfo.getOrderid();
- String userid = orderInfo.getUserid();
- String pwd = cpSpBean.get("NETPWD").toString();
- userid = DESUtil.encode(userid, pwd);
- String timestamp = (System.currentTimeMillis() / 1000) + "";
- //MD5(orderid+userid+timestamp+pwd)转换为十六进制ASCII 码字符串,共32 个字符,全小写,userid= Des(手机号码,pwd)
- 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";
- logger.info("vipurl: "+vipurl);
- //http://114.255.201.228:86/activity/youkuHX?userid=6KctCdsp977A%2FFBLeuKYPQ%3D%3D&orderid=1bfba331-cc63-4129-831a-cd1d3179415d&cpid=youtu&spid=1168×tamp=1559723577&signature=00daf9dc938b8f2ee241cf35c5db485c&apptype=2
- String result = URLUtil.get(vipurl,10*1000); //调赠送会员接口,超时时间设置为10秒
- logger.info("活动会员赠送结果" +orderInfo.getUserid()+","+result);
- Map<?,?> map = JsonUtil.jsonToMap(result);
- resultcode = (String)map.get("resultcode");
- if(resultcode.equals("0")){
- logger.info("赠送成功");
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.error("userid: "+orderInfo.getUserid()+"赠送会员失败,"+e);
- //throw new BusinessException("9004","赠送会员出现异常,"+e.getMessage(), new String[0]);
- }
- return resultcode;
- }
-
- /**
- * 查询本地订购关系表是否存在订购记录
- * @param orderInfo
- * @return
- */
- private boolean hasOrder(OrderInfo orderInfo) throws Exception{
- boolean result = false;
- try {
- //查询用户本地订购关系表未失效的订购数据
- Map currOrderInfo = youtuDao.findByUserAndSpid(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
- if(currOrderInfo != null && currOrderInfo.size() > 0){//本地有订购关系
- result = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.error("查询订购关系出现异常,"+e.getMessage());
- throw new BusinessException("9020", "查询订购关系出现异常", new String[0]);
- }
- return result;
- }
-
- /**
- * 查询本地订购关系表当前是否有已生效的订购关系
- * @param orderInfo
- * @return
- * @throws Exception
- */
- private Map hasEffect(OrderInfo orderInfo) throws Exception{
- Map reMap = new HashMap();
- boolean hasEffect = false;
- Map currOrderInfo = null;
- try{
- String currentTime = youtuDao.getCurrentTime();
- //查询用户本地订购关系表未失效的订购数据
- currOrderInfo = youtuDao.findByUserAndSpid(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
- if(currOrderInfo != null){//本地有订购关系
- if(Long.parseLong(currOrderInfo.get("ENDTIME").toString()) >= Long.parseLong(currentTime)){ //结束时间大于或等于当前时间,订购关系有效
- hasEffect = true;
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- log.error("查询订购关系出现异常,"+e.getMessage());
- throw new BusinessException("9020", "查询订购关系出现异常", new String[0]);
- }
- reMap.put("hasEffect", hasEffect);
- reMap.put("currOrderInfo", currOrderInfo);
- return reMap;
- }
-
- /**
- * 判断业务是否互斥
- * @param orderInfo
- * @throws Exception
- */
- private boolean hasMutual(OrderInfo orderInfo) throws Exception{
- boolean result = false;
- try {
- List<OrderInfo> listOrderInfo = youtuDao.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.getSpid().equals(mutexSpid)) {
- result = true;
- break;
- }
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("查询互斥订购关系出现异常,"+e.getMessage());
- e.printStackTrace();
- throw new BusinessException("9021", "查询互斥订购关系出现异常,"+e.getMessage(), new String[0]);
- }
- return result;
- }
-
- /**
- * 判断当前时间是否在指定时间内
- * @return
- */
- private boolean valiDate(){
- boolean result = false;
- try{
- final Date date = new Date();
- //业务办理日期 6月10-6月30日
- String starttime = null;
- String endtime = null;
- String youtuBackDate = dictionaryDao.getValue("youtuBackDate");
- if(youtuBackDate != null && youtuBackDate.split("~").length == 2){
- starttime = youtuBackDate.split("~")[0];
- endtime = youtuBackDate.split("~")[1];
- }
- if (starttime != null && endtime != null && NumberUtils.toLong(DateFormatUtils.format(date, "yyyyMMddHHmmss")) >= Long.parseLong(starttime)
- && NumberUtils.toLong(DateFormatUtils.format(date, "yyyyMMddHHmmss")) <= Long.parseLong(endtime)) {
- result = true;
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return result;
- }
-
- /**
- * 判断是否有相同订单
- * @param orderInfo
- * @return true是,false否
- */
- private boolean hasDupliOrderid(OrderInfo orderInfo) throws Exception{
- boolean result = false;
- try {
- List<TdYoutuOrderRecBean> list = youtuDao.findOrderRecByOrderid(orderInfo.getOrderid());
- if(list != null && list.size() > 0){
- for(TdYoutuOrderRecBean bean : list){
- if(orderInfo.getOrderid().equals(bean.getOrderid())){
- result = true;
- break;
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.error("判断是否有相同订单出现异常,"+e.getMessage());
- throw new BusinessException("9022", "查判断是否有相同订单出现异常,"+e.getMessage(), new String[0]);
- }
- return result;
- }
- 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.youtuDao.addOrderLog(orderLog);
- }
- }
|