LogisticsController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.caimei.modules.logistics.web;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.caimei.modules.bulkpurchase.dao.OrderProductDao;
  5. import com.caimei.modules.bulkpurchase.entity.OrderProduct;
  6. import com.caimei.modules.logistics.entity.LogisticsInfo;
  7. import com.caimei.modules.logistics.entity.Router;
  8. import com.caimei.modules.logistics.service.LogisticsService;
  9. import com.caimei.modules.opensearch.CoreServiceUitls;
  10. import com.caimei.utils.Encodes;
  11. import com.caimei.utils.RandomStringUtil;
  12. import com.google.common.collect.Lists;
  13. import com.google.common.collect.Maps;
  14. import com.thinkgem.jeesite.common.config.Global;
  15. import com.thinkgem.jeesite.common.utils.StringUtils;
  16. import com.thinkgem.jeesite.common.web.BaseController;
  17. import org.apache.commons.collections.CollectionUtils;
  18. import org.apache.commons.collections.MapUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.ui.Model;
  22. import org.springframework.web.bind.annotation.ModelAttribute;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RequestParam;
  25. import org.springframework.web.bind.annotation.ResponseBody;
  26. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  27. import java.io.BufferedReader;
  28. import java.io.InputStreamReader;
  29. import java.net.URL;
  30. import java.net.URLConnection;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * 物流信息基础控制器
  36. *
  37. * @author LG
  38. * @create 2017-08-08
  39. **/
  40. @Controller
  41. @RequestMapping(value = "${adminPath}/logistics")
  42. public class LogisticsController extends BaseController{
  43. @Autowired
  44. private LogisticsService logisticsService;
  45. @Autowired
  46. private OrderProductDao orderProductDao;
  47. @Autowired
  48. private CoreServiceUitls coreServiceUitls;
  49. @ModelAttribute
  50. public LogisticsInfo get(@RequestParam(required=false) String id) {
  51. LogisticsInfo entity = null;
  52. if (StringUtils.isNotBlank(id)){
  53. entity = logisticsService.get(id);
  54. }
  55. if (entity == null){
  56. entity = new LogisticsInfo();
  57. }
  58. return entity;
  59. }
  60. /**
  61. * 物流信息列表
  62. * @param logisticsInfo
  63. * @param model
  64. * @return
  65. */
  66. @RequestMapping(value = {"list", ""})
  67. public String list(LogisticsInfo logisticsInfo, Model model) {
  68. List<LogisticsInfo> list = logisticsService.findList(logisticsInfo);
  69. if (CollectionUtils.isNotEmpty(list)) {
  70. for (LogisticsInfo li : list) {
  71. String info = li.getInfo();
  72. if (StringUtils.isNotEmpty(info)) {
  73. logger.info("物流跟踪信息 list 》》》》" + info);
  74. li.setRouters(JSONArray.parseArray(info, Router.class));
  75. }
  76. }
  77. }
  78. model.addAttribute("list", list);
  79. model.addAttribute("orderId", logisticsInfo.getOrderId());
  80. model.addAttribute("shopId", logisticsInfo.getShopId());
  81. return "modules/logistics/list";
  82. }
  83. /**
  84. * 供应商订单商品信息
  85. * @param orderId
  86. * @param model
  87. * @return
  88. */
  89. @RequestMapping(value = "orderShopInfo")
  90. public String orderShopInfo(Integer orderId, Model model) {
  91. List<Map<String,Object>> maps= Lists.newArrayList();
  92. List<OrderProduct> bpOrderProducts= orderProductDao.findByOrderIdGroupByShopId(orderId);
  93. if(CollectionUtils.isNotEmpty(bpOrderProducts)){
  94. for (OrderProduct b:bpOrderProducts) {
  95. Map<String,Object> map= Maps.newLinkedHashMap();
  96. map.put("shopId",b.getShopId());
  97. map.put("shopName",b.getShopName());
  98. OrderProduct db=new OrderProduct();
  99. db.setOrderId(b.getOrderId());
  100. db.setShopId(b.getShopId());
  101. List<OrderProduct> list =orderProductDao.findAllList(db);
  102. map.put("products",list);
  103. maps.add(map);
  104. }
  105. }
  106. model.addAttribute("maps", maps);
  107. model.addAttribute("orderId", orderId);
  108. return "modules/logistics/shopOrderInfo";
  109. }
  110. /**
  111. * 打开添加物流页面
  112. * @param logisticsInfo
  113. * @param model
  114. * @return
  115. */
  116. @RequestMapping(value = "logisticsForm")
  117. public String form(LogisticsInfo logisticsInfo, Model model) {
  118. model.addAttribute("logisticsInfo", logisticsInfo);
  119. return "modules/logistics/logisticsForm";
  120. }
  121. /**
  122. * 添加物流信息
  123. * @param logisticsInfo
  124. * @param model
  125. * @param redirectAttributes
  126. * @return
  127. */
  128. @ResponseBody
  129. @RequestMapping(value = "save")
  130. public Map<String, Object> save(LogisticsInfo logisticsInfo, Model model, RedirectAttributes redirectAttributes) {
  131. Map<String, Object> map = Maps.newLinkedHashMap();
  132. try {
  133. String result = coreServiceUitls.queryLogisticsGet(logisticsInfo.getNu(), logisticsInfo.getLogisticsCompanyCode(), "");
  134. Map<String, Object> resultMap = JSONObject.parseObject(result, Map.class);
  135. String code = MapUtils.getString(resultMap, "code");
  136. if (StringUtils.equals(code, "0")) {
  137. Map<String, Object> data = MapUtils.getMap(resultMap, "data");
  138. String status = MapUtils.getString(data, "status");
  139. if (StringUtils.equals(status, "200")) {
  140. logisticsInfo.setLogisticsCompanyCode(MapUtils.getString(data, "com"));
  141. logisticsInfo.setState(Integer.parseInt(MapUtils.getString(data, "state")));
  142. logisticsInfo.setInfo(MapUtils.getString(data, "data"));
  143. logger.info("物流100跟踪 add 》》》》info 信息:" + logisticsInfo.getInfo());
  144. } else {
  145. logger.warn("物流100跟踪 add >>>>>>>>>>>"+MapUtils.getString(data, "message"));
  146. }
  147. }else{
  148. logger.warn("物流100跟踪 add >>>>>>>>>>>"+MapUtils.getString(resultMap, "msg"));
  149. }
  150. // String url = "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=" + logisticsInfo.getLogisticsCompanyCode() +
  151. // "&nu=" + logisticsInfo.getNu();
  152. // logger.info(">>>请求链接:"+url);
  153. // String result = getLogistics(url);
  154. // logger.info(">>>请求结果:"+result);
  155. // Map<String, Object> maps = JSONObject.parseObject(result, Map.class);
  156. // String error_code = MapUtils.getString(maps, "error_code");
  157. // if (StringUtils.equals(error_code, "0")) {
  158. // Map<String, Object> data = MapUtils.getMap(maps, "data");
  159. // Map<String, Object> info = MapUtils.getMap(data, "info");
  160. // logisticsInfo.setState(Integer.parseInt(MapUtils.getString(info, "state")));
  161. // logisticsInfo.setInfo(MapUtils.getString(info, "context"));
  162. // logger.info("物流跟踪 add 》》》》info 信息:" + logisticsInfo.getInfo());
  163. // }else{
  164. // logger.warn("物流跟踪 add >>>>>>>>>>>"+MapUtils.getString(map, "msg"));
  165. // }
  166. logisticsInfo.setUpdateDate(new Date());
  167. logisticsService.save(logisticsInfo);
  168. map.put("success",true);
  169. map.put("msg", "添加成功");
  170. } catch (Exception e) {
  171. logger.error(e.toString(),e);
  172. map.put("success",false);
  173. map.put("msg", "添加失败");
  174. }
  175. return map;
  176. }
  177. private String getLogistics(String url) {
  178. StringBuffer result = new StringBuffer();
  179. BufferedReader in = null;
  180. try {
  181. URL realUrl = new URL(url);
  182. // 打开和URL之间的连接
  183. URLConnection connection = realUrl.openConnection();
  184. // 设置通用的请求属性
  185. connection.setRequestProperty("cookie", "BAIDUID="+ com.caimei.utils.StringUtils.upperCase(RandomStringUtil.getRandomString())+":FG=1");
  186. connection.setConnectTimeout(5000);
  187. // 建立实际的连接
  188. connection.connect();
  189. // 获取所有响应头字段
  190. Map<String, List<String>> map = connection.getHeaderFields();
  191. // 遍历所有的响应头字段
  192. for (String key : map.keySet()) {
  193. // System.out.println(key + "--->" + map.get(key));
  194. }
  195. // 定义 BufferedReader输入流来读取URL的响应
  196. in = new BufferedReader(new InputStreamReader(
  197. connection.getInputStream()));
  198. String line;
  199. while ((line = in.readLine()) != null) {
  200. result.append(line);
  201. }
  202. } catch (Exception e) {
  203. logger.error(e.toString(), e);
  204. }
  205. // 使用finally块来关闭输入流
  206. finally {
  207. try {
  208. if (in != null) {
  209. in.close();
  210. }
  211. } catch (Exception e2) {
  212. logger.error(e2.toString(), e2);
  213. }
  214. }
  215. return Encodes.urlDecode(result.toString());
  216. }
  217. @RequestMapping(value = "delete")
  218. public String delete(LogisticsInfo logisticsInfo, RedirectAttributes redirectAttributes) {
  219. logisticsService.delete(logisticsInfo);
  220. addMessage(redirectAttributes, "删除成功");
  221. return "redirect:"+ Global.getAdminPath()+"/logistics/list?orderId="+logisticsInfo.getOrderId()+"&shopId="+logisticsInfo.getShopId();
  222. }
  223. }