package com.yc.sdk.shopping.action.workapp;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpSession;
|
|
import org.apache.commons.fileupload.FileItem;
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import com.google.gson.JsonObject;
|
import com.yc.action.BaseAction;
|
import com.yc.entity.DataSourceEntity;
|
import com.yc.entity.attachment.AttachmentEntity;
|
import com.yc.entity.attachment.AttachmentWhereEntity;
|
import com.yc.multiData.MultiDataSource;
|
import com.yc.multiData.SpObserver;
|
import com.yc.sdk.shopping.action.api.ShopCcCode;
|
import com.yc.sdk.shopping.entity.SettingEntity;
|
import com.yc.sdk.shopping.entity.ShopCcCodeEntity;
|
import com.yc.sdk.shopping.service.SettingIfc;
|
import com.yc.sdk.shopping.service.uploadimage.UploadImageIfc;
|
import com.yc.sdk.shopping.util.SettingKey;
|
import com.yc.utils.SessionKey;
|
|
@Controller
|
public class BusinessItem extends BaseAction{
|
File tempPathFile;
|
@Autowired
|
UploadImageIfc uploadImageIfc ;
|
@Autowired
|
SettingIfc settingIfc ;
|
@RequestMapping("/shopping/newbusiness.do")
|
public void newBusiness(HttpServletRequest request, HttpServletResponse response) {
|
HttpSession session = request.getSession();
|
//String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
|
String hostUrl = SettingKey.getHostUrl(request) ;
|
//将微信corpid组装成url
|
String wxQueryString = SettingKey.getQueryStringByWx(request);
|
boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
|
//String openId = request.getParameter(SessionKey.WEIXIN_OPENID);
|
String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? ""
|
: (String) session.getAttribute(SessionKey.WEIXIN_OPENID) ) ;
|
String userCode = (session.getAttribute(SessionKey.USERCODE) == null ? ""
|
: (String)session.getAttribute(SessionKey.USERCODE));
|
|
|
//String uploadPath = request.getSession().getServletContext().getRealPath("/")+ "uploads/"; // 上传文件的目录
|
String uploadPath=System.getProperty("java.io.tmpdir");
|
JsonObject json = new JsonObject();
|
JsonObject errJson = new JsonObject();
|
try {
|
// Create a factory for disk-based file items
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
// Set factory constraints
|
factory.setSizeThreshold(40960); // 设置缓冲区大小,这里是4kb
|
factory.setRepository(tempPathFile);// 设置缓冲区目录
|
|
// Create a new file upload handler
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
|
// Set overall request size constraint
|
//upload.setSizeMax(419430400); // 设置最大文件尺寸,这里是4MB
|
|
@SuppressWarnings("unchecked")
|
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
|
Iterator<FileItem> i = items.iterator();
|
Map<String, String> param = new HashMap<String, String>(); //所有填写的文本字段
|
List<File> fileList = new ArrayList<File>() ; //所有上传的文件
|
while (i.hasNext()) {
|
FileItem fi = (FileItem) i.next();
|
String fileName = fi.getName();
|
if (fileName != null) {
|
File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); // 解决文件名乱码问题
|
File savedFile = new File(uploadPath, fullFile.getName());
|
fi.write(savedFile);
|
fileList.add(savedFile);
|
}else {
|
param.put(fi.getFieldName(),fi.getString("utf-8"));// 如果你页面编码是utf-8的
|
}
|
}
|
|
DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
|
SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
|
SettingEntity settingEntity = settingIfc.getSettingEntity(request) ;
|
|
//取网店 shopcccode
|
ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
|
|
String salesPriceByPicture = param.get("SalesPriceByPicture") ;
|
if (salesPriceByPicture == null || "".equals(salesPriceByPicture)) {
|
salesPriceByPicture = "0";
|
}
|
|
//产生新单
|
String docCode = uploadImageIfc.saveMatCodeDocCode(openId, userCode,
|
param.get("MatName"),param.get("MatGroup"), param.get("Uom"),
|
Double.parseDouble(salesPriceByPicture), param.get("HDMemo"), shopCcCodeEntity.getShopCcCode()) ;
|
|
AttachmentEntity attachmentEntity = new AttachmentEntity();
|
attachmentEntity.setDoccode(docCode);
|
attachmentEntity.setFormId(710146);
|
for (int j =0;fileList != null && j <fileList.size();j++) {
|
if (j == 0) {
|
attachmentEntity.setFieldId("PhotoPath");
|
}else {
|
attachmentEntity.setFieldId("Image");
|
}
|
|
//上传图片
|
AttachmentWhereEntity attachmentWhereEntity = uploadImageIfc.saveImage(attachmentEntity ,fileList.get(j)) ;
|
|
//保存图片 unid 和 seq
|
if (j == 0) {
|
uploadImageIfc.savePhotoPath(attachmentWhereEntity.getUnid(), attachmentWhereEntity.getSeq(), docCode) ;
|
}else {
|
uploadImageIfc.savePhotoDetails(attachmentWhereEntity.getUnid(), attachmentWhereEntity.getSeq(), docCode) ;
|
}
|
}
|
|
uploadImageIfc.doPostMatCodeDocCode(docCode, "提交") ;
|
|
json.addProperty("information","上传成功,总部正在为您审核,请继续上传!");
|
String url = hostUrl + "/shopping/" + (isMoblieBrowser?"mobile/":"") + "purchase/uploadimage.jsp"+(wxQueryString == null||"".equals(wxQueryString)?"":"?" + wxQueryString) ;
|
json.addProperty("redirect",url);
|
//System.out.print("upload succeed");
|
this.printJson(response, json.toString());
|
return;
|
} catch (DataAccessException e) {
|
errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
|
json.add("error", errJson);
|
this.printJson(response, json.toString());
|
return;
|
}catch (Exception e ){
|
errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
|
json.add("error", errJson);
|
this.printJson(response, json.toString());
|
return;
|
} finally {
|
SpObserver.setDBtoInstance();
|
}
|
}
|
}
|