package cn.reghao.tnb.sb.db.autobatis.mapper.crud;

import cn.reghao.tnb.sb.db.autobatis.mapper.element.BaseElement;
import cn.reghao.tnb.sb.db.autobatis.mapper.element.ContentGenerator;
import cn.reghao.tnb.sb.db.autobatis.mapper.element.InsertElement;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建 insert sql
 *
 * @author reghao
 * @date 2020-04-03 22:17:59
 */
public class InsertGenerator implements ContentGenerator {
    @Override
    public void setElementContent(BaseElement element) throws Exception {
        if (!(element instanceof InsertElement)) {
            throw new Exception("类型不是 InsertElement");
        }

        InsertElement insertElement = (InsertElement) element;
        List<String> content = insertContent(insertElement);

        List<String> classFields = insertElement.getClassFields();
        InsertElement foreachElement = insertElement.getForeachElement();
        if (foreachElement != null) {
            // values 语句
            content.add("        values ");
            foreachElement.setContent(writeForeachClassFields(classFields));
        } else {
            // values 语句
            content.add("        " + String.format("values %s", System.lineSeparator()));
            // 写入类字段
            content.add("        " + writeClassFields(classFields) + System.lineSeparator());
        }
        insertElement.setContent(content);
    }

    private List<String> insertContent(InsertElement insertElement) {
        List<String> tableFields = insertElement.getTableFields();
        List<String> classFields = insertElement.getClassFields();
        List<String> content = new ArrayList<>();
        // insert 语句
        content.add(String.format("%s        insert ignore into %s %s",
                System.lineSeparator(), insertElement.getTableName(), System.lineSeparator()));
        // 表字段
        content.add("        " + writeTableFields(tableFields) + System.lineSeparator());
        // values 语句
        //content.add("        " + String.format("values %s", System.lineSeparator()));
        // 类字段
        //content.add("        " + writeClassFields(classFields) + System.lineSeparator());
        return content;
    }

    /**
     * 插入表字段
     *
     * @param
     * @return
     * @date 2021-08-10 下午4:00
     */
    private String writeTableFields(List<String> tableFields) {
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        tableFields.forEach(field -> {
            sb.append("`").append(field).append("`,");
        });
        sb.append(") ");
        return sb.toString();
    }

    /**
     * 插入类字段
     *
     * @param
     * @return
     * @date 2021-08-10 下午4:01
     */
    private String writeClassFields(List<String> classFields) {
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        classFields.forEach(field -> {
            sb.append("#{").append(field).append("},");
        });
        sb.append(") ");
        return sb.toString();
    }

    /**
     * foreach 标签中的类字段
     *
     * @param
     * @return
     * @date 2021-08-11 下午5:04
     */
    private List<String> writeForeachClassFields(List<String> classFields) {
        List<String> list = new ArrayList<>();
        String item = "item";
        StringBuilder sb = new StringBuilder();
        sb.append(System.lineSeparator());
        sb.append("            (");
        int i = 0;
        for (; i < classFields.size()-1; i++) {
            sb.append("#{").append(item).append(".").append(classFields.get(i)).append("},");
        }
        sb.append("#{").append(item).append(".").append(classFields.get(i)).append("})");
        sb.append(System.lineSeparator());

        list.add(sb.toString());
        return list;
    }
}
