
package com.jeelowcode.framework.plus.build.build.dql;


import com.jeelowcode.framework.plus.build.ISQLExpression;
import com.jeelowcode.framework.plus.build.buildmodel.SQLInterpretContext;
import com.jeelowcode.framework.plus.build.buildmodel.dql.SqlInfoOrderModel;
import com.jeelowcode.framework.plus.core.dql.select.JeeLowCodeQueryWrapper;
import com.jeelowcode.framework.plus.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 {
        JeeLowCodeQueryWrapper wrapper=new JeeLowCodeQueryWrapper();

        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);
    }



}
