xinyb
2024-08-30 f5cc47742dd3d2f2ffd8443ffc82a912683f7824
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
package com.yc.action.personalized;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.yc.multiData.SpObserver;
import com.yc.service.personalized.PanelGridIfc;
import com.yc.service.personalized.PanelUserGridIfc;
import com.yc.utils.SessionKey;
 
@Controller
public class PanelUserLayoutAction {
 
    
    @Autowired
    private PanelUserGridIfc userGridIfc;
    
    @Autowired
    private PanelGridIfc gridIfc;
 
    
    @RequestMapping("/savePanelUserLayout.do")
    public void updateGroup(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String jsonData = request.getParameter("jsonData");
        String formId_str = request.getParameter("formId");
        String showRowNumber_str = request.getParameter("showrownumber");
        if(StringUtils.isBlank(jsonData))
            return;
        Gson gson = new Gson();
        try{
            int formId = Integer.parseInt(formId_str);
            int showRowNumber = -1;
            if(!StringUtils.isBlank(showRowNumber_str))
                showRowNumber = Integer.parseInt(showRowNumber_str);
            String userCode = (String)request.getSession().getAttribute(SessionKey.USERCODE);
            List<Map<String,String>> saveList = gson.fromJson(jsonData, new TypeToken<List<Map<String,String>>>(){}.getType());
            SpObserver.setDBtoInstance("_"+request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            List<Map<String,Object>> tmp = userGridIfc.getGridCfg(userCode, formId);
            if(tmp!=null&&tmp.size()>0){
                userGridIfc.deleteGridCfg(formId, userCode);
                userGridIfc.addGridCfg(formId,userCode,saveList);
            }else{
                userGridIfc.addGridCfg(formId, userCode, saveList);
            }
            userGridIfc.updateShowNumber(formId, userCode, showRowNumber>100?100:showRowNumber);
            print(response, "{\"code\":\"success\",\"info\":\"保存成功!\"}");
        }catch(NumberFormatException e){
            print(response, "{\"code\":\"error\",\"info\":\"数据格式转换有误!\"}");
        }catch (JsonParseException e) {
            print(response, "{\"code\":\"error\",\"info\":\"数据格式转换有误!\"}");
        }catch (Exception e) {
            e.printStackTrace();
            print(response, "{\"code\":\"error\",\"info\":\"出现异常!\"}");
        }finally{
             SpObserver.setDBtoInstance();
         }
    }
    
    @RequestMapping("/restorePanelUserLayout.do")
    public void restorePanelUserLayout(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String formId_str = request.getParameter("formId");
        try{
            SpObserver.setDBtoInstance("_"+request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            int formid = Integer.parseInt(formId_str);
            String userCode = (String)request.getSession().getAttribute(SessionKey.USERCODE);
            userGridIfc.deleteGridCfg(formid, userCode);
            print(response, "{\"code\":\"success\",\"info\":\"保存成功!\"}");
        }catch (NumberFormatException e) {
            print(response, "{\"code\":\"error\",\"info\":\"数据格式转换有误!\"}");
        }catch (Exception e) {
            e.printStackTrace();
            print(response, "{\"code\":\"error\",\"info\":\"出现异常!\"}");
        }finally{
             SpObserver.setDBtoInstance();
         }
    }
    
    @RequestMapping("/getPanelUserLayout.do")
    public void getUserLayout(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String formId_str = request.getParameter("formId");
        try{
            SpObserver.setDBtoInstance("_"+request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            int formid = Integer.parseInt(formId_str);
            String userCode = (String)request.getSession().getAttribute(SessionKey.USERCODE);
            List<Map<String,Object>> gridInfo =userGridIfc.getGridCfg(userCode,formid);
//            String showRow = null;
            if(gridInfo==null||gridInfo.size()==0){
                gridInfo=gridIfc.getPanelGrid(formid,"formid,fieldid,fieldname,align,width,sort,1 as isshow ");
                if(gridInfo==null||gridInfo.size()==0)return;
            }
            Gson gson = new Gson();
            String jsonData = gson.toJson(gridInfo, new TypeToken<List<Map<String,Object>>>(){}.getType());
            StringBuilder sb = new StringBuilder();
            sb.append("{\"rows\":").append(jsonData).append("}");
            print(response, sb.toString());
            jsonData = null;
            sb = null;
        }catch (NumberFormatException e) {
            print(response, "{\"code\":\"error\",\"info\":\"数据格式转换有误!\"}");
        }catch (Exception e) {
            e.printStackTrace();
            print(response, "{\"code\":\"error\",\"info\":\"出现异常!\"}");
        }finally{
             SpObserver.setDBtoInstance();
         }
    }
 
    /**
     * 输出信息到客户端
     * 
     * @param response
     * @param str
     * @throws IOException
     */
    public void print(HttpServletResponse response, String str)
            throws IOException {
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write(str);
        out.flush();
        out.close();
    }
}