订单 图片(一对多关系),地址保存在一个字段中(Json数组对象格式)

订单---->图片(一对多关系),地址保存在一个字段中(Json数组对象格式)
1、前端列表动态添加:
jsp:
2、web后台: @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST) @ResponseBody public Object uploadPhoto(@RequestParam MultipartFile[] sourceFiles, int type, String orderid,Long id) throws IOException { // 更新订单对应的图片 WLOrder wlOrder = new WLOrder(); wlOrder.setOrderid(orderid); List strlist=new ArrayList();//保存在数据库的文件地址,文件与文件用 ’-‘ 相连,格式endport/bucketName/foldname/文件名字。类型 int uploadCount=0; String typeUrl = ""; for (int i = 0; i < sourceFiles.length; i++) { String fileName = sourceFiles[i].getOriginalFilename(); String suffixFileName =fileName.substring(fileName.lastIndexOf(".") + 1); // 获得文件后缀名称 InputStream fileContent = sourceFiles[i].getInputStream(); String url= OSSUploadUtil.uploadFile(fileContent,suffixFileName,orderid,type); if(url!=null){ strlist.add(url); uploadCount++; } System.out.println("-----------"+url); } //拼接多文件字符串地址 List photos=null;//保存在数据库中的地址格式 WLOrder wlo = wlOrderService.findWLOrderById(id); String JsonStr=null; if (0 == type) {// type="金域达交接单"; JsonStr= wlo.getTransferorderphoto(); } else if (1 == type) {// type="金域达运单"; JsonStr=wlo.getWaybillnophoto(); } else if (2 == type) {// type="温控报告"; JsonStr=wlo.getTemperreportphoto(); } if(StringUtils.isEmpty(JsonStr)){//数据库中查询是否有数据 photos=new ArrayList(); }else{ //数据库中已经存在数据,在拼接 JSONArray jsonArray=JSONArray.fromObject(JsonStr); List list2 = JSONArray.toList(jsonArray, new HashMap<>(), new JsonConfig()); photos=list2; } for (int i = 0; i photoAddress=new HashMap<>(); photoAddress.put("id", String.valueOf((photos.size()+i+1))); photoAddress.put("name", strlist.get(i)); //photoAddress.put(String.valueOf(i+1), strlist.get(i)); photos.add(photoAddress); } JSONArray listArray=JSONArray.fromObject(photos); System.out.println("==================------------------------------"+typeUrl); if (0 == type) { // type="金域达交接单"; wlOrder.setTransferorderphoto(listArray.toString()); } else if (1 == type) { // type="金域达运单"; wlOrder.setWaybillnophoto(listArray.toString()); } else if (2 == type) { // type="温控报告"; wlOrder.setTemperreportphoto(listArray.toString()); } else { return renderError("上传失败"); } wlOrderService.updateWLOrderByOrderID(wlOrder); String typeStr = null; return renderSuccess("上传成功文件数量为:"+uploadCount); } //3、列表显示: /** 获取bucket中文件对象 * @author * @date * @param * @return */ @RequestMapping(value = "/getBucketKey", method = RequestMethod.POST) @ResponseBody public Object getBucketKey(String orderid,int type,Long id,String sort, String order) { List photos=null;//保存在数据库中的地址格式 WLOrder wlo = wlOrderService.findWLOrderById(id); String JsonStr=null; if (0 == type) {// type="金域达交接单"; JsonStr= wlo.getTransferorderphoto(); } else if (1 == type) {// type="金域达运单"; JsonStr=wlo.getWaybillnophoto(); } else if (2 == type) {// type="温控报告"; JsonStr=wlo.getTemperreportphoto(); } if(StringUtils.isEmpty(JsonStr)){//数据库中查询是否有数据 photos=new ArrayList(); }else{ JSONArray jsonArray=JSONArray.fromObject(JsonStr); List list2 = JSONArray.toList(jsonArray, new HashMap<>(), new JsonConfig()); photos=list2; } List> res= photos; return res; } //4、删除: /*** 删除操作 * @param objectKey * @param type * @return */ @RequestMapping(value="/delPhoto",method=RequestMethod.GET) @ResponseBody public Object delPhoto(String objectKey,int type,Long id,String orderid){ boolean res= OSSUploadUtil.deleteFile(objectKey, type); if(!res){ return renderError("删除失败"); } List photos=null;//保存在数据库中的地址格式 WLOrder wlo = wlOrderService.findWLOrderById(id); String JsonStr=null; if (0 == type) {// type="金域达交接单"; JsonStr= wlo.getTransferorderphoto(); } else if (1 == type) {// type="金域达运单"; JsonStr=wlo.getWaybillnophoto(); } else if (2 == type) {// type="温控报告"; JsonStr=wlo.getTemperreportphoto(); } JSONArray jsonArray=JSONArray.fromObject(JsonStr); //List list2 = JSONArray.toList(jsonArray, new HashMap<>(), new JsonConfig()); int index=-1; for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i); //JSONObject jsonObject2 = jsonObject.discard("name"); String str= (String) jsonObject.get("name"); //jsonArray1.add(jsonObject2); if(objectKey.equals(str)){ index=i; } } jsonArray.remove(index); WLOrder wlOrder = new WLOrder(); wlOrder.setOrderid(orderid); if (0 == type) { // type="金域达交接单"; wlOrder.setTransferorderphoto(jsonArray.toString()); } else if (1 == type) { // type="金域达运单"; wlOrder.setWaybillnophoto(jsonArray.toString()); } else if (2 == type) { // type="温控报告"; wlOrder.setTemperreportphoto(jsonArray.toString()); } wlOrderService.updateWLOrderByOrderID(wlOrder); return renderSuccess(); }


你可能感兴趣的:(web,前端)