// Path: tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java
// public class ShopOrder extends BaseOrder {
// 
// }
// 
// Path: tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java
// public class TShardingRoutingHandler implements DataSourceRoutingHandler {
// 
//     @Override
//     public String dynamicRoute(Method method, Object[] args) {
//         //route逻辑见TShardingRoutingInvokeFactory
//         return "testschema";
//     }
// 
//     @Override
//     public Collection<String> values() {
//         //暂未使用
//         return null;
//     }
// }
// 
// Path: tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java
// public class MapperResourceEnhancer extends MapperEnhancer{
// 
//     Logger logger = LoggerFactory.getLogger(MapperResourceEnhancer.class);
// 
//     public MapperResourceEnhancer(Class<?> mapperClass) {
//         super(mapperClass);
//     }
// 
//     public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configuration, Long shardingPara) {
// 
//         String tableName = ShardingCaculator.caculateTableName(shardingPara);
//         SqlSource result = null;
// 
//         try {
//             if (ms.getSqlSource() instanceof DynamicSqlSource) {
// 
//                 DynamicSqlSource sqlSource = (DynamicSqlSource) ms.getSqlSource();
// 
//                 Class sqlSourceClass = sqlSource.getClass();
// 
//                 Field sqlNodeField = sqlSourceClass.getDeclaredField("rootSqlNode");
//                 sqlNodeField.setAccessible(true);
// 
//                 MixedSqlNode rootSqlNode = (MixedSqlNode) sqlNodeField.get(sqlSource);
// 
//                 Class mixedSqlNodeClass = rootSqlNode.getClass();
//                 Field contentsField = mixedSqlNodeClass.getDeclaredField("contents");
//                 contentsField.setAccessible(true);
//                 List<SqlNode> textSqlNodes = (List<SqlNode>) contentsField.get(rootSqlNode);
//                 List<SqlNode> newSqlNodesList = new ArrayList();
// 
//                 //StaticTextSqlNode
//                 Class textSqlNodeClass = textSqlNodes.get(0).getClass();
//                 Field textField = textSqlNodeClass.getDeclaredField("text");
//                 textField.setAccessible(true);
//                 for (SqlNode node : textSqlNodes) {
//                     if (node instanceof StaticTextSqlNode) {
//                         StaticTextSqlNode textSqlNode = (StaticTextSqlNode) node;
//                         String text = (String) textField.get(textSqlNode);
//                         if(!text.contains("TradeOrder")){
//                             newSqlNodesList.add(node);
//                         }else {
//                             newSqlNodesList.add(new StaticTextSqlNode(replaceWithShardingTableName(text, tableName, shardingPara)));
//                         }
//                     }else{
//                         newSqlNodesList.add(node);
//                     }
//                 }
// 
//                 MixedSqlNode newrootSqlNode = new MixedSqlNode(newSqlNodesList);
//                 result = new DynamicSqlSource(configuration, newrootSqlNode);
//                 return result;
// 
//             } else if (ms.getSqlSource() instanceof RawSqlSource) {
// 
//                 RawSqlSource sqlSource = (RawSqlSource) ms.getSqlSource();
//                 Class sqlSourceClass = sqlSource.getClass();
//                 Field sqlSourceField = sqlSourceClass.getDeclaredField("sqlSource");
//                 sqlSourceField.setAccessible(true);
//                 StaticSqlSource staticSqlSource = (StaticSqlSource) sqlSourceField.get(sqlSource);
//                 Field sqlField = staticSqlSource.getClass().getDeclaredField("sql");
//                 Field parameterMappingsField = staticSqlSource.getClass().getDeclaredField("parameterMappings");
//                 sqlField.setAccessible(true);
//                 parameterMappingsField.setAccessible(true);
// 
//                 //sql处理
//                 String sql = (String) sqlField.get(staticSqlSource);
// 
//                 if(!sql.contains("TradeOrder")){
//                     result = sqlSource;
//                 }else {
//                     sql = replaceWithShardingTableName(sql, tableName, shardingPara);
//                     result = new RawSqlSource(configuration, sql, null);
//                     //为sqlSource对象设置mappering参数
//                     StaticSqlSource newStaticSqlSource = (StaticSqlSource) sqlSourceField.get(result);
//                     List<ParameterMapping> parameterMappings = (List<ParameterMapping>)parameterMappingsField.get(staticSqlSource);
//                     parameterMappingsField.set(newStaticSqlSource, parameterMappings);
//                 }
//                 return result;
//             } else {
//                 throw new RuntimeException("wrong sqlSource type!" + ms.getResource());
//             }
// 
//         } catch (Exception e) {
//             logger.error("reflect error!, ms resources:" + ms.getResource(), e);
//         }
//         return result;
//     }
// 
// 
//     private String replaceWithShardingTableName(String text, String tableName, Long shardingPara){
//         if(text.contains(" TradeOrderPressureTest")){
//             return text.replace(" TradeOrderPressureTest", " TradeOrderPressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara));
//         }
//         return text.replace(" TradeOrder", " " + tableName);
//     }
// }


import com.mogujie.service.tsharding.bean.ShopOrder;
import com.mogujie.trade.db.DataSourceRouting;
import com.mogujie.trade.tsharding.annotation.ShardingExtensionMethod;
import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara;
import com.mogujie.trade.tsharding.route.TShardingRoutingHandler;
import com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer;
import org.apache.ibatis.annotations.Param;
import java.util.List;
package com.mogujie.service.tsharding.mapper;



