Procházet zdrojové kódy

Merge branch 'master' of http://175.27.169.173:10880/zkqy-sass-platform/mec-cloud_-intelligent-manufacturing_client

sql715 před 2 měsíci
rodič
revize
d997e2a46a

+ 25 - 0
zkqy-business/src/main/java/com/zkqy/business/controller/CommonBtnController.java

@@ -111,11 +111,36 @@ public class CommonBtnController extends BaseController {
                 return executeBtn(commonEntity, dragTableBtn);
             case "INITIATED":  // 发起流程
                 return initiatedBtn(commonEntity, dragTableBtn);
+            case "CALCULATE":
+                return calculateBtn(commonEntity, dragTableBtn);
             default:
                 return warn("暂不支持该操作!");
         }
     }
 
+    private AjaxResult calculateBtn(CommonEntity commonEntity, DragTableBtn dragTableBtn) {
+        //点击按钮触发的操作 visible状态用来区分是否是打开还是关闭状态
+        if (commonEntity.getBasicMap().get("visible").toString().equals("false") && dragTableBtn.getBtnFormType().equals("calculateForm")) {
+            Map<String, Object> map = new HashMap<>();
+            //查询动态表单信息
+            DragForm dragForm = dragFormService.selectDragFormByformKey(dragTableBtn.getBtnFormKey());
+            map.put("template", dragForm);
+            String dfVueTemplate = dragForm.getDfVueTemplate();
+            //先查出表单的数据
+            CommonEntity common = commonService.queryCommonByCalculateBtn(commonEntity, dfVueTemplate);
+            if (common == null){
+                return AjaxResult.btnMessage(-1);
+            }
+            map.put("result", common);
+            return success(map);
+        // 点击确认按钮时对数据不做处理
+        }else if (commonEntity.getBasicMap().get("visible").toString().equals("true")){
+            return AjaxResult.btnMessage(200);
+        }else{
+            return warn("暂不支持该操作!");
+        }
+    }
+
     /**
      * 新增类型按钮
      *

+ 21 - 0
zkqy-business/src/main/java/com/zkqy/business/controller/DragTreeController.java

@@ -1,6 +1,7 @@
 package com.zkqy.business.controller;
 
 import com.zkqy.business.entity.DragTree;
+import com.zkqy.business.entity.dto.DragTreeNodeDto;
 import com.zkqy.business.entity.vo.DragTreeVo;
 import com.zkqy.business.service.IDragTreeService;
 import com.zkqy.common.annotation.Anonymous;
@@ -51,6 +52,26 @@ public class DragTreeController extends BaseController
         return AjaxResult.success(list);
     }
 
+
+    //新增树节点
+    @PostMapping("/node/add")
+    public AjaxResult treeNodeAdd(@RequestBody DragTreeNodeDto treeNodeDto)
+    {
+        return toAjax(dragTreeService.treeNodeAdd(treeNodeDto));
+    }
+    //新增树节点
+    @PostMapping("/node/update")
+    public AjaxResult treeNodeupdate(@RequestBody DragTreeNodeDto treeNodeDto)
+    {
+        return toAjax(dragTreeService.treeNodeUpdate(treeNodeDto));
+    }
+    //删除
+    @PostMapping("/node/delete")
+    public AjaxResult treeNodeDelete(@RequestBody DragTreeNodeDto treeNodeDto)
+    {
+        return toAjax(dragTreeService.treeNodeDelete(treeNodeDto));
+    }
+
     /**
      * 导出拖拽树结构列表
      */

+ 9 - 0
zkqy-business/src/main/java/com/zkqy/business/entity/DragTree.java

@@ -20,6 +20,7 @@ public class DragTree extends BaseEntity
 
     private String  treeShowLabel;
     private String  treeTableJoinTableCondition;
+    private String  treeTableJoinTablePrimaryKey;
 
     /** 唯一标识符,自增主键 */
     private Long id;
