
package com.lideeyunji.tool.framework.sql.build.build.dql;


import com.lideeyunji.tool.framework.sql.build.ISQLExpression;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoOrderModel;
import com.lideeyunji.tool.framework.sql.core.dql.select.lideeYunJiQueryWrapper;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import net.sf.jsqlparser.JSQLParserException;

import java.util.*;

/**
 * 排序
 */
public class BuildOrderBy implements ISQLExpression<SqlFormatModel>  {



    @Override
    public SqlFormatModel interpret(SQLInterpretContext context) throws JSQLParserException {
        lideeYunJiQueryWrapper wrapper=new lideeYunJiQueryWrapper();

        SqlInfoOrderModel sqlInfoOrderModel = context.getOrderModel();
        List<SqlInfoOrderModel.OrderByModel> orderModels = sqlInfoOrderModel.getOrderByList();
        orderModels.forEach(orderModel -> {
            String[] columns = orderModel.getColumns();//列
            boolean isAsc = orderModel.isAsc();
            wrapper.orderBy(true,isAsc, Arrays.asList(columns));
        });

        String orderByStr = wrapper.getCustomSqlSegment();

        return new SqlFormatModel(orderByStr);
    }



}
