Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
@@ -4,10 +4,10 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vci.ubcs.code.bo.CodeClassifyFullInfoBO;
import com.vci.ubcs.code.bo.CodeTemplateAttrSqlBO;
import com.vci.ubcs.code.constant.FrameWorkDefaultValueConstant;
import com.vci.ubcs.code.constant.MdmBtmTypeConstant;
import com.vci.ubcs.code.dto.CodeDeleteBatchDTO;
import com.vci.ubcs.code.dto.CodeOrderDTO;
import com.vci.ubcs.code.dto.datapush.BaseModelDTO;
@@ -18,13 +18,17 @@
import com.vci.ubcs.code.mapper.CommonsMapper;
import com.vci.ubcs.code.service.*;
import com.vci.ubcs.code.vo.CodeKeyAttrRepeatVO;
import com.vci.ubcs.code.vo.pagemodel.*;
import com.vci.ubcs.code.vo.pagemodel.UITableFieldVO;
import com.vci.ubcs.code.vo.pagemodel.UITablePageVO;
import com.vci.ubcs.code.vo.pagemodel.*;
import com.vci.ubcs.omd.feign.IBtmTypeClient;
import com.vci.ubcs.omd.feign.IEnumClient;
import com.vci.ubcs.omd.feign.IEnumItemClient;
import com.vci.ubcs.omd.feign.IRevisionRuleClient;
import com.vci.ubcs.omd.vo.BtmTypeVO;
import com.vci.ubcs.omd.vo.EnumVO;
import com.vci.ubcs.omd.vo.RevisionRuleVO;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.revision.model.BaseModel;
import com.vci.ubcs.starter.revision.model.TreeWrapperOptions;
import com.vci.ubcs.starter.revision.service.RevisionModelUtil;
import com.vci.ubcs.starter.web.constant.QueryOptionConstant;
@@ -37,21 +41,32 @@
import com.vci.ubcs.starter.web.util.*;
import com.vci.ubcs.system.entity.DictBiz;
import com.vci.ubcs.system.feign.IDictBizClient;
import net.logstash.logback.encoder.org.apache.commons.lang3.ObjectUtils;
import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;
import oracle.sql.TIMESTAMP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@@ -120,6 +135,16 @@
    */
   @Autowired
   private ICodePhaseAttrService phaseAttrService;
   /**
    * 业务类型的服务
    */
   @Autowired
   private IBtmTypeClient btmTypeClient;
   /**
    * 版本规则的服务
    */
   @Resource
   private IRevisionRuleClient revisionRuleClient;