@@ -257,4 +258,12 @@ public class DragTree extends BaseEntity
     public void setTreeTableJoinTableCondition(String treeTableJoinTableCondition) {
         this.treeTableJoinTableCondition = treeTableJoinTableCondition;
     }
+
+    public String getTreeTableJoinTablePrimaryKey() {
+        return treeTableJoinTablePrimaryKey;
+    }
+
+    public void setTreeTableJoinTablePrimaryKey(String treeTableJoinTablePrimaryKey) {
+        this.treeTableJoinTablePrimaryKey = treeTableJoinTablePrimaryKey;
+    }
 }

+ 90 - 0
zkqy-business/src/main/java/com/zkqy/business/entity/dto/DragTreeNodeDto.java

@@ -0,0 +1,90 @@
+package com.zkqy.business.entity.dto;
+
+public class DragTreeNodeDto {
+    //树的表名
+    private String tableName;
+
+    //主键名
+    private String primaryName;
+
+    private String primaryValue;
+
+    //字段名
+    private String columnName;
+
+    //字段值
+    private String columnValue;
+
+    //指向上节点的字段名
+    private String parentName;
+
+    private String parentValue;
+
+    // 增加的层级,是本级还是下一级 cur next
+    private String addLevel;
+
+    public String getTableName() {
+        return tableName;
+    }
+
+    public void setTableName(String tableName) {
+        this.tableName = tableName;
+    }
+
+
+    public String getColumnName() {
+        return columnName;
+    }
+
+    public void setColumnName(String columnName) {
+        this.columnName = columnName;
+    }
+
+    public String getColumnValue() {
+        return columnValue;
+    }
+
+    public void setColumnValue(String columnValue) {
+        this.columnValue = columnValue;
+    }
+
+    public String getPrimaryName() {
+        return primaryName;
+    }
+
+    public void setPrimaryName(String primaryName) {
+        this.primaryName = primaryName;
+    }
+
+    public String getPrimaryValue() {
+        return primaryValue;
+    }
+
+    public void setPrimaryValue(String primaryValue) {
+        this.primaryValue = primaryValue;
+    }
+
+    public String getParentName() {
+        return parentName;
+    }
+
+    public void setParentName(String parentName) {
+        this.parentName = parentName;
+    }
+
+    public String getParentValue() {
+        return parentValue;
+    }
+
+    public void setParentValue(String parentValue) {
+        this.parentValue = parentValue;
+    }
+
+    public String getAddLevel() {
+        return addLevel;
+    }
+
+    public void setAddLevel(String addLevel) {
+        this.addLevel = addLevel;
+    }
+}

+ 13 - 0
zkqy-business/src/main/java/com/zkqy/business/mapper/DragTreeMapper.java

@@ -3,6 +3,7 @@ package com.zkqy.business.mapper;
 
 
 import com.zkqy.business.entity.DragTree;
+import com.zkqy.business.entity.dto.DragTreeNodeDto;
 import com.zkqy.execution.produce.dispersed.entity.CommonEntity;
 import org.apache.ibatis.annotations.Param;
 
@@ -88,4 +89,16 @@ public interface DragTreeMapper
      * @return 结果
      */
     public int deleteDragTreeByIds(Long[] ids);
+
+    int insertTreeNodeCur(DragTreeNodeDto treeNodeDto);
+
+    int insertTreeNodeNext(DragTreeNodeDto treeNodeDto);
+
+    int updateTreeNode(DragTreeNodeDto treeNodeDto);
+
+    int deleteTreeNodeByIds(@Param("treeMapData") List<String> treeMapData, @Param("tableName") String tableName, @Param("primaryName") String primaryName);
+
+    List<Map<String, String>> selectTreeNodeList(@Param("tableName") String tableName);
+
+
 }

+ 7 - 0
zkqy-business/src/main/java/com/zkqy/business/service/IDragTreeService.java

@@ -3,6 +3,7 @@ package com.zkqy.business.service;
 
 
 import com.zkqy.business.entity.DragTree;
+import com.zkqy.business.entity.dto.DragTreeNodeDto;
 import com.zkqy.business.entity.vo.DragTreeVo;
 
 import java.util.List;
