| | |
| | | import com.yc.action.grid.GridUtils; |
| | | import com.yc.app.util.ResponseMessage; |
| | | import com.yc.app.v2.entity.RefreshDataEntity; |
| | | import com.yc.app.v2.service.BaseFormService; |
| | | import com.yc.exception.ApplicationException; |
| | | import com.yc.exception.CallBackMessage; |
| | | import com.yc.factory.FactoryBean; |
| | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.dao.EmptyResultDataAccessException; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.StringJoiner; |
| | | import java.util.concurrent.Callable; |
| | | |
| | | public class RefreashDataCallable implements Callable<Object> { |
| | | RefreshDataEntity entity; |
| | | String dbid; |
| | | |
| | | public RefreashDataCallable(RefreshDataEntity entity, String dbid) { |
| | | this.entity=entity; |
| | | this.dbid=dbid; |
| | | this.entity = entity; |
| | | this.dbid = dbid; |
| | | } |
| | | |
| | | @Override |
| | | public Object call() throws Exception { |
| | | CallBackMessage callBackMessage=new CallBackMessage(); |
| | | CallBackMessage callBackMessage = new CallBackMessage(); |
| | | try { |
| | | SpObserver.setDBtoInstance("_" + dbid); |
| | | |
| | | final Map<String, Object> formData = this.getFormData(entity); |
| | | callBackMessage.setInfo(formData); |
| | | callBackMessage.sendSuccessMessage(ResponseMessage.SUCCESS); |
| | | return callBackMessage.toJSONObject(); |
| | | } catch (EmptyResultDataAccessException ex){ |
| | | } catch (EmptyResultDataAccessException ex) { |
| | | callBackMessage.sendErrorMessage("查找不到此单据数据"); |
| | | return callBackMessage.toJSONObject(); |
| | | }catch (Exception e) { |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | callBackMessage.sendErrorMessage(e.getCause()!=null?e.getCause().getMessage() :e.getMessage()); |
| | | callBackMessage.sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
| | | return callBackMessage.toJSONObject(); |
| | | } finally { |
| | | SpObserver.setDBtoInstance(); |
| | | } |
| | | } |
| | | private Map<String, Object> getFormData(RefreshDataEntity entity){ |
| | | if(entity.getFormType()==18&&StringUtils.isBlank(entity.getPostParmBy18())){ |
| | | |
| | | private Map<String, Object> getFormData(RefreshDataEntity entity) { |
| | | if (entity.getFormType() == 18 && StringUtils.isBlank(entity.getPostParmBy18())) { |
| | | throw new ApplicationException("18类型取数时postParmBy18参数不能为空"); |
| | | } |
| | | String where=""; |
| | | for(Map.Entry<String, Object> entry:entity.getWhere().entrySet()){ |
| | | if(StringUtils.isBlank(where)) |
| | | where+=entry.getKey()+"="+entry.getValue(); |
| | | else |
| | | where+=" and "+entry.getKey()+"="+entry.getValue(); |
| | | String where = ""; |
| | | for (Map.Entry<String, Object> entry : entity.getWhere().entrySet()) { |
| | | if (StringUtils.isBlank(where)) { |
| | | where += entry.getKey() + "=" + entry.getValue(); |
| | | } else { |
| | | where += " and " + entry.getKey() + "=" + entry.getValue(); |
| | | } |
| | | String tableNameBy18=""; |
| | | GridServiceIfc gridServiceIfc= (GridServiceIfc) FactoryBean.getBean("gridServiceImpl"); |
| | | if(StringUtils.isNotBlank(entity.getPostParmBy18())){ |
| | | } |
| | | String tableNameBy18 = ""; |
| | | GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl"); |
| | | BaseFormService baseFormService = (BaseFormService) FactoryBean.getBean("baseFormServiceImpl"); |
| | | if (StringUtils.isNotBlank(entity.getPostParmBy18())) { |
| | | //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' |
| | | try { |
| | | SpObserver.setDBtoInstance("_" + dbid); |
| | | 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()); |
| | | String[] table =tableName.split("\\|"); |
| | | if(table.length<2){ |
| | | String[] table = tableName.split("\\|"); |
| | | if (table.length < 2) { |
| | | throw new ApplicationException("18类型表名称后面需要定义有参数列表,请在9802配置"); |
| | | } |
| | | final String functionParm = gridServiceIfc.getFunctionParm(table[1], entity.getPostParmBy18(), entity.getEnv()); |
| | | if(functionParm.length()>0){ |
| | | tableNameBy18=table[0]+"("+functionParm+")"; |
| | | }else{ |
| | | tableNameBy18=table[0]+"()"; |
| | | if (functionParm.length() > 0) { |
| | | tableNameBy18 = table[0] + "(" + functionParm + ")"; |
| | | } else { |
| | | tableNameBy18 = table[0] + "()"; |
| | | } |
| | | } catch (Exception e) { |
| | | throw e; |
| | | }finally { |
| | | SpObserver.setDBtoInstance(); |
| | | } |
| | | } |
| | | |
| | | //---------生成字段列表,需要附加权限 |
| | | String filedList = createFiledList(entity); |
| | | //---替换参数 |
| | | //1---如果是单据需要查一次表头数据,因为有可能替换参数从表头取数 |
| | | if(entity.getMainPrimaryKey()!=null&&entity.getMainPrimaryKey().size()>0){ |
| | | String maiWwhere = ""; |
| | | for (Map.Entry<String, Object> entry : entity.getMainPrimaryKey().entrySet()) { |
| | | if (StringUtils.isBlank(maiWwhere)) { |
| | | maiWwhere += entry.getKey() + "=" + entry.getValue(); |
| | | } else { |
| | | maiWwhere += " and " + entry.getKey() + "=" + entry.getValue(); |
| | | } |
| | | } |
| | | String sql="set nocount on\n" + |
| | | " declare @sql nvarchar(max),@formid int=?,@headflag int=?,@formType int=?,@where varchar(4000)=?,@hdtable varchar(2500),@dttable varchar(2500),@sqlList varchar(max),@tableName varchar(2500) \n"; |
| | | if(entity.getFormType()!=18) { |
| | | sql+= " select @hdtable=hdtable,@dttable=dttable from gform where formid=@formid\n" + |
| | | " set @tableName=@hdtable\n" + |
| | | " if @headflag=1 \n" + |
| | | " begin\n" + |
| | | " if @formType=5 or @formType=8 or @formType=496 or @formType=498\n" + |
| | | " begin\n" + |
| | | " set @tableName=@dttable\n" + |
| | | " end\n" + |
| | | " end\n" ; |
| | | }else { |
| | | |
| | | sql+= " begin \n" + |
| | | " if @formType=18 \n" + |
| | | " begin\n" + |
| | | " set @headflag=0 \n" + |
| | | " set @tableName=" + GridUtils.prossSqlParm(tableNameBy18) + |
| | | " \nend\n" + |
| | | " end \n" ; |
| | | RefreshDataEntity refreshDataEntity =new RefreshDataEntity(); |
| | | refreshDataEntity.setFormid(entity.getFormid()); |
| | | refreshDataEntity.setFormType(entity.getFormType()); |
| | | refreshDataEntity.setHeadFlag(0);//指定取表头 |
| | | final Map<String, Object> mainTableData = getMainTableData(null, refreshDataEntity, maiWwhere, null); |
| | | mainTableData.forEach((key,value)->{ |
| | | entity.getEnv().put(key,value+""); |
| | | }); |
| | | } |
| | | filedList = baseFormService.prossExpressionFormSessionValues(filedList, entity.getEnv()); |
| | | //-----取指定的数据 |
| | | return getMainTableData(tableNameBy18,entity, where,filedList); |
| | | } |
| | | private Map<String, Object> getMainTableData(String tableNameBy18,RefreshDataEntity entity,String where,String filedList){ |
| | | try { |
| | | SpObserver.setDBtoInstance("_" + dbid); |
| | | String sql = "set nocount on\n" + |
| | | " 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"; |
| | | if (entity.getFormType() != 18) { |
| | | sql += " select @hdtable=hdtable,@dttable=dttable from gform where formid=@formid\n" + |
| | | " set @tableName=@hdtable\n" + |
| | | " if @headflag=1 \n" + |
| | | " begin\n" + |
| | | " if @formType=5 or @formType=8 or @formType=496 or @formType=498\n" + |
| | | " begin\n" + |
| | | " set @tableName=@dttable\n" + |
| | | " end\n" + |
| | | " end\n";//增加 |
| | | } else { |
| | | sql += " begin \n" + |
| | | " if @formType=18 \n" + |
| | | " begin\n" + |
| | | " set @headflag=0 \n" + |
| | | " set @tableName=" + GridUtils.prossSqlParm(tableNameBy18) + |
| | | " \nend\n" + |
| | | " end \n"; |
| | | } |
| | | if(filedList==null) { |
| | | //排除数据库不存在的字段 |
| | | String controltype=" not in(33,34,38,41)"; |
| | | if(entity.getHeadFlag()==0){ |
| | | controltype=" and controltype "+controltype; |
| | | }else { |
| | | controltype=" and gridcontroltype "+controltype; |
| | | } |
| | | 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"+ |
| | | " select @sql =' select top 1 '+ LOWER(@sqlList)+' from '+ @tableName+' where '+ @where\n"; |
| | | }else { |
| | | sql += " select @sql =' select top 1 '+ LOWER(@filedList)+' from '+ @tableName+' where '+ @where\n"; |
| | | } |
| | | sql+= " EXEC sp_executesql @sql"; |
| | | GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl"); |
| | | return gridServiceIfc.getSimpleJdbcTemplate().queryForMap(sql); |
| | | } catch (Exception ex) { |
| | | throw ex; |
| | | } finally { |
| | | SpObserver.setDBtoInstance(); |
| | | } |
| | | } |
| | | private String createFiledList(RefreshDataEntity entity) { |
| | | //---从9802取得字段filedId,和对应的Showfieldvalueexpression,Stylecss,Tipsexpression |
| | | StringJoiner joiner = new StringJoiner(","); |
| | | try { |
| | | SpObserver.setDBtoInstance("_" + dbid); |
| | | GridServiceIfc gridServiceIfc = (GridServiceIfc) FactoryBean.getBean("gridServiceImpl"); |
| | | 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()); |
| | | if (list != null) { |
| | | for (Map<String, Object> map : list) { |
| | | int controltype = 0; |
| | | if (entity.getHeadFlag() == 0) { |
| | | controltype = GridUtils.prossRowSetDataType_Int(map, "controltype"); |
| | | } else { |
| | | controltype = GridUtils.prossRowSetDataType_Int(map, "gridcontroltype"); |
| | | } |
| | | if (controltype == 33 || controltype == 34 || controltype == 38 || controltype == 41) { |
| | | continue; |
| | | } |
| | | String Showfieldvalueexpression = GridUtils.prossRowSetDataType_String(map, "Showfieldvalueexpression"); |
| | | String csstype = GridUtils.prossRowSetDataType_String(map, "Stylecss"); |
| | | String tipsexpression = GridUtils.prossRowSetDataType_String(map, "Tipsexpression"); |
| | | String fieldId = GridUtils.prossRowSetDataType_String(map, "fieldid"); |
| | | joiner.add("[" + fieldId + "]"); |
| | | if (StringUtils.isNotBlank(Showfieldvalueexpression)) { |
| | | //----0,1,2的权限值 |
| | | joiner.add(" case when (" + Showfieldvalueexpression + ") =1 then 1 when (" + Showfieldvalueexpression + ") =2 then 2 else 0 end as [" + fieldId + "_expr]"); |
| | | } |
| | | if (StringUtils.isNotBlank(csstype)) { |
| | | joiner.add(" (" + prossExclamationMark(csstype) + " ) as [" + fieldId + "_cssexpr]"); |
| | | } |
| | | if (StringUtils.isNotBlank(tipsexpression)) { |
| | | joiner.add(" (" + prossExclamationMark(tipsexpression) + " ) as [" + fieldId + "_tipsexpr]"); |
| | | } |
| | | } |
| | | sql+= " select @sqlList=stuff((SELECT ',' + CONVERT(VARCHAR, isnull(fieldid,''))+'' from gField where formid=@formid and headflag=@headflag and isLoad=1 FOR XML PATH ('')),1,1,'')\n" + |
| | | " select @sql =' select top 1 '+ LOWER(@sqlList)+' from '+ @tableName+' where '+ @where\n" + |
| | | " EXEC sp_executesql @sql"; |
| | | return gridServiceIfc.getSimpleJdbcTemplate().queryForMap(sql, entity.getFormid(),entity.getHeadFlag(),entity.getFormType(),where); |
| | | } |
| | | return joiner.toString(); |
| | | } catch (Exception ex) { |
| | | throw ex; |
| | | } finally { |
| | | SpObserver.setDBtoInstance(); |
| | | } |
| | | |
| | | } |
| | | |
| | | private String prossExclamationMark(String str) { |
| | | |
| | | return StringUtils.contains(str, "!") ? str.replaceFirst("!", "") : ("'" + str + "'"); |
| | | } |
| | | } |