Browse Source

修改更改费用描述修改为空不会更新的bug
协议描述中的描述修改为空不会更新的bug

hxl13994548489 2 years ago
parent
commit
096eb48bf4

+ 25 - 0
src/main/java/com/ydtech/config/mybatisPlusNull/MySqlInjector.java

@@ -0,0 +1,25 @@
+package com.ydtech.config.mybatisPlusNull;
+
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+/**
+ *  @version
+ *  @author: hxl
+ *  @Date: 2024/6/12 9:30
+ *  @Description:  mybatis的空值插入的sql
+ */
+@Component
+public class MySqlInjector extends DefaultSqlInjector {
+    @Override
+    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
+        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
+        // 根据id更新所有数据
+        methodList.add(new UpdateWithNullMethod());
+        return methodList;
+    }
+
+}

+ 105 - 0
src/main/java/com/ydtech/config/mybatisPlusNull/UpdateWithNullMethod.java

@@ -0,0 +1,105 @@
+package com.ydtech.config.mybatisPlusNull;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlSource;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import static java.util.stream.Collectors.joining;
+
+/**
+ *  @version
+ *  @author: hxl
+ *  @Date: 2024/6/12 9:32
+ *  @Description:  mybatis-plus 更新空值的公共类
+ */
+@Slf4j
+public class UpdateWithNullMethod extends AbstractMethod {
+    protected UpdateWithNullMethod() {
+        super("updateWithNull");
+    }
+
+    @Override
+    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
+        String sql = "<script>\nupdate %s %s where %s=#{%s}\n</script>";
+
+        final List<TableFieldInfo> fieldList = tableInfo.getFieldList();
+        for (TableFieldInfo tableFieldInfo : fieldList) {
+            final Class<? extends TableFieldInfo> aClass = tableFieldInfo.getClass();
+            try {
+                final Field fieldFill = aClass.getDeclaredField("fieldFill");
+                fieldFill.setAccessible(true);
+                fieldFill.set(tableFieldInfo, FieldFill.UPDATE);
+            } catch (NoSuchFieldException | IllegalAccessException e) {
+                log.error("获取fieldFill失败", e);
+            }
+        }
+        String setSql = this.sqlSet(tableInfo);
+        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
+        log.debug("sqlResult----->{}", sqlResult);
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
+        // 第三个参数必须和rootMapper的自定义方法名一致
+        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateWithNull", sqlSource);
+    }
+
+    protected String sqlSet(TableInfo table) {
+        String sqlScript = table.getFieldList()
+                .stream().map(i -> this.getSqlSet(i, StringPool.EMPTY)).collect(joining(StringPool.NEWLINE));
+        sqlScript = SqlScriptUtils.convertTrim(sqlScript, "SET", null, null, ",");
+        return sqlScript;
+    }
+
+    public String getSqlSet(TableFieldInfo i, String prefix) {
+        String newPrefix = prefix == null ? StringPool.EMPTY : prefix;
+        String column = i.getColumn();
+        String update = i.getUpdate();
+        FieldFill fieldFill = i.getFieldFill();
+        String el = i.getEl();
+
+        // 默认:column=
+        String sqlSet = column + StringPool.EQUALS;
+        if (StringUtils.isNotEmpty(update)) {
+            sqlSet += String.format(update, column);
+        } else {
+            sqlSet += SqlScriptUtils.safeParam(newPrefix +el);
+        }
+
+        sqlSet += StringPool.COMMA;
+        if (fieldFill == FieldFill.UPDATE || fieldFill == FieldFill.INSERT_UPDATE) {
+            // 不进行if包裹
+            return sqlSet;
+        }
+        return convertIf(sqlSet, convertIfProperty(newPrefix, column), i.getUpdateStrategy(),
+                i.getPropertyType().isPrimitive(), StringUtils.isCharSequence(i.getPropertyType()));
+    }
+
+    private String convertIfProperty(String prefix, String property) {
+        return StringUtils.isNotBlank(prefix)
+                ? prefix.substring(0, prefix.length() - 1) + "['" + property + "']" : property;
+    }
+
+
+    private String convertIf(final String sqlScript, final String property, final FieldStrategy fieldStrategy,
+                             boolean isPrimitive, boolean isCharSequence) {
+        if (fieldStrategy == FieldStrategy.NEVER) {
+            return null;
+        }
+        if (isPrimitive || fieldStrategy == FieldStrategy.IGNORED) {
+            return sqlScript;
+        }
+        if (fieldStrategy == FieldStrategy.NOT_EMPTY && isCharSequence) {
+            return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s !=''", property, property), false);
+        }
+        return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false);
+    }
+}

+ 3 - 0
src/main/java/com/ydtech/modules/protocol/dao/PtlAgreementMapper.java

@@ -27,6 +27,9 @@ public interface PtlAgreementMapper extends BaseMapper<PtlAgreement> {
 
 
     PtlAgreement selectAgreeId(HashMap<String, Object> params);
+
+
+    boolean updateWithNull(PtlAgreement obj);
 }
 
 

+ 6 - 1
src/main/java/com/ydtech/modules/protocol/service/impl/PtlAgreementServiceImpl.java

@@ -81,6 +81,9 @@ public class PtlAgreementServiceImpl extends ServiceImpl<PtlAgreementMapper, Ptl
     private PtlAgreementDeptHistoryService ptlAgreementDeptHistoryService;
 
 
