|
楼主 |
发表于 2023-6-10 16:20:54
|
显示全部楼层
以下为朋友分享的共享工具类
- package nc.bs.arap.waasvo;
- import nc.bs.framework.common.InvocationInfoProxy;
- import nc.pubitf.bd.accessor.GeneralAccessorFactory;
- import nc.vo.bd.accessor.IBDData;
- import org.apache.commons.lang3.StringUtils;
- /**
- * @author:lxd
- * @Description: 通用存取器工厂
- * 注意:如果接口使用,使用前需要加载环境上下文信息如:InvocationInfoProxy.getInstance().setGroupId("主键")
- */
- public class GeneralAccessorUtil {
- // 元数据主键
- private String classId;
- // 主键
- private String pk;
- // 名称
- private String name;
- // 编码
- private String code;
- // 业务单元/集团,不传值默认为集团
- private String pkOrg;
- // 主键数组
- private String[] pks;
- // 名称数组
- private String[] names;
- // 编码数组
- private String[] codes;
- // 异常信息
- private String errorMessage;
- public static void main(String[] args) {
- //测试示例:物料元数据id(eae040f4-3c88-413d-abc9-b15774463d46)
- // 1.正常情况使用
- IBDData bdData = new GeneralAccessorUtil("eae040f4-3c88-413d-abc9-b15774463d46").setCode("1030103622").getBdData();
- System.out.println(bdData);
- // 2.如果元数据id有问题,或者参数有问题导致数据没有查询出来
- GeneralAccessorUtil bdData1 = new GeneralAccessorUtil("1-3c88-413d-abc9-b15774463d46").setPk("1");
- // 3.此时bdData1.getBDData()等于null,这时候在不影响业务的情况下,通过方法getErrorMessage获取异常信息记录下来
- System.out.println(bdData1.getBdData());
- System.out.println(bdData1.getErrorMessage());
- }
- /**
- * 备注:元数据id必传
- * @Param: [classId]
- * @return:
- * @Author: lxd
- */
- public GeneralAccessorUtil(String classId) {
- this.classId = classId;
- }
- public GeneralAccessorUtil(String classId,String pk) {
- this.classId = classId;
- this.pk = pk;
- }
- public GeneralAccessorUtil(String classId,String pk,String name) {
- this.classId = classId;
- this.name = name;
- this.pk = pk;
- }
- public GeneralAccessorUtil(String classId,String pk,String name,String code) {
- this.classId = classId;
- this.pk = pk;
- this.name = name;
- this.code = code;
- }
- public GeneralAccessorUtil(String classId,String pk,String name,String code,String[] pks) {
- this.classId = classId;
- this.pk = pk;
- this.name = name;
- this.code = code;
- this.pks = pks;
- }
- public GeneralAccessorUtil(String classId,String pk,String name,String code,String[] pks,String[] names) {
- this.classId = classId;
- this.pk = pk;
- this.name = name;
- this.code = code;
- this.pks = pks;
- this.names = names;
- }
- public GeneralAccessorUtil(String classId,String pk,String name,String code,String[] pks,String[] names,String[] codes) {
- this.classId = classId;
- this.pk = pk;
- this.name = name;
- this.code = code;
- this.pks = pks;
- this.names = names;
- this.codes = codes;
- }
- /**
- * 备注:获取单个档案
- * @Param: []
- * @return: nc.vo.bd.accessor.IBDData
- * @Author: lxd
- */
- public IBDData getBdData() {
- try {
- if(StringUtils.isNotEmpty(pk)){
- return GeneralAccessorFactory.getAccessor(classId).getDocByPk(pk);
- }
- if(StringUtils.isNotEmpty(name)){
- return GeneralAccessorFactory.getAccessor(classId).getDocByNameWithMainLang(getPkOrg(),name);
- }
- if(StringUtils.isNotEmpty(code)){
- return GeneralAccessorFactory.getAccessor(classId).getDocByCode(getPkOrg(),code);
- }
- } catch (Exception e) {
- errorMessage = getStackTrace(e);
- }
- return null;
- }
- /**
- * 备注:获取多个档案数据
- * @Param: []
- * @return: nc.vo.bd.accessor.IBDData[]
- * @Author: lxd
- */
- public IBDData[] getBdDatas() {
- try {
- if(pks.length > 0){
- return GeneralAccessorFactory.getAccessor(classId).getDocbyPks(pks);
- }
- if(names.length > 0){
- return GeneralAccessorFactory.getAccessor(classId).getDocByNamesWithMainLang(getPkOrg(),names);
- }
- if(codes.length > 0){
- return GeneralAccessorFactory.getAccessor(classId).getDocByCodes(getPkOrg(),codes);
- }
- } catch (Exception e) {
- errorMessage = getStackTrace(e);
- }
- return null;
- }
- public String getClassId() {
- return classId;
- }
- public GeneralAccessorUtil setClassId(String classId) {
- this.classId = classId;
- return this;
- }
- public String getPk() {
- return pk;
- }
- public GeneralAccessorUtil setPk(String pk) {
- this.pk = pk;
- return this;
- }
- public String getName() {
- return name;
- }
- public GeneralAccessorUtil setName(String name) {
- this.name = name;
- return this;
- }
- public String getCode() {
- return code;
- }
- public GeneralAccessorUtil setCode(String code) {
- this.code = code;
- return this;
- }
- public String getPkOrg() {
- if(StringUtils.isEmpty(pkOrg)) {
- // 获取集团信息
- pkOrg = InvocationInfoProxy.getInstance().getGroupId();
- }
- return pkOrg;
- }
- public GeneralAccessorUtil setPkOrg(String pkOrg) {
- this.pkOrg = pkOrg;
- return this;
- }
- public String[] getPks() {
- return pks;
- }
- public GeneralAccessorUtil setPks(String[] pks) {
- this.pks = pks;
- return this;
- }
- public String[] getNames() {
- return names;
- }
- public GeneralAccessorUtil setNames(String[] names) {
- this.names = names;
- return this;
- }
- public String[] getCodes() {
- return codes;
- }
- public GeneralAccessorUtil setCodes(String[] codes) {
- this.codes = codes;
- return this;
- }
- public String getErrorMessage() {
- return errorMessage;
- }
- public void setErrorMessage(String errorMessage) {
- this.errorMessage = errorMessage;
- }
- /**
- * 备注:获取堆栈信息
- * @Param: [e]
- * @return: java.lang.String
- * @Author: lxd
- */
- private static final String getStackTrace(Exception e) {
- StringBuffer msg = new StringBuffer(e.getMessage() + "\r\n");
- StackTraceElement[] stackTrace = e.getStackTrace();
- for (int i = 0, j = stackTrace.length; i < j; i++) {
- msg.append(" " + stackTrace[i] + "\r\n");
- }
- return msg.toString();
- }
- }
复制代码 |
|