7a5e29467007fd637fd621984e8259b6469367b3.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package com.chinacreator.videoalliance.order.service;
  2. import java.text.ParseException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import com.chinacreator.common.exception.BusinessException;
  9. import com.chinacreator.common.util.MD5;
  10. import com.chinacreator.common.util.UsermobUtil;
  11. import com.chinacreator.videoalliance.common.bean.AreaInfo;
  12. import com.chinacreator.videoalliance.common.bean.CPInfo;
  13. import com.chinacreator.videoalliance.common.util.AreaUtil;
  14. import com.chinacreator.videoalliance.common.util.ConfigUtil;
  15. import com.chinacreator.videoalliance.order.bean.OrderInfo;
  16. import com.chinacreator.videoalliance.order.bean.OrderLog;
  17. import com.chinacreator.videoalliance.order.bean.SPInfo;
  18. import com.chinacreator.videoalliance.order.bean.TdPreOrderidRecBean;
  19. import com.chinacreator.videoalliance.order.dao.SPDao;
  20. import com.chinacreator.videoalliance.order.dao.TcPreDao;
  21. import org.apache.commons.lang.math.NumberUtils;
  22. import org.apache.commons.lang.time.DateUtils;
  23. import org.apache.log4j.Logger;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Component;
  26. import org.springframework.util.StringUtils;
  27. @Component
  28. public class TcPreOrderService {
  29. private static Logger log = Logger.getLogger("orderError");
  30. @Autowired
  31. private SPDao spDao;
  32. @Autowired
  33. private TcPreDao tcPreDao;
  34. /**
  35. * 验证及解密手机号码
  36. * @param orderInfo
  37. * @throws Exception
  38. */
  39. private void checkUserid(OrderInfo orderInfo) throws Exception {
  40. String userid = orderInfo.getUserid();
  41. if ((StringUtils.isEmpty(userid)) || ("null".equals(userid)))
  42. throw new BusinessException("9051", "手机号码无效", new String[0]);
  43. if (!UsermobUtil.isValid(userid)) {
  44. userid = ConfigUtil.decrypt(userid, orderInfo.getCpid());
  45. userid = ConfigUtil.getUserid(userid, orderInfo.getCpid());
  46. }
  47. if (!UsermobUtil.isValid(userid)) {
  48. throw new BusinessException("9052", "手机号码无效", new String[0]);
  49. }
  50. orderInfo.setUserid(userid);
  51. }
  52. /**
  53. * CPID验证
  54. * @param orderInfo
  55. * @throws Exception
  56. */
  57. public void checkCpInfo(OrderInfo orderInfo) throws Exception {
  58. CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null);
  59. if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) {
  60. throw new BusinessException("9053", "该业务暂未开放,敬请期待。", new String[0]);
  61. }
  62. }
  63. /**
  64. * 验证SPID
  65. * @param orderInfo
  66. * @throws Exception
  67. */
  68. private void checkSpInfo(OrderInfo orderInfo) throws Exception {
  69. SPInfo spInfo = getSPInfo(orderInfo);
  70. if (spInfo.getCanorder() == 1 && StringUtils.isEmpty(orderInfo.getOrderchannel())) {
  71. throw new BusinessException("9054", "该产品不支持订购操作", new String[0]);
  72. }
  73. orderInfo.setSpid(spInfo.getSpid());
  74. }
  75. /**
  76. * 验证签名是否有效
  77. * @param orderInfo
  78. * @param deuserid 加密的手机号码
  79. * @throws Exception
  80. */
  81. public void checkSign(OrderInfo orderInfo, String deuserid) throws Exception {
  82. String sign = orderInfo.getSignature();
  83. if (StringUtils.isEmpty(sign)) {
  84. throw new BusinessException("9054", "signature不能为空");
  85. }
  86. Map cpSpBean = tcPreDao.getCpSp(orderInfo.getCpid(),orderInfo.getSpid());
  87. String pwd = cpSpBean.get("NETPWD").toString();
  88. String localSign = MD5.MD5Encode(orderInfo.getOrderid() + orderInfo.getType() + orderInfo.getCpid()
  89. + orderInfo.getSpid() + deuserid + pwd + orderInfo.getTimestamp());
  90. if (!localSign.equals(orderInfo.getSignature())) {
  91. throw new BusinessException("9054", "signature校验失败");
  92. }
  93. }
  94. /**
  95. * 订购前验证
  96. * @param orderInfo
  97. * @param deuserid 原始加密手机号码,用于签名验证
  98. * @throws Exception
  99. */
  100. public void checkOrder(OrderInfo orderInfo, String deuserid) throws Exception {
  101. checkSign(orderInfo,deuserid);
  102. checkUserid(orderInfo);
  103. checkCpInfo(orderInfo);
  104. checkSpInfo(orderInfo);
  105. String userid = orderInfo.getUserid();
  106. //获取地市信息
  107. AreaInfo areaInfo = getAreaInfo(userid);
  108. orderInfo.setProvince(areaInfo.getProvince());
  109. orderInfo.setArea(areaInfo.getArea());
  110. CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), areaInfo.getProvince());
  111. if (cpInfo == null) {
  112. throw new BusinessException("9011", "CP标识无效", new String[0]);
  113. }
  114. if(!tcPreDao.valiBusi(orderInfo)){
  115. throw new BusinessException("9031", "不能办理非指定业务");
  116. }
  117. }
  118. /**
  119. * 获取手机号码的地市信息
  120. * @param userid
  121. * @return
  122. * @throws Exception
  123. */
  124. private AreaInfo getAreaInfo(String userid) throws Exception {
  125. AreaInfo areaInfo = AreaUtil.getAreaInfoByUserid(userid);
  126. if (areaInfo == null) {
  127. areaInfo = new AreaInfo();
  128. }
  129. return areaInfo;
  130. }
  131. /**
  132. * 获取sp信息
  133. * @param orderInfo
  134. * @return
  135. * @throws Exception
  136. */
  137. private SPInfo getSPInfo(OrderInfo orderInfo) throws Exception {
  138. SPInfo spInfo = null;
  139. if (StringUtils.isEmpty(orderInfo.getSpid()))
  140. spInfo = this.spDao.findDefaultByCP(orderInfo.getCpid());
  141. else {
  142. spInfo = this.spDao.findById(orderInfo.getSpid());
  143. }
  144. if (spInfo == null) {
  145. throw new BusinessException("9015", "产品标识符无效", new String[0]);
  146. }
  147. orderInfo.setSpid(spInfo.getSpid());
  148. orderInfo.setMutex(spInfo.getMutex());
  149. orderInfo.setRelationSp(spInfo.getRelationSp());
  150. orderInfo.setPaytype(spInfo.getPaytype());
  151. orderInfo.setErrorhandle(spInfo.getErrorhandle());
  152. return spInfo;
  153. }
  154. /**
  155. * 获取订单关联bean
  156. * @param orderInfo
  157. * @return
  158. * @throws Exception
  159. */
  160. private TdPreOrderidRecBean getTporBean(OrderInfo orderInfo) throws Exception{
  161. TdPreOrderidRecBean tporBean = new TdPreOrderidRecBean();
  162. tporBean.setId(tcPreDao.generateID());
  163. tporBean.setArea(orderInfo.getArea());
  164. tporBean.setOrderid(orderInfo.getOrderid());
  165. tporBean.setProvince(orderInfo.getProvince());
  166. tporBean.setUserid(orderInfo.getUserid());
  167. tporBean.setCpid(orderInfo.getCpid());
  168. tporBean.setSpid(orderInfo.getSpid());
  169. tporBean.setLogid(orderInfo.getReorderid()); //上海权益日志报文记录表ID
  170. if(tporBean.getLogid() == null){
  171. tporBean.setLogid("");
  172. }
  173. if(orderInfo.getOrderchannel() != null){
  174. tporBean.setChannel(orderInfo.getOrderchannel());
  175. }else{
  176. tporBean.setChannel(orderInfo.getCancelchannel());
  177. }
  178. return tporBean;
  179. }
  180. /**
  181. * 办理业务
  182. * @param orderInfo
  183. * @return
  184. */
  185. public String orderBusi(OrderInfo orderInfo)throws Exception{
  186. String errorcode = "0";
  187. String errorinfo = "成功";
  188. String result = "-1";
  189. String orderPreid = tcPreDao.getPreOrderId();
  190. orderInfo.setId(orderPreid);
  191. String currTime = tcPreDao.getCurrTime();
  192. //业务类型:0订购,1退订,2预订购,3预退订,4预订购转换
  193. TdPreOrderidRecBean tporBean = getTporBean(orderInfo);
  194. tporBean.setBusitype(orderInfo.getType()+"");
  195. try {
  196. //畅视2元处理流程:
  197. if(!"115".equals(orderInfo.getSpid())){
  198. if(0 != orderInfo.getType() && 1 != orderInfo.getType()){
  199. throw new BusinessException("80015", "参数不合法,TYPE", new String[0]);
  200. }
  201. }
  202. //有带订购ID的,要判断是否重复订单
  203. if(orderInfo.getOrderid() != null && !"".equals(orderInfo.getId().trim()) && !"null".equals(orderInfo.getOrderid().trim())){
  204. if(this.hasDupliOrderid(orderInfo)){//判断是否重复订单ID
  205. if(orderInfo.getType() == 0 || orderInfo.getType() == 2){
  206. orderInfo.setStatus(0);
  207. }else{
  208. tporBean.setBusitype("1");
  209. orderInfo.setStatus(1);
  210. }
  211. throw new BusinessException("9110", "重复订单,不处理", new String[0]);
  212. }
  213. }
  214. //订购时在判断业务是否互斥
  215. if(0 == orderInfo.getType() && this.hasMutual(orderInfo)){
  216. throw new BusinessException("9117","已办理互斥业务", new String[0]);
  217. }
  218. if(0 == orderInfo.getType() || 2 == orderInfo.getType()){ //订购,预订购
  219. orderInfo.setOrdertime(currTime);
  220. orderInfo.setEffecttime(currTime);
  221. orderInfo.setCancelchannel("");
  222. orderInfo.setStatus(0);
  223. if("1184".equals(orderInfo.getSpid()) && "changshi".equals(orderInfo.getCpid())){
  224. orderInfo.setEndtime(tcPreDao.endtimeMonthLastDay("24"));
  225. }
  226. String hasEffect = this.hasEffect(orderInfo, tporBean);
  227. if("1".equals(hasEffect)){//订购表已是订购状态
  228. throw new BusinessException("9115","已有订购关系,不能重复订购", new String[0]);
  229. }else if("2".equals(hasEffect) && "115".equals(orderInfo.getSpid())){//预订购表已是订购状态,只有广东腾讯2元才判断预订购
  230. throw new BusinessException("9116","已有预订购关系,不能重复订购", new String[0]);
  231. }
  232. tporBean.setOrdertime(currTime);
  233. orderInfo.setStatus(0);
  234. if(0 == orderInfo.getType()){//订购
  235. tporBean.setBusitype("0"); //订购
  236. tcPreDao.order(orderInfo);
  237. }else{//预订购
  238. tporBean.setBusitype("2");//预订购
  239. tporBean.setOrderpreid(orderPreid);
  240. tcPreDao.orderPre(orderInfo);
  241. }
  242. }else if(1 == orderInfo.getType()){//退订
  243. String endtime = tcPreDao.getEndDayOfCurrentMonth();
  244. orderInfo.setCanceltime(currTime);
  245. orderInfo.setOrderchannel("");
  246. orderInfo.setStatus(1);
  247. orderInfo.setEndtime(endtime);
  248. tporBean.setBusitype("1"); //退订
  249. String hasEffect = this.hasEffect(orderInfo, tporBean);
  250. if("0".equals(hasEffect)){//订购表和预订购表无订购状态的数据
  251. throw new BusinessException("9118","未订购,不能退订", new String[0]);
  252. }else if("3".equals(hasEffect)){//订购表和预订购表无订购状态的数据
  253. throw new BusinessException("9118","预订购正在处理,不能退订", new String[0]);
  254. }
  255. tporBean.setCanceltime(currTime);
  256. if("1".equals(hasEffect)){//订购表已是订购状态
  257. tcPreDao.cancelOrder(orderInfo);
  258. }else if("2".equals(hasEffect) && "115".equals(orderInfo.getSpid())){//预订购表已是订购状态,只有广东腾讯才有预退订
  259. tporBean.setBusitype("3"); //预退订
  260. tcPreDao.cancelOrderPre(orderInfo);
  261. }else{//有预订购,但不是广东腾讯2元(SPID不是115)
  262. throw new BusinessException("9118","未订购,不能退订", new String[0]);
  263. }
  264. }
  265. result = "0";
  266. } catch (Exception e) {
  267. e.printStackTrace();
  268. log.error("error=>orderid: "+orderInfo.getOrderid()+". userid: "+orderInfo.getUserid()+",cpid: "+orderInfo.getCpid()+". spid: "+orderInfo.getSpid()+",订购出现异常,"+e);
  269. errorcode = "8000";
  270. errorinfo ="系统忙";
  271. if ((e instanceof BusinessException)) {
  272. errorcode = ((BusinessException) e).getCode();
  273. errorinfo = ((BusinessException) e).getMessage();
  274. if(errorinfo.length()>200){
  275. errorinfo = errorinfo.substring(0, 200);
  276. }
  277. }
  278. if(!errorcode.equals("9999")){
  279. throw e;
  280. }
  281. } finally {
  282. tporBean.setResultcode(errorcode);
  283. tporBean.setResultinfo(errorinfo);
  284. tcPreDao.addOrderRec(tporBean);
  285. tcPreDao.updShInPaInfo(tporBean); //更新上海权益2元报文表(如果可以更新的话)
  286. saveLog(orderInfo, errorcode, errorinfo);
  287. }
  288. return result;
  289. }
  290. /**
  291. * 查询本地订购关系表/预订购表当前是否存在订购状态
  292. * @param orderInfo
  293. * @return 0无订购,1订购关系表有订购,2预订购表有订购
  294. * @throws Exception
  295. */
  296. private String hasEffect(OrderInfo orderInfo, TdPreOrderidRecBean tporBean) throws Exception{
  297. String hasEffect = "0";
  298. //[{ORDERIDA=2019052, AREA=长沙, SPID=1167, PROVINCE=湖南, ID=201905241107578658666, ORDERCHANNEL=t, STATUS=1, ORDERTIME=20190524104129, USERID=18673197465, CPID=youtu}]
  299. //查询用户本地订购关系表未失效的订购数据
  300. List<HashMap> list = tcPreDao.findOrderRelaAll(orderInfo.getUserid());
  301. if(list != null && list.size()>0){//本地有订购关系
  302. for(HashMap hm : list){
  303. if(orderInfo.getCpid().equals(hm.get("CPID")) && orderInfo.getSpid().equals(hm.get("SPID")) && "0".equals(hm.get("STATUS"))){
  304. hasEffect = "1";
  305. break;
  306. }
  307. if(orderInfo.getCpid().equals(hm.get("CPID")) && orderInfo.getSpid().equals(hm.get("SPID")) && "1".equals(hm.get("STATUS")) && hm.get("CANCELTIME") == null){
  308. hasEffect = "1";
  309. break;
  310. }
  311. }
  312. }
  313. if("0".equals(hasEffect)){
  314. //查询预订购表是否有订购状态的数据
  315. list = tcPreDao.findPreOrderRelaAll(orderInfo.getUserid());
  316. if(list != null && list.size()>0){//本地有订购关系
  317. for(HashMap hm : list){
  318. if(orderInfo.getCpid().equals(hm.get("CPID"))
  319. && orderInfo.getSpid().equals(hm.get("SPID"))
  320. && "0".equals(hm.get("STATUS"))
  321. && ("1".equals(hm.get("SYNSTATUS")) ||"2".equals(hm.get("SYNSTATUS")))){ //未同步或正在同步
  322. hasEffect = "2";
  323. if(orderInfo.getType() == 1){ //如果是退订,设置预订购表ID
  324. tporBean.setOrderpreid(hm.get("ID")+"");
  325. if("2".equals(hm.get("SYNSTATUS"))){
  326. hasEffect = "3"; //正在同步,不能退订
  327. }
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. return hasEffect;
  335. }
  336. /**
  337. * 判断业务是否互斥
  338. * @param orderInfo
  339. * @throws Exception
  340. */
  341. private boolean hasMutual(OrderInfo orderInfo) throws Exception{
  342. boolean result = false;
  343. try {
  344. //[{ORDERIDA=2019052, AREA=长沙, SPID=1167, PROVINCE=湖南, ID=201905241107578658666, ORDERCHANNEL=t, STATUS=1, ORDERTIME=20190524104129, USERID=18673197465, CPID=youtu}]
  345. //查询用户本地订购关系表未失效的订购数据
  346. List<HashMap> list = tcPreDao.findOrderRelaAll(orderInfo.getUserid());
  347. if (list != null && list.size() > 0) {
  348. if (!StringUtils.isEmpty(orderInfo.getMutex())) {
  349. String[] mutexSpids = orderInfo.getMutex().trim().split(",");
  350. for (String mutexSpid : mutexSpids) {
  351. for (HashMap hm : list) {
  352. if (!hm.get("SPID").equals(orderInfo.getSpid()) && hm.get("SPID").equals(mutexSpid)
  353. && !"2".equals(hm.get("STATUS"))) {
  354. result = true;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. } catch (Exception e) {
  362. log.error("查询互斥订购关系出现异常,"+e.getMessage());
  363. e.printStackTrace();
  364. throw new BusinessException("9021", "查询互斥订购关系出现异常,"+e.getMessage(), new String[0]);
  365. }
  366. return result;
  367. }
  368. /**
  369. * 判断是否有相同订单
  370. * @param orderInfo
  371. * @return true是,false否
  372. */
  373. private boolean hasDupliOrderid(OrderInfo orderInfo) throws Exception{
  374. boolean result = false;
  375. try {
  376. //[{AREA=长沙, ORDERID=20190524103510000001, ORDERLOGID=1111111111111111, PROVINCE=湖南, ID=201905241043108658661, RESULTINFO=成功, STATUS=0, RESULTCODE=0, ORDERTIME=20190524104125, USERID=18673197465}]
  377. List<HashMap> list = tcPreDao.findOrderId(orderInfo.getOrderid());
  378. System.out.println(list);
  379. if(list != null && list.size() > 0){
  380. for(HashMap bean : list){
  381. if(orderInfo.getOrderid().equals(bean.get("ORDERID"))){
  382. result = true;
  383. break;
  384. }
  385. }
  386. }
  387. } catch (Exception e) {
  388. e.printStackTrace();
  389. log.error("判断是否有相同订单出现异常,"+e.getMessage());
  390. e.printStackTrace();
  391. throw new BusinessException("9022", "查判断是否有相同订单出现异常,"+e.getMessage(), new String[0]);
  392. }
  393. return result;
  394. }
  395. public String getEndTime(String endTimeStr) throws ParseException {
  396. Calendar calendar = Calendar.getInstance();
  397. Date date = DateUtils.parseDate(endTimeStr.substring(0, 8), new String[] { "yyyyMMdd" });
  398. calendar.setTime(date);
  399. String day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + "";
  400. String year = calendar.get(Calendar.YEAR) + "";
  401. String month = calendar.get(Calendar.MONTH) + 1 < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1)
  402. : calendar.get(Calendar.MONTH) + 1 + "";
  403. return year + month + day + "235959";
  404. }
  405. /**
  406. * 写订购日志
  407. * @param orderInfo
  408. * @param errorcode
  409. * @param errorinfo
  410. */
  411. public void saveLog(OrderInfo orderInfo, String errorcode, String errorinfo) {
  412. OrderLog orderLog = new OrderLog();
  413. orderLog.setApptype(orderInfo.getApptype());
  414. orderLog.setArea(orderInfo.getArea());
  415. orderLog.setChannel(orderInfo.getOrderchannel());
  416. orderLog.setOrderstatus(orderInfo.getOrderstatus());
  417. orderLog.setStatus(orderInfo.getStatus());
  418. orderLog.setCpid(orderInfo.getCpid());
  419. orderLog.setIsexperience(orderInfo.getIsexperience());
  420. orderLog.setOrdertype(orderInfo.getOrdertype() + "");
  421. orderLog.setProvince(orderInfo.getProvince());
  422. orderLog.setArea(orderInfo.getArea());
  423. orderLog.setSpid(orderInfo.getSpid());
  424. orderLog.setUserid(orderInfo.getUserid());
  425. orderLog.setErrorcode(errorcode);
  426. orderLog.setErrorinfo(errorinfo);
  427. if(!orderInfo.getTimes().isEmpty()){
  428. orderLog.setTimes((System.currentTimeMillis()-NumberUtils.toLong(orderInfo.getTimes()))+"");
  429. }
  430. this.tcPreDao.addOrderLog(orderLog);
  431. }
  432. }