package com.lifeonwalden.codeGenerator.mybatis.impl;

import org.mybatis.generator.dom.xml.Attribute;
import org.mybatis.generator.dom.xml.TextElement;
import org.mybatis.generator.dom.xml.XmlElement;

import com.lifeonwalden.codeGenerator.bean.Column;
import com.lifeonwalden.codeGenerator.bean.Table;
import com.lifeonwalden.codeGenerator.bean.config.Config;
import com.lifeonwalden.codeGenerator.constant.JdbcTypeEnum;
import com.lifeonwalden.codeGenerator.mybatis.TableElementGenerator;
import com.lifeonwalden.codeGenerator.mybatis.constant.XMLAttribute;
import com.lifeonwalden.codeGenerator.mybatis.constant.XMLTag;
import com.lifeonwalden.codeGenerator.util.StringUtil;

public class SQLQueryConditionElementGenerator implements TableElementGenerator {

  public XmlElement getElement(Table table, Config config) {
    XmlElement element = new XmlElement(XMLTag.SQL.getName());
    element.addAttribute(new Attribute(XMLAttribute.ID.getName(), "queryCondition"));

    XmlElement trimElement = new XmlElement(XMLTag.TRIM.getName());
    trimElement.addAttribute(new Attribute(XMLAttribute.PREFIX.getName(), "WHERE"));
    trimElement.addAttribute(new Attribute(XMLAttribute.PREFIX_OVERRIDES.getName(), "AND"));
    element.addElement(trimElement);

    for (Column column : table.getColumns()) {
      XmlElement ifElement = new XmlElement(XMLTag.IF.getName());
      ifElement.addAttribute(
          new Attribute(XMLAttribute.TEST.getName(), StringUtil.removeUnderline(column.getName()) + " != null"));

      StringBuilder setValueText = new StringBuilder();
      setValueText.append("AND ").append(column.getName()).append(" = ");
      setValueText.append("#{").append(StringUtil.removeUnderline(column.getName())).append(", jdbcType=")
          .append(JdbcTypeEnum.nameOf(column.getType().toUpperCase()).getName());
      if (null != column.getTypeHandler()) {
        setValueText.append(", typeHandler=").append(column.getTypeHandler());
      }
      setValueText.append("}");
      TextElement setValue = new TextElement(setValueText.toString());
      ifElement.addElement(setValue);

      trimElement.addElement(ifElement);
    }

    return element;
  }
}

@@ -1420,28 +1420,16 @@
 tribute(
-%0D%0A          
 new Attr
@@ -2177,35 +2177,2139 @@
 );%0D%0A
-    %7D%0D%0A%0D%0A    return element
+%0D%0A      JdbcTypeEnum jdbcType = JdbcTypeEnum.nameOf(column.getType().toUpperCase());%0D%0A      if (jdbcType.equals(JdbcTypeEnum.DATE) %7C%7C jdbcType.equals(JdbcTypeEnum.DATETIME) %7C%7C jdbcType.equals(JdbcTypeEnum.TIME)%0D%0A          %7C%7C jdbcType.equals(JdbcTypeEnum.TIMESTAMP)) %7B%0D%0A        dateFieldExtension(column, trimElement);%0D%0A      %7D%0D%0A    %7D%0D%0A%0D%0A    return element;%0D%0A  %7D%0D%0A%0D%0A  private void dateFieldExtension(Column column, XmlElement trimElement) %7B%0D%0A    // start part%0D%0A    XmlElement ifElement = new XmlElement(XMLTag.IF.getName());%0D%0A    ifElement.addAttribute(new Attribute(XMLAttribute.TEST.getName(), StringUtil.removeUnderline(column.getName()) + %22Start != null%22));%0D%0A%0D%0A    StringBuilder setValueText = new StringBuilder();%0D%0A    setValueText.append(%22%3C!%5BCDATA%5B AND %22).append(column.getName()).append(%22 %3E= %22);%0D%0A    setValueText.append(%22#%7B%22).append(StringUtil.removeUnderline(column.getName())).append(%22Start, jdbcType=%22)%0D%0A        .append(JdbcTypeEnum.nameOf(column.getType().toUpperCase()).getName());%0D%0A    if (null != column.getTypeHandler()) %7B%0D%0A      setValueText.append(%22, typeHandler=%22).append(column.getTypeHandler());%0D%0A    %7D%0D%0A    setValueText.append(%22%7D %5D%5D%3E%22);%0D%0A    TextElement setValue = new TextElement(setValueText.toString());%0D%0A    ifElement.addElement(setValue);%0D%0A%0D%0A    trimElement.addElement(ifElement);%0D%0A%0D%0A    // end part%0D%0A    ifElement = new XmlElement(XMLTag.IF.getName());%0D%0A    ifElement.addAttribute(new Attribute(XMLAttribute.TEST.getName(), StringUtil.removeUnderline(column.getName()) + %22End != null%22));%0D%0A%0D%0A    setValueText = new StringBuilder();%0D%0A    setValueText.append(%22%3C!%5BCDATA%5B AND %22).append(column.getName()).append(%22 %3C= %22);%0D%0A    setValueText.append(%22#%7B%22).append(StringUtil.removeUnderline(column.getName())).append(%22End, jdbcType=%22)%0D%0A        .append(JdbcTypeEnum.nameOf(column.getType().toUpperCase()).getName());%0D%0A    if (null != column.getTypeHandler()) %7B%0D%0A      setValueText.append(%22, typeHandler=%22).append(column.getTypeHandler());%0D%0A    %7D%0D%0A    setValueText.append(%22%7D %5D%5D%3E%22);%0D%0A    setValue = new TextElement(setValueText.toString());%0D%0A    ifElement.addElement(setValue);%0D%0A%0D%0A    trimElement.addElement(ifElement)
 ;%0D%0A 

add extension query condition