@@ -70,4 +71,10 @@ public interface IDragTreeService
      * @return
      */
     DragTreeVo recursionTree(String treeTableKey);
+
+    int treeNodeAdd(DragTreeNodeDto treeNodeDto);
+
+    int treeNodeUpdate(DragTreeNodeDto treeNodeDto);
+
+    int treeNodeDelete(DragTreeNodeDto treeNodeDto);
 }

+ 89 - 4
zkqy-business/src/main/java/com/zkqy/business/service/impl/DragTreeServiceImpl.java

@@ -3,6 +3,7 @@ package com.zkqy.business.service.impl;
 import com.alibaba.fastjson2.JSON;
 import com.fasterxml.jackson.databind.json.JsonMapper;
 import com.zkqy.business.entity.DragTree;
+import com.zkqy.business.entity.dto.DragTreeNodeDto;
 import com.zkqy.business.entity.vo.DragTreeVo;
 import com.zkqy.business.mapper.DragTreeMapper;
 import com.zkqy.business.service.IDragTreeService;
@@ -11,13 +12,12 @@ import com.zkqy.business.utils.TreeBuilder;
 import com.zkqy.common.exception.base.BaseException;
 import com.zkqy.common.utils.DateUtils;
 import com.zkqy.common.utils.SecurityUtils;
-import com.zkqy.execution.produce.dispersed.entity.CommonEntity;
+import com.zkqy.common.utils.StringUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * 拖拽树结构Service业务层处理
@@ -114,7 +114,7 @@ public class DragTreeServiceImpl implements IDragTreeService
      */
     @Override
     public DragTreeVo recursionTree(String treeTableKey) {
-        treeTableKey="48e90c10-147f-4bfa-a1a0-f702ba93b5c8";
+//        treeTableKey="48e90c10-147f-4bfa-a1a0-f702ba93b5c8";
         // 首先通过tableKey查询模版数据
         DragTree dragTree = dragTreeMapper.selectDragTreeByTreeTableKey(treeTableKey);
         // 数据表名称 ammunition_plan_information_intermediate_table
@@ -123,6 +123,8 @@ public class DragTreeServiceImpl implements IDragTreeService
         String tableId = dragTree.getTreeTablePrimaryKey();
         // 递归列
         String treeTableDgl = dragTree.getTreeTableDgl();
+        // 下划线转驼峰
+        treeTableDgl = StringUtils.toCamelCase(treeTableDgl);
         // 展示字段(label)
         String treeShowLabel = dragTree.getTreeShowLabel();
         // 当前表字段的所有列名
@@ -159,4 +161,87 @@ public class DragTreeServiceImpl implements IDragTreeService
         //查询对应的表格信息
         return dragTreeVo;
     }
