johnswang
2022-01-15 22907b8763fc073011872b637dc23023ef7fb4f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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();
        }
    }
}