123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package com.caimei.modules.logistics.web;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.caimei.modules.bulkpurchase.dao.OrderProductDao;
- import com.caimei.modules.bulkpurchase.entity.OrderProduct;
- import com.caimei.modules.logistics.entity.LogisticsInfo;
- import com.caimei.modules.logistics.entity.Router;
- import com.caimei.modules.logistics.service.LogisticsService;
- import com.caimei.modules.opensearch.CoreServiceUitls;
- import com.caimei.utils.Encodes;
- import com.caimei.utils.RandomStringUtil;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import com.thinkgem.jeesite.common.config.Global;
- import com.thinkgem.jeesite.common.utils.StringUtils;
- import com.thinkgem.jeesite.common.web.BaseController;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.collections.MapUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * 物流信息基础控制器
- *
- * @author LG
- * @create 2017-08-08
- **/
- @Controller
- @RequestMapping(value = "${adminPath}/logistics")
- public class LogisticsController extends BaseController{
- @Autowired
- private LogisticsService logisticsService;
- @Autowired
- private OrderProductDao orderProductDao;
- @Autowired
- private CoreServiceUitls coreServiceUitls;
- @ModelAttribute
- public LogisticsInfo get(@RequestParam(required=false) String id) {
- LogisticsInfo entity = null;
- if (StringUtils.isNotBlank(id)){
- entity = logisticsService.get(id);
- }
- if (entity == null){
- entity = new LogisticsInfo();
- }
- return entity;
- }
- /**
- * 物流信息列表
- * @param logisticsInfo
- * @param model
- * @return
- */
- @RequestMapping(value = {"list", ""})
- public String list(LogisticsInfo logisticsInfo, Model model) {
- List<LogisticsInfo> list = logisticsService.findList(logisticsInfo);
- if (CollectionUtils.isNotEmpty(list)) {
- for (LogisticsInfo li : list) {
- String info = li.getInfo();
- if (StringUtils.isNotEmpty(info)) {
- logger.info("物流跟踪信息 list 》》》》" + info);
- li.setRouters(JSONArray.parseArray(info, Router.class));
- }
- }
- }
- model.addAttribute("list", list);
- model.addAttribute("orderId", logisticsInfo.getOrderId());
- model.addAttribute("shopId", logisticsInfo.getShopId());
- return "modules/logistics/list";
- }
- /**
- * 供应商订单商品信息
- * @param orderId
- * @param model
- * @return
- */
- @RequestMapping(value = "orderShopInfo")
- public String orderShopInfo(Integer orderId, Model model) {
- List<Map<String,Object>> maps= Lists.newArrayList();
- List<OrderProduct> bpOrderProducts= orderProductDao.findByOrderIdGroupByShopId(orderId);
- if(CollectionUtils.isNotEmpty(bpOrderProducts)){
- for (OrderProduct b:bpOrderProducts) {
- Map<String,Object> map= Maps.newLinkedHashMap();
- map.put("shopId",b.getShopId());
- map.put("shopName",b.getShopName());
- OrderProduct db=new OrderProduct();
- db.setOrderId(b.getOrderId());
- db.setShopId(b.getShopId());
- List<OrderProduct> list =orderProductDao.findAllList(db);
- map.put("products",list);
- maps.add(map);
- }
- }
- model.addAttribute("maps", maps);
- model.addAttribute("orderId", orderId);
- return "modules/logistics/shopOrderInfo";
- }
- /**
- * 打开添加物流页面
- * @param logisticsInfo
- * @param model
- * @return
- */
- @RequestMapping(value = "logisticsForm")
- public String form(LogisticsInfo logisticsInfo, Model model) {
- model.addAttribute("logisticsInfo", logisticsInfo);
- return "modules/logistics/logisticsForm";
- }
- /**
- * 添加物流信息
- * @param logisticsInfo
- * @param model
- * @param redirectAttributes
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "save")
- public Map<String, Object> save(LogisticsInfo logisticsInfo, Model model, RedirectAttributes redirectAttributes) {
- Map<String, Object> map = Maps.newLinkedHashMap();
- try {
- String result = coreServiceUitls.queryLogisticsGet(logisticsInfo.getNu(), logisticsInfo.getLogisticsCompanyCode(), "");
- Map<String, Object> resultMap = JSONObject.parseObject(result, Map.class);
- String code = MapUtils.getString(resultMap, "code");
- if (StringUtils.equals(code, "0")) {
- Map<String, Object> data = MapUtils.getMap(resultMap, "data");
- String status = MapUtils.getString(data, "status");
- if (StringUtils.equals(status, "200")) {
- logisticsInfo.setLogisticsCompanyCode(MapUtils.getString(data, "com"));
- logisticsInfo.setState(Integer.parseInt(MapUtils.getString(data, "state")));
- logisticsInfo.setInfo(MapUtils.getString(data, "data"));
- logger.info("物流100跟踪 add 》》》》info 信息:" + logisticsInfo.getInfo());
- } else {
- logger.warn("物流100跟踪 add >>>>>>>>>>>"+MapUtils.getString(data, "message"));
- }
- }else{
- logger.warn("物流100跟踪 add >>>>>>>>>>>"+MapUtils.getString(resultMap, "msg"));
- }
- // String url = "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=" + logisticsInfo.getLogisticsCompanyCode() +
- // "&nu=" + logisticsInfo.getNu();
- // logger.info(">>>请求链接:"+url);
- // String result = getLogistics(url);
- // logger.info(">>>请求结果:"+result);
- // Map<String, Object> maps = JSONObject.parseObject(result, Map.class);
- // String error_code = MapUtils.getString(maps, "error_code");
- // if (StringUtils.equals(error_code, "0")) {
- // Map<String, Object> data = MapUtils.getMap(maps, "data");
- // Map<String, Object> info = MapUtils.getMap(data, "info");
- // logisticsInfo.setState(Integer.parseInt(MapUtils.getString(info, "state")));
- // logisticsInfo.setInfo(MapUtils.getString(info, "context"));
- // logger.info("物流跟踪 add 》》》》info 信息:" + logisticsInfo.getInfo());
- // }else{
- // logger.warn("物流跟踪 add >>>>>>>>>>>"+MapUtils.getString(map, "msg"));
- // }
- logisticsInfo.setUpdateDate(new Date());
- logisticsService.save(logisticsInfo);
- map.put("success",true);
- map.put("msg", "添加成功");
- } catch (Exception e) {
- logger.error(e.toString(),e);
- map.put("success",false);
- map.put("msg", "添加失败");
- }
- return map;
- }
- private String getLogistics(String url) {
- StringBuffer result = new StringBuffer();
- BufferedReader in = null;
- try {
- URL realUrl = new URL(url);
- // 打开和URL之间的连接
- URLConnection connection = realUrl.openConnection();
- // 设置通用的请求属性
- connection.setRequestProperty("cookie", "BAIDUID="+ com.caimei.utils.StringUtils.upperCase(RandomStringUtil.getRandomString())+":FG=1");
- connection.setConnectTimeout(5000);
- // 建立实际的连接
- connection.connect();
- // 获取所有响应头字段
- Map<String, List<String>> map = connection.getHeaderFields();
- // 遍历所有的响应头字段
- for (String key : map.keySet()) {
- // System.out.println(key + "--->" + map.get(key));
- }
- // 定义 BufferedReader输入流来读取URL的响应
- in = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- result.append(line);
- }
- } catch (Exception e) {
- logger.error(e.toString(), e);
- }
- // 使用finally块来关闭输入流
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- logger.error(e2.toString(), e2);
- }
- }
- return Encodes.urlDecode(result.toString());
- }
- @RequestMapping(value = "delete")
- public String delete(LogisticsInfo logisticsInfo, RedirectAttributes redirectAttributes) {
- logisticsService.delete(logisticsInfo);
- addMessage(redirectAttributes, "删除成功");
- return "redirect:"+ Global.getAdminPath()+"/logistics/list?orderId="+logisticsInfo.getOrderId()+"&shopId="+logisticsInfo.getShopId();
- }
- }
|