package com.storlead.sales.enums;

import com.storlead.framework.common.system.query.MatchTypeEnum;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @program: storlead-saas-platform
 * @description:
 * @author: chenkq
 * @create: 2026-05-15 15:55
 */
@Getter
public enum QueryRangeTypeEnum {

    ALL_USER_SQL(0, "","所有"),
    QUERY_MY_OWNERBY_SQL(10, " owner_by = ${currentUserId}","我负责的"),
    QUERY_CREATEBYMY_SQL(20," create_by = ${currentUserId}", "我发起的(创建)"),
    QUERY_SUBORDINATE_SQL(30," owner_by in (select id from `user` WHERE  FIND_IN_SET(${currentUserId},managers))", "下属的"),
    QUERY_MY_COOPERA_SQL(40,"", "我协作的"),
    QUERY_SUBORDINATE_COOPERA_SQL(50,"", "下属协作"),
    QUERY_COOPERA_SQL(60,"", "重点"),
    QUERY_MY_DEAL_SQL(70,"", "我成交的"),
    QUERY_TRANSFERRED_CUSTOMER_SQL(80,"", "已转客户"),
    QUERY_CUSTOM_SQL(999,"", "自定义");


    private Integer code;
    private String sqlScript;
    private String desc;


    public static QueryRangeTypeEnum [] queryRangeTypeEnums = QueryRangeTypeEnum.values();


    QueryRangeTypeEnum(Integer code,String sqlScript, String desc) {
        this.code = code;
        this.desc = desc;
        this.sqlScript = sqlScript;
    }

    public Integer getCode() {
        return code;
    }

    public String sql(QueryRangeTypeEnum queryRangeTypeEnum, Map map) {
        return queryRangeTypeEnum.sqlScript;
    }


    public static QueryRangeTypeEnum getQueryRangeTypeEnum(Integer queryRangeTypeCode) {
        for(QueryRangeTypeEnum fieldTypeEnum: queryRangeTypeEnums) {
            if (fieldTypeEnum.getCode().equals(queryRangeTypeCode)){
                return fieldTypeEnum;
            }
        }
        return null;
    }

    public static String getQueryRangeSql(Integer queryRangeTypeCode,Long currentUserId) {
        for(QueryRangeTypeEnum fieldTypeEnum: queryRangeTypeEnums) {
            if (fieldTypeEnum.getCode().equals(queryRangeTypeCode)){
                return fieldTypeEnum.getSqlScript().replace("${currentUserId}",currentUserId.toString());
            }
        }
        return null;
    }

    public static String getQueryRangeSql(Integer queryRangeTypeCode, Long currentUserId, MatchTypeEnum matchTypeEnum) {
        for(QueryRangeTypeEnum fieldTypeEnum: queryRangeTypeEnums) {
            if (fieldTypeEnum.getCode().equals(queryRangeTypeCode)){
                if (StringUtils.isEmpty(fieldTypeEnum.getSqlScript())) {
                    return null;
                }
                return matchTypeEnum.getValue()+""+fieldTypeEnum.getSqlScript().replace("${currentUserId}",currentUserId.toString());
            }
        }
        return null;
    }
}

