package cn.com.capcloud.core.noentitydb;

import cn.com.capcloud.api.action.service.DataSourceService;
import java.util.List;
import java.util.Map;

/**
 * @Author LiaoJiaHui
 * @Create 2026/4/29
 */
public class DefaultActionUtil {

    public static JtPage applyDesensitize(String jsonStr, JtPage jtPage, boolean exportMode, DataSourceService  dsService) {
        List<Map<String, Object>> fieldList = dsService.getQueryInfo(jsonStr);
        for (Map<String, Object> fieldMap : fieldList) {
            String name = String.valueOf(fieldMap.get("name"));
            String isDesensitize = String.valueOf(fieldMap.get("is_desensitize"));
            String desensitizeType = String.valueOf(fieldMap.get("desensitize_type"));
            boolean shouldDesensitize =
                    "Y".equals(isDesensitize) &&
                            (exportMode
                                    ? ("1".equals(desensitizeType) || "3".equals(desensitizeType))//type:1查询和导出都脱敏，type：3仅导出加密
                                    : ("1".equals(desensitizeType) || "2".equals(desensitizeType)));//type:1查询和导出都脱敏，type：2仅查询加密
            if (!shouldDesensitize) {
                continue;
            }
            String desensitizeExpr = String.valueOf(fieldMap.get("desensitize_expr"));
            String desensitizeReplacement = String.valueOf(fieldMap.get("desensitize_replacement"));
            for (Map<String, Object> row : jtPage.getContent()) {
                String value = String.valueOf(row.get(name));
                row.put(name, value.replaceAll(desensitizeExpr, desensitizeReplacement));
            }
        }
        return jtPage;
    }

}
