fs-danaus
2022-05-25 e9229c1fcca9f0ce7a3a90200eb1de1012e66ce2
提交 | 用户 | age
fe7da5 1 package com.yc.app.v2.controller;
F 2
d5c2f7 3 import com.yc.action.grid.GridUtils;
fe7da5 4 import com.yc.app.util.ResponseMessage;
F 5 import com.yc.app.v2.entity.RefreshDataEntity;
e9229c 6 import com.yc.app.v2.service.BaseFormService;
d5c2f7 7 import com.yc.exception.ApplicationException;
fe7da5 8 import com.yc.exception.CallBackMessage;
F 9 import com.yc.factory.FactoryBean;
10 import com.yc.multiData.SpObserver;
d5c2f7 11 import com.yc.service.grid.GridServiceIfc;
fe7da5 12 import org.apache.commons.lang3.StringUtils;
F 13 import org.springframework.dao.EmptyResultDataAccessException;
14
e9229c 15 import java.util.List;
fe7da5 16 import java.util.Map;
e9229c 17 import java.util.StringJoiner;
fe7da5 18 import java.util.concurrent.Callable;
F 19
20 public class RefreashDataCallable implements Callable<Object> {
21     RefreshDataEntity entity;
22     String dbid;
e9229c 23
fe7da5 24     public RefreashDataCallable(RefreshDataEntity entity, String dbid) {
e9229c 25         this.entity = entity;
F 26         this.dbid = dbid;
fe7da5 27     }
F 28
29     @Override
30     public Object call() throws Exception {
e9229c 31         CallBackMessage callBackMessage = new CallBackMessage();
fe7da5 32         try {
F 33             final Map<String, Object> formData = this.getFormData(entity);
34             callBackMessage.setInfo(formData);
35             callBackMessage.sendSuccessMessage(ResponseMessage.SUCCESS);
36             return callBackMessage.toJSONObject();
e9229c 37         } catch (EmptyResultDataAccessException ex) {
fe7da5 38             callBackMessage.sendErrorMessage("查找不到此单据数据");
F 39             return callBackMessage.toJSONObject();
e9229c 40         } catch (Exception e) {
fe7da5 41             e.printStackTrace();
e9229c 42             callBackMessage.sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
fe7da5 43             return callBackMessage.toJSONObject();
F 44         }
45     }
e9229c 46
F 47     private Map<String, Object> getFormData(RefreshDataEntity entity) {
48         if (entity.getFormType() == 18 && StringUtils.isBlank(entity.getPostParmBy18())) {
d5c2f7 49             throw new ApplicationException("18类型取数时postParmBy18参数不能为空");
F 50         }
e9229c 51         String where = "";
F 52         for (Map.Entry<String, Object> entry : entity.getWhere().entrySet()) {
53             if (StringUtils.isBlank(where)) {
54                 where += entry.getKey() + "=" + entry.getValue();
55             } else {
56                 where += " and " + entry.getKey() + "=" + entry.getValue();
fe7da5 57             }
e9229c 58         }
F 59         String tableNameBy18 = "";
60         GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl");
61         BaseFormService baseFormService = (BaseFormService) FactoryBean.getBean("baseFormServiceImpl");
62         if (StringUtils.isNotBlank(entity.getPostParmBy18())) {
d5c2f7 63 //myentercode='' and nowcccode='LY001' and mybuyercode='' and nowccname='溧阳红星德立店' and mycompanyid='' and myvndname='' and mydoctype='' and beginday='2022-04-01' and mydocstatus='' and mydoccode='' and endday='2022-04-30' and myvndcode='' and nowusername='张伟' and mycccode='' and myhdmemo='' and nowusercode='z9000011'
e9229c 64             try {
F 65                 SpObserver.setDBtoInstance("_" + dbid);
d5c2f7 66                 final String tableName = gridServiceIfc.getJdbcTemplate().queryForObject("set nocount on \n declare @tableName varchar(500) \n select @tableName=dttable from gform where  formid=? \n select @tableName", String.class, entity.getFormid());
e9229c 67                 String[] table = tableName.split("\\|");
F 68                 if (table.length < 2) {
d5c2f7 69                     throw new ApplicationException("18类型表名称后面需要定义有参数列表,请在9802配置");
F 70                 }
71                 final String functionParm = gridServiceIfc.getFunctionParm(table[1], entity.getPostParmBy18(), entity.getEnv());
e9229c 72                 if (functionParm.length() > 0) {
F 73                     tableNameBy18 = table[0] + "(" + functionParm + ")";
74                 } else {
75                     tableNameBy18 = table[0] + "()";
76                 }
77             } catch (Exception e) {
78                 throw e;
79             }finally {
80                 SpObserver.setDBtoInstance();
81             }
82         }
83
84         //---------生成字段列表,需要附加权限
85          String filedList = createFiledList(entity);
86         //---替换参数
87         //1---如果是单据需要查一次表头数据,因为有可能替换参数从表头取数
88         if(entity.getMainPrimaryKey()!=null&&entity.getMainPrimaryKey().size()>0){
89             String maiWwhere = "";
90             for (Map.Entry<String, Object> entry : entity.getMainPrimaryKey().entrySet()) {
91                 if (StringUtils.isBlank(maiWwhere)) {
92                     maiWwhere += entry.getKey() + "=" + entry.getValue();
93                 } else {
94                     maiWwhere += " and " + entry.getKey() + "=" + entry.getValue();
d5c2f7 95                 }
F 96             }
e9229c 97             RefreshDataEntity refreshDataEntity =new RefreshDataEntity();
F 98             refreshDataEntity.setFormid(entity.getFormid());
99             refreshDataEntity.setFormType(entity.getFormType());
100             refreshDataEntity.setHeadFlag(0);//指定取表头
101             final Map<String, Object> mainTableData = getMainTableData(null, refreshDataEntity, maiWwhere, null);
102              mainTableData.forEach((key,value)->{
103                  entity.getEnv().put(key,value+"");
104              });
105         }
106         filedList = baseFormService.prossExpressionFormSessionValues(filedList, entity.getEnv());
107         //-----取指定的数据
108         return getMainTableData(tableNameBy18,entity, where,filedList);
109     }
110 private  Map<String, Object> getMainTableData(String tableNameBy18,RefreshDataEntity entity,String where,String filedList){
111     try {
112         SpObserver.setDBtoInstance("_" + dbid);
113     String sql = "set nocount on\n" +
114             "  declare @sql nvarchar(max),@formid int="+entity.getFormid()+",@headflag int="+entity.getHeadFlag()+",@formType int="+entity.getFormType()+",@where varchar(4000)="+GridUtils.prossSqlParm(where)+",@filedList varchar(max)="+GridUtils.prossSqlParm(filedList)+",@hdtable varchar(2500),@dttable varchar(2500),@sqlList varchar(max),@tableName varchar(2500) \n";
115     if (entity.getFormType() != 18) {
116         sql += "   select @hdtable=hdtable,@dttable=dttable from gform where  formid=@formid\n" +
117                 "   set @tableName=@hdtable\n" +
118                 "   if @headflag=1 \n" +
119                 "   begin\n" +
120                 "    if @formType=5 or @formType=8 or @formType=496 or @formType=498\n" +
121                 "    begin\n" +
122                 "    set @tableName=@dttable\n" +
123                 "    end\n" +
124                 "   end\n";//增加
125     } else {
126         sql += "  begin \n" +
127                 "    if @formType=18 \n" +
128                 "    begin\n" +
129                 "    set @headflag=0 \n" +
130                 "    set @tableName=" + GridUtils.prossSqlParm(tableNameBy18) +
131                 "    \nend\n" +
132                 "  end \n";
133     }
134     if(filedList==null) {
135         //排除数据库不存在的字段
136         String controltype=" not in(33,34,38,41)";
137         if(entity.getHeadFlag()==0){
138             controltype=" and controltype "+controltype;
139         }else {
140             controltype=" and gridcontroltype "+controltype;
141         }
142         sql +="   select @sqlList=stuff((SELECT ',' + CONVERT(VARCHAR,  isnull(fieldid,''))+''  from gField where formid=@formid and headflag=@headflag and isLoad=1 "+controltype+" FOR XML PATH ('')),1,1,'')\n"+
143           "     select @sql =' select top 1 '+ LOWER(@sqlList)+' from '+ @tableName+' where '+ @where\n";
144     }else {
145         sql += "   select @sql =' select top 1 '+ LOWER(@filedList)+' from '+ @tableName+' where '+ @where\n";
146     }
147                    sql+= "   EXEC sp_executesql @sql";
148     GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl");
149         return gridServiceIfc.getSimpleJdbcTemplate().queryForMap(sql);
150     } catch (Exception ex) {
151         throw ex;
152     } finally {
153         SpObserver.setDBtoInstance();
154     }
155 }
156     private String createFiledList(RefreshDataEntity entity) {
157         //---从9802取得字段filedId,和对应的Showfieldvalueexpression,Stylecss,Tipsexpression
158         StringJoiner joiner = new StringJoiner(",");
159         try {
160             SpObserver.setDBtoInstance("_" + dbid);
161             GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl");
162             final List<Map<String, Object>> list = gridServiceIfc.getJdbcTemplate().queryForList("set nocount on\n select fieldid,Showfieldvalueexpression,Stylecss,Tipsexpression,controltype,gridcontroltype from gField where formid=? and headflag=? and isnull(isLoad,0)=1",entity.getFormid(),entity.getHeadFlag());
163             if (list != null) {
164                 for (Map<String, Object> map : list) {
165                     int controltype = 0;
166                     if (entity.getHeadFlag() == 0) {
167                         controltype = GridUtils.prossRowSetDataType_Int(map, "controltype");
168                     } else {
169                         controltype = GridUtils.prossRowSetDataType_Int(map, "gridcontroltype");
170                     }
171                     if (controltype == 33 || controltype == 34 || controltype == 38 || controltype == 41) {
172                         continue;
173                     }
174                     String Showfieldvalueexpression = GridUtils.prossRowSetDataType_String(map, "Showfieldvalueexpression");
175                     String csstype = GridUtils.prossRowSetDataType_String(map, "Stylecss");
176                     String tipsexpression = GridUtils.prossRowSetDataType_String(map, "Tipsexpression");
177                     String fieldId = GridUtils.prossRowSetDataType_String(map, "fieldid");
178                     joiner.add("[" + fieldId + "]");
179                     if (StringUtils.isNotBlank(Showfieldvalueexpression)) {
180                         //----0,1,2的权限值
181                         joiner.add(" case when (" + Showfieldvalueexpression + ") =1 then 1   when (" + Showfieldvalueexpression + ") =2 then 2 else 0 end as [" + fieldId + "_expr]");
182                     }
183                     if (StringUtils.isNotBlank(csstype)) {
184                         joiner.add(" (" + prossExclamationMark(csstype) + " ) as [" + fieldId + "_cssexpr]");
185                     }
186                     if (StringUtils.isNotBlank(tipsexpression)) {
187                         joiner.add(" (" + prossExclamationMark(tipsexpression) + " ) as [" + fieldId + "_tipsexpr]");
188                     }
d5c2f7 189                 }
e9229c 190             }
F 191             return joiner.toString();
192         } catch (Exception ex) {
193             throw ex;
194         } finally {
195             SpObserver.setDBtoInstance();
196         }
197
198     }
199
200     private String prossExclamationMark(String str) {
201
202         return StringUtils.contains(str, "!") ? str.replaceFirst("!", "") : ("'" + str + "'");
fe7da5 203     }
F 204 }