fs-danaus
2022-11-14 12560372988b49cc9465e92c32d852db2d0e8612
提交 | 用户 | age
a6a76f 1 package com.yc.action.personalized;
F 2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.List;
6 import java.util.Map;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import com.yc.action.personalized.build.BuildCharPage;
13 import com.yc.action.personalized.build.BuildGridPage;
14 import com.yc.action.personalized.build.BuildPersonliaedPageIfc;
15 import com.yc.entity.DataSourceEntity;
16 import com.yc.multiData.MultiDataSource;
17 import com.yc.multiData.SpObserver;
18 import com.yc.sdk.shopping.util.SettingKey;
19 import com.yc.sdk.weixincp.util.AsynchronousExecution;
20 import com.yc.service.personalized.PanelDataIfc;
21 import com.yc.service.personalized.PanelGridIfc;
22 import com.yc.utils.SessionKey;
23
24 @Controller
25 public class BuildTemplateAction {
26
27     @Autowired
28     private PanelDataIfc panelDataIfc;
29
30     @Autowired
31     private PanelGridIfc panelGridIfc;
32     
33     /**
34      * @param request
35      * @param response
36      * @throws IOException
37      */
38     @RequestMapping("/buildPersonlizedTemplate.do")
39     public void buildPersonlizedTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
40         //HttpSession session = request.getSession();
41         //String isSuperUser = (String) session.getAttribute(SessionKey.SUPPER_USER);
42         //if (!"1".equals(isSuperUser)) return;// 超级用户方可以生成
43         String formidStr = request.getParameter("formId");
44         //String dbid = request.getParameter("dbid");
45         String isAutoRefresh = (String) request.getAttribute("isAutoRefresh");
46         //if( dbid==null||"".equalsIgnoreCase(dbid)) dbid=Maintaince.getDatabaseId(request);
47         
48         if(isAutoRefresh==null||"".equalsIgnoreCase(isAutoRefresh)){
49             isAutoRefresh =request.getParameter("isAutoRefresh");
50             
51         }
52         //System.out.println("isAutoRefresh:"+isAutoRefresh);
53         boolean flg=false;
54         if(formidStr==null){
55             formidStr=(String)request.getAttribute("formId");//by danaus 增加在一键生成的支持
56             flg=true;
57         }
58         int formid = -1;
59         try {
60             formid = Integer.parseInt(formidStr);
61         } catch (Exception e) {
62             return;
63         }
64         
65     
66         try{
67             //System.out.println("dbid="+dbid);
68             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
69             request.setAttribute(SessionKey.DATA_BASE_ID, dataSourceEntity.getDbId()+"");
70         
71             String rtn = buildPersonlizedTemplate( formid , dataSourceEntity.getDbId()+"",request.getServletContext().getRealPath("")) ;
72             if (isAutoRefresh==null || "".equals(isAutoRefresh )) {  //是否从 loginfilter.java 调用
73             //System.out.println("pageIfc.build--isAutoRefresh:"+isAutoRefresh);
74                 if(flg)
75                     print(response, rtn);
76                 else
77                     print(response, "{\"code\":\"success\",\"info\":\"" + rtn + "\"}");
78                 String hostUrl = SettingKey.getHostUrl(request) ;
79                 //System.out.println("hostUrl="+hostUrl+" "+"/buildPersonlizedTemplate.do?isAutoRefresh=true&formId=" + formidStr);
80                 
81                 AsynchronousExecution.doRefreshUri(hostUrl,"/buildPersonlizedTemplate.do?isAutoRefresh=true&formId=" + formidStr+"&dbid="+dataSourceEntity.getDbId(), "");  //doRefreshDataSource();    
82             }
83             
84         } catch (Exception e) {
85             e.printStackTrace();
86             print(response, "{\"code\":\"error\",\"info\":\"" + e.getMessage().replace("\"", "") + "\"}");
87         }
88     }
89
90     @RequestMapping("/buildPersonlizedTemplateForAll.do")
91     /**
92      * 生成所有数据库的浮动窗体页面,使其打开时保持最新 ,Added by Johns Wang, 20190-96-04
93      * @param request
94      * @param response
95      * @throws IOException
96      */
97     public void buildPersonlizedTemplateForAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
98         response.setCharacterEncoding("utf-8");
99         response.setContentType("text/html; charset=utf-8");
100         PrintWriter out = response.getWriter();
101         try {
102             String formidStr = request.getParameter("formid");   //多个功能号用逗号隔开 
103             if (formidStr == null || "".equals(formidStr)) {
104                 out.write("必须传递功能号 formid 参数,多个功能号用逗号隔开");
105                 return ;
106             }
107             String rootPath = request.getServletContext().getRealPath("") ;
108             Map<String, DataSourceEntity> list = MultiDataSource.getDataSourceMaps(); 
109             
110             for (Map.Entry<String, DataSourceEntity> entry : list.entrySet()) {
111                 DataSourceEntity dataSourceEntity = entry.getValue();
112                 try {
113                     String formIds[] = formidStr.split(",") ;
114                     for (int i = 0 ;i <formIds.length;i++) {
115                         int formid = Integer.parseInt(formIds[i]); 
116                         buildPersonlizedTemplate(formid , dataSourceEntity.getDbId()+"",rootPath) ;
117                     }
118                     String msg = "\r\n"+(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new java.util.Date()) + " 成功生成id为" + dataSourceEntity.getDbId()+"【" + dataSourceEntity.getSystemID()+ 
119                             "】浮动窗体模版功能号:" + formidStr ;
120                     System.out.println(msg);
121                     out.write("<br/>"+msg);
122                 }catch(Exception e){
123                     String msg = (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new java.util.Date()) + " 正在生成id为" + dataSourceEntity.getDbId()+"【" + dataSourceEntity.getSystemID()+ 
124                             "】浮动窗体模版功能号:" + formidStr + ",错误消息为:"+e.getMessage() ;
125                     System.out.println(msg);
126                     e.printStackTrace();
127                     out.write("<br/>"+msg);
128                     continue ;
129                 }
130                 
131             }
132         }catch(Exception e) {
133             e.printStackTrace();
134             System.out.println("错误:" + (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
135             out.write("错误:" + (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
136         }finally {
137             out.flush();
138             out.close();
139         }
140     }
141     
142     private String buildPersonlizedTemplate(int formid , String dbId,String rootPath) throws Exception {
143         try{
144             SpObserver.setDBtoInstance("_"+dbId);//切换数据源
145             Map<String, Object> panelDataInfo = panelDataIfc.getPanelData(formid);
146             if (panelDataInfo == null) {// 数据库中没有该功能号的定义
147                 return "数据库不存在该功能号的信息!" ;
148             }
149             // 查询其详细配置
150             List<Map<String, Object>> panelGridInfo = panelGridIfc.getPanelGrid(formid);
151             // 生成页面接口
152             BuildPersonliaedPageIfc pageIfc = null;
153     
154             int type = Integer.parseInt(panelDataInfo.get("panelshowtype").toString());
155             switch (type) {
156                 case 1:
157                 pageIfc = new BuildGridPage();
158                 break;
159                 case 2:
160                 case 3:
161                 pageIfc = new BuildCharPage();
162                 break;
163                 default:
164                 pageIfc = new BuildGridPage();
165                 break;
166             }
167             pageIfc.build(panelDataInfo, panelGridInfo, dbId,rootPath);
168             return "生成成功";
169         } catch (Exception e) {
170             throw e ;
171         }finally {
172             SpObserver.setDBtoInstance();
173         }
174     }
175     /**
176      * 输出信息到客户端
177      * 
178      * @param response
179      * @param str
180      * @throws IOException
181      */
182     public void print(HttpServletResponse response, String str) throws IOException {
183     response.setCharacterEncoding("utf-8");
184     PrintWriter out = response.getWriter();
185     out.write(str);
186     out.flush();
187     out.close();
188     }
189 }