//
   /**
    * 通用查询
@@ -180,6 +205,14 @@
    * 空格
    */
   public static final String SPACE = " ";
   /**
    * 缓存key
    */
   public static final String BTM_INIT_CACHE = "ubcs-code:btm";
   /**
    *
    */
   public static final String BTM_NAME = "btm:name";
   /**
    * 密级的字段
@@ -316,7 +349,7 @@
    * @return 返回编码的内容
    */
   @Override
   public String addSaveCode(CodeOrderDTO orderDTO) {
   public String addSaveCode(CodeOrderDTO orderDTO) throws Exception {
      VciBaseUtil.alertNotNull(orderDTO, "编码申请相关的属性和码段的内容都为空", orderDTO.getCodeClassifyOid(), "主题库分类的主键",
         orderDTO.getTemplateOid(), "模板的主键", orderDTO.getCodeRuleOid(), "编码规则的主键");
      CodeClassifyFullInfoBO classifyFullInfo = classifyService.getClassifyFullInfo(orderDTO.getCodeClassifyOid());
@@ -340,14 +373,14 @@
      switchDateAttrOnOrder(templateVO, orderDTO);
      //9.生成编码的信息
//      ClientBusinessObject cbo = boService.createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
      CodeWupin cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
      BaseModel cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
//      //默认的属性都不用从前端拷贝
//      //设置编码需要的默认属性的内容
      copyValueToCBO(classifyFullInfo, cbo, orderDTO, templateVO, false);
//      //TODO:因为默认的属性都不拷贝,目前集团码叫name,并没有从DTO拷贝到cbo里。增加一个单独处理,以后再看要不要调整
      cbo.setName(orderDTO.getName() == null ? "" : orderDTO.getName());
//      //end -- modify by lihang @20220407
      List<CodeWupin> cboList = new ArrayList<>();
      List<BaseModel> cboList = new ArrayList<>();
      //备注
      cbo.setDescription(orderDTO.getDescription());
@@ -358,8 +391,8 @@
      List<String> charList = new ArrayList<>();
      for (CodeWupin wupinEntity : cboList) {
         charList.add(wupinEntity.getId());
      for (BaseModel baseModel : cboList) {
         charList.add(baseModel.getId());
      }
      batchSaveSelectChar(templateVO, charList);
      return codeList.size() > 0 ? codeList.get(0) : "";
@@ -709,7 +742,7 @@
    * @param templateVO       模板的显示对象
    * @param edit             是否为修改
    */
   private void copyValueToCBO(CodeClassifyFullInfoBO classifyFullInfo, CodeWupin cbo,
   private void copyValueToCBO(CodeClassifyFullInfoBO classifyFullInfo, BaseModel cbo,
                        CodeOrderDTO orderDTO, CodeClassifyTemplateVO templateVO,
                        boolean edit)  {
      String fullPath = "";
@@ -738,14 +771,18 @@
      try {
         BeanUtilForVCI.copyPropertiesIgnoreNull(BeanUtilForVCI.convertMap(CodeWupin.class,orderDTO.getData()),cbo);
         cbo.setCodeclsfid(classifyFullInfo.getCurrentClassifyVO().getOid());
         cbo.setCodetemplateoid(templateVO.getOid());
         cbo.setCodeclsfpath(fullPath);
         BeanUtilForVCI.copyPropertiesIgnoreNull(BeanUtilForVCI.convertMap(BaseModel.class,orderDTO.getData()),cbo);
         Map<String,String> data = new HashMap<>();
         data.put(CODE_CLASSIFY_OID_FIELD,classifyFullInfo.getCurrentClassifyVO().getOid());
         data.put(CODE_TEMPLATE_OID_FIELD,templateVO.getOid());
         data.put(CODE_FULL_PATH_FILED,fullPath);
         cbo.setData(data);
//         cbo.setCodeclsfid(classifyFullInfo.getCurrentClassifyVO().getOid());
//         cbo.setTemplateOid(templateVO.getOid());
//         cbo.setCodeclsfpath(fullPath);
         cbo.setTs(new Date());
         if (!edit && StringUtils.isBlank(orderDTO.getLcStatus())) {
            //找生命周期的起始状态,插个点,生命周期是否需要创建
            //先写固定,后面生命周期好了在编写
            if (StringUtils.isNotBlank(cbo.getLctid())) {
//               OsLifeCycleVO lifeCycleVO = lifeCycleService.getLifeCycleById(cbo.getLctid());
//               if (lifeCycleVO != null) {
@@ -782,72 +819,65 @@
    * @throws VciBaseException 初始化出错的是会抛出异常
    */
   @Override
   public CodeWupin createCBOByBtmName(String btmName)
   public BaseModel createCBOByBtmName(String btmName)
      throws VciBaseException {
      if(btmName!=null){
         btmName = btmName.trim().toLowerCase();
      }
      String userid = AuthUtil.getUser().getUserName();
//      if(!hasCreatedCbos.containsKey(btmName)){
//         if(StringUtils.isEmpty(userid)){
//            throw new VciBaseException(msgCodePrefix +"noHasUserid");
//         }
//         try {
//            hasCreatedCbos.put(btmName, createBusinessObject(btmName));
//         } catch (Exception e) {
//            logger.error("创建业务类型对象",e);
//            throw new VciBaseException(msgCodePrefix + "initBoError",new String[]{btmName});
//         }
//      }
//      ClientBusinessObject cbo = cloneClientBusinessObject(hasCreatedCbos.get(btmName));
//      QueryWrapper<CodeOsbtmtypeEntity> btmWrapper = new QueryWrapper<>();
//      btmWrapper.eq("ID",btmName);
//      CodeOsbtmtypeEntity btmTypeVO = codeOsbtmtypeMapper.selectOne(btmWrapper);
//      OsBtmTypeVO btmTypeVO = btmService.getBtmById(boName);
      String userName = AuthUtil.getUser().getUserName();
      CodeWupin wupinEntity = new CodeWupin();
      wupinEntity.setOid(VciBaseUtil.getPk());
//      bo.setRevisionid((new ObjectUtility()).getNewObjectID36());
//      bo.setNameoid((new ObjectUtility()).getNewObjectID36());
      wupinEntity.setBtmname(btmName);
      wupinEntity.setLastR(String.valueOf(1));
      wupinEntity.setFirstR(String.valueOf(1));
      wupinEntity.setFirstV(String.valueOf(1));
      wupinEntity.setLastV(String.valueOf(1));
      wupinEntity.setCreator(userName);
      wupinEntity.setCreateTime(new Date());
      wupinEntity.setLastModifier(userName);
      wupinEntity.setLastModifyTime(new Date());
      wupinEntity.setRevisionRule("numberversionrule");
      wupinEntity.setVersionRule("0");
//      if(StringUtils.isNotBlank(btmTypeVO.getRevisionruleid())){
         //
//         OsRevisionRuleVO revisionRuleVO = revisionRuleService.getRevisionRuleById(btmTypeVO.getRevisionruleid());
      wupinEntity.setRevisionValue("1");
//      }
      wupinEntity.setRevisionSeq(1);
      wupinEntity.setVersionSeq(1);
      //插个点,需要问勇哥版本问题,展示默认为1
      wupinEntity.setVersionValue("1");
      wupinEntity.setLctid("wupinLC");
      wupinEntity.setLcStatus("Editing");
      wupinEntity.setId("");
      wupinEntity.setName("");
      wupinEntity.setDescription("");
      wupinEntity.setOwner(userName);
      wupinEntity.setCheckinby(userName);
      wupinEntity.setCopyFromVersion("");
      wupinEntity.setMaterialtype(1001);
      wupinEntity.setCaigouwl("true");
      wupinEntity.setShifoupihaoguanli("true");
      wupinEntity.setKucunwl("true");
      wupinEntity.setXiaoshouwl("false");
      wupinEntity.setPassing("true");
      try {
         String keyPrefix = BTM_NAME.concat(StringPool.DASH).concat(AuthUtil.getTenantId()).concat(StringPool.COLON);
         String finalBtmName = btmName;
         return CacheUtil.get(BTM_INIT_CACHE, keyPrefix, btmName, () -> {
            BaseModel baseModel = createBaseModel(finalBtmName);
            return baseModel;
         });
      } catch (Exception e) {
         logger.error("创建业务类型对象",e);
         throw new VciBaseException("initBtmError",new String[]{btmName});
      }
//      String userName = AuthUtil.getUser().getUserName();
//      CodeWupin wupinEntity = new CodeWupin();
//      wupinEntity.setOid(VciBaseUtil.getPk());
////      bo.setRevisionid((new ObjectUtility()).getNewObjectID36());
////      bo.setNameoid((new ObjectUtility()).getNewObjectID36());
//      wupinEntity.setBtmname(btmName);
//      wupinEntity.setLastR(String.valueOf(1));
//      wupinEntity.setFirstR(String.valueOf(1));
//      wupinEntity.setFirstV(String.valueOf(1));
//      wupinEntity.setLastV(String.valueOf(1));
//      wupinEntity.setCreator(userName);
//      wupinEntity.setCreateTime(new Date());
//      wupinEntity.setLastModifier(userName);
//      wupinEntity.setLastModifyTime(new Date());
//      wupinEntity.setRevisionRule("numberversionrule");
//      wupinEntity.setVersionRule("0");
////      if(StringUtils.isNotBlank(btmTypeVO.getRevisionruleid())){
//         //
////         OsRevisionRuleVO revisionRuleVO = revisionRuleService.getRevisionRuleById(btmTypeVO.getRevisionruleid());
//      wupinEntity.setRevisionValue("1");
////      }
//
//      wupinEntity.setRevisionSeq(1);
//      wupinEntity.setVersionSeq(1);
//      //插个点,需要问勇哥版本问题,展示默认为1
//      wupinEntity.setVersionValue("1");
//      wupinEntity.setLctid("wupinLC");
//      wupinEntity.setLcStatus("Editing");
//      wupinEntity.setId("");
//      wupinEntity.setName("");
//      wupinEntity.setDescription("");
//      wupinEntity.setOwner(userName);
//      wupinEntity.setCheckinby(userName);
//      wupinEntity.setCopyFromVersion("");
//      wupinEntity.setMaterialtype(1001);
//      wupinEntity.setCaigouwl("true");
//      wupinEntity.setShifoupihaoguanli("true");
//      wupinEntity.setKucunwl("true");
//      wupinEntity.setXiaoshouwl("false");
//      wupinEntity.setPassing("true");
//      this.initTypeAttributeValue(wupinEntity,btmTypeVO);
      return wupinEntity;
//      return wupinEntity;
//      return cbo;
@@ -1236,8 +1266,8 @@
      //没有限制分类,但是一个模板只可能在一个业务类型里面,所以直接查询这个业务类型即可
      if (!CollectionUtils.isEmpty(conditionMap)) {
         Map<String, String> andConditionMap = new HashMap<>();
         andConditionMap.put("islastr", "1");
         andConditionMap.put("islastv", "1");
         andConditionMap.put("lastr", "1");
         andConditionMap.put("lastv", "1");
         if (StringUtils.isNotBlank(orderDTO.getOid())) {
            andConditionMap.put("oid", QueryOptionConstant.NOTEQUAL + orderDTO.getOid());
         }
@@ -2066,7 +2096,7 @@
      } else {
         //表需要改
//         conditionMap.put(CODE_CLASSIFY_OID_FIELD, QueryOptionConstant.IN + "(select oid from " + VciBaseUtil.getTableName(MdmBtmTypeConstant.CODE_CLASSIFY)
         conditionMap.put(CODE_CLASSIFY_OID_FIELD, QueryOptionConstant.IN + "(select oid from pl_code_wupin where lcstatus='" + FrameWorkDefaultValueConstant.FRAMEWORK_DATA_ENABLED +
         conditionMap.put(CODE_CLASSIFY_OID_FIELD, QueryOptionConstant.IN + "(select oid from pl_code_classify where lcstatus='" + FrameWorkDefaultValueConstant.FRAMEWORK_DATA_ENABLED +
            "' start with parentCodeClassifyOid = '" + codeClassifyOid + "' CONNECT BY PRIOR OID = parentCodeClassifyOid )");
      }
      conditionMap.put("lastr", "1");
@@ -2883,4 +2913,146 @@
      }
      return buttonVOList;
   }
   /**
    * 传入业务类型以及相关数据进行批量插入操作
    *
    * @param btmType 业务类型
    * @param baseModels      处理数据
    * @return 处理成功数据条数
    */
   @Override
   public Integer insertBatchByType(String btmType, List<BaseModel> baseModels) throws Exception {
      //使用传入的业务类型查询表
      R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(btmType));
      if(listR.getData().size() == 0){
         throw new VciBaseException("传入业务类型未查询到相应表单,请检查!");
      }
      //将bean转为map,mybatis统一处理
      List<Map<String,String>> maps = new ArrayList<>();
      baseModels.stream().forEach(model-> {
         try {
            maps.add(VciBaseUtil.convertBean2Map(model));
         } catch (Exception e) {
            throw new VciBaseException("类型转换错误:" + e.toString());
         }
      });
      return commonsMapper.insertByBaseModel(listR.getData().get(0).getTableName(), maps.get(0), maps);
   }
   /**
    * 传入业务类型以及相oid集合查询数据进行返回
    *
    * @param btmType 业务类型
    * @param oids      需要查询的oid集合 逗号分开
    * @return 查询出的数据
    */
   @Override
   public List<BaseModel> selectByTypeAndOid(String btmType, String oids) throws IllegalAccessException, NoSuchFieldException, InstantiationException, InvocationTargetException, IntrospectionException, SQLException {
      //使用传入的业务类型查询表
      R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(btmType));
      if(listR.getData().size() == 0){
         throw new VciBaseException("传入业务类型未查询到相应表单,请检查!");
      }
      //查询数据
      List<Map> maps = commonsMapper.selectBySql("select * from " + listR.getData().get(0).getTableName() + " where oid in ("
         + VciBaseUtil.toInSql(oids.toString()) + ")");
      List<BaseModel> baseModels = new ArrayList<>();
      //将查询到的数据转换为basemodel,使用的反射方式来进行创建的
      for (Map map : maps) {
         Object obj = BaseModel.class.newInstance();
         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
         for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
               //oracle的时间为TIMESTAMP的,需要进行转换成data,否则将报错
               if(map.get(property.getName().toUpperCase()) instanceof TIMESTAMP){
                  LocalDateTime localDateTime = ((TIMESTAMP) map.get(property.getName().toUpperCase())).toLocalDateTime();
                  ZoneId zoneId = ZoneId.systemDefault();
                  ZonedDateTime zdt = localDateTime.atZone(zoneId);
                  Date date = Date.from(zdt.toInstant());
                  setter.invoke(obj,date);
                  map.remove(property.getName().toUpperCase());
               } //oracle的数字为BigDecimal的,需要进行转换成Integer,否则将报错
               else if (map.get(property.getName().toUpperCase()) instanceof BigDecimal
                  && ("Integer").equals(setter.getParameterTypes()[0].getSimpleName())){
                  setter.invoke(obj, ((BigDecimal)map.get(property.getName().toUpperCase())).intValue());
                  map.remove(property.getName().toUpperCase());
               }else if(map.get(property.getName().toUpperCase()) != null){
                  setter.invoke(obj, map.get(property.getName().toUpperCase()));
                  map.remove(property.getName().toUpperCase());
               }
            }
         }
         ((BaseModel) obj).setData(map);
         baseModels.add((BaseModel) obj);
      }
      return baseModels;
   }
   /**
    * 根据业务类型名称创建业务数据源对象
    * @param boName 业务类型名称
    * @return 业务数据对象
    */
   public BaseModel createBaseModel(String boName)  {
      R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(boName));
      String userName = AuthUtil.getUser().getUserName();
      BaseModel bo = new BaseModel();
      bo.setOid(VciBaseUtil.getPk());
//      bo.setRevisionid(VciBaseUtil.getPk());
//      bo.setNameoid(VciBaseUtil.getPk());
      bo.setBtmname(boName);
      bo.setLastR("1");
      bo.setFirstR("1");
      bo.setFirstV("1");
      bo.setLastV("1");
      bo.setCreator(userName);
      bo.setCreateTime(new Date());
      bo.setLastModifier(userName);
      bo.setLastModifyTime(new Date());
      bo.setRevisionRule(listR.getData().get(0).getRevisionRuleId());
      bo.setVersionRule(String.valueOf(listR.getData().get(0).getVersionRule()));
      if(StringUtils.isNotBlank(listR.getData().get(0).getRevisionRuleId())){
         R<List<RevisionRuleVO>> revisionRuleVO = revisionRuleClient
            .selectByIdCollection(Collections.singletonList(listR.getData().get(0).getRevisionRuleId()));
         bo.setRevisionValue(revisionRuleVO.getData().get(0).getStartCode());
      }
      bo.setRevisionSeq(1);
      bo.setVersionSeq(1);
      bo.setVersionValue(getVersionValue(WebUtil.getInt(listR.getData().get(0).getVersionRule())));
      bo.setLctid(listR.getData().get(0).getLifeCycleId());
//      if(StringUtils.isNotBlank(listR.getData().get(0).getLifeCycleId())){
//         OsLifeCycleVO lifeCycleVO = lifeService.getLifeCycleById(listR.getData().get(0).getLifeCycleId());
         bo.setLcStatus("Editing");
//      }
      bo.setId("");
      bo.setName("");
      bo.setDescription("");
      bo.setOwner(userName);
//      bo.setCheckinby(userName);
      bo.setCopyFromVersion("");
//      this.initTypeAttributeValue(bo,btmTypeVO);
      return bo;
   }
   /**
    * 获取版次的值
    * @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 "";
   }
}