254c5eaed9ee6b4846bfe3d0581e0f287f7a6ffc.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package com.chinacreator.videoalliance.order.service;
  2. import java.sql.SQLException;
  3. import java.text.ParseException;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.List;
  7. import net.sf.json.JSONArray;
  8. import net.sf.json.JSONObject;
  9. import org.apache.commons.lang.math.NumberUtils;
  10. import org.apache.commons.lang.time.DateUtils;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.util.StringUtils;
  16. import com.chinacreator.common.exception.BusinessException;
  17. import com.chinacreator.common.util.URLUtil;
  18. import com.chinacreator.common.util.UsermobUtil;
  19. import com.chinacreator.videoalliance.common.bean.AreaInfo;
  20. import com.chinacreator.videoalliance.common.bean.CPInfo;
  21. import com.chinacreator.videoalliance.common.dao.DictionaryDao;
  22. import com.chinacreator.videoalliance.common.util.AreaUtil;
  23. import com.chinacreator.videoalliance.common.util.ConfigUtil;
  24. import com.chinacreator.videoalliance.order.bean.OrderInfo;
  25. import com.chinacreator.videoalliance.order.bean.OrderLog;
  26. import com.chinacreator.videoalliance.order.bean.SPInfo;
  27. import com.chinacreator.videoalliance.order.dao.OrderDao;
  28. import com.chinacreator.videoalliance.order.dao.OrderLogDao;
  29. import com.chinacreator.videoalliance.order.dao.OrderSysDao;
  30. import com.chinacreator.videoalliance.order.dao.SPDao;
  31. /**
  32. * vac订购关系查询服务类
  33. * @author xu.zhou
  34. * @date 20190315
  35. */
  36. @Component
  37. public class VacQryService {
  38. private static final Logger logger = LoggerFactory.getLogger(VacQryService.class);
  39. @Autowired
  40. private OrderDao orderDao;
  41. @Autowired
  42. private SPDao spDao;
  43. @Autowired
  44. private DictionaryDao dictionaryDao;
  45. @Autowired
  46. private OrderLogDao orderLogDao;
  47. @Autowired
  48. private OrderSysDao sysDao;
  49. /**
  50. * 检查手机号码
  51. * @param orderInfo
  52. * @throws Exception
  53. */
  54. private void checkUserid(OrderInfo orderInfo) throws Exception {
  55. String userid = orderInfo.getUserid();
  56. if ((StringUtils.isEmpty(userid)) || ("null".equals(userid)))
  57. throw new BusinessException("7002", "手机号码无效", new String[0]);
  58. if (!UsermobUtil.isValid(userid)) {
  59. userid = ConfigUtil.decrypt(userid, orderInfo.getCpid());
  60. userid = ConfigUtil.getUserid(userid, orderInfo.getCpid());
  61. }
  62. if (!UsermobUtil.isValid(userid)) {
  63. throw new BusinessException("7002", "手机号码无效", new String[0]);
  64. }
  65. orderInfo.setUserid(userid);
  66. }
  67. private void checkCpInfo(OrderInfo orderInfo) throws Exception {
  68. CPInfo cpInfo = ConfigUtil.getCPInfo(orderInfo.getCpid(), null);
  69. if ((cpInfo.getStatus() != 0) && (cpInfo.getStatus() != 4)) {
  70. throw new BusinessException("9002", "该业务暂未开放,敬请期待。", new String[0]);
  71. }
  72. }
  73. private SPInfo checkSpInfo(OrderInfo orderInfo) throws Exception {
  74. SPInfo spInfo = getSPInfo(orderInfo);
  75. if (spInfo.getCanorder() == 1 && StringUtils.isEmpty(orderInfo.getOrderchannel())) {
  76. throw new BusinessException("9012", "该产品不支持订购操作", new String[0]);
  77. }
  78. orderInfo.setSpid(spInfo.getSpid());
  79. return spInfo;
  80. }
  81. /**
  82. * 查询VAC订购关系
  83. * @param orderInfo
  84. * @return
  85. * @throws Exception
  86. */
  87. public String qryVacOrder(OrderInfo orderInfo) throws Exception {
  88. //检查手机号码
  89. checkUserid(orderInfo);
  90. //查询CPID
  91. checkCpInfo(orderInfo);
  92. //查询SPID
  93. SPInfo spInfo = checkSpInfo(orderInfo);
  94. String userid = orderInfo.getUserid();
  95. //查询当前是否有订购关系
  96. hasExists(orderInfo);
  97. AreaInfo areaInfo = getAreaInfo(userid);
  98. orderInfo.setProvince(areaInfo.getProvince());
  99. orderInfo.setArea(areaInfo.getArea());
  100. String result = qryVac( orderInfo, spInfo);
  101. if("0".equals(result)){
  102. recOrderRelations(orderInfo);
  103. }
  104. return result;
  105. }
  106. /**
  107. * 获取区域信息
  108. * @param userid
  109. * @return
  110. * @throws Exception
  111. */
  112. private AreaInfo getAreaInfo(String userid) throws Exception {
  113. AreaInfo areaInfo = AreaUtil.getAreaInfoByUserid(userid);
  114. if (areaInfo == null) {
  115. areaInfo = new AreaInfo();
  116. }
  117. return areaInfo;
  118. }
  119. /**
  120. * 获取SP信息
  121. * @param orderInfo
  122. * @return
  123. * @throws Exception
  124. */
  125. private SPInfo getSPInfo(OrderInfo orderInfo) throws Exception {
  126. SPInfo spInfo = null;
  127. if (StringUtils.isEmpty(orderInfo.getSpid()))
  128. spInfo = this.spDao.findDefaultByCP(orderInfo.getCpid());
  129. else {
  130. spInfo = this.spDao.findById(orderInfo.getSpid());
  131. }
  132. if (spInfo == null) {
  133. throw new BusinessException("5305", "产品标识符无效", new String[0]);
  134. }
  135. orderInfo.setSpid(spInfo.getSpid());
  136. orderInfo.setMutex(spInfo.getMutex());
  137. orderInfo.setRelationSp(spInfo.getRelationSp());
  138. orderInfo.setPaytype(spInfo.getPaytype());
  139. orderInfo.setErrorhandle(spInfo.getErrorhandle());
  140. return spInfo;
  141. }
  142. /**
  143. * 判断是否已订购
  144. * @param orderInfo
  145. * @throws Exception
  146. */
  147. private void hasExists(OrderInfo orderInfo) throws Exception {
  148. logger.info("++++++++++++++++++++++++查询是否有已存在的订购关系");
  149. List<OrderInfo> listOrderInfo = orderDao.findByUser(orderInfo.getUserid());
  150. if (listOrderInfo != null && listOrderInfo.size() > 0) {
  151. if (!StringUtils.isEmpty(orderInfo.getMutex())) {
  152. String[] mutexSpids = orderInfo.getMutex().trim().split(",");
  153. for (String mutexSpid : mutexSpids) {
  154. for (OrderInfo oldOrderInfo : listOrderInfo) {
  155. if (oldOrderInfo.getSpid().equals(orderInfo.getSpid())
  156. && oldOrderInfo.getStatus() == 0) {
  157. throw new BusinessException("9010", "已订购此产品", new String[0]);
  158. }
  159. // if (oldOrderInfo.getSpid().equals(mutexSpid)) {
  160. // throw new BusinessException("9011", "您已订购同类产品!", new String[0]);
  161. // }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * 查询VAC如果有订购关系存在,保存到本地订购关系表,及订购日志表
  169. * @param orderInfo
  170. * @throws Exception
  171. */
  172. private void recOrderRelations(OrderInfo orderInfo) throws Exception {
  173. //logger.info("++++++++++++++++++++++入库订购关系");
  174. String errorcode = "0";
  175. String errorinfo = "";
  176. try {
  177. this.hasExists( orderInfo);
  178. String currentTime = this.sysDao.getCurrentTime();
  179. if (orderInfo.getOrdertime() == null) {
  180. orderInfo.setOrdertime(currentTime);
  181. }
  182. if (orderInfo.getEffecttime() == null) {
  183. orderInfo.setEffecttime(currentTime);
  184. }
  185. this.orderDao.order(orderInfo);
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. errorcode = "8000";
  189. errorinfo ="系统忙";
  190. if ((e instanceof BusinessException)) {
  191. errorcode = ((BusinessException) e).getCode();
  192. errorinfo = ((BusinessException) e).getMessage();
  193. if(errorinfo.length()>200){
  194. errorinfo = errorinfo.substring(0, 200);
  195. }
  196. }
  197. if(!errorcode.equals("9999")){
  198. throw e;
  199. }
  200. } finally {
  201. saveLog(orderInfo, errorcode, errorinfo);
  202. }
  203. }
  204. public String getEndTime(String endTimeStr) throws ParseException {
  205. Calendar calendar = Calendar.getInstance();
  206. Date date = DateUtils.parseDate(endTimeStr.substring(0, 8), new String[] { "yyyyMMdd" });
  207. calendar.setTime(date);
  208. String day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + "";
  209. String year = calendar.get(Calendar.YEAR) + "";
  210. String month = calendar.get(Calendar.MONTH) + 1 < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1)
  211. : calendar.get(Calendar.MONTH) + 1 + "";
  212. return year + month + day + "235959";
  213. }
  214. /**
  215. * 写订购日志
  216. * @param orderInfo
  217. * @param errorcode
  218. * @param errorinfo
  219. */
  220. public void saveLog(OrderInfo orderInfo, String errorcode, String errorinfo) {
  221. OrderLog orderLog = new OrderLog();
  222. orderLog.setApptype(orderInfo.getApptype());
  223. orderLog.setArea(orderInfo.getArea());
  224. orderLog.setChannel(orderInfo.getOrderchannel());
  225. orderLog.setOrderstatus(orderInfo.getOrderstatus());
  226. orderLog.setStatus(orderInfo.getStatus());
  227. orderLog.setCpid(orderInfo.getCpid());
  228. orderLog.setIsexperience(orderInfo.getIsexperience());
  229. orderLog.setOrdertype(orderInfo.getOrdertype() + "");
  230. orderLog.setProvince(orderInfo.getProvince());
  231. orderLog.setArea(orderInfo.getArea());
  232. orderLog.setSpid(orderInfo.getSpid());
  233. orderLog.setUserid(orderInfo.getUserid());
  234. orderLog.setErrorcode(errorcode);
  235. orderLog.setErrorinfo(errorinfo);
  236. if(!orderInfo.getTimes().isEmpty()){
  237. orderLog.setTimes((System.currentTimeMillis()-NumberUtils.toLong(orderInfo.getTimes()))+"");
  238. }
  239. this.orderLogDao.addOrderLog(orderLog);
  240. }
  241. /**
  242. * 调VAC接口查询订购信息
  243. * @param orderInfo
  244. * @param spInfo
  245. * @param cpInfo
  246. * @return
  247. * @throws Exception
  248. */
  249. private String qryVac(OrderInfo orderInfo, SPInfo spInfo)throws Exception{
  250. String result = "-1";
  251. logger.info("***********进入订购关系判断*************");
  252. try {
  253. String url = dictionaryDao.getValue("vacQuery");
  254. //外网地址
  255. //url = "http://127.0.0.52:9999/vacquery/vacQuery.do";
  256. logger.info("++++++++资费ID:"+spInfo.getVacproductid()+"+++++++++++调用url:"+url);
  257. url = url+"?userId="+orderInfo.getUserid();
  258. String resultstr = URLUtil.get(url);
  259. //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}";
  260. logger.info("查询返回信息=====>"+resultstr);
  261. JSONObject json = JSONObject.fromObject(resultstr);
  262. if (json.getString("resultCode").equals("0")) {
  263. JSONArray jsons = json.getJSONArray("orderItems");
  264. for (int i = 0; i < jsons.size(); i++){
  265. //logger.debug("++++++++++++++++++"+jsons.getJSONObject(i).get("id"));
  266. //if("8000610900".equals(jsons.getJSONObject(i).get("id"))){
  267. if(spInfo.getVacproductid().equals(jsons.getJSONObject(i).get("id"))){
  268. orderInfo.setOrdertime(jsons.getJSONObject(i).get("effectiveTime").toString());
  269. orderInfo.setEffecttime(jsons.getJSONObject(i).get("effectiveTime").toString());
  270. orderInfo.setStatus(0);
  271. orderInfo.setOrderchannel("localQry");
  272. orderInfo.setType(1);//app订购
  273. orderInfo.setOrderstatus(3); //营业厅订购
  274. result = "0";
  275. }
  276. }
  277. }
  278. logger.info("***************"+(result.equals("1")?"vac无订购关系":"vac有订购关系"));
  279. } catch (Exception e) {
  280. throw new BusinessException("9060", "查询VAC订购关系出现异常!"+e.getMessage(), new String[0]);
  281. }
  282. return result;
  283. }
  284. }