package com.gree.mall.manager.zfire.bean;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gree.mall.manager.plus.entity.AdminField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Data
public class ZfireParamBean {

    @ApiModelProperty(value = "页号，不传默认1")
    private Integer pageNum = 1;

    @ApiModelProperty(value = "页大小，不传默认10，查全部传 -1")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段例如：table.create_time desc")
    private String orderBy = "";

    @ApiModelProperty(value = "查询条件")
    private List<QueryParamBean> params;

    @ApiModelProperty(value = "导出的字段,导出必传")
    private List<AdminField> exportFields;

    @ApiModelProperty(value = "预留字段(前端忽略该字段)")
    private String query;

    @ApiModelProperty(value = "查询项")
    private String selected;


    @ApiModelProperty(value = "导出用字段")
    private Integer exportCount = 0;

    @JsonIgnore
    @ApiModelProperty(value = "返回类的类型")
    private Class clazzType;

    @JsonIgnore
    @ApiModelProperty("商户字段")
    private String companyWechatId;

    @JsonIgnore
    private List<String> companyIds;

    @JsonIgnore
    private List<String> adminWebsitIds;

    /**
     * 查询指定条件内容
     */
    public Object getParamValue(String param){
        if(CollectionUtils.isEmpty(params))
            return null;
        List<com.gree.mall.manager.zfire.bean.QueryParamBean> collect = params.stream().filter(v -> v.getParam().equals(param)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            return null;
        }
        return collect.get(0).getValue();
    }

    /**
     * 指定查询条件内容
     */
    public Boolean setParamValue(String param,String compare,Object value){
        if(CollectionUtils.isEmpty(params)) {
            params = new ArrayList<>();
        }
        //1. if exists remove
        this.removeParam(param);
        //2. add
        com.gree.mall.manager.zfire.bean.QueryParamBean queryParamBean = new com.gree.mall.manager.zfire.bean.QueryParamBean();
        queryParamBean.setParam(param);
        queryParamBean.setCompare(compare);
        queryParamBean.setValue(value);
        params.add(queryParamBean);
        return true;
    }

    /**
     * 删除指定查询条件
     */
    public Boolean removeParam(String param){
        if(CollectionUtils.isEmpty(params)) {
            return true;
        }
        params.removeIf(v -> v.getParam().equals(param));
        return true;
    }
}
