package com.vci.web.service.impl; import com.vci.client.common.oq.OQTool; import com.vci.client.common.providers.ClientServiceProvider; import com.vci.common.qt.object.*; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.PLException; import com.vci.corba.omd.data.AttributeValue; import com.vci.corba.omd.data.BusinessObject; import com.vci.corba.omd.data.LinkObject; import com.vci.corba.omd.qtm.QTInfo; import com.vci.corba.query.ObjectQueryService; import com.vci.corba.query.ObjectQueryServicePrx; import com.vci.corba.query.data.BtmRefQueryOption; import com.vci.corba.query.data.KV; import com.vci.frameworkcore.compatibility.SmUserQueryServiceI; import com.vci.omd.utils.ObjectTool; import com.vci.starter.web.annotation.Column; import com.vci.starter.web.constant.QueryOptionConstant; import com.vci.starter.web.enumpck.BooleanEnum; import com.vci.starter.web.exception.VciBaseException; import com.vci.starter.web.pagemodel.DataGrid; import com.vci.starter.web.pagemodel.PageHelper; import com.vci.starter.web.util.BeanUtil; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.wrapper.VciQueryWrapperForDO; import com.vci.web.pageModel.*; import com.vci.web.properties.WebProperties; import com.vci.web.service.*; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; /** * 业务类型操作服务---只能在dao层调用,其他的服务不允许使用 * 自动封装了平台相关的操作 * @author weidy */ @Service public class WebBoServiceImpl implements WebBoServiceI{ /** * 枚举服务 */ @Autowired private OsEnumServiceI enumService;//枚举服务 /** * 属性服务 */ @Autowired private OsAttributeServiceI attrService;//属性服务 /** * 生命周期的服务 */ @Autowired private OsLifeCycleServiceI lifeService;//生命周期服务 /** * 状态的服务 */ @Autowired private OsStatusServiceI statusService; /** * 用户查询服务 */ @Autowired private SmUserQueryServiceI userQueryService; /** * 链接类型服务 */ @Autowired private WebLoServiceI loService; /** * 配置信息 */ @Autowired private WebProperties webProperties; /** * 多语言前缀 */ private final String msgCodePrefix = "com.vci.web.boService."; /** * 日志对象 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 业务类型的服务 */ @Autowired private OsBtmServiceI btmService; /** * 版本规则的服务 */ @Autowired private OsRevisionRuleServiceI revisionRuleService; /** * 平台调用客户端 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 已经创建过的业务对象 */ private static Map hasCreatedCbos = new HashMap(); /** * 业务对象默认属性 */ public static final Map BO_BASE_FIELD_MAP = new HashMap<>(); public WebBoServiceImpl(){ List fields = WebUtil.getAllFieldForObj(BusinessObject.class); if(!CollectionUtils.isEmpty(fields)) { fields.stream().forEach(field -> { BO_BASE_FIELD_MAP.put(field.getName(),WebUtil.getCboAttrNameFromField(field,BusinessObject.class)); }); } } /** * 初始化业务类型 * --第一次调用的时候会相对慢一下,后续获取就很快的 * --注意在服务启动时才会清除缓存 * --创建人默认为当前用户,如果需要修改,可以在获取后自行处理 * @param btmName 业务类型的名称,会自动变成小写 * @return 业务数据的对象 * @throws VciBaseException 初始化出错的是会抛出异常 */ @Override public BusinessObject createCBOByBtmName(String btmName) throws VciBaseException { if(btmName!=null){ btmName = btmName.trim().toLowerCase(); } String userid = WebUtil.getCurrentUserId(); if(!hasCreatedCbos.containsKey(btmName)){ if(StringUtils.isEmpty(userid)){ throw new VciBaseException(msgCodePrefix +"noHasUserid"); } try { // ClientServiceProvider.getBOFService().initBusinessObject(btmName); 可以进行替换 hasCreatedCbos.put(btmName, createBusinessObject(btmName)); } catch (Exception e) { logger.error("创建业务类型对象",e); throw new VciBaseException(msgCodePrefix + "initBoError",new String[]{btmName}); } } BusinessObject cbo = cloneBusinessObject(hasCreatedCbos.get(btmName)); return cbo; } /** * 根据业务类型名称创建业务数据源对象 * @param boName 业务类型名称 * @return 业务数据对象 */ public BusinessObject createBusinessObject(String boName) throws PLException { // ClientServiceProvider.getBOFService().initBusinessObject(boName); OsBtmTypeVO btmTypeVO = btmService.getBtmById(boName); String userName = WebUtil.getCurrentUserId(); BusinessObject bo = new BusinessObject(); bo.oid = (new ObjectUtility()).getNewObjectID36(); bo.revisionid = (new ObjectUtility()).getNewObjectID36(); bo.nameoid = (new ObjectUtility()).getNewObjectID36(); bo.btName = boName; bo.isLastR = true; bo.isFirstR = true; bo.isFirstV = true; bo.isLastV = true; bo.creator = userName; bo.createTime = System.currentTimeMillis(); bo.modifier = userName; bo.modifyTime = System.currentTimeMillis(); bo.revisionRule = btmTypeVO.getRevisionRuleId(); bo.versionRule = String.valueOf(btmTypeVO.getVersionRule()); if(StringUtils.isNotBlank(btmTypeVO.getRevisionRuleId())){ OsRevisionRuleVO revisionRuleVO = revisionRuleService.getRevisionRuleById(btmTypeVO.getRevisionRuleId()); bo.revisionValue = revisionRuleVO.getStartCode(); } bo.revisionSeq = (short) 1; bo.versionSeq = (short) 1; bo.versionValue = getVersionValue(WebUtil.getInt(btmTypeVO.getVersionRule())); bo.lctId = btmTypeVO.getLifeCycleId(); if(StringUtils.isNotBlank(btmTypeVO.getLifeCycleId())){ OsLifeCycleVO lifeCycleVO = lifeService.getLifeCycleById(btmTypeVO.getLifeCycleId()); //todo 无法获取数据 bo.lcStatus = lifeCycleVO == null ? "" : lifeCycleVO.getStartStatus(); } bo.id = ""; bo.name = ""; bo.description = ""; bo.owner = userName; // bo.setCheckinBy(userName); bo.fromVersion = ""; this.initTypeAttributeValue(bo,btmTypeVO); return bo; } /** * 初始化业务类型的默认值 * @param cbo 业务数据对象 * @param btmTypeVO 业务类型的显示对象 */ private void initTypeAttributeValue(BusinessObject cbo,OsBtmTypeVO btmTypeVO) { Optional.ofNullable(btmTypeVO.getAttributes()).orElseGet(()->new ArrayList<>()).stream().forEach(attribute->{ ObjectTool.setBOAttributeValue(cbo,attribute.getId(),attribute.getDefaultValue()); }); } /** * 获取版次的值 * @param verRuleName 版次的规则 * @return 版次的值,没有规则则为空 */ private String getVersionValue(int verRuleName) { if (verRuleName == 0) { return "1"; } else if (verRuleName == 1) { return "a"; } else if (verRuleName == 2) { return "0"; } return ""; } /** * 拷贝以前的业务数据 * @param cbo 以前的业务数据对象 * @return 拷贝后的对象 */ private BusinessObject cloneBusinessObject(BusinessObject cbo){ if(cbo !=null){ BusinessObject businessObject = new BusinessObject(); businessObject.oid = new ObjectUtility().getNewObjectID36(); businessObject.revisionid = new ObjectUtility().getNewObjectID36(); businessObject.nameoid = new ObjectUtility().getNewObjectID36(); businessObject.btName = cbo.btName; businessObject.isLastR = cbo.isLastR; businessObject.isFirstR = cbo.isFirstR; businessObject.isLastV = cbo.isLastV; businessObject.isFirstV = cbo.isFirstV; businessObject.creator = WebUtil.getCurrentUserId(); businessObject.createTime = System.currentTimeMillis(); businessObject.modifier = cbo.modifier; businessObject.modifyTime = cbo.modifyTime; businessObject.revisionRule = cbo.revisionRule; businessObject.versionRule = cbo.versionRule; businessObject.revisionSeq = cbo.revisionSeq; businessObject.revisionValue = cbo.revisionValue; businessObject.versionSeq = cbo.versionSeq; businessObject.versionValue = cbo.versionValue; businessObject.lctId = cbo.lctId; businessObject.lcStatus = cbo.lcStatus; businessObject.ts = System.currentTimeMillis(); businessObject.id = cbo.id; businessObject.name = cbo.name; businessObject.description = cbo.description; businessObject.owner = businessObject.creator; businessObject.fromVersion = cbo.fromVersion; if(cbo.newAttrValList !=null){ businessObject.newAttrValList = clone(cbo.newAttrValList); } if(cbo.hisAttrValList !=null){ businessObject.hisAttrValList = clone(cbo.hisAttrValList); } return businessObject; }else { return null; } } /** * 拷贝属性的值 * @param newAttrValList 属性值对象数组 * @return 拷贝后的新属性数组 */ private AttributeValue[] clone(AttributeValue[] newAttrValList) { AttributeValue[] n = new AttributeValue[newAttrValList.length]; for (int i = 0; i < newAttrValList.length; i++) { n[i] = new AttributeValue(newAttrValList[i].attrName,newAttrValList[i].attrVal); } return n; } /** * 前端的查询条件转换为平台需要的查询对象 * @param conditionMap 查询条款 * @return 查询对象 */ @Override public Condition getConditionByMap(Map conditionMap){ Condition mergeCondition = null;//组合后的查询条件 if(conditionMap!=null && conditionMap.size() > 0){ //先把所有的查询条件都转换为小写 //然后开始逐个遍历,主要是对_start和_end这样的 Iterator itor = conditionMap.keySet().iterator(); while(itor.hasNext()){ String key = itor.next(); String value = conditionMap.get(key); Condition condition = null;//本次的条件 Map thisConditionMap = new HashMap(); boolean isOr = false; if(WebUtil.isNotNull(value) && value.startsWith(QueryOptionConstant.OR)){ isOr = true; value = value.substring(4); } if(key.indexOf("_start")>-1 || key.indexOf("_end")>-1){//介于....之间 if(key.indexOf("_start")>-1){ String attr = key.replace("_start", ""); thisConditionMap.put(attr, value); condition = OQTool.getCondition(thisConditionMap); if(conditionMap.containsKey(attr + "_end")){//如果没有结束,其实也是允许的; Map endConditonMap = new HashMap(); endConditonMap.put(attr, conditionMap.get(attr + "_end")); condition = OQTool.mergeCondition(condition, OQTool.getCondition(endConditonMap), Connector.AND); } } }else{ if(("creator".equalsIgnoreCase(key) || "lastmodifier".equalsIgnoreCase(key)) && StringUtils.isNotBlank(value) ){ //说明是查询创建人和最后修改人 //模糊,等于,不等于,其余的不处理姓名 if(value.contains("*")) { //说明是like String userValue = value.replace("*", "%"); thisConditionMap.put(key, QueryOptionConstant.IN + "(select plusername from pluser where plusername like '" + userValue + "' or pltruename like '" + userValue + "')"); }else if(value.startsWith(QueryOptionConstant.NOTEQUAL)){ thisConditionMap.put(key, QueryOptionConstant.NOTIN + "(select plusername from pluser where plusername = '" + value + "' or pltruename = '" + value + "')"); }else if(value.startsWith(QueryOptionConstant.EQUAL)){ thisConditionMap.put(key, QueryOptionConstant.IN + "(select plusername from pluser where plusername = '" + value + "' or pltruename = '" + value + "')"); }else{ thisConditionMap.put(key, value); } }else { thisConditionMap.put(key, value); } condition = OQTool.getCondition(thisConditionMap); } if(mergeCondition == null){ mergeCondition = condition; }else{ mergeCondition = OQTool.mergeCondition(mergeCondition, condition, isOr? Connector.OR:Connector.AND); } } } return mergeCondition; } /** * 根据属性来查询主键 * * @param referInfo 业务类型.属性名 * @param value 属性的值 * @return 查询后的值 * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public Map queryOidByPropertie(String referInfo, List value) throws VciBaseException { WebUtil.alertNotNull(referInfo,"参照信息",value,"参照的内容"); if(referInfo.indexOf(".") < 0){ throw new VciBaseException("参照的信息必须是【业务类型.属性】这样的格式"); } String btmName = referInfo.split("\\.")[0].trim().toLowerCase(); String fieldName = referInfo.split("\\.")[1].trim().toLowerCase(); Map conditionMap = new HashMap(); conditionMap.put(fieldName,QueryOptionConstant.IN + "(" + WebUtil.toInSql(value.toArray(new String[0])) + ")"); List cbos = queryCBO(btmName,conditionMap,null, Arrays.asList(new String[]{"oid",fieldName})); Map data = new HashMap(); if(cbos!=null && cbos.size() > 0){ for(BusinessObject cbo : cbos){ data.put(ObjectTool.getBOAttributeValue(cbo,fieldName),cbo.oid); } } return data; } /** * 校验特定属性的值是否存在 * * @param btmName 业务类型 * @param columnName 列名称 * @param columnValues 列的值 * @return true表示存在这个值 * @throws VciBaseException 查询出错会抛出异常 */ @Override public boolean checkDataExsitByColumn(String btmName, String columnName, Collection columnValues) throws VciBaseException { WebUtil.alertNotNull(btmName,"业务类型",columnName,"属性名称",columnValues,"属性" + columnName + "的值"); Map conditionMap = new HashMap(); Set oidSet = new HashSet(); for(String oid : columnValues){ if(StringUtils.isNotBlank(oid)){ oidSet.add(oid); } } if(oidSet.size() == 0){ throw new VciBaseException("属性" + columnName + "的值为空"); } conditionMap.put(columnName,QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(oidSet.toArray(new String[0])) + ")"); List cbos = queryCBO(btmName,conditionMap,null,Arrays.asList(new String[]{columnName})); if(cbos !=null && cbos.size() >0){ for(BusinessObject cbo : cbos){ String oid = ObjectTool.getBOAttributeValue(cbo,columnName); if(oidSet.contains(oid)){ oidSet.remove(oid); } } if(oidSet.size()>0){ throw new VciBaseException("属性" + columnName + "的值有" + oidSet.size() + "个不存在"); } return true; }else{ throw new VciBaseException("属性" + columnName + "的值全部不存在"); } } /** * 校验特定属性的值是否存在 * * @param btmName 业务类型 * @param columnName 列名称 * @param columnValues 列的值 * @return * @throws VciBaseException */ @Override public boolean checkDataExsitByColumn(String btmName, String columnName, String columnValues) throws VciBaseException { WebUtil.alertNotNull(columnValues,"属性" + columnName + "的值"); return checkDataExsitByColumn(btmName,columnName,Arrays.asList(columnValues.split(","))); } /** * 根据查询条件来查询业务类型下的数据 * @param btmType 业务类型的名称,会自动变成小写 * @param conditionMap 查询条件,注意介于...之间使用xxx_start和xxx_end * @return 业务数据列表 * @throws VciBaseException 查询出错的是抛出异常 */ @Override public List queryCBO(String btmType, Map conditionMap) throws VciBaseException { List clauseList = new ArrayList(); clauseList.add("*"); PageHelper ph = new PageHelper(-1); return queryCBO(btmType,conditionMap,ph,clauseList); } /** * 支持排序和分页的查询业务类型下的数据 * @param btmType 业务类型的名称 * @param conditionMap 查询条件,注意介于...之间使用xxx_start和xxx_end * @param ph 排序和分页 * @return 业务数据列表 * @throws VciBaseException 查询出错的是抛出异常 */ @Override public List queryCBO(String btmType, Map conditionMap, PageHelper ph) throws VciBaseException { List clauseList = new ArrayList(); clauseList.add("*"); return queryCBO(btmType,conditionMap,ph,clauseList); } /** * 使用查询方案来查询数据,返回CBO * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @param ph 分页对象 * @param clauseList 查询字段 * @return 业务数据列表 * @throws VciBaseException 查询出错的是抛出异常 */ @Override public List queryCBO(String btmType, Map conditionMap, PageHelper ph, List clauseList) throws VciBaseException { QueryTemplate qt = new QueryTemplate(); //基础设置 qt.setId("queryBOHasPage"); qt.setType(QTConstants.TYPE_BTM); qt.setBtmType(btmType.toLowerCase()); return baseQueryCBO(qt, conditionMap, ph, clauseList); } /** * 设置是否过滤数据权限 * @param qt 查询模板 * @param conditionMap 查询条件 */ private void setRightValueToQueryTemplate(QueryTemplate qt,Map conditionMap){ boolean filterDataRight = webProperties.isDataRight(); if(conditionMap == null){ conditionMap = new HashMap(); } if(conditionMap.containsKey(QUERY_FILTER_DATARIGHT)){ if(BooleanEnum.TRUE.getValue().equals(conditionMap.get(QUERY_FILTER_DATARIGHT).toLowerCase())){ filterDataRight = true; }else{ filterDataRight = false; } conditionMap.remove(QUERY_FILTER_DATARIGHT); } boolean filterSecret = webProperties.isSecretRight(); if(conditionMap.containsKey(QUERY_FILTER_SECRET)){ if(BooleanEnum.TRUE.getValue().equals(conditionMap.get(QUERY_FILTER_SECRET).toLowerCase())){ filterSecret = true; }else{ filterDataRight = false; } conditionMap.remove(QUERY_FILTER_SECRET); } qt.setRightFlag(filterDataRight); qt.setSecretFlag(filterSecret); } /** * 基础的查询方法 * @param qt 查询模板 * @param conditionMap 查询条件 * @param ph 分页 * @param clauseList 查询字段 * @return 查询出来的值 * @throws VciBaseException 查询出错的时候会抛出异常 */ private List baseQueryCBO(QueryTemplate qt,Map conditionMap,PageHelper ph,List clauseList) throws VciBaseException{ List allCbos = new ArrayList(); if(clauseList == null){ clauseList = new ArrayList(); clauseList.add("*"); } setRightValueToQueryTemplate(qt,conditionMap);//设置密级查询,和数据权限控制 List refOpts = new ArrayList(); List enumAttrName = new ArrayList(); List thisQueryAttr = new ArrayList(); if(clauseList!=null && clauseList.size()>0){ for(String attrName : clauseList){ if(attrName.indexOf(".")>-1){ String[] kvs = attrName.split("\\."); int len = kvs.length; if(len == 2){ // 一层参照 a.b // 第二个参数给空,是此不处从属性中查其参数的业务类型,由下下专用掊中进行处理 BtmRefQueryOption refOpt = new BtmRefQueryOption( kvs[0], "", new String[]{kvs[1]} ); refOpts.add(refOpt); } else if(len == 3){ // 二层参照 a.b.c // TODO 需要支持两层参照 } else { // 三层以的参照 a.b.c.d a.b.c.d.e // TODO 需要支持三层以及以上的参照 } }else if(attrName.indexOf("_")>-1 && !"lcStatus_text".toLowerCase().equalsIgnoreCase(attrName.toLowerCase())){ enumAttrName.add(attrName); }else{ thisQueryAttr.add(attrName); } } } if(thisQueryAttr.isEmpty()){ thisQueryAttr.add("*"); } //设置查询字段,这个必须是当前业务类型中的查询字段 qt.setClauseList(thisQueryAttr); conditionMap = WebUtil.getNotNullMap(conditionMap); Condition condition = getConditionByMap(conditionMap); if(qt.getCondition() !=null) { qt.setCondition(OQTool.mergeCondition(qt.getCondition(), condition, Connector.AND)); }else { qt.setCondition(condition); } qt.setLevel(-1);//对于业务类型来说没有作用 //设置分页信息和排序 setPageAndOrderToQT(qt,ph); try { ObjectQueryServicePrx qtService = platformClientUtil.getQueryService(); if(qtService == null){ logger.error("没有找到查询服务"); throw new VciBaseException(msgCodePrefix+"qtServerNull", new String[]{}); } ObjectQueryService.FindBTMObjectsV3Result bos = qtService.findBTMObjectsV3(qt.getId(), OQTool.qtTOXMl(qt).asXML(), refOpts.toArray(new BtmRefQueryOption[refOpts.size()])); if (bos != null && bos.count > 0) { for (BusinessObject bo : bos.returnValue) { // BusinessObject cbo = new BusinessObject(); // cbo = bo; queryEnumText(bo,enumAttrName); allCbos.add(bo); } //因为修改了查询的方式,所以就不需要单独查询参照 } queryLcStatus(allCbos); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } return allCbos; } public AttributeValue[] copyToAttributeValue(AttributeValue[] attributeValues){ AttributeValue[] attributeValueList = new AttributeValue[attributeValues.length]; for (int i = 0; i < attributeValues.length; i++) { AttributeValue newAttributeValue = new AttributeValue(); newAttributeValue.attrName = attributeValues[i].attrName; newAttributeValue.attrVal = attributeValues[i].attrVal; attributeValueList[i] = newAttributeValue; } return attributeValueList; } /** * 拷贝分页和排序的设置到查询模板 * @param qt 查询,模板 * @param pageHelper 分页对象 */ @Override public void setPageAndOrderToQT(QueryTemplate qt,PageHelper pageHelper){ if(pageHelper!=null){ if(pageHelper.getLimit()>0){ PageInfo pageInfo = new PageInfo(); pageInfo.setPageNO(pageHelper.getPage()); pageInfo.setRowCount(pageHelper.getLimit()); qt.setPageInfo(pageInfo); } if(WebUtil.isNotNull(pageHelper.getSort()) && WebUtil.isNotNull(pageHelper.getOrder())){ //设置了排序 String[] sort = pageHelper.getSort().split(","); String[] order = pageHelper.getOrder().split(","); if(sort.length != order.length){ throw new VciBaseException(msgCodePrefix + "sortlengtherror",new Object[]{sort.length,order.length}); } List orderInfoList = new ArrayList(); for(int i = 0 ; i < sort.length ;i++){ OrderInfo oi = new OrderInfo(); oi.setOrderField(sort[i]); oi.setOrderMode(order[i]); oi.setLevel(sort.length -i);//值越大优先级越大,而排在数组的前面的优先级越大 orderInfoList.add(oi); } qt.setOrderInfoList(orderInfoList); } } } /** * 获取所有的枚举属性 * @param allAttrMap 所有的属性 * @return 获取属性中的枚举属性 */ private List getEnumAttrName(Map allAttrMap){ List enumAttrName = new ArrayList(); if(allAttrMap!=null && allAttrMap.size() > 0){ for(String attr : allAttrMap.keySet()){ if(attr.indexOf("_")>-1 && !"lcStatus_text".toLowerCase().equalsIgnoreCase(attr)){ enumAttrName.add(attr); } } } return enumAttrName; } /** * 获取引用的属性---只支持一级参照 * @param allAttrMap 所有的属性 * @return 枚举属性 */ private Map getReferAttrName(Map allAttrMap){ Map referAttrName = new HashMap(); if(allAttrMap!=null && allAttrMap.size() > 0){ for(String attr : allAttrMap.keySet()){ if(attr.indexOf(".")>-1){ referAttrName.put(attr,allAttrMap.get(attr)); } } } return referAttrName; } /** * 查询生命周期的值 * @param cbos 业务对象 */ private void queryLcStatus(List cbos){ if(!CollectionUtils.isEmpty(cbos)){ Map statusVOMap = statusService.selectAllStatusMap(); cbos.stream().forEach(cbo->{ try{ ObjectTool.setBOAttributeValue(cbo,"lcStatus_text", statusVOMap.getOrDefault(cbo.lcStatus,new OsStatusVO()).getName()); }catch(Exception e){ logger.error("获取生命周期中状态的显示文本出错",e); } }); } } /** * 查询业务对象上的枚举值 * @param cbo 业务对象 * @param enumAttrName 枚举属性的值 * @throws VciBaseException */ private void queryEnumText(BusinessObject cbo,List enumAttrName) throws VciBaseException{ queryEnumText(cbo,null,enumAttrName); } /** * 查询枚举的信息 * @param cbo 业务数据 * @param clo 链接属性 * @param enumAttrName 枚举的名称 * @throws VciBaseException 查询枚举出错的时候会抛出异常 */ @Override public void queryEnumText(BusinessObject cbo, LinkObject clo, List enumAttrName) throws VciBaseException{ if(enumAttrName.size()>0){//查询枚举 for(String enumAttr:enumAttrName){//格式为 code_field code是枚举的编码,field是当前业务类型存储枚举值的字段 if(enumAttr.toLowerCase().equals("creator_name")){ //创建人的名称 String creator = ""; if(cbo!=null){ creator = cbo.creator; }else{ creator = clo.creator; } if(StringUtils.isNotBlank(creator)){ String userTrueName = userQueryService.getUserNameByUserId(creator); setValueToCboOrClo(cbo,clo,"creator_name",userTrueName); } }else if(enumAttr.toLowerCase().equals("lastmodifier_name")){ //最后修改人名称 String modifier = ""; if(cbo!=null){ modifier = cbo.modifier; }else{ modifier = clo.modifier; } if(StringUtils.isNotBlank(modifier)){ String userTrueName = userQueryService.getUserNameByUserId(modifier); setValueToCboOrClo(cbo,clo,"lastmodifier_name",userTrueName); } }else if(enumAttr.toLowerCase().endsWith("${user_name}")){ String usernameField = enumAttr.toLowerCase(); String fields= usernameField.replace("${user_name}",""); if(fields.contains("_")){ String valueField = ""; valueField = fields.split("_")[0]; String value = ObjectTool.getBOAttributeValue(cbo,valueField);; if(StringUtils.isNotBlank(value)){ String userTrueName = userQueryService.getUserNameByUserId(value); setValueToCboOrClo(cbo,clo,enumAttr,userTrueName); } } }else { String[] tempArray = enumAttr.split("_"); String enumCode = tempArray[0];//枚举编码 if (tempArray.length > 1) { //从枚举中获取值 String valueFieldName = tempArray[1]; String comboxField = ""; if(valueFieldName.contains("#")){ valueFieldName = tempArray[1].split("#")[0]; comboxField = tempArray[1].split("#")[1]; } String enumKey = ""; if (cbo != null) { enumKey = ObjectTool.getBOAttributeValue(cbo,valueFieldName); } else if (clo != null) { enumKey = ObjectTool.getLOAttributeValue(clo,valueFieldName); } String enumText = ""; if (WebUtil.isNotNull(enumKey)) { enumText = enumService.getValue(enumCode, enumKey); } if(StringUtils.isNotBlank(comboxField)){ setValueToCboOrClo(cbo, clo, comboxField, enumText); }else { setValueToCboOrClo(cbo, clo, enumAttr.toLowerCase(), enumText); } } } } } } /** * 为业务类型设置值 * @param cbo 业务类型 * @param clo 链接类型 * @param attr 属性名 * @param value 值 */ private void setValueToCboOrClo(BusinessObject cbo,LinkObject clo,String attr,String value){ try { if (cbo != null) { ObjectTool.setBOAttributeValue(cbo, attr, value); }else{ setAttributeValueForClo(clo,attr, value); } } catch (Exception e) { logger.error("setValueToCboOrClo",e); } } /** * 给链接类型设置属性 * @param clo 链接类型 * @param attributeName 属性的名称 * @param attributeValue 属性的值 */ @Override public void setAttributeValueForClo(LinkObject clo, String attributeName, String attributeValue) { AttributeValue[] attrValues = clo.newAttrValList; ArrayList attrValList = new ArrayList(); AttributeValue attrVal; int i; if (attrValues != null && attrValues.length > 0) { AttributeValue[] var9 = attrValues; i = attrValues.length; for (int var7 = 0; var7 < i; ++var7) { attrVal = var9[var7]; attrValList.add(attrVal); } } attrVal = null; boolean isExist = false; for (i = 0; i < attrValList.size(); ++i) { attrVal = (AttributeValue) attrValList.get(i); if (attrVal.attrName.toUpperCase().equals(attributeName.toUpperCase())) { attrVal.attrVal = attributeValue; isExist = true; break; } } if (!isExist) { attrVal = new AttributeValue(); attrVal.attrName = attributeName.toUpperCase(); attrVal.attrVal = attributeValue; attrValList.add(attrVal); } clo.newAttrValList = attrValList.toArray(new AttributeValue[attrValList.size()]); } /** * 查询链接类型中的枚举属性的对应值 * @param clo 链接类型 * @param enumAttrName 枚举属性 * @throws VciBaseException 查询出错的时候会抛出异常 */ private void queryEnumTextClo(LinkObject clo,List enumAttrName) throws VciBaseException{ queryEnumText(null,clo,enumAttrName); } /** * 使用查询方案来查询数据,返回CBO,支持分页 * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案里的变量值 * @return 业务类型 * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public List queryCBOByScheme(String queryScheme, Map conditionMap, Map replaceMap) throws VciBaseException { PageHelper ph = new PageHelper(-1); return queryCBOByScheme(queryScheme,conditionMap,replaceMap,ph); } /** * 使用查询方案来查询数据,返回CBO,支持自定义查询字段,包括参照和枚举;参照字段使用xxx.yy;枚举字段使用xxx_enumCode * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案里的变量值 * @param ph 分页和排序 * @return 业务数据 * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public List queryCBOByScheme(String queryScheme, Map conditionMap, Map replaceMap, PageHelper ph) throws VciBaseException { List clauseList = new ArrayList(); clauseList.add("*"); return queryCBOByScheme(queryScheme,conditionMap,replaceMap,ph,clauseList); } /** * 使用名字获取查询模板 * @param name 名称 * @param replaceMap 源数据 * @return 查询模板 * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public QueryTemplate getQtByName(String name, Map replaceMap) throws VciBaseException{ QueryTemplate qt = null; try{ VciBaseUtil.alertNotNull(name,"查询模板的名称"); QTInfo wrapper = platformClientUtil.getQTDService().getQT(name); qt = OQTool.getQTByDoc(DocumentHelper.parseText(wrapper.qtText), name); //因为之前平台的Tool.replaceQTValues 没有处理 ${xxx}。, 为了忽略大小写,我们这里单独处理 weidy@2021-3-10 //我们转换为小写 Condition condition = qt.getCondition(); Map replaceLowMap = new HashMap<>(); if(condition !=null && condition.getCIMap()!=null){ // Map systemVarValueMap = VciSystemVarConstants.getSystemVarValueMap(); if(!CollectionUtils.isEmpty(replaceMap)){ replaceMap.forEach((key,value)->{ replaceLowMap.put(key.toLowerCase(),value); }); } Map ciMap = condition.getCIMap(); ciMap.forEach((ciId,ci)->{ if(ci.isLeaf()) { LeafInfo lInfo = ci.getLeafInfo(); String value = lInfo.getValue().getOrdinaryValue(); if(StringUtils.isNotBlank(value)){ if(value.contains("#")){ //系统变量.支持一个 //lInfo.getValue().setOrdinaryValue(systemVarValueMap.getOrDefault(value.toUpperCase(),"")); }else if(value.contains("${")){ //有${xxxx}的方式 if(!CollectionUtils.isEmpty(replaceLowMap)) { try { lInfo.getValue().setOrdinaryValue(VciBaseUtil.replaceByFreeMarker(value, replaceLowMap)); }catch (Throwable e){ logger.error("可能配置有问题,在转换freemarker表达式的时候,没有找到对应的值,目前表达式为{}",new String[]{value},e); } } }else if(replaceLowMap.containsKey(value.toLowerCase())){ lInfo.getValue().setOrdinaryValue(replaceLowMap.get(value.toLowerCase())); } } } }); } }catch(PLException e){ logger.error(e.code,e); throw WebUtil.getVciBaseException(e); } catch (DocumentException e) { logger.error("查询模板转换",e); throw new VciBaseException(this.msgCodePrefix + "qtError", new Object[]{name}); } return qt; } /** * 根据查询模板来查询数据 * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案里的变量值 * @param ph 分页和排序 * @param clauseList 查询字段 * @return 业务数据列表 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryCBOByScheme(String queryScheme, Map conditionMap, Map replaceMap, PageHelper ph, List clauseList) throws VciBaseException { QueryTemplate qt = getQtByName(queryScheme,replaceMap); return baseQueryCBO(qt,conditionMap,ph,clauseList); } /** * 查询个数 * @param qt 查询模板 * @param conditionMap 查询条件 * @return 个数 * @throws VciBaseException 查询出错会抛出异常 */ private int baseQueryCount(QueryTemplate qt,Map conditionMap) throws VciBaseException{ List clauseList = new ArrayList(); clauseList.add("*"); qt.setClauseList(clauseList); setRightValueToQueryTemplate(qt,conditionMap);//设置密级查询,和数据权限控制 conditionMap = WebUtil.getNotNullMap(conditionMap); Condition condition = getConditionByMap(conditionMap); if(qt.getCondition() !=null) { qt.setCondition(OQTool.mergeCondition(qt.getCondition(), condition, Connector.AND)); }else { qt.setCondition(condition); } long count = 0; try{ count = platformClientUtil.getQueryService().findTotalCount(qt.getId(), OQTool.qtTOXMl(qt).asXML()); }catch (PLException e) { logger.error(e.code,e); throw WebUtil.getVciBaseException(e); } return (int) count; } /** * 查询个数 * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @return 个数 * @throws VciBaseException 查询出错会抛出异常 */ @Override public int queryCount(String btmType, Map conditionMap) throws VciBaseException { QueryTemplate qt = new QueryTemplate(); qt.setId("queryBOCount"); if(btmType.endsWith("_link")){ qt.setType(QTConstants.TYPE_LINK); }else{ qt.setType(QTConstants.TYPE_BTM); } qt.setBtmType(btmType.toLowerCase()); return baseQueryCount(qt,conditionMap); } /** * 使用sql查询个数,最终的字段必须是count * @param sql 查询的sql * @param conditionMap 查询条件 * @return 个数, * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public int queryCountBySql(String sql,Map conditionMap) throws VciBaseException{ WebUtil.alertNotNull(sql,"sql语句"); AttributeValue[] ava = null; if(conditionMap!=null&&!conditionMap.isEmpty()){ ava = new AttributeValue[conditionMap.size()]; Iterator it = conditionMap.keySet().iterator(); int i = 0 ; while(it.hasNext()){ String key = it.next(); String value = conditionMap.get(key); if(value == null){ value = ""; } AttributeValue av = new AttributeValue(key, value); ava[i] = av; i++; } }else{ ava = new AttributeValue[0]; } try { String[][] results = platformClientUtil.getBOFactoryService().getSqlQueryResult(sql, ava); if(results!=null && results.length>0 && results[0] != null && results[0].length>0){ return WebUtil.getInt(results[0][0]); } } catch (PLException e) { throw new RuntimeException(e); } return 0; } /** * 根据方案查询个数 * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案里的变量值 * @return 个数 * @throws VciBaseException 查询出错的时候会抛出异常 */ @Override public int queryCountByScheme(String queryScheme, Map conditionMap, Map replaceMap) throws VciBaseException { QueryTemplate qt = getQtByName(queryScheme,replaceMap); return baseQueryCount(qt,conditionMap); } /** * 使用对象里的查询方案来查询数量 * @param c 对象所属类 * @param conditionMap 查询条件 * @return 数量 * @throws VciBaseException 查询出错会抛出异常 */ @Override public int queryCount(Class c, Map conditionMap) throws VciBaseException { return queryCount(WebUtil.getBtmTypeByObject(c), conditionMap); } /** * 根据sql语句来查询内容 --sql不能是select * 或者 select t.* * @param sql 为了防止SQL注入,值必须在sql语句里以:xxx格式,如 id =:idvalue,然后在conditionMap中添加建为idvalue的格式 * @param conditionMap 查询条件,必须与sql里对应 * @return CBO * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryBySql(String sql, Map conditionMap) throws VciBaseException { List allData = queryBySqlForMap(sql,conditionMap); if(allData == null || allData.size() == 0){ return new ArrayList(); }else{ return map2Cbos(allData); } } /** * 只用sql语句查询,这个和queryBySql区别是,特殊的sql(如包含有函数的)可以用这个方法,但是有sql注入风险 * @param sql sql语句,查询条件必须自行在其中处理完成 * @return CBO * @throws VciBaseException 查询出错活抛出异常 */ @Override public List queryByOnlySql(String sql) throws VciBaseException{ List allData = queryByOnlySqlForMap(sql); if(allData == null || allData.size() == 0){ return new ArrayList(); }else{ return map2Cbos(allData); } } /** * 只用sql语句查询,这个和queryBySqlForMap区别是,特殊的sql(如包含有函数的)可以用这个方法,但是有sql注入风险 * @param sql sql语句,查询条件必须自行在其中处理完成 * @return 数据映射 * @throws VciBaseException 查询出错活抛出异常 */ @Override public List queryByOnlySqlForMap(String sql) throws VciBaseException{ if(StringUtils.isBlank(sql)){ return new ArrayList<>(); } List dataList = new ArrayList<>(); try { KV[][] kvs = platformClientUtil.getQueryService().queryBySql(sql); if(kvs!=null && kvs.length>0){ for (int i = 0; i < kvs.length; i++) { Map data = new HashMap<>(); KV[] kv = kvs[i]; if(kv!=null && kv.length >0){ for (int j = 0; j < kv.length; j++) { KV kv1 = kv[j]; data.put(kv1.key,kv1.value); } } dataList.add(data); } } } catch (PLException e) { throw WebUtil.getVciBaseException(e); } return dataList; } /** * 使用sql语句查询后转换为对象 * @param sql sql语句 * @param tClass 对象的类 * @param 泛型 * @return 数据的列表 * @throws VciBaseException 查询和转换出错会抛出异常 */ @Override public List queryByOnlySqlForObj(String sql, Class tClass) throws VciBaseException{ List allData = queryByOnlySqlForMap(sql); //需要转换一下cbo的那个属性 Map fieldNameMap = WebUtil.getFieldNameMap(tClass); List dataList = new ArrayList<>(); if(!CollectionUtils.isEmpty(allData)){ allData.stream().forEach(data->{ Map thisData = new HashMap(); if(!CollectionUtils.isEmpty(data)){ data.forEach((key,value)->{ thisData.put(fieldNameMap.getOrDefault(((String)key).toLowerCase(Locale.ROOT), (String) key),value); }); } try { Object o = VciBaseUtil.mapToBean(thisData, tClass); dataList.add((T)o); } catch (Exception e) { if(logger.isErrorEnabled()){ logger.error("转换错误",e); } } }); } return dataList; } /** * 根据sql语句来查询内容,返回Map----sql不能是select * 或者 select t.* * @param sql 为了防止SQL注入,值必须在sql语句里以:xxx格式,如 id =:idvalue,然后在conditionMap中添加建为idvalue的格式 * @param conditionMap 查询条件,必须与sql里对应 * @return 业务数据的映射 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryBySqlForMap(String sql, Map conditionMap) throws VciBaseException { return queryBySqlForMap(sql,conditionMap,null); } /** * 和上个方法一样,多传了查询字段 selectKeys * @param sql sql语句 * @param conditionMap 查询条件 * @param selectKeys 查询的字段 * @return 数据 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryBySqlForMap(String sql,Map conditionMap, String[] selectKeys) throws VciBaseException { if(WebUtil.isNull(sql) || sql.toLowerCase(Locale.ROOT).indexOf("select") <0 || sql.toLowerCase(Locale.ROOT).indexOf("from") < 0){ throw new VciBaseException(msgCodePrefix + "sqlError",new String[]{sql}); } //处理大写FROM,平台查询会报错 if(sql.contains(" FROM ")){ sql = sql.replaceAll(" FROM "," from "); } AttributeValue[] ava = null; if(conditionMap!=null&&!conditionMap.isEmpty()){ ava = new AttributeValue[conditionMap.size()]; Iterator it = conditionMap.keySet().iterator(); int i = 0 ; while(it.hasNext()){ String key = it.next(); String value = conditionMap.get(key); if(value == null){ value = ""; } AttributeValue av = new AttributeValue(key, value); ava[i] = av; i++; } }else{ ava = new AttributeValue[0]; } try { String[][] results =platformClientUtil.getSqlQueryService(sql, ava); if(results.length>0){ if(selectKeys==null) { String selectSql = sql.substring(sql.toLowerCase(Locale.ROOT).indexOf("select") + 6, sql.toLowerCase(Locale.ROOT).indexOf(" from")).trim(); selectKeys = selectSql.split(","); } List allData = new ArrayList(); for(int i = 0 ; i < results.length ;i ++){ String[] values = results[i]; Map map = new HashMap(); for(int j = 0 ; j < selectKeys.length; j ++){ String field = selectKeys[j]; if(WebUtil.isNotNull(field)){ //field = field.toLowerCase().trim(); //有可能有as的情况 或者空格 或者有.的情况 //必须优先处理as和空格的情况,最后处理. if(field.indexOf(" as ")>-1){ field = field.substring(field.lastIndexOf(" as ") + 4); }else if(field.indexOf(" ")>-1){ field =field.substring(field.lastIndexOf(" ") + 1); }else if(field.indexOf(".")>-1){ field = field.substring(field.lastIndexOf(".") + 1); } //获取值 String value = ""; if(values.length>j){ value = values[j] == null?"":values[j]; } map.put(field, value); } } allData.add(map); } return allData; }else{ return new ArrayList(); } } catch (PLException e) { // logger.error(e.error_code,e); throw WebUtil.getVciBaseException(e); } } /** * 查询数据,返回对象,对象的字段上可以设置参照和枚举 * @param c 对象所属类 * @param conditionMap 查询条件 * @return 业务数据对象 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryObject(Class c, Map conditionMap) throws VciBaseException { return queryObject(c,conditionMap,new PageHelper(-1)); } /** * 查询数据,返回对象,支持分页 * @param c 对象所属类 * @param conditionMap 查询条件 * @param ph 分页组件 * @return 业务数据对象 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryObject(Class c, Map conditionMap, PageHelper ph) throws VciBaseException { //直接从对象里去查询业务类型的名称 swapConditionMap(c,conditionMap); //获取要查询的所有的列 Map allFieldAttrMap = WebUtil.getFieldNameMap(c); if(!allFieldAttrMap.containsKey("lctid")){ allFieldAttrMap.put("lctid","lctid"); } List queryAttr = new ArrayList(); Iterator it = allFieldAttrMap.keySet().iterator(); while(it.hasNext()){ queryAttr.add(it.next()); } for(String columnName:queryAttr){ String fieldName = allFieldAttrMap.get(columnName); allFieldAttrMap.remove(columnName); allFieldAttrMap.put(columnName.toLowerCase(),fieldName); } List allObject = new ArrayList(); List allCbos = queryCBO(WebUtil.getBtmTypeByObject(c), conditionMap, ph, allFieldAttrMap.keySet().stream().collect(Collectors.toList()));//执行查询 List oids = new ArrayList(); if(allCbos!=null&&allCbos.size()>0){ for(BusinessObject cbo : allCbos){ T obj = null; try { obj = c.newInstance(); WebUtil.copyValueToObjectFromCbos(cbo, obj,allFieldAttrMap);//为了少去查询一次字段 } catch (InstantiationException e) { } catch (IllegalAccessException e) { } if(obj !=null){ oids.add(cbo.oid); allObject.add(obj); } } } return allObject; } /** * 转换查询条件 * @param c 对象类 * @param conditionMap 查询条件 * @throws VciBaseException 转换出错 */ private void swapConditionMap(Class c ,Map conditionMap) throws VciBaseException{ WebUtil.alertNotNull(c,"对象所属类"); if(conditionMap == null){ conditionMap = new HashMap(); } conditionMap = WebUtil.getNotNullMap(conditionMap); //对于是参照字段的,也要检查conditionMap里面是否有,有的话需要替换成xxx.yy这种格式 try{ if(conditionMap.size()>0){ Map fieldMap = WebUtil.getFieldNameMap(c); Iterator it = fieldMap.keySet().iterator(); while(it.hasNext()){ String attrName = it.next(); if(attrName.indexOf(".")>-1){ //说明是参照的 String fieldName = fieldMap.get(attrName).toLowerCase(); if(conditionMap.containsKey(fieldName)){ String value = conditionMap.get(fieldName); conditionMap.remove(fieldName); conditionMap.put(attrName, value); } } } } }catch(Throwable e){ if(logger.isErrorEnabled()){ logger.error("转换条件出错",e); } } } /** * 使用查询方案来查询数据,返回对象,支持分页 * @param queryScheme 查询方案 * @param c 对象所属类 * @param conditionMap 查询条件 * @param ph 分页组件 * @return 业务对象 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryObjectByScheme(String queryScheme, Class c, Map conditionMap, PageHelper ph) throws VciBaseException { return queryObjectByScheme(queryScheme,c,conditionMap,ph,null); } /** * 使用查询方案来查询数据,返回对象,支持分页 * @param queryScheme 查询方案 * @param c 对象所属类 * @param conditionMap 查询条件 * @param ph 分页组件 * @param replaceMap 替换查询方案中的变量值 * @return 数据对象 * @throws VciBaseException 查询出错会抛出异常 */ @Override public List queryObjectByScheme(String queryScheme, Class c, Map conditionMap, PageHelper ph, Map replaceMap) throws VciBaseException { WebUtil.alertNotNull(queryScheme,"查询方案"); //直接从对象里去查询业务类型的名称 swapConditionMap(c,conditionMap); //获取要查询的所有的列 Map allFieldAttrMap = WebUtil.getFieldNameMap(c); List queryAttr = new ArrayList(); Iterator it = allFieldAttrMap.keySet().iterator(); while(it.hasNext()){ queryAttr.add(it.next()); } for(String columnName:queryAttr){ String fieldName = allFieldAttrMap.get(columnName); allFieldAttrMap.remove(columnName); allFieldAttrMap.put(columnName.toLowerCase(),fieldName); } List allObject = new ArrayList(); List allCbos = queryCBOByScheme(queryScheme, conditionMap, replaceMap,ph, allFieldAttrMap.keySet().stream().collect(Collectors.toList())); List oids = new ArrayList(); if(allCbos!=null&&allCbos.size()>0){ for(BusinessObject cbo : allCbos){ T obj = null; try { obj = c.newInstance(); WebUtil.copyValueToObjectFromCbos(cbo, obj,allFieldAttrMap);//为了少去查询一次字段 } catch (InstantiationException e) { } catch (IllegalAccessException e) { } if(obj !=null){ oids.add(cbo.oid); allObject.add(obj); } } } return allObject; } /** * 保存数据,可以在request中设置是否持久化 * * @param obj 要保存的对象 * @return 保存后包含的数据对象 * @throws VciBaseException 添加出错会抛出异常 */ @Override public BatchCBO addSave(T obj) throws VciBaseException { List list = new ArrayList(); list.add(obj); return batchAddSave(list); } /** * 值加到查询条件的map里 * @param field 属性名字 * @param value 属性值 * @return 查询条件的map */ public Map getOneQuery(String field, String value) { return this.getOneQuery(field, value, ""); } /** * 值加到查询条件的map里 * @param field 属性名字 * @param value 属性值 * @param options 操作符 * @return 查询条件的map */ public Map getOneQuery(String field, String value, String options) { Map conditionMap = new HashMap(); options = WebUtil.isNull(options) ? "" : (options.indexOf("\\") > -1 ? options.toUpperCase() : "\\" + options.toUpperCase()); conditionMap.put(field, options + value); return conditionMap; } /** * 设置值到业务数据对象上 * @param obj 对象的值 * @param btmType 业务类型的名称 * @param cbo 业务数据对象 * @param isEdit 是否为编辑 * @throws VciBaseException 设置出错会抛出异常 */ private void setValueToCbo(Object obj,String btmType,BusinessObject cbo ,boolean isEdit) throws VciBaseException{ Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}类中没有定义主键属性",new Object[]{obj.getClass()}); } //主键 Object pkValue = WebUtil.getValueFromField(pkField.getName(), obj); if(pkValue == null || (pkValue instanceof String && WebUtil.isNull(pkValue.toString()))){ WebUtil.setValueToField(pkField.getName(), obj, cbo.oid); pkValue = cbo.oid; }else{ cbo.oid = pkValue.toString(); } //进行非空,长度,重复的校验。。校验通过的赋值 Map fieldMap = WebUtil.getFieldNameMap(obj.getClass()); Iterator it = fieldMap.keySet().iterator(); while(it.hasNext()){ String attrName = it.next(); String fieldName = fieldMap.get(attrName); attrName = attrName.toLowerCase(); if(attrName.indexOf(".")<0 && attrName.indexOf("_")<0 && !attrName.toLowerCase().equalsIgnoreCase("lcstatus_text") && !attrName.toLowerCase().equals("lcstatustext")){//不是参照的属性才能赋值 Field thisField = WebUtil.getFieldForObject(fieldName, obj); String value = WebUtil.getValueFromFieldForCbo(thisField, obj); if(thisField.isAnnotationPresent(Column.class)){ //需要校验重复和为空.而且配置了Column的 Column column = (Column)thisField.getAnnotation(Column.class); String columnText = column.columnDefinition(); columnText = StringUtils.isNotBlank(columnText)?columnText:fieldName; //四个默认属性不判断非空 boolean is4 = false;//是不是4个属性 String[] defaultFieldName_notChecks = {"creator","lastModifier","ts","oid","createTime"}; //判断fieldName不是 for (String defaultFieldName_notCheck:defaultFieldName_notChecks){ if(defaultFieldName_notCheck.equals(fieldName)){ is4 = true; } } if(!is4) {//不是4个属性才判断非空 //判断非空 if (!column.nullable() && WebUtil.isNull(value)) { throw new VciBaseException("{0}字段不能为空", new Object[]{columnText}); } } value = value==null?"":value; if(column.length()>0 && value.length()> column.length()){//长度 throw new VciBaseException("{0}字段的长度太长,要求{1},实际{2}", new Object[]{columnText,column.length(),value.length()}); } if(!thisField.equals(pkField) && column.unique()){ //说明不能重复,那么就需要查询一下 Map queryRepeatMap = getOneQuery(attrName, value); if(isEdit){ queryRepeatMap.put("oid", QueryOptionConstant.NOTEQUAL + cbo.oid); } if(queryCount(btmType, queryRepeatMap)>0){ throw new VciBaseException("{0}字段的值{1}在系统中已经存在", new Object[]{columnText,value}); } } } if(value==null){ continue; } if(isEdit && checkUnAttrUnEdit(attrName)){ //编辑的时候,TS和OID不能设置.因为平台那接口设置了就会报错 }else { //需要判断是否默认的属性,如果是默认属性,则使用对象的赋值的方式 if (WebUtil.isDefaultField(fieldName) && WebUtil.inArray(new String[]{"id", "name", "description","lcstatus","revisionvalue","versionvalue"}, fieldName.toLowerCase())) { WebUtil.setValueToField(fieldName, cbo, value); ObjectTool.setBOAttributeValue(cbo, attrName.toLowerCase(), value); } else { ObjectTool.setBOAttributeValue(cbo, attrName.toLowerCase(), value); } } } } } /** * 批量保存数据,可以在request里设置不持久化 * @param list 要保存的数据对象列表 * @return 需要创建的CBO,CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 报错出错会抛出异常 */ @Override public BatchCBO batchAddSave(List list) throws VciBaseException { if(list==null){ throw new VciBaseException(VciBaseException.paramNull); } Set allCbos = new HashSet(); for(T obj : list){ //需要处理默认值 Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}对象中没有设置主键的属性",new Object[]{obj.getClass()}); } String btmType = WebUtil.getBtmTypeByObject(obj.getClass()); BusinessObject cbo = createCBOByBtmName(btmType); setValueToCbo(obj,btmType,cbo,false); allCbos.add(cbo); } //因为使用了平台,事务没办法统一,所以只能是在服务调用服务之间的时候,返回要保存的数据; BatchCBO batchCbo = new BatchCBO(); batchCbo.setCreateCbos(allCbos); if(allCbos.size()>0 && WebUtil.isPersistence()){ if(logger.isInfoEnabled()) { logger.info("执行了持久化"); } persistenceBatch(batchCbo); } return batchCbo; } /** * 修改数据,可以在request里设置不持久化 * * @param obj 修改数据对象 * @return 需要创建的CBO, CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 修改出错会抛出异常 */ @Override public BatchCBO editSave(T obj) throws VciBaseException { List list = new ArrayList(); list.add(obj); return batchEditSave(list); } /** * 批量修改数据,可以在request里设置不持久化 * @param list 要保存的数据对象列表 * @return 需要创建的CBO,CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 修改出错会抛出异常 */ @Override public BatchCBO batchEditSave(List list) throws VciBaseException { if(list==null){ throw new VciBaseException(VciBaseException.paramNull); } Set allUpdateCbos = new HashSet(); Set allDeleteCbos = new HashSet(); Set allAddCbos = new HashSet(); Map btmOidsMap = new HashMap(); for(T obj : list){ String btmType = WebUtil.getBtmTypeByObject(obj.getClass()); String tempOids = ""; if(btmOidsMap.containsKey(btmType)){ tempOids = btmOidsMap.get(btmType); } Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}对象中没有设置主键的属性",new Object[]{obj.getClass()}); } //主键 Object pkValue = WebUtil.getValueFromField(pkField.getName(), obj); if(pkValue == null || (pkValue instanceof String && WebUtil.isNull(pkValue.toString()))){ }else{ tempOids += "'" + pkValue.toString() + "',"; } btmOidsMap.put(btmType, tempOids); } List needUpdateCbos = new ArrayList(); Iterator it = btmOidsMap.keySet().iterator(); while(it.hasNext()){ String btmType = it.next(); String oids = WebUtil.removeComma(btmOidsMap.get(btmType)); Map conditionMap = new HashMap(); conditionMap.put("oid", QueryOptionConstant.IN +"(" + oids + ")"); needUpdateCbos.addAll(queryCBO(btmType, conditionMap)); } //需要先从后台查询 for(T obj : list){ //需要处理默认值 Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}对象中没有设置主键的属性",new Object[]{obj.getClass()}); } //主键 Object pkValue = WebUtil.getValueFromField(pkField.getName(), obj); BusinessObject cbo = null; if(pkValue == null || (pkValue instanceof String && WebUtil.isNull(pkValue.toString()))){ continue; }else{ for(BusinessObject tempCbo : needUpdateCbos){ if(tempCbo.oid.equalsIgnoreCase(pkValue.toString().trim())){ cbo = tempCbo; break; } } } String btmType = WebUtil.getBtmTypeByObject(obj.getClass()); //拷贝之前先清除已经有的值 cbo.newAttrValList = new AttributeValue[0]; setValueToCbo(obj,btmType,cbo,true); allUpdateCbos.add(cbo); } BatchCBO batchCbo = new BatchCBO(); batchCbo.setCreateCbos(allAddCbos); batchCbo.setUpdateCbos(allUpdateCbos); batchCbo.setDeleteCbos(allDeleteCbos); //因为使用了平台,事务没办法统一,所以只能是在服务调用服务之间的时候,返回要保存的数据; if(WebUtil.isPersistence()){ persistenceBatch(batchCbo); } return batchCbo; } /** * 删除数据,可以在request里设置不持久化 * * @param obj 删除数据对象 * @return 需要创建的CBO, CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 删除出错会抛出异常 */ @Override public BatchCBO delete(T obj) throws VciBaseException { return delete(obj,true); } @Override public BatchCBO delete(T obj,boolean checkRefered) throws VciBaseException{ List list = new ArrayList(); list.add(obj); return batchDelete(list,checkRefered); } /** * 批量删除数据,可以在request里设置不持久化 * @param list 需要删除数据对象列表 * @return 需要创建的CBO,CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 删除出错会抛出异常 */ @Override public BatchCBO batchDelete(List list) throws VciBaseException { return batchDelete(list,true); } /** * 批量删除 * @param list 需要删除数据对象列表 * @param checkRefered 是否删除引用的数据 * @param 类型 * @return 受影响的行数 * @throws VciBaseException 执行出错会抛出异常 */ @Override public BatchCBO batchDelete(List list,boolean checkRefered) throws VciBaseException{ if(list==null){ throw new VciBaseException(VciBaseException.paramNull); } Set allDeleteCbos = new HashSet(); Map> btmBos = new HashMap>(); for(Object obj : list){ //有可能不属于同一个业务类型下 Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}对象中没有设置主键的属性",new Object[]{obj.getClass()}); } String btmType = WebUtil.getBtmTypeByObject(obj.getClass()); String pkValue = WebUtil.getValueFromFieldForCbo(pkField, obj); //所以这里也是直接查询的单个' List deleteCbos = queryCBO(btmType, getOneQuery( WebUtil.getCboAttrNameFromField(pkField, obj.getClass()), pkValue)); if(deleteCbos!=null){ allDeleteCbos.addAll(deleteCbos); if(!btmBos.containsKey(btmType)){ btmBos.put(btmType, deleteCbos); }else{ btmBos.get(btmType).addAll(deleteCbos); } } } //不执行验证,是true才检查,默认不检查 if(checkRefered) { checkCanDelete(btmBos); } BatchCBO batchCbo = new BatchCBO(); batchCbo.setDeleteCbos(allDeleteCbos); if(allDeleteCbos.size()>0 && WebUtil.isPersistence()){ persistenceBatch(batchCbo); } return batchCbo; } /** * 校验是否可以删除 * @param btmBos 业务数据 * @throws VciBaseException 被引用的时候会抛出异常 */ private void checkCanDelete(Map> btmBos) throws VciBaseException{ //查找表是否被其他的属性引用 if(!btmBos.isEmpty()){ Iterator it = btmBos.keySet().iterator(); while(it.hasNext()){ //每一个业务类型找一次引用字段 String btmType = it.next(); List usedAttrVOs = btmService.listBtmUsedInfo(btmType); Map> allReferAttr = Optional.ofNullable(usedAttrVOs).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.groupingBy(s -> s.getPkBtmType())); List thisBtmTypeBo = btmBos.get(btmType); if(thisBtmTypeBo.size()== 0){ return; } String oids = ""; for(BusinessObject cbo : thisBtmTypeBo){ oids += "'" + cbo.oid + "',"; } oids = WebUtil.removeComma(oids); if(!allReferAttr.isEmpty()){ Iterator atIt = allReferAttr.keySet().iterator(); while(atIt.hasNext()){ String referBtmType = atIt.next();//拥有参照当前业务类型的属性所在业务类型 List allAttr = allReferAttr.get(referBtmType);//该业务类型中所有的引用当前业务类型的 Map conditionMap = new HashMap(); for(OsUsedAttributeVO attr : allAttr){ conditionMap.put(attr.getId(),QueryOptionConstant.IN +"(" + oids + ")");//平台所有的引用都是oid } if(queryCount(referBtmType, conditionMap)>0){ throw new VciBaseException(msgCodePrefix + "refered", new String[]{referBtmType}); } } } } } } /** * 根据查询条件来删除数据 * @param c 删除数据对象所属类 * @param conditionMap 查询条件 * @return 需要创建的CBO,CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 删除出错会抛出异常 */ @Override public BatchCBO deleteByCondition(Class c, Map conditionMap) throws VciBaseException { swapConditionMap(c,conditionMap); String btmType = WebUtil.getBtmTypeByObject(c); return deleteByCondition(btmType,conditionMap); } /** * 根据查询条件来删除数据 * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @return 需要创建的CBO,CLO;需要修改的CBO,CLO;需要删除的CBO,CLO * @throws VciBaseException 删除出错会抛出异常 */ @Override public BatchCBO deleteByCondition(String btmType, Map conditionMap) throws VciBaseException { if(conditionMap == null){ conditionMap = new HashMap(); } conditionMap.put(QUERY_FILTER_SECRET,BooleanEnum.FASLE.getValue());//查询的时候不能查询密级 List allCbos = queryCBO(btmType, conditionMap); BatchCBO batchCbo = new BatchCBO(); if(allCbos.size()>0){ Map> btmBos = new HashMap>(); btmBos.put(btmType, allCbos); checkCanDelete(btmBos); Set allDeleteCbos = new HashSet(); allDeleteCbos.addAll(allCbos); batchCbo.setDeleteCbos(allDeleteCbos); } if(allCbos.size()>0 && WebUtil.isPersistence()){ persistenceBatch(batchCbo); } return batchCbo; } /** * 通过sql语句来删除---不到万不得已不用使用这个方法 * @param sql 防止SQL注入,值必须使用:xxx方式,如id=:idvalue,并在conditionMap里放置idvalue为key的数据 * @param conditionMap 查询条件 * @return 删除的数据量 * @throws VciBaseException 删除出错会抛出异常 */ @Override public BatchCBO deleteBySql(String sql, Map conditionMap) throws VciBaseException { //很遗憾,平台不支持使用sql来删除 //所有我们需要先查询出来,然后校验是否可以删除 sql = sql.replace("delete", "select oid,btmname "); if(sql.toLowerCase().indexOf(" from ") < 0){ throw new VciBaseException(msgCodePrefix + "sqlError",new String[]{sql}); } BatchCBO batchCbo = new BatchCBO(); String selectSql = sql.substring(0,sql.toLowerCase().indexOf(" from ")); String fromSql = sql.substring(sql.toLowerCase().indexOf(" from ")); if(selectSql.toLowerCase().indexOf("btmname")<0){ selectSql += ",btmname "; } sql = selectSql + fromSql; List allDelete =queryBySql(sql, conditionMap); if(allDelete.size() == 0){ return batchCbo;//没有删除任何数据 } //找btmType; String btmType = ObjectTool.getBOAttributeValue(allDelete.get(0),"btmName"); Map> btmBos = new HashMap>(); btmBos.put(btmType, allDelete); checkCanDelete(btmBos); Set allDeleteCbos = new HashSet(); //我们需要从后台查询整个的内容才可以执行删除, String oids = ""; for(BusinessObject cbo : allDelete){ oids += "'" + cbo.oid + "',"; } conditionMap.clear(); conditionMap.put("oid", QueryOptionConstant.IN + "(" + WebUtil.removeComma(oids) + ")"); List inDbCbos = queryCBO(btmType, conditionMap); allDeleteCbos.addAll(inDbCbos); batchCbo.setDeleteCbos(allDeleteCbos); if(allDeleteCbos.size()>0 && WebUtil.isPersistence()){ persistenceBatch(batchCbo); } return batchCbo; } /** * 检验是否为最新的- * @param obj 数据对象,里面必须包括主键和ts字段 * @return 相等时为true * @throws VciBaseException 不符合要求会抛出异常 */ @Override public boolean checkTs(Object obj) throws VciBaseException { Field pkField = WebUtil.getPkFieldForObj(obj.getClass()); if(pkField == null){ throw new VciBaseException("{0}对象中没有设置主键的属性",new Object[]{obj.getClass()}); } String pkValue = WebUtil.getValueFromFieldForCbo(pkField, obj); Field tsField = WebUtil.getTsField(obj.getClass()); if(tsField == null){ throw new VciBaseException("{0}对象中没有TS字段",new Object[]{obj.getClass()}); } String ts = WebUtil.getValueFromFieldForCbo(tsField, obj); Map conditionMap = getOneQuery("oid", pkValue); List causeList = new ArrayList<>(); causeList.add("ts"); List cbos = queryCBO(WebUtil.getBtmTypeByObject(obj.getClass()), conditionMap,new PageHelper(-1),causeList); if(CollectionUtils.isEmpty(cbos)){ return false; } if(ts.contains(".")){ ts = ts.substring(0,ts.lastIndexOf(".")); } return cbos.get(0).ts == Long.valueOf(ts); } /** * 根据业务类型来查询列表数据; * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByBo(String btmType, Map conditionMap) throws VciBaseException { return queryGridByBo(btmType,conditionMap,new PageHelper(-1,true)); } /** * 根据业务类型来查询列表数据;可以在pageHelper中设置是否查询总数 * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @param ph 分页和排序组件 * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByBo(String btmType, Map conditionMap, PageHelper ph) throws VciBaseException { return queryGridByBo(btmType,conditionMap,ph,null); } /** * 根据业务类型来查询列表数据;可以设置要查询的字段,包括参照和枚举字段;可以在pageHelper中设置是否查询总数 * @param btmType 业务类型名称 * @param conditionMap 查询条件 * @param ph 分页和排序组件 * @param clauseList 列表数据,数据是Map形式的, * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByBo(String btmType, Map conditionMap, PageHelper ph, List clauseList) throws VciBaseException { if(ph == null){ ph = new PageHelper(-1); ph.setQueryTotal(true); } List allCbos = queryCBO(btmType, conditionMap, ph, clauseList); DataGrid dg = new DataGrid(); if(allCbos.size()>0){ List mapList = cbos2Map(allCbos); dg.setData(mapList); //肯定是当前分页有值,才会有总数 if(ph.isQueryTotal()){ dg.setTotal(queryCount(btmType, conditionMap)); } } dg.setLimit(ph.getLimit()); dg.setPage(ph.getPage()); return dg; } /** * 根据查询方案来查询列表数据; * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByScheme(String queryScheme, Map conditionMap) throws VciBaseException { return queryGridByScheme(queryScheme,conditionMap,null); } /** * 根据查询方案来查询列表数据; * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案的变量值 * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByScheme(String queryScheme, Map conditionMap, Map replaceMap) throws VciBaseException { return queryGridByScheme(queryScheme,conditionMap,replaceMap,new PageHelper(-1,true),null); } /** * 根据查询方案来查询列表数据;可以在pageHelper中设置是否查询总数,pageHelper的优先级最大; * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案的变量值 * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByScheme(String queryScheme, Map conditionMap, Map replaceMap, PageHelper ph) throws VciBaseException { return queryGridByScheme(queryScheme,conditionMap,replaceMap,ph,null); } /** * 根据查询方案来查询列表数据,可以自定义查询的列,包括参照和枚举 * @param queryScheme 查询方案 * @param conditionMap 查询条件 * @param replaceMap 替换查询方案的变量值 * @param ph 分页和排序字段 * @param clauseList 查询的列,参照使用xx.yy;枚举使用xx_enumCode; * @return 列表数据,数据是Map形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridByScheme(String queryScheme, Map conditionMap, Map replaceMap, PageHelper ph, List clauseList) throws VciBaseException { if(ph == null){ ph = new PageHelper(-1,true); } List allCbos = queryCBOByScheme(queryScheme, conditionMap,replaceMap, ph, clauseList); DataGrid dg = new DataGrid(); if(allCbos.size()>0){ List mapList = cbos2Map(allCbos); dg.setData(mapList); //肯定是当前分页有值,才会有总数 if(ph.isQueryTotal()){ dg.setTotal(queryCountByScheme(queryScheme, conditionMap, replaceMap)); } } dg.setLimit(ph.getLimit()); dg.setPage(ph.getPage()); return dg; } /** * 查询对象的列表数据 * @param c 查询对象所属类 * @param conditionMap 查询条件 * @return 列表数据,数据是对象形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridClass(Class c, Map conditionMap) throws VciBaseException { return queryGridClass(c,conditionMap,new PageHelper(-1,true)); } /** * 查询对象的列表数据 * @param c 查询对象所属类 * @param conditionMap 查询条件 * @param ph 分页和排序字段,可以设置是否查询总数 * @return 列表数据,数据是对象形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridClass(Class c, Map conditionMap, PageHelper ph) throws VciBaseException { if(ph == null){ ph = new PageHelper(-1,true); } List allObjs = queryObject(c, conditionMap, ph); DataGrid dg = new DataGrid(); if(allObjs.size()>0){ dg.setData(allObjs); //肯定是当前分页有值,才会有总数 if(ph.isQueryTotal()){ dg.setTotal(queryCount(c, conditionMap)); } } dg.setLimit(ph.getLimit()); dg.setPage(ph.getPage()); return dg; } /** * 通过查询方案查询对象的列表数据 * @param queryScheme 查询方案 * @param c 查询对象所属类 * @param conditionMap 查询条件 * @return 列表数据,数据是对象形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridClassByScheme(String queryScheme, Class c, Map conditionMap,Map replaceMap) throws VciBaseException { return queryGridClassByScheme(queryScheme,c,conditionMap,replaceMap,new PageHelper(-1,true)); } /** * 通过查询方案查询对象的列表数据 * @param queryScheme 查询方案 * @param c 查询对象所属类 * @param conditionMap 查询条件 * @param ph 分页和排序字段,可以设置是否查询总数 * @return 列表数据,数据是对象形式的,包含分页信息 * @throws VciBaseException 查询出错会抛出异常 */ @Override public DataGrid queryGridClassByScheme(String queryScheme, Class c, Map conditionMap, Map replaceMap,PageHelper ph) throws VciBaseException { if(ph == null){ ph = new PageHelper(-1,true); } List allObjs = queryObjectByScheme(queryScheme, c, conditionMap, ph, replaceMap); DataGrid dg = new DataGrid(); if(allObjs.size()>0){ dg.setData(allObjs); //肯定是当前分页有值,才会有总数 if(ph.isQueryTotal()){ dg.setTotal(queryCountByScheme(queryScheme, conditionMap, replaceMap)); } } dg.setLimit(ph.getLimit()); dg.setPage(ph.getPage()); return dg; } /** * BusinessObject 转为HashMap * @param cbos CBOS * @return map * @throws VciBaseException 转换出错会抛出异常 */ @Override public List cbos2Map(List cbos) throws VciBaseException { List mapList = new ArrayList(); for(BusinessObject cbo : cbos){ mapList.add(cbo2Map(cbo)); } return mapList; } /** * BusinessObject 转为HashMap * @param cbo 业务数据对象 * @return map * @throws VciBaseException 转换出错会抛出异常 */ @Override public Map cbo2Map(BusinessObject cbo) throws VciBaseException { Map map = new HashMap(); WebUtil.copyValueToMapFromCbos(cbo, map); return map; } /** * map转为BusinessObject * @param mapList map列表 * @return 业务数据列表 * @throws VciBaseException 转换出错会抛出异常 */ @Override public List map2Cbos(List mapList) throws VciBaseException { List cboList = new ArrayList(); for(Map map : mapList){ cboList.add(map2Cbo(map)); } return cboList; } /** * map转为BusinessObject * @param map map * @return 业务数据 * @throws VciBaseException 转换出错会抛出异常 */ @Override public BusinessObject map2Cbo(Map map) throws VciBaseException { BusinessObject cbo = new BusinessObject(); WebUtil.copyValueToCboFromMap(cbo, map); return cbo; } /** * 批量保存业务类型和链接类型的信息 * @param batchCbos 批量数据容器 * @throws VciBaseException 保存出错 */ @Override public void persistenceBatch(BatchCBO batchCbos) throws VciBaseException { persistenceBatch(batchCbos,false); } /** * 批量保存业务类型和链接类型的信息---只有确保数据容器里有参照字段,枚举字段等内容的时候,才调用这个方法,因为这个方法性能低下 * @param batchCbos 批量数据容器 * @param isCheckReferColumn 是否校验是否含有参照字段 * @throws VciBaseException 保存出错 */ @Override public void persistenceBatch(BatchCBO batchCbos, boolean isCheckReferColumn) throws VciBaseException { if(batchCbos == null ){ return; } if(isCheckReferColumn){ deleteReferAttrInCbo(batchCbos.getCreateCbos()); deleteReferAttrInCbo(batchCbos.getUpdateCbos()); deleteReferAttrInCbo(batchCbos.getDeleteCbos()); } try { platformClientUtil.getBOFactoryService().batchCUDBOLO(cboArray2Bo(batchCbos.getCreateCboArray()), cloArray2Lo(batchCbos.getCreateCloArray()), cboArray2Bo(batchCbos.getUpdateCboArray()), cloArray2Lo(batchCbos.getUpdateCloArray()), cboArray2Bo(batchCbos.getDeleteCboArray()), cloArray2Lo(batchCbos.getDeleteCloArray())); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } } /** * 转换为cbo * @param cbos 客户端对象 * @return 业务对象 */ private BusinessObject[] cboArray2Bo(BusinessObject[] cbos){ if(cbos == null ||cbos.length == 0){ return new BusinessObject[0]; } BusinessObject[] bos = new BusinessObject[cbos.length]; for(int i = 0; i < cbos.length; i++){ bos[i] = cbos[i]; } return bos; } /** * 转换clob * @param clos 客户端对象 * @return 业务对象 */ private LinkObject[] cloArray2Lo(LinkObject[] clos){ if(clos == null ||clos.length == 0){ return new LinkObject[0]; } LinkObject[] bos = new LinkObject[clos.length]; for(int i = 0; i < clos.length; i++){ bos[i] = clos[i]; } return bos; } /** * 删除枚举和参照的属性 * @param cbos 业务数据 */ private void deleteReferAttrInCbo(Set cbos){ if(cbos !=null){ for(BusinessObject cbo : cbos){ BusinessObject bo = cbo; List newAttr = new ArrayList(); if (bo.newAttrValList != null) { for (int i = 0; i < bo.newAttrValList.length; ++i) { AttributeValue av = bo.newAttrValList[i]; if (WebUtil.isNormalAttr(av.attrName)) { if(av.attrVal == null){ av.attrVal = ""; } newAttr.add(av); } } } bo.newAttrValList = newAttr.toArray(new AttributeValue[0]); cbo = bo; } } } /** * 使用主键获取对象 * @param oid 主键 * @param doClass 数据对象 * @return 数据对象 * @throws VciBaseException 参数为空,数据对象不存在会抛出异常 */ @Override public T selectByOid(String oid, Class doClass) throws VciBaseException { VciBaseUtil.alertNotNull(oid,"主键"); Map conditionMap = new HashMap(); conditionMap.put("oid", oid.trim()); conditionMap.put(QUERY_FILTER_SECRET,BooleanEnum.FASLE.getValue()); conditionMap.put(QUERY_FILTER_DATARIGHT,BooleanEnum.FASLE.getValue()); List list = queryObject(doClass, conditionMap); if(CollectionUtils.isEmpty(list)){ throw new VciBaseException("数据在系统中不存在{0}",new String[]{oid}); } return list.get(0); } /** * 使用主键获取对象 * * @param oid 主键 * @param btmName 业务类型的名称 * @return cbo * @throws VciBaseException 参数为空,数据不存在会抛出异常 */ @Override public BusinessObject selectCBOByOid(String oid, String btmName) throws VciBaseException { VciBaseUtil.alertNotNull(oid,"主键",btmName,"业务类型"); Map conditionMap = new HashMap(); conditionMap.put("oid", oid.trim()); List cbos = queryCBO(btmName, conditionMap); if(CollectionUtils.isEmpty(cbos)){ throw new VciBaseException("数据在系统中不存在{0}",new String[]{oid}); } return cbos.get(0); } /** * 使用主键集合获取对象 * @param oidCollection 主键集合 * @param doClass 数据对象的类 * @return 数据对象, 为空的时候列表为空,不是Null * @throws VciBaseException 参数为空,查询出错会抛出异常 */ @Override public List selectByOidCollection(Collection oidCollection, Class doClass) throws VciBaseException { VciBaseUtil.alertNotNull(oidCollection,"主键集合"); Collection> oidCollections = WebUtil.switchCollectionForOracleIn(oidCollection); List doList = new ArrayList(); for(Collection oids: oidCollections){ Map conditionMap = new HashMap(); conditionMap.put("oid",QueryOptionConstant.IN + "(" + WebUtil.toInSql(oids.toArray(new String[0])) + ")"); List list = queryObject(doClass, conditionMap); if(CollectionUtils.isEmpty(list)){ throw new VciBaseException("{0}条数据在系统中不存在",new String[]{String.valueOf(oids.size())}); } doList.addAll(list); } return doList; } /** * 使用主键集合获取对象 * * @param oidCollection 主键集合 * @param btmName 业务类型的名称 * @return 业务数据的集合 * @throws VciBaseException 参数为空,查询出错会抛出异常 */ @Override public List selectCBOByOidCollection(Collection oidCollection, String btmName) throws VciBaseException { VciBaseUtil.alertNotNull(oidCollection,"主键集合",btmName,"业务类型"); Collection> oidCollections = WebUtil.switchCollectionForOracleIn(oidCollection); List doList = new ArrayList(); for(Collection oids: oidCollections){ Map conditionMap = new HashMap(); conditionMap.put("oid",QueryOptionConstant.IN + "(" + WebUtil.toInSql(oids.toArray(new String[0])) + ")"); List list = queryCBO(btmName, conditionMap); if(CollectionUtils.isEmpty(list)){ throw new VciBaseException("{0}条数据在系统中不存在",new String[]{String.valueOf(oids.size())}); } doList.addAll(list); } return doList; } /** * 使用新的查询封装器来查询 * * @param queryWrapper 查询封装器 * @param doClass 对象的所属类 * @return 数据 * @throws VciBaseException 查询出错的会抛出异常 */ @Override public List selectByQueryWrapper(VciQueryWrapperForDO queryWrapper, Class doClass) throws VciBaseException { String sql= queryWrapper.getSelectFieldSql() + " from " + queryWrapper.getTableName() + " " + queryWrapper.getTableNick() + queryWrapper.getLinkTableSql() ; String whereSql = queryWrapper.getWhereSql(); if(StringUtils.isNotBlank(whereSql)){ sql += " where " + whereSql; } return queryByOnlySqlForObj(sql,doClass); } /** * 使用新的查询封装器来查询总数 * * @param queryWrapper 查询封装器 * @param doClass 对象的所属类 * @return 数据 * @throws VciBaseException 查询出错的会抛出异常 */ @Override public int countByQueryWrapper(VciQueryWrapperForDO queryWrapper, Class doClass) throws VciBaseException { VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(null,doClass); BeanUtil.convert(queryWrapper,queryWrapperForDO); queryWrapperForDO.clearPage(); queryWrapperForDO.wrapperSql(); String sql= queryWrapperForDO.getSelectFieldSql() + " from " + queryWrapperForDO.getTableName() + " " + queryWrapperForDO.getTableNick() + queryWrapperForDO.getLinkTableSql() ; String whereSql = queryWrapperForDO.getWhereSql(); if(StringUtils.isNotBlank(whereSql)){ sql += " where " + whereSql; } return queryCountBySql("select count(*) from (" +sql + ")",null); } }