package {{packageName}}.core.{{entity.module}}.filter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import cn.ibizlab.util.filter.MongoQueryContext;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.alibaba.fastjson.annotation.JSONField;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
{{#entity.enableES}}
import org.elasticsearch.index.query.QueryBuilders;
{{/entity.enableES}}
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
/**
 * 关系型数据实体[{{entity.codeName}}] 查询条件对象
 */
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@ApiModel("{{entity.logicName}}")
public class {{entity.codeName}}SearchContext extends MongoQueryContext {

    {{#each entity.fields as |field|}}
            {{#each field.allPSDEFSearchModes as |formitem|}}
            {{#unless formitem.valueFunc}}
                    {{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}
    @JsonProperty("{{lowerCase formitem.name}}")
    @JSONField(name = "{{lowerCase formitem.name}}")
                    {{else}}
    @JsonProperty("{{lowerCase formitem.name}}")
                        {{#timeType}}
    @JsonFormat(pattern = "{{format}}", locale = "zh", timezone = "GMT+8")
                        {{/timeType}}
    @JSONField(name = "{{lowerCase formitem.name}}"{{#timeType}} , format = "{{format}}"{{/timeType}})
                        {{#eq type.java "Long"}}
    @JsonSerialize(using = ToStringSerializer.class)            
                        {{/eq}}
                    {{/if}}
    @ApiModelProperty("{{field.logicName}}{{formitem.valueOP}}")
	private {{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}String{{else}}{{field.type.java}}{{/if}} {{camelCase field.codeName}}{{formitem.valueOP}};
	public {{entity.codeName}}SearchContext set{{pascalCase field.codeName}}{{formitem.valueOP}}({{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL"))}}String{{else}}{{field.type.java}}{{/if}} {{camelCase field.codeName}}{{formitem.valueOP}}) {
        this.{{camelCase field.codeName}}{{formitem.valueOP}} = {{camelCase field.codeName}}{{formitem.valueOP}};
        if(!ObjectUtils.isEmpty(this.{{camelCase field.codeName}}{{formitem.valueOP}})){
                    {{#eq formitem.valueOP "LIKE"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "LEFTLIKE"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "RIGHTLIKE"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").regex({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "EQ"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").is({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "NOTEQ"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").ne({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "GT"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").gt({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "GTANDEQ"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").gte({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "LT"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").lt({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "LTANDEQ"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").lte({{camelCase field.codeName}}{{formitem.valueOP}}));
                    {{/eq}}
                    {{#eq formitem.valueOP "ISNOTNULL"}}
			if(this.{{camelCase field.codeName}}{{formitem.valueOP}}.equals("1")){
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").ne(null));
			}
                    {{/eq}}
                    {{#eq formitem.valueOP "ISNULL"}}
			if(this.{{camelCase field.codeName}}{{formitem.valueOP}}.equals("1")){
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").is(null));
			}
                    {{/eq}}
                    {{#eq formitem.valueOP "IN"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").in({{camelCase field.codeName}}{{formitem.valueOP}}.split(";|,|;|,")));
                    {{/eq}}
                    {{#eq formitem.valueOP "NOTIN"}}
            this.getSearchCond().addCriteria(Criteria.where("{{camelCase field.codeName}}").nin({{camelCase field.codeName}}{{formitem.valueOP}}.split(";|,|;|,")));
                    {{/eq}} 
        }
        return this;
    }
                    
            {{/unless}}
            {{/each}}
    {{/each}}

    /**
	 * 启用快速搜索
	 */
	public void setQuery(String query)
	{
		this.query=query;
		if(!StringUtils.isEmpty(query)){
            Criteria criteria = new Criteria();
            criteria.orOperator(
    {{#each entity.quickSearchFields as |field|}}
            {{#if @first}}
                Criteria.where("{{camelCase field.codeName}}").regex(query)
            {{else}}
                , Criteria.where("{{camelCase field.codeName}}").regex(query)
            {{/if}}
    {{/each}}
            );
            this.getSearchCond().addCriteria(criteria);
		}
	}
}