@DataSourceRouting(handler = TShardingRoutingHandler.class)
public interface ShopOrderMapper {

// Path: tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java
// public class ShopOrder extends BaseOrder {
// 
// }
// 
// Path: tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java
// public class TShardingRoutingHandler implements DataSourceRoutingHandler {
// 
//     @Override
//     public String dynamicRoute(Method method, Object[] args) {
//         //route逻辑见TShardingRoutingInvokeFactory
//         return "testschema";
//     }
// 
//     @Override
//     public Collection<String> values() {
//         //暂未使用
//         return null;
//     }
// }
// 
// Path: tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java
// public class MapperResourceEnhancer extends MapperEnhancer{
// 
//     Logger logger = LoggerFactory.getLogger(MapperResourceEnhancer.class);
// 
//     public MapperResourceEnhancer(Class<?> mapperClass) {
//         super(mapperClass);
//     }
// 
//     public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configuration, Long shardingPara) {
// 
//         String tableName = ShardingCaculator.caculateTableName(shardingPara);
//         SqlSource result = null;
// 
//         try {
//             if (ms.getSqlSource() instanceof DynamicSqlSource) {
// 
//                 DynamicSqlSource sqlSource = (DynamicSqlSource) ms.getSqlSource();
// 
//                 Class sqlSourceClass = sqlSource.getClass();
// 
//                 Field sqlNodeField = sqlSourceClass.getDeclaredField("rootSqlNode");
//                 sqlNodeField.setAccessible(true);
// 
//                 MixedSqlNode rootSqlNode = (MixedSqlNode) sqlNodeField.get(sqlSource);
// 
//                 Class mixedSqlNodeClass = rootSqlNode.getClass();
//                 Field contentsField = mixedSqlNodeClass.getDeclaredField("contents");
//                 contentsField.setAccessible(true);
//                 List<SqlNode> textSqlNodes = (List<SqlNode>) contentsField.get(rootSqlNode);
//                 List<SqlNode> newSqlNodesList = new ArrayList();
// 
//                 //StaticTextSqlNode
//                 Class textSqlNodeClass = textSqlNodes.get(0).getClass();
//                 Field textField = textSqlNodeClass.getDeclaredField("text");
//                 textField.setAccessible(true);
//                 for (SqlNode node : textSqlNodes) {
//                     if (node instanceof StaticTextSqlNode) {
//                         StaticTextSqlNode textSqlNode = (StaticTextSqlNode) node;
//                         String text = (String) textField.get(textSqlNode);
//                         if(!text.contains("TradeOrder")){
//                             newSqlNodesList.add(node);
//                         }else {
//                             newSqlNodesList.add(new StaticTextSqlNode(replaceWithShardingTableName(text, tableName, shardingPara)));
//                         }
//                     }else{
//                         newSqlNodesList.add(node);
//                     }
//                 }
// 
//                 MixedSqlNode newrootSqlNode = new MixedSqlNode(newSqlNodesList);
//                 result = new DynamicSqlSource(configuration, newrootSqlNode);
//                 return result;
// 
//             } else if (ms.getSqlSource() instanceof RawSqlSource) {
// 
//                 RawSqlSource sqlSource = (RawSqlSource) ms.getSqlSource();
//                 Class sqlSourceClass = sqlSource.getClass();
//                 Field sqlSourceField = sqlSourceClass.getDeclaredField("sqlSource");
//                 sqlSourceField.setAccessible(true);
//                 StaticSqlSource staticSqlSource = (StaticSqlSource) sqlSourceField.get(sqlSource);
//                 Field sqlField = staticSqlSource.getClass().getDeclaredField("sql");
//                 Field parameterMappingsField = staticSqlSource.getClass().getDeclaredField("parameterMappings");
//                 sqlField.setAccessible(true);
//                 parameterMappingsField.setAccessible(true);
// 
//                 //sql处理
//                 String sql = (String) sqlField.get(staticSqlSource);
// 
//                 if(!sql.contains("TradeOrder")){
//                     result = sqlSource;
//                 }else {
//                     sql = replaceWithShardingTableName(sql, tableName, shardingPara);
//                     result = new RawSqlSource(configuration, sql, null);
//                     //为sqlSource对象设置mappering参数
//                     StaticSqlSource newStaticSqlSource = (StaticSqlSource) sqlSourceField.get(result);
//                     List<ParameterMapping> parameterMappings = (List<ParameterMapping>)parameterMappingsField.get(staticSqlSource);
//                     parameterMappingsField.set(newStaticSqlSource, parameterMappings);
//                 }
//                 return result;
//             } else {
//                 throw new RuntimeException("wrong sqlSource type!" + ms.getResource());
//             }
// 
//         } catch (Exception e) {
//             logger.error("reflect error!, ms resources:" + ms.getResource(), e);
//         }
//         return result;
//     }
// 
// 
//     private String replaceWithShardingTableName(String text, String tableName, Long shardingPara){
//         if(text.contains(" TradeOrderPressureTest")){
//             return text.replace(" TradeOrderPressureTest", " TradeOrderPressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara));
//         }
//         return text.replace(" TradeOrder", " " + tableName);
//     }
// }


// Path: tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java
import com.mogujie.service.tsharding.bean.ShopOrder;
import com.mogujie.trade.db.DataSourceRouting;
import com.mogujie.trade.tsharding.annotation.ShardingExtensionMethod;
import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara;
import com.mogujie.trade.tsharding.route.TShardingRoutingHandler;
import com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer;
import org.apache.ibatis.annotations.Param;
import java.util.List;

package com.mogujie.service.tsharding.mapper;



@DataSourceRouting(handler = TShardingRoutingHandler.class)
public interface ShopOrderMapper {

    @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL")