霍林河露天煤矿生产一体化平台
1
13693261870
2023-04-07 6fa0ffc3a98467cc81634f585210e3335c91a264
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.terra.coal.extend;
 
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.sql.SqlScriptUtils;
import com.terra.coal.entity.StaticData;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
 
/**
 * UpdateBatchMethod
 * @author WWW
 */
@SuppressWarnings("ALL")
public class UpdateBatchMethod extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
        String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
        //String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
        String setSql = getSqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
 
        // update %s %s where %s=#{%s} %s
        // update tab set a=#{a} where gid=1
 
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
 
        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
    }
 
    private String getSqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias, final String prefix) {
        // String sqlScript = table.getAllSqlSet(logic, prefix);
        String sqlScript = getSqlSet(table);
        if (judgeAliasNull) {
            sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
        }
        if (ew) {
            sqlScript = sqlScript + "\n";
            sqlScript = sqlScript + this.convertIfEwParam("ew.sqlSet", false);
        }
 
        sqlScript = SqlScriptUtils.convertSet(sqlScript);
 
        return sqlScript;
    }
 
    private String getSqlSet(TableInfo tableInfo) {
        StringBuilder sb = new StringBuilder();
        for (TableFieldInfo f : tableInfo.getFieldList()) {
            if (StaticData.UPDATE_EXCLUDE_FIELDS.contains(f.getProperty())) {
                continue;
            }
 
            if ("geom".equals(f.getColumn())) {
                sb.append("<if test=\"item['geom'] != null\">geom=${item.geom},</if>\n");
                continue;
            }
 
            sb.append(String.format("<if test=\"item['%s'] != null\">%s=#{item.%s},</if>\n", f.getProperty(), f.getColumn(), f.getProperty()));
        }
        sb.deleteCharAt(sb.length() - 1);
 
        return sb.toString();
    }
}