+
+    @Override
+    public int treeNodeAdd(DragTreeNodeDto treeNodeDto) {
+        Boolean legalParam = isLegalParam(treeNodeDto);
+        //增加情况额外判断
+        String parentValue = treeNodeDto.getParentValue();
+        String addLevel = treeNodeDto.getAddLevel();
+        if (legalParam.equals(false) || StringUtils.isBlank(parentValue) || StringUtils.isBlank(addLevel)){
+            return -1;
+        }
+        //当前层级添加
+        if (addLevel.equals("cur")){
+            return dragTreeMapper.insertTreeNodeCur(treeNodeDto);
+        //下一层级添加
+        }else if(addLevel.equals("next")){
+            return dragTreeMapper.insertTreeNodeNext(treeNodeDto);
+        }else{
+            return -1;
+        }
+    }
+
+    @Override
+    public int treeNodeUpdate(DragTreeNodeDto treeNodeDto) {
+        Boolean legalParam = isLegalParam(treeNodeDto);
+        if (legalParam.equals(false)){
+            return -1;
+        }
+        return dragTreeMapper.updateTreeNode(treeNodeDto);
+    }
+
+    @Override
+    public int treeNodeDelete(DragTreeNodeDto treeNodeDto) {
+        Boolean legalParam = isLegalParam(treeNodeDto);
+        if (legalParam.equals(false)){
+            return -1;
+        }
+        String tableName = treeNodeDto.getTableName();
+        String primaryValue = treeNodeDto.getPrimaryValue();
+        String primaryName = treeNodeDto.getPrimaryName();
+        String parentName = treeNodeDto.getParentName();
+        // 查询出所有的数据
+        List<Map<String, String>> mapList = dragTreeMapper.selectTreeNodeList(tableName);
+        //递归得到要删除的树形id集合
+        List<String> ids = new ArrayList<>();
+        // 下划线转驼峰
+        parentName = StringUtils.toCamelCase(parentName);
+        List<String> treeMapData = TreeBuilder.getTreeIdList(mapList,primaryValue,primaryName,parentName,ids);
+        // 删除这些id
+        return dragTreeMapper.deleteTreeNodeByIds(treeMapData,tableName,primaryName);
+    }
+
+    //参数校验
+    public Boolean isLegalParam(DragTreeNodeDto treeNodeDto){
+        if (treeNodeDto == null){
+            return false;
+        }
+        boolean a = StringUtils.isNotBlank(treeNodeDto.getTableName());
+        boolean a1 = StringUtils.isNotBlank(treeNodeDto.getPrimaryName());
+        boolean a2 = StringUtils.isNotBlank(treeNodeDto.getPrimaryValue());
+        boolean a3 = StringUtils.isNotBlank(treeNodeDto.getColumnName());
+        boolean a4 = StringUtils.isNotBlank(treeNodeDto.getColumnValue());
+        boolean a5 = StringUtils.isNotBlank(treeNodeDto.getParentName());
+        if (a && a1 && a2 && a3 && a4 && a5){
+            return true;
+        }
+        return false;
+    }
+    public static void main(String[] args) {
+        String s = "parentId";
+        String s1 = "asdsadas";
+        String s2 = "   ";
+        String s3 = "asd_asdd_as";
+        String s4 = StringUtils.toCamelCase(s3);
+        String s5 = StringUtils.toCamelCase(s);
+        String s6 = StringUtils.toCamelCase(s1);
+        String s7 = StringUtils.toCamelCase(s2);
+        System.out.println(s4);
+        System.out.println(s5);
+        System.out.println(s6);
+        System.out.println(s7);
+    }
+
+
 }

+ 24 - 4
zkqy-business/src/main/java/com/zkqy/business/utils/TreeBuilder.java

@@ -28,6 +28,24 @@ public class TreeBuilder {
                 .collect(Collectors.toList());
     }
 
