2dd7cdda43692b8606dd4f23521841fb84fbb2d6.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package com.chinacreator.videoalliance.order.service;
  2. import java.net.URLEncoder;
  3. import java.text.ParseException;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import com.chinacreator.common.exception.BusinessException;
  10. import com.chinacreator.common.util.DESUtil;
  11. import com.chinacreator.common.util.MD5;
  12. import com.chinacreator.common.util.URLUtil;
  13. import com.chinacreator.common.util.UsermobUtil;
  14. import com.chinacreator.videoalliance.common.bean.AreaInfo;
  15. import com.chinacreator.videoalliance.common.bean.CPInfo;
  16. import com.chinacreator.videoalliance.common.dao.DictionaryDao;
  17. import com.chinacreator.videoalliance.common.dao.ProvinceControlDao;
  18. import com.chinacreator.videoalliance.common.util.AreaUtil;
  19. import com.chinacreator.videoalliance.common.util.ConfigUtil;
  20. import com.chinacreator.videoalliance.order.bean.OrderInfo;
  21. import com.chinacreator.videoalliance.order.bean.OrderLog;
  22. import com.chinacreator.videoalliance.order.bean.SPInfo;
  23. import com.chinacreator.videoalliance.order.bean.TdYoutuOrderRecBean;
  24. import com.chinacreator.videoalliance.order.dao.BlackWhiteDao;
  25. import com.chinacreator.videoalliance.order.dao.OrderDao;
  26. import com.chinacreator.videoalliance.order.dao.SPDao;
  27. import com.chinacreator.videoalliance.order.dao.YoutuDao;
  28. import com.chinacreator.videoalliance.order.util.JsonUtil;
  29. import org.apache.commons.lang.math.NumberUtils;
  30. import org.apache.commons.lang.time.DateFormatUtils;
  31. import org.apache.commons.lang.time.DateUtils;
  32. import org.apache.log4j.Logger;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Component;
  35. import org.springframework.util.StringUtils;
  36. @Component
  37. public class YoutuOrderService {
  38. private static Logger log = Logger.getLogger("orderError");
  39. private static Logger logger = Logger.getLogger(YoutuOrderService.class);
  40. @Autowired
  41. private OrderDao orderDao;
  42. @Autowired
  43. private BlackWhiteDao blackWhiteDao;
  44. @Autowired
  45. private SPDao spDao;
  46. @Autowired
  47. private DictionaryDao dictionaryDao;
  48. @Autowired
  49. private ProvinceControlDao provinceControlDao;
  50. @Autowired
  51. private YoutuDao youtuDao;
  52. /**
  53. * 验证及解密手机号码
  54. * @param orderInfo
  55. * @throws Exception
  56. */
  57. private void checkUserid(OrderInfo orderInfo) throws Exception {
  58. String userid = orderInfo.getUserid();
  59. if ((StringUtils.isEmpty(userid)) || ("null".equals(userid)))
  60. throw new BusinessException("9051", "手机号码无效", new String[0]);
  61. if (!UsermobUtil.isValid(userid)) {
  62. userid = ConfigUtil.decrypt(userid, orderInfo.getCpid());
  63. userid = ConfigUtil.getUserid(userid, orderInfo.getCpid());
  64. }
  65. if (!UsermobUtil.isValid(userid)) {
  66. throw new BusinessException("9052", "手机号码无效", new String[0]);
  67. }
  68. orderInfo.setUserid(userid);
  69. }
  70. /**
  71. * CPID验证
  72. * @param orderInfo
  73. * @throws Exception
  74. */
  75. public void checkCpInfo(OrderInfo orderInfo) throws Exception {
  76. CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null);
  77. if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) {
  78. throw new BusinessException("9053", "该业务暂未开放,敬请期待。", new String[0]);
  79. }
  80. }
  81. /**
  82. * 验证签名是否有效
  83. * @param orderInfo
  84. * @param deuserid 加密的手机号码
  85. * @throws Exception
  86. */
  87. public void checkSignChannel(OrderInfo orderInfo, String deuserid) throws Exception {
  88. String sign = orderInfo.getSignature();
  89. if (StringUtils.isEmpty(sign)) {
  90. throw new BusinessException("9054", "signature不能为空");
  91. }
  92. Map cpSpBean = youtuDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
  93. String pwd = cpSpBean.get("NETPWD").toString();
  94. String localSign = MD5.MD5Encode(orderInfo.getOrderid() + orderInfo.getType() + orderInfo.getCpid()
  95. + orderInfo.getSpid() + deuserid + pwd + orderInfo.getTimestamp());
  96. if (!localSign.equals(orderInfo.getSignature())) {
  97. throw new BusinessException("9054", "signature校验失败");
  98. }
  99. }
  100. /**
  101. * 验证SPID
  102. * @param orderInfo
  103. * @throws Exception
  104. */
  105. private void checkSpInfo(OrderInfo orderInfo) throws Exception {
  106. SPInfo spInfo = getSPInfo(orderInfo);
  107. if (spInfo.getCanorder() == 1 && StringUtils.isEmpty(orderInfo.getOrderchannel())) {
  108. throw new BusinessException("9055", "该产品不支持订购操作", new String[0]);
  109. }
  110. orderInfo.setSpid(spInfo.getSpid());
  111. }
  112. /**
  113. * 订购前验证
  114. * @param orderInfo
  115. * @throws Exception
  116. */
  117. public void checkOrder(OrderInfo orderInfo, String deuserid) throws Exception {
  118. checkSignChannel(orderInfo,deuserid);
  119. checkUserid(orderInfo);
  120. checkCpInfo(orderInfo);
  121. checkSpInfo(orderInfo);
  122. String userid = orderInfo.getUserid();
  123. //判断用户黑名单
  124. if (this.blackWhiteDao.isBlackUser(userid, orderInfo.getCpid())) {
  125. throw new BusinessException("9010", "用户为黑名单用户", new String[0]);
  126. }
  127. //获取地市信息
  128. AreaInfo areaInfo = getAreaInfo(userid);
  129. orderInfo.setProvince(areaInfo.getProvince());
  130. orderInfo.setArea(areaInfo.getArea());
  131. CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), areaInfo.getProvince());
  132. if (cpInfo == null) {
  133. throw new BusinessException("9011", "CP标识无效", new String[0]);
  134. }
  135. String allowOrder = this.dictionaryDao.getValue("allowOrder");
  136. if (("1".equals(allowOrder)) || (areaInfo.getStatus() == 1)) {
  137. throw new BusinessException("9012", "该业务暂未开放,敬请期待。", new String[0]);
  138. }
  139. if ((areaInfo.getProvince() != null) && (this.provinceControlDao.isNotOpen(areaInfo.getProvince())))
  140. throw new BusinessException("9013", "您号码的归属地{0}暂未开放该业务,敬请期待。", new String[] { areaInfo.getProvince() });
  141. if(this.orderDao.isOpen(orderInfo.getSpid(),orderInfo.getCpid(),orderInfo.getProvince())){ // status 为2 表示省份尚未开通
  142. throw new BusinessException("9014", "省份尚未开通此业务,敬请期待!");
  143. }
  144. }
  145. /**
  146. * 获取手机号码的地市信息
  147. * @param userid
  148. * @return
  149. * @throws Exception
  150. */
  151. private AreaInfo getAreaInfo(String userid) throws Exception {
  152. AreaInfo areaInfo = AreaUtil.getAreaInfoByUserid(userid);
  153. if (areaInfo == null) {
  154. areaInfo = new AreaInfo();
  155. }
  156. return areaInfo;
  157. }
  158. private SPInfo getSPInfo(OrderInfo orderInfo) throws Exception {
  159. SPInfo spInfo = null;
  160. if (StringUtils.isEmpty(orderInfo.getSpid()))
  161. spInfo = this.spDao.findDefaultByCP(orderInfo.getCpid());
  162. else {
  163. spInfo = this.spDao.findById(orderInfo.getSpid());
  164. }
  165. if (spInfo == null) {
  166. throw new BusinessException("9015", "产品标识符无效", new String[0]);
  167. }
  168. orderInfo.setSpid(spInfo.getSpid());
  169. orderInfo.setMutex(spInfo.getMutex());
  170. orderInfo.setRelationSp(spInfo.getRelationSp());
  171. orderInfo.setPaytype(spInfo.getPaytype());
  172. orderInfo.setErrorhandle(spInfo.getErrorhandle());
  173. return spInfo;
  174. }
  175. private TdYoutuOrderRecBean getOrderRecBean(OrderInfo orderInfo) throws Exception{
  176. TdYoutuOrderRecBean bean = new TdYoutuOrderRecBean();
  177. bean.setId(youtuDao.generateID());
  178. bean.setArea(orderInfo.getArea());
  179. bean.setBusitype(orderInfo.getType()+"");
  180. bean.setCpid(orderInfo.getCpid());
  181. bean.setSpid(orderInfo.getSpid());
  182. bean.setOrderid(orderInfo.getOrderid());
  183. bean.setProvince(orderInfo.getProvince());
  184. bean.setUserid(orderInfo.getUserid());
  185. bean.setVipstatus("1");
  186. return bean;
  187. }
  188. /**
  189. * 检查业务
  190. * @param orderInfo
  191. * @return
  192. */
  193. public String orderBusi(OrderInfo orderInfo)throws Exception{
  194. String errorcode = "-1";
  195. String errorinfo = "";
  196. String result = "-1";
  197. TdYoutuOrderRecBean bean = getOrderRecBean(orderInfo);
  198. try {
  199. orderInfo.setType(0); //设置为订购
  200. // if(!this.valiDate()){
  201. // throw new BusinessException("9007", "不在业务办理期限", new String[0]);
  202. // }
  203. if(this.hasDupliOrderid(orderInfo)){//判断是否重复订单ID
  204. throw new BusinessException("9001", "重复订单,不处理", new String[0]);
  205. }
  206. // if(this.hasOrder(orderInfo)){//订购关系表已存在订购记录
  207. // throw new BusinessException("9006", "重复订购,不处理", new String[0]);
  208. // }
  209. if(this.hasMutual(orderInfo)){//已办理互斥业务
  210. throw new BusinessException("9003","已办理互斥业务,办理失败", new String[0]);
  211. }
  212. String newEndtime = youtuDao.curr31Day();
  213. //查询本地订购关系表当前是否有已生效的订购关系
  214. Map reMap = this.hasEffect(orderInfo);
  215. if((Boolean)reMap.get("hasEffect")){ //存在有效订购关系
  216. Map currOrderInfo = (Map)reMap.get("currOrderInfo");
  217. newEndtime = youtuDao.endtime31Day(currOrderInfo.get("ENDTIME").toString());
  218. }
  219. bean.setResultcode("9555");
  220. bean.setResultinfo("正在赠送会员");
  221. youtuDao.addOrderRec(bean); //添加订购记录
  222. String sendVipRes = sendVip(orderInfo); //赠送会员
  223. //小毛会更新vipstatus值,不以接口返回的为准
  224. sendVipRes = youtuDao.getVipstatus(orderInfo.getOrderid());
  225. if("0".equals(sendVipRes)){//赠送成功
  226. String currtime = youtuDao.getCurrentTime();
  227. orderInfo.setEndtime(newEndtime);
  228. orderInfo.setOrdertime(currtime);
  229. orderInfo.setEffecttime(currtime);
  230. bean.setVipstatus("0");
  231. youtuDao.order(orderInfo);
  232. }else{
  233. throw new BusinessException("9002","赠送会员未成功", new String[0]);
  234. }
  235. result = "0";
  236. errorcode = "0";
  237. errorinfo = "成功";
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. log.error("userid:"+orderInfo.getUserid()+"订购出现异常,"+e);
  241. errorcode = "8000";
  242. errorinfo ="系统忙";
  243. if ((e instanceof BusinessException)) {
  244. errorcode = ((BusinessException) e).getCode();
  245. errorinfo = ((BusinessException) e).getMessage();
  246. if(errorinfo.length()>200){
  247. errorinfo = errorinfo.substring(0, 200);
  248. }
  249. }
  250. if(!errorcode.equals("9999")){
  251. throw e;
  252. }
  253. } finally {
  254. bean.setResultcode(errorcode);
  255. bean.setResultinfo(errorinfo);
  256. youtuDao.addOrderRec(bean); //添加订购记录
  257. saveLog(orderInfo, errorcode, errorinfo);
  258. }
  259. return result;
  260. }
  261. /**
  262. * 赠送会员
  263. * @param orderInfo
  264. * @return
  265. * @throws Exception
  266. */
  267. private String sendVip(OrderInfo orderInfo)throws Exception{
  268. String resultcode = "-1";
  269. try {
  270. //{SPID=1167, MOBTYPE=0, SPCODE=88226, MUTEXTYPE=0, VACPRODUCTID=8000654300, MUTEX=21156, TYPE=1, NETPWD=ea044e50, CPID=youtu}
  271. Map cpSpBean = youtuDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
  272. //http://114.255.201.238:8090/video-activity/youkuHX
  273. String vipurl = this.dictionaryDao.getValue("youtuVipUrl") ;
  274. String cpid = orderInfo.getCpid();
  275. String spid = orderInfo.getSpid();
  276. String orderid = orderInfo.getOrderid();
  277. String userid = orderInfo.getUserid();
  278. String pwd = cpSpBean.get("NETPWD").toString();
  279. userid = DESUtil.encode(userid, pwd);
  280. String timestamp = (System.currentTimeMillis() / 1000) + "";
  281. //MD5(orderid+userid+timestamp+pwd)转换为十六进制ASCII 码字符串,共32 个字符,全小写,userid= Des(手机号码,pwd)
  282. String signature = MD5.MD5Encode(orderid + userid + timestamp + pwd );
  283. signature = signature.toLowerCase();
  284. vipurl = vipurl + "?userid=" + URLEncoder.encode(userid, "utf-8")+ "&orderid="+ orderid + "&cpid=" + cpid + "&spid=" + spid + "&timestamp="
  285. + timestamp + "&signature=" + signature+ "&apptype=2";
  286. logger.info("vipurl: "+vipurl);
  287. //http://114.255.201.228:86/activity/youkuHX?userid=6KctCdsp977A%2FFBLeuKYPQ%3D%3D&orderid=1bfba331-cc63-4129-831a-cd1d3179415d&cpid=youtu&spid=1168&timestamp=1559723577&signature=00daf9dc938b8f2ee241cf35c5db485c&apptype=2
  288. String result = URLUtil.get(vipurl,10*1000); //调赠送会员接口,超时时间设置为10秒
  289. logger.info("活动会员赠送结果" +orderInfo.getUserid()+","+result);
  290. Map<?,?> map = JsonUtil.jsonToMap(result);
  291. resultcode = (String)map.get("resultcode");
  292. if(resultcode.equals("0")){
  293. logger.info("赠送成功");
  294. }
  295. } catch (Exception e) {
  296. e.printStackTrace();
  297. log.error("userid: "+orderInfo.getUserid()+"赠送会员失败,"+e);
  298. //throw new BusinessException("9004","赠送会员出现异常,"+e.getMessage(), new String[0]);
  299. }
  300. return resultcode;
  301. }
  302. /**
  303. * 查询本地订购关系表是否存在订购记录
  304. * @param orderInfo
  305. * @return
  306. */
  307. private boolean hasOrder(OrderInfo orderInfo) throws Exception{
  308. boolean result = false;
  309. try {
  310. //查询用户本地订购关系表未失效的订购数据
  311. Map currOrderInfo = youtuDao.findByUserAndSpid(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
  312. if(currOrderInfo != null && currOrderInfo.size() > 0){//本地有订购关系
  313. result = true;
  314. }
  315. } catch (Exception e) {
  316. e.printStackTrace();
  317. log.error("查询订购关系出现异常,"+e.getMessage());
  318. throw new BusinessException("9020", "查询订购关系出现异常", new String[0]);
  319. }
  320. return result;
  321. }
  322. /**
  323. * 查询本地订购关系表当前是否有已生效的订购关系
  324. * @param orderInfo
  325. * @return
  326. * @throws Exception
  327. */
  328. private Map hasEffect(OrderInfo orderInfo) throws Exception{
  329. Map reMap = new HashMap();
  330. boolean hasEffect = false;
  331. Map currOrderInfo = null;
  332. try{
  333. String currentTime = youtuDao.getCurrentTime();
  334. //查询用户本地订购关系表未失效的订购数据
  335. currOrderInfo = youtuDao.findByUserAndSpid(orderInfo.getUserid(),orderInfo.getCpid(),orderInfo.getSpid());
  336. if(currOrderInfo != null){//本地有订购关系
  337. if(Long.parseLong(currOrderInfo.get("ENDTIME").toString()) >= Long.parseLong(currentTime)){ //结束时间大于或等于当前时间,订购关系有效
  338. hasEffect = true;
  339. }
  340. }
  341. }catch(Exception e){
  342. e.printStackTrace();
  343. log.error("查询订购关系出现异常,"+e.getMessage());
  344. throw new BusinessException("9020", "查询订购关系出现异常", new String[0]);
  345. }
  346. reMap.put("hasEffect", hasEffect);
  347. reMap.put("currOrderInfo", currOrderInfo);
  348. return reMap;
  349. }
  350. /**
  351. * 判断业务是否互斥
  352. * @param orderInfo
  353. * @throws Exception
  354. */
  355. private boolean hasMutual(OrderInfo orderInfo) throws Exception{
  356. boolean result = false;
  357. try {
  358. List<OrderInfo> listOrderInfo = youtuDao.findByUser(orderInfo.getUserid());
  359. if (listOrderInfo != null && listOrderInfo.size() > 0) {
  360. if (!StringUtils.isEmpty(orderInfo.getMutex())) {
  361. String[] mutexSpids = orderInfo.getMutex().trim().split(",");
  362. for (String mutexSpid : mutexSpids) {
  363. for (OrderInfo oldOrderInfo : listOrderInfo) {
  364. if (!oldOrderInfo.getSpid().equals(orderInfo.getSpid()) && oldOrderInfo.getSpid().equals(mutexSpid)) {
  365. result = true;
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. } catch (Exception e) {
  373. log.error("查询互斥订购关系出现异常,"+e.getMessage());
  374. e.printStackTrace();
  375. throw new BusinessException("9021", "查询互斥订购关系出现异常,"+e.getMessage(), new String[0]);
  376. }
  377. return result;
  378. }
  379. /**
  380. * 判断当前时间是否在指定时间内
  381. * @return
  382. */
  383. private boolean valiDate(){
  384. boolean result = false;
  385. try{
  386. final Date date = new Date();
  387. //业务办理日期 6月10-6月30日
  388. String starttime = null;
  389. String endtime = null;
  390. String youtuBackDate = dictionaryDao.getValue("youtuBackDate");
  391. if(youtuBackDate != null && youtuBackDate.split("~").length == 2){
  392. starttime = youtuBackDate.split("~")[0];
  393. endtime = youtuBackDate.split("~")[1];
  394. }
  395. if (starttime != null && endtime != null && NumberUtils.toLong(DateFormatUtils.format(date, "yyyyMMddHHmmss")) >= Long.parseLong(starttime)
  396. && NumberUtils.toLong(DateFormatUtils.format(date, "yyyyMMddHHmmss")) <= Long.parseLong(endtime)) {
  397. result = true;
  398. }
  399. }catch(Exception e){
  400. e.printStackTrace();
  401. }
  402. return result;
  403. }
  404. /**
  405. * 判断是否有相同订单
  406. * @param orderInfo
  407. * @return true是,false否
  408. */
  409. private boolean hasDupliOrderid(OrderInfo orderInfo) throws Exception{
  410. boolean result = false;
  411. try {
  412. List<TdYoutuOrderRecBean> list = youtuDao.findOrderRecByOrderid(orderInfo.getOrderid());
  413. if(list != null && list.size() > 0){
  414. for(TdYoutuOrderRecBean bean : list){
  415. if(orderInfo.getOrderid().equals(bean.getOrderid())){
  416. result = true;
  417. break;
  418. }
  419. }
  420. }
  421. } catch (Exception e) {
  422. e.printStackTrace();
  423. log.error("判断是否有相同订单出现异常,"+e.getMessage());
  424. throw new BusinessException("9022", "查判断是否有相同订单出现异常,"+e.getMessage(), new String[0]);
  425. }
  426. return result;
  427. }
  428. public String getEndTime(String endTimeStr) throws ParseException {
  429. Calendar calendar = Calendar.getInstance();
  430. Date date = DateUtils.parseDate(endTimeStr.substring(0, 8), new String[] { "yyyyMMdd" });
  431. calendar.setTime(date);
  432. String day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + "";
  433. String year = calendar.get(Calendar.YEAR) + "";
  434. String month = calendar.get(Calendar.MONTH) + 1 < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1)
  435. : calendar.get(Calendar.MONTH) + 1 + "";
  436. return year + month + day + "235959";
  437. }
  438. /**
  439. * 写订购日志
  440. * @param orderInfo
  441. * @param errorcode
  442. * @param errorinfo
  443. */
  444. public void saveLog(OrderInfo orderInfo, String errorcode, String errorinfo) {
  445. OrderLog orderLog = new OrderLog();
  446. orderLog.setApptype(orderInfo.getApptype());
  447. orderLog.setArea(orderInfo.getArea());
  448. orderLog.setChannel(orderInfo.getOrderchannel());
  449. orderLog.setOrderstatus(orderInfo.getOrderstatus());
  450. orderLog.setStatus(orderInfo.getStatus());
  451. orderLog.setCpid(orderInfo.getCpid());
  452. orderLog.setIsexperience(orderInfo.getIsexperience());
  453. orderLog.setOrdertype(orderInfo.getOrdertype() + "");
  454. orderLog.setProvince(orderInfo.getProvince());
  455. orderLog.setArea(orderInfo.getArea());
  456. orderLog.setSpid(orderInfo.getSpid());
  457. orderLog.setUserid(orderInfo.getUserid());
  458. orderLog.setErrorcode(errorcode);
  459. orderLog.setErrorinfo(errorinfo);
  460. if(!orderInfo.getTimes().isEmpty()){
  461. orderLog.setTimes((System.currentTimeMillis()-NumberUtils.toLong(orderInfo.getTimes()))+"");
  462. }
  463. this.youtuDao.addOrderLog(orderLog);
  464. }
  465. }