package com.example.sqlrun.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.sqlrun.entity.TablespacePartitionRule; import com.example.sqlrun.fixSql.MakePartitionSql; import com.example.sqlrun.fixSql.MakeTableSpace; import com.example.sqlrun.myEnum.SpaceEnum; import com.example.sqlrun.service.SpaceAndPartitionService; import com.example.sqlrun.service.TablespacePartitionRuleService; import com.shanghaigeography.Util.DateUtils; import java.sql.SQLException; import java.text.ParseException; import java.time.YearMonth; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Statement; /** * @ClassName: SpaceAndPartitionServiceImpl * * @description: TODO * @author: sbj * @time: 2023/12/5 15:55 @Version: 1.0 */ @Service public class SpaceAndPartitionServiceImpl implements SpaceAndPartitionService { @Resource(name = "statement") private Statement statement; @Override public void runSQL(TablespacePartitionRule rule) throws ParseException, SQLException { // 获取需要创建表空间的sql List tableSpaces = null; if ("MONTH".equals(rule.getTableSpaceType())){ tableSpaces = MakeTableSpace.makeTableSpaceSqlByMonth( rule.getTablespacePrefix(), rule.getDatafilePrefix(), rule.getDatafilePath(), rule.getInitSize(), rule.getNextSize(), rule.getMaxSize(), rule.getLastAddTablespaceTime(), rule.getWanted()); }else if ("YEAR".equals(rule.getTableSpaceType())){ tableSpaces = MakeTableSpace.makeTableSpaceSqlByYear( rule.getTablespacePrefix(), rule.getDatafilePrefix(), rule.getDatafilePath(), rule.getInitSize(), rule.getNextSize(), rule.getMaxSize(), rule.getLastAddTablespaceTime(), rule.getWanted()); }else if ("QUARTER".equals(rule.getTableSpaceType())){ tableSpaces = MakeTableSpace.makeTableSpaceSqlByQuarter( rule.getTablespacePrefix(), rule.getDatafilePrefix(), rule.getDatafilePath(), rule.getInitSize(), rule.getNextSize(), rule.getMaxSize(), rule.getLastAddTablespaceTime(), rule.getWanted()); } if (tableSpaces == null || tableSpaces.isEmpty()){ return; } for (Map tableSpace : tableSpaces) { List partitionSql = null; if ("DAY".equals(rule.getPartitionType()) && "MONTH".equals(rule.getTableSpaceType())){ Date date = (Date) tableSpace.get(SpaceEnum.DATE.getName()); partitionSql = MakePartitionSql.makePartitionSqlByDay( rule.getTableName(), rule.getPartitionPrefix(), tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(), date, YearMonth.of(date.getYear() + 1900, date.getMonth() + 1).lengthOfMonth()); }else if ("MONTH".equals(rule.getPartitionType()) && "YEAR".equals(rule.getTableSpaceType())){ partitionSql = MakePartitionSql.makePartitionSqlByMonth(rule.getTableName(), rule.getPartitionPrefix(), tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),(Date) tableSpace.get(SpaceEnum.DATE.getName())); }else if("DAY".equals(rule.getPartitionType()) && "QUARTER".equals(rule.getTableSpaceType())){ partitionSql = MakePartitionSql.makePartitionSqlByDayForQuarter(rule.getTableName(),rule.getPartitionPrefix(),tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),(Date) tableSpace.get(SpaceEnum.DATE.getName())); } System.out.println(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString() + ";"); // statement.execute(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString()); // partitionSql.forEach( // i -> { // try { // statement.addBatch(i); // } catch (SQLException throwables) { // throwables.printStackTrace(); // } // }); // statement.executeBatch(); // statement.clearBatch(); assert partitionSql != null; partitionSql.forEach(i -> System.out.println(i + ";")); } Date lastDate = (Date) tableSpaces.get(tableSpaces.size() - 1).get(SpaceEnum.DATE.getName()); rule.setLastAddTablespaceTime(DateUtils.monthAddNum(lastDate, 1)); rule.setLastAddPartitionTime(DateUtils.dayAddNum(DateUtils.monthAddNum(lastDate, 1), -1)); //ruleService.updateById(rule); } @Autowired private TablespacePartitionRuleService ruleService; @Override public List getSqlNeed() { return ruleService.list( Wrappers.lambdaQuery() .eq(TablespacePartitionRule::getIsDeleted, 0)); } @Override public String createTableSpace(Map paramBody) { try { String tableSpaceNamePrefix = paramBody.get("tableSpaceName"); String tableSpaceFileNamePrefix = paramBody.get("tableSpaceFileName"); String tableSpacePath = paramBody.get("tableSpacePath"); Integer initSize = Integer.valueOf(paramBody.get("initSize")); Integer nextSize = Integer.valueOf(paramBody.get("nextSize")); Integer maxSize = Integer.valueOf(paramBody.get("maxSize")); Date startDate = DateUtils.stringToDate(paramBody.get("startDate"), "yyyy-MM-dd"); Integer wanted = Integer.valueOf(paramBody.get("wanted")); List maps = MakeTableSpace.makeTableSpaceSqlByMonth( tableSpaceNamePrefix, tableSpaceFileNamePrefix, tableSpacePath, initSize, nextSize, maxSize, startDate, wanted); for (Map map : maps) { statement.execute(map.get(SpaceEnum.TABLESPACE_SQL.getName()).toString()); } Map map = maps.get(maps.size() - 1); return "最后一个表空间时间" + map.get(SpaceEnum.DATE.getName()) + "\n" + "最后一个表空间名称" + map.get(SpaceEnum.TABLESPACENAME.getName()); } catch (Exception e) { return "error" + e.getMessage(); } } @Override public String createTablePartition(Map param) { try { String tableName = param.get("tableName"); String partitionNamePrefix = param.get("partitionNamePrefix"); String tableSpaceName = param.get("tableSpaceName"); Date startDate = DateUtils.stringToDate(param.get("startDate"), "yyyy-MM-dd"); Integer wantedDay = null; if (StringUtils.isBlank(param.get("wantedDay"))) { Calendar instance = Calendar.getInstance(); instance.setTime(startDate); wantedDay = instance.getActualMaximum(Calendar.DAY_OF_MONTH) - instance.get(Calendar.DAY_OF_MONTH) + 1; } else { wantedDay = Integer.valueOf(param.get("wantedDay")); } List partitionSql = MakePartitionSql.makePartitionSqlByDay( tableName, partitionNamePrefix, tableSpaceName, startDate, wantedDay); for (String s : partitionSql) { statement.addBatch(s); } statement.executeBatch(); statement.clearBatch(); // h return "success"; } catch (Exception e) { return "error" + e.getMessage(); } } }