+    // 递归的找到所有本节点和子节点的id
+    public static List<String> getTreeIdList(List<Map<String, String>> allNodes, String parentId,String tableId,String treeTableDgl,List<String> resIds) {
+        resIds.add(parentId);
+         allNodes.stream()
+                .filter(node ->Objects.equals(convertToString(parentId), convertToString(node.get(treeTableDgl))))
+                .map(parentNode -> {
+                    // 确保 tableId 对应的值被正确地转换为字符串
+                    Object tableIdObj = parentNode.get(tableId);
+                    String tableIdStr = convertToString(tableIdObj);
+                    System.out.println(tableIdStr);
+                    getTreeIdList(allNodes, tableIdStr, tableId,treeTableDgl,resIds);
+                    return parentNode;
+                })
+                .collect(Collectors.toList());
+        List<String> resIdList = resIds.stream().distinct().collect(Collectors.toList());
+        return resIdList;
+    }
+
     private static String convertToString(Object obj) {
         if (obj instanceof Integer) {
             return ((Integer) obj).toString();
@@ -48,7 +66,7 @@ public class TreeBuilder {
                 new HashMap<String, String>() {{
                     put("id", "1");
                     put("name", "根节点");
-                    put("parentId", null); // 根节点
+                    put("parentId", "0"); // 根节点
                 }},
                 new HashMap<String, String>() {{
                     put("id", "2");
@@ -67,9 +85,11 @@ public class TreeBuilder {
                 }});
 
         // 构建树形结构
-        List<Map<String, ?>> tree = buildTree(mapList, null,"id","parentId");
-        String jsonString = JSON.toJSONString(tree);
-        System.out.println(jsonString);
+        ArrayList<String> strings = new ArrayList<>();
+        List<String> treeIdList = getTreeIdList(mapList, "2", "id", "parentId", strings);
+//        List<Map<String, ?>> tree = buildTree(mapList, "1","id","parentId");
+//        String jsonString = JSON.toJSONString(tree);
+//        System.out.println(jsonString);
         // 打印结果
 //        printTree(tree);
     }

+ 37 - 5
zkqy-business/src/main/resources/mapper/dragmapper/DragTreeMapper.xml

@@ -27,10 +27,11 @@
         <result property="treeTableKey" column="tree_table_key"/>
         <result property="treeShowLabel" column="tree_show_label"/>
         <result property="treeTableJoinTableCondition" column="tree_table_join_table_condition"/>
+        <result property="treeTableJoinTablePrimaryKey" column="tree_table_join_table_primary_key"/>
     </resultMap>
 
     <sql id="selectDragTreeVo">
-        select id, menu_id, menu_name, tree_desc, tree_table_name, tree_table_primary_key, tree_table_dgl, tree_table_condition, tree_table_join_table, table_name_des, del_flag, create_by, create_by_id, create_time, update_by, update_by_id, update_time,table_id,tree_table_key,tree_show_label,tree_table_join_table_condition from {DBNAME}.drag_tree
+        select id, menu_id, menu_name, tree_desc, tree_table_name, tree_table_primary_key, tree_table_dgl, tree_table_condition, tree_table_join_table, table_name_des, del_flag, create_by, create_by_id, create_time, update_by, update_by_id, update_time,table_id,tree_table_key,tree_show_label,tree_table_join_table_condition,tree_table_join_table_primary_key from {DBNAME}.drag_tree
     </sql>
 
     <select id="selectDragTreeList" parameterType="com.zkqy.business.entity.DragTree" resultMap="DragTreeResult">
@@ -49,6 +50,9 @@
             <if test="updateById != null "> and update_by_id = #{updateById}</if>
             <if test="tableId != null">and table_id=#{tableId},</if>
             <if test="treeTableKey!=null">and tree_table_key=#{treeTableKey}</if>
+            <if test="treeShowLabel!=null">and tree_show_label=#{treeShowLabel}</if>
+            <if test="treeTableJoinTableCondition!=null">and tree_table_join_table_condition=#{treeTableJoinTableCondition}</if>
+            <if test="treeTableJoinTablePrimaryKey!=null">and tree_table_join_table_primary_key=#{treeTableJoinTablePrimaryKey}</if>
         </where>
     </select>
 
@@ -74,8 +78,10 @@
         WHERE TABLE_SCHEMA = #{dbname}
           AND TABLE_NAME = #{tableName}
     </select>
-
-
+    <select id="selectTreeNodeList" resultType="java.util.Map">
+        select *
+        from {DBNAME}.${tableName}
+    </select>
 
 
     <insert id="insertDragTree" parameterType="com.zkqy.business.entity.DragTree" useGeneratedKeys="true" keyProperty="id">
@@ -99,6 +105,9 @@
             <if test="updateTime != null">update_time,</if>
             <if test="tableId != null">table_id,</if>
             <if test="treeTableKey!=null">tree_table_key,</if>
+            <if test="treeShowLabel!=null">tree_show_label,</if>
+            <if test="treeTableJoinTableCondition!=null">tree_table_join_table_condition,</if>
+            <if test="treeTableJoinTablePrimaryKey!=null">tree_table_join_table_primary_key,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="menuId != null">#{menuId},</if>
@@ -118,9 +127,23 @@
             <if test="updateById != null">#{updateById},</if>
             <if test="updateTime != null">#{updateTime},</if>
             <if test="tableId != null">#{tableId},</if>
-            <if test="treeTableKey!=null">#{treeTableKey}</if>
+            <if test="treeTableKey!=null">#{treeTableKey},</if>
+            <if test="treeShowLabel!=null">#{treeShowLabel},</if>
+            <if test="treeTableJoinTableCondition!=null">#{treeTableJoinTableCondition},</if>
+            <if test="treeTableJoinTablePrimaryKey!=null">#{treeTableJoinTablePrimaryKey},</if>
         </trim>
     </insert>
+    <insert id="insertTreeNodeCur" parameterType="com.zkqy.business.entity.dto.DragTreeNodeDto">
+        insert into {DBNAME}.${tableName} (${columnName},${parentName}) values(#{columnValue},#{parentValue})
+    </insert>
+    <insert id="insertTreeNodeNext" parameterType="com.zkqy.business.entity.dto.DragTreeNodeDto">
+        insert into {DBNAME}.${tableName} (${columnName},${parentName}) values(#{columnValue},#{primaryValue})
+    </insert>
+    <update id="updateTreeNode">
+        update {DBNAME}.${tableName}
+        set ${columnName} = #{columnValue}
+        where ${primaryName} = #{primaryValue}
+    </update>
 
     <update id="updateDragTree" parameterType="com.zkqy.business.entity.DragTree">
         update {DBNAME}.drag_tree
@@ -142,7 +165,10 @@
             <if test="updateById != null">update_by_id = #{updateById},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
             <if test="tableId != null">table_id=#{tableId},</if>
-            <if test="treeTableKey!=null">tree_table_key=#{treeTableKey}</if>
+            <if test="treeTableKey!=null">tree_table_key=#{treeTableKey},</if>
+            <if test="treeShowLabel!=null">tree_show_label=#{treeShowLabel}</if>
+            <if test="treeTableJoinTableCondition!=null">tree_table_join_table_condition=#{treeTableJoinTableCondition},</if>
+            <if test="treeTableJoinTablePrimaryKey!=null">tree_table_join_table_primary_key=#{treeTableJoinTablePrimaryKey},</if>
         </trim>
         where id = #{id}
     </update>
@@ -157,5 +183,11 @@
             #{id}
         </foreach>
     </delete>
+    <delete id="deleteTreeNodeByIds">
+        delete from {DBNAME}.${tableName} where ${primaryName} in
+        <foreach item="id" collection="treeMapData" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </delete>
 
 </mapper>

+ 10 - 0
zkqy-process-execution/src/main/java/com/zkqy/execution/produce/dispersed/entity/CommonEntity.java

@@ -59,6 +59,9 @@ public class CommonEntity extends BaseEntity implements Serializable,Cloneable {
      */
     private Map<String, Object> btnParametersMap;
 
+    // 计算表单的结果返回存放位置
+    private Map<String,Object> calculateResultMap;
+
     public List<Map<String, String>> getAddListMap() {
         return addListMap;
     }
@@ -166,6 +169,13 @@ public class CommonEntity extends BaseEntity implements Serializable,Cloneable {
         this.execlMap = execlMap;
     }
 
+    public Map<String, Object> getCalculateResultMap() {
+        return calculateResultMap;
+    }
+
+    public void setCalculateResultMap(Map<String, Object> calculateResultMap) {
+        this.calculateResultMap = calculateResultMap;
+    }
 }
 /*
 

+ 1 - 1
zkqy-process-execution/src/main/java/com/zkqy/execution/produce/dispersed/service/ICommonService.java

@@ -98,5 +98,5 @@ public interface ICommonService {
      */
     CommonEntity queryDropDownBoxData(List<CommonEntity> commonEntityList);
 
-
+    CommonEntity queryCommonByCalculateBtn(CommonEntity commonEntity, String dfVueTemplate);
 }

+ 135 - 11
zkqy-process-execution/src/main/java/com/zkqy/execution/produce/dispersed/service/impl/CommonServiceImpl.java

@@ -25,10 +25,14 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import static com.zkqy.common.utils.StringUtils.*;
 
@@ -355,19 +359,9 @@ public class CommonServiceImpl implements ICommonService {
         // 可能会存在多个时间范围的查询,根据查询字段名称做筛选条件
         Map<String, Object> timeMap = JSONObject.parseObject(queryMap.get("timehorizon") != null ? queryMap.get("timehorizon").toString() : String.valueOf(new HashMap<String, Object>()));
 
-
         String beginTimeSql = " AND date_format(#{field},'%Y%m%d%H%i%s') >= date_format('#{beginTime}','%Y%m%d%H%i%s')";
         String endTimeSql = " AND date_format(#{field},'%Y%m%d%H%i%s') <= date_format('#{endTime}','%Y%m%d%H%i%s')";
 
-
-        //		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
-        //			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
-        //		</if>
-        //		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
-        //			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
-        //		</if>
-
-
         //  处理时间sql拼接
         timeMap.keySet().forEach(item -> {
             Map<String, Object> endtime = (Map) timeMap.get(item);
@@ -517,7 +511,90 @@ public class CommonServiceImpl implements ICommonService {
         return commonEntity;
     }
 
-    public static String extractSubstring(String input, String identifier) {
+    @Override
+    public CommonEntity queryCommonByCalculateBtn(CommonEntity commonEntity, String dfVueTemplate) {
+        //分析模板数据
+        JSONObject options = new JSONObject();
+        List<Map> columnList = new ArrayList<>();
+        try {
+            JSONObject dfVtemplateJson = JSONObject.parseObject(dfVueTemplate);
+            options = dfVtemplateJson
+                    .getJSONArray("list")
+                    .getJSONObject(0)
+                    .getJSONObject("options");
+            JSONArray tableColumns = options
+                    .getJSONArray("tableColumns");
+            columnList = tableColumns.toJavaList(Map.class);
+        }catch (NullPointerException e){
+            e.printStackTrace();
+            return null;
+        }
+        // 得到需要查询的结果列
+        List<String> columns = columnList.stream()
+                .filter(map -> (map.get("type") != null && !map.get("type").equals("result")) || (map.get("type") == null))
+                .flatMap(map -> {
+                    Object prop = map.get("prop");
+                    return prop != null ? Stream.of(prop.toString()) : Stream.empty();
+                })
+                .collect(Collectors.toList());
+        //构建得到sql所需要的查询列
+        String queryColumn = "";
+        for (int i = 0; i < columns.size(); i++) {
+            queryColumn += columns.get(i);
+            if (i != columns.size()-1){
+                queryColumn += ",";
+            }
+        }
+        //动态构建sql语句 得到查询结果
+        String mainTableName = options.getString("tableName");
+        String mainTableCondition = options.getString("mainTableCondition");
+        String joinTableCondition = options.getString("joinTableCondition");
+        // 判空
+        if (StringUtils.isBlank(mainTableName) || StringUtils.isBlank(mainTableCondition) || StringUtils.isBlank(joinTableCondition)){
+            return null;
+        }
+        CommonEntity infoById = getInfoById(commonEntity);
+        String joinTableConditionValue = infoById.getResultMap().get(joinTableCondition).toString();
+        String sql = "select " + queryColumn + " from {DBNAME}." + mainTableName + " where " + mainTableCondition + "= " + joinTableConditionValue;
+        //这里查询到的是驼峰的k-v,需要整体数组拷贝成下划线的,方便展示
+        List<Map<String, Object>> maps = commonMapper.executeSqlSelectData(sql);
+        List<Map<String, Object>> mapsUnderLine = copyListMapWithCamelToUnderline(maps);
+        //得到需要手动计算的特殊列名
+        List<String> columnsNeedHandle = columnList.stream()
+                .filter(map -> (map.get("type") != null && map.get("type").equals("result")))
+                .flatMap(map -> {
+                    Object prop = map.get("prop");
+                    return prop != null ? Stream.of(prop.toString()) : Stream.empty();
+                })
+                .collect(Collectors.toList());
+        //为每一行的返回结果加上手动计算列,根据数据库查询出来的数据二次计算
+        for (Map<String, Object> map : mapsUnderLine) {
+            for (String column : columnsNeedHandle) {
+                map.put(column,calculate(column,map));
+            }
+        }
+        HashMap<String, Object> map = new HashMap<>();
+        map.put("tableColumn",columnList);
+        map.put("tableValue",mapsUnderLine);
+        // 计算公式类型返回
+        map.put("formula",options.get("formula"));
+        map.put("showSummary",options.get("showSummary"));
+        infoById.setCalculateResultMap(map);
+        return infoById;
+    }
+
+    // 复制集合,将驼峰转换成下划线
+    private List<Map<String, Object>> copyListMapWithCamelToUnderline(List<Map<String, Object>> maps) {
+        List<Map<String, Object>> resMap = new ArrayList<>();
+        for (Map<String, Object> map : maps) {
+            HashMap<String, Object> tempMap = new HashMap<>();
+            map.forEach((s, o) -> tempMap.put(StringUtils.toUnderScoreCase(s),o));
+            resMap.add(tempMap);
+        }
+        return resMap;
+    }
+
+    public String extractSubstring(String input, String identifier) {
         int startIndex = input.indexOf(identifier);
         if (startIndex == -1) {
             return null; // 如果找不到指定的标识符,则返回空
@@ -530,6 +607,53 @@ public class CommonServiceImpl implements ICommonService {
         String sqlColumn = input.substring(startIndex + identifier.length() + 1, endIndex);
         return convertToCamelCase(sqlColumn);
     }
+
+    // 目前只支持两列的加减乘除,后续如果需要支持多列待补充
+    public static Object calculate(String columns, Map<String, Object> resMap) {
+        try {
+            if (columns.contains("~+~")) {
+                String[] split = columns.split("~\\+~");
+                BigDecimal one = getBigDecimalFromMap(resMap, split[0]);
+                BigDecimal two = getBigDecimalFromMap(resMap, split[1]);
+                return roundToTwoDecimalPlaces(one.add(two));
+            } else if (columns.contains("~-~")) {
+                String[] split = columns.split("~-~");
+                BigDecimal one = getBigDecimalFromMap(resMap, split[0]);
+                BigDecimal two = getBigDecimalFromMap(resMap, split[1]);
+                return roundToTwoDecimalPlaces(one.subtract(two));
+            } else if (columns.contains("~*~")) {
+                String[] split = columns.split("~\\*~");
+                BigDecimal one = getBigDecimalFromMap(resMap, split[0]);
+                BigDecimal two = getBigDecimalFromMap(resMap, split[1]);
+                return roundToTwoDecimalPlaces(one.multiply(two));
+            } else if (columns.contains("~/~")) {
+                String[] split = columns.split("~/~");
+                BigDecimal one = getBigDecimalFromMap(resMap, split[0]);
+                BigDecimal two = getBigDecimalFromMap(resMap, split[1]);
+                if (two.compareTo(BigDecimal.ZERO) == 0) {
+                    return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP); // 除数为零时返回 0.00
+                }
+                return roundToTwoDecimalPlaces(one.divide(two, RoundingMode.HALF_UP));
+            } else {
+                throw new IllegalArgumentException("无效的表达式");
+            }
+        } catch (Exception e) {
+            throw new IllegalArgumentException("计算失败: " + e.getMessage());
+        }
+    }
+
+    private static BigDecimal getBigDecimalFromMap(Map<String, Object> resMap, String key) {
+        Object value = resMap.getOrDefault(key, BigDecimal.ZERO);
+        if (value instanceof Number) {
+            return new BigDecimal(value.toString()); // 将 Number 转换为 BigDecimal
+        } else {
+            throw new IllegalArgumentException("值不是数字类型: " + key);
+        }
+    }
+
+    private static BigDecimal roundToTwoDecimalPlaces(BigDecimal number) {
+        return number.setScale(2, RoundingMode.HALF_UP); // 保留两位小数,四舍五入
+    }
 }
 
 /*