+    @Autowired
+    private PtlAgreementMapper ptlAgreementMapper;
+
     public PtlAgreementServiceImpl(PtlAgreementProductCostsService ptlAgreementProductCostsService,
                                    PtlAgreementAttributionService ptlAgreementAttributionService,
                                    PtlAgreementDeptService ptlAgreementDeptService,
@@ -561,8 +564,10 @@ public class PtlAgreementServiceImpl extends ServiceImpl<PtlAgreementMapper, Ptl
         ptlAgreement.setModifiedBy(userName);
         ptlAgreement.setModifiedName(BaseController.getUser().getName());
         ptlAgreement.setUpdateTime(LocalDateTime.now());
+        //add by hxl  2024-06-12 修改协议改为可以更新null的判断
+        boolean b = ptlAgreementMapper.updateWithNull(ptlAgreement);
         // 修改协议
-        boolean b = updateById(ptlAgreement);
+        //boolean b = updateById(ptlAgreement);
         AssertionUtils.isSucceed(b, "协议信息修改失败");
         // 关联表都是先删除后添加
         proxyObject.deleteAssociationInformationExcludeCosts(agreementId);

+ 7 - 2
src/main/java/com/ydtech/modules/protocols/dao/PtlAgreementProductCostsNewMapper.java

@@ -3,8 +3,6 @@ package com.ydtech.modules.protocols.dao;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ydtech.modules.protocols.entity.po.PtlAgreementProductCostsNew;
-import com.ydtech.modules.protocols.entity.po.PtlAgreementUndwrtRulesAttr;
-import com.ydtech.modules.protocols.entity.vo.AgreementProductCosts;
 import com.ydtech.modules.protocols.entity.vo.AgreementProductQueryVo;
 import org.apache.ibatis.annotations.Param;
 
@@ -16,6 +14,13 @@ import org.apache.ibatis.annotations.Param;
  */
 public interface PtlAgreementProductCostsNewMapper extends BaseMapper<PtlAgreementProductCostsNew> {
     Page<PtlAgreementProductCostsNew> getCostsPage(Page page,@Param("params") AgreementProductQueryVo agreementProductQueryVo);
+    /**
+     *  @version
+     *  @author: hxl
+     *  @Date: 2024/6/12 10:18
+     *  @Description:  添加通过
+     */
+    boolean updateWithNull(PtlAgreementProductCostsNew obj);
 }
 
 

+ 11 - 9
src/main/java/com/ydtech/modules/protocols/service/impl/PtlAgreementProductCostsNewServiceImpl.java

@@ -7,9 +7,6 @@ import com.ydtech.exception.SystemException;
 import com.ydtech.modules.admin.model.SysUser;
 import com.ydtech.modules.admin.service.SysUserService;
 import com.ydtech.modules.base.controller.BaseController;
-import com.ydtech.modules.fee.dto.vo.DeptManagerFee;
-import com.ydtech.modules.fee.dto.vo.EntranceFee;
-import com.ydtech.modules.fee.dto.vo.ExportFee;
 import com.ydtech.modules.fee.dto.vo.RetailStoreFee;
 import com.ydtech.modules.fee.service.CostsCalcService;
 import com.ydtech.modules.fee.service.FeeRuleSchemeService;
@@ -28,18 +25,18 @@ import com.ydtech.modules.protocols.entity.po.*;
 import com.ydtech.modules.protocols.entity.vo.*;
 import com.ydtech.modules.protocols.service.*;
 import com.ydtech.utils.idgen.IdGenerate;
-import io.swagger.annotations.ApiOperation;
-import org.apache.commons.collections4.trie.analyzer.StringKeyAnalyzer;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
-import java.math.RoundingMode;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -81,6 +78,10 @@ public class PtlAgreementProductCostsNewServiceImpl extends ServiceImpl<PtlAgree
     @Autowired
     PtlAgreementProductCostsNewService ptlAgreementProductCostsNewService;
 
+    @Autowired
+    private PtlAgreementProductCostsNewMapper ptlAgreementProductCostsNewMapper;
+
+
     @Override
     public Page<PtlAgreementProductCostsNew> getByAgreementId(Page page, AgreementProductQueryVo agreementProductQueryVo) {
         LambdaQueryWrapper<PtlAgreementProductCostsNew> ptlAgreementProductCostsNewLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -683,8 +684,9 @@ public class PtlAgreementProductCostsNewServiceImpl extends ServiceImpl<PtlAgree
         String newProjectName = agreementProductCosts.getProductName().get(0);
         ptlAgreementProductCostsNew.setProductId(newProjectId);
         ptlAgreementProductCostsNew.setProductName(newProjectName);
-        int update = baseMapper.updateById(ptlAgreementProductCostsNew);
-
+        //add by hxl  修改可以更新null值
+        //int update = baseMapper.updateById(ptlAgreementProductCostsNew);
+        boolean update = ptlAgreementProductCostsNewMapper.updateWithNull(ptlAgreementProductCostsNew);
         //更新费用出口表信息
         PtlAgreementProductCostsExport ptlAgreementProductCostsExport = ptlAgreementProductCostsExportService.getOne(
                 new LambdaQueryWrapper<PtlAgreementProductCostsExport>()