package com.it.rattan.intceptor;

import com.ejlchina.searcher.SearchSql;
import com.ejlchina.searcher.SqlInterceptor;
import com.ejlchina.searcher.util.StringUtils;
import com.it.rattan.SpcCloudApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MySqlInterceptor implements SqlInterceptor {
    @Override
    public <T> SearchSql<T> intercept(SearchSql<T> searchSql, Map<String, Object> paraMap) {
        if (searchSql.isShouldQueryList()) {
            String listSql = searchSql.getListSqlString();
            String countSql = searchSql.getClusterSqlString();
            String prodSearchKey = (String) paraMap.get("prodName-prodModel");
            String orderByStr = "";
            if(listSql.toUpperCase().contains(" ORDER BY ")){
                orderByStr = listSql.substring(listSql.toUpperCase().indexOf("ORDER BY"));
                orderByStr = orderByStr.substring(0,orderByStr.indexOf("limit"));
            }
            if(StringUtils.isNotBlank(prodSearchKey)){
                listSql = listSql.replace(orderByStr,"");
                if(listSql.toUpperCase().contains(" WHERE ")){
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "and (t.prod_name like '%"+prodSearchKey+"%' or pd.prod_Model like '%"+prodSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit") );
                    countSql = countSql + "and (t.prod_name like '%"+prodSearchKey+"%' or pd.prod_Model like '%"+prodSearchKey+"%')";
                }else{
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "where (t.prod_name like '%"+prodSearchKey+"%' or pd.prod_Model like '%"+prodSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit"));
                    countSql = countSql + " where (t.prod_name like '%"+prodSearchKey+"%' or pd.prod_Model like '%"+prodSearchKey+"%')";
                }
            }

            String quotationSearchKey = (String) paraMap.get("projName-quotationNum");

            if(StringUtils.isNotBlank(quotationSearchKey)){
                listSql = listSql.replace(orderByStr,"");
                if(listSql.toUpperCase().contains(" WHERE ")){
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "and (t.proj_Name like '%"+quotationSearchKey+"%' or t.quotation_Num like '%"+quotationSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit") );
                    countSql = countSql + "and (t.proj_Name like '%"+quotationSearchKey+"%' or t.quotation_Num like '%"+quotationSearchKey+"%')";
                }else{
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "where (t.proj_Name like '%"+quotationSearchKey+"%' or t.quotation_Num like '%"+quotationSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit"));
                    countSql = countSql + " where (t.proj_Name like '%"+quotationSearchKey+"%' or t.quotation_Num like '%"+quotationSearchKey+"%')";
                }
            }

            String contractSearchKey = (String) paraMap.get("contractNum-contractName");

            if(StringUtils.isNotBlank(contractSearchKey)){
                listSql = listSql.replace(orderByStr,"");
                if(listSql.toUpperCase().contains(" WHERE ")){
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "and (t.contract_Num like '%"+contractSearchKey+"%' or t.contract_Name like '%"+contractSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit") );
                    countSql = countSql + "and (t.contract_Num like '%"+contractSearchKey+"%' or t.contract_Name like '%"+contractSearchKey+"%')";
                }else{
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "where (t.contract_Num like '%"+contractSearchKey+"%' or t.contract_Name like '%"+contractSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit"));
                    countSql = countSql + " where (t.contract_Num like '%"+contractSearchKey+"%' or t.contract_Name like '%"+contractSearchKey+"%')";
                }
            }



            String userSearchKey = (String) paraMap.get("userAccount-userName");
            if(StringUtils.isNotBlank(userSearchKey)){
                listSql = listSql.replace(orderByStr,"");
                if(listSql.toUpperCase().contains(" WHERE ")){
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "and (u.user_Account like '%"+userSearchKey+"%' or u.user_Name like '%"+userSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit") );
                    countSql = countSql + "and (u.user_Account like '%"+userSearchKey+"%' or u.user_Name like '%"+userSearchKey+"%')";
                }else{
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "where (u.user_Account like '%"+userSearchKey+"%' or u.user_Name like '%"+userSearchKey+"%') " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit"));
                    countSql = countSql + " where (u.user_Account like '%"+userSearchKey+"%' or u.user_Name like '%"+userSearchKey+"%')";
                }
            }



            String searchDay = (String) paraMap.get("searchDay");
            //YEARWEEK( FROM_UNIXTIME( `created_at`, "%Y-%m-%d %H:%i:%s" ) ,1) = YEARWEEK( now(),1 )
            if(StringUtils.isNotBlank(searchDay) && ("today".equals(searchDay) || "week".equals(searchDay))){
                listSql = listSql.replace(orderByStr,"");
                if(listSql.toUpperCase().contains(" WHERE ")){
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "and ("+("today".equals(searchDay) ? "to_days(t.create_time) = to_days(now()) " : "YEARWEEK(date_format(t.create_time,'%Y-%m-%d'),1)=YEARWEEK(now(),1)")+") " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit") );
                    countSql = countSql + "and ("+("today".equals(searchDay) ? "to_days(t.create_time) = to_days(now()) " : "YEARWEEK(date_format(t.create_time,'%Y-%m-%d'),1)=YEARWEEK(now(),1)")+")";
                }else{
                    listSql = listSql.substring(0,listSql.indexOf("limit")) + "where ("+("today".equals(searchDay) ? "to_days(t.create_time) = to_days(now()) " : "YEARWEEK(date_format(t.create_time,'%Y-%m-%d'),1)=YEARWEEK(now(),1)")+") " +(StringUtils.isNotBlank(orderByStr) ? orderByStr : "")+" "+ listSql.substring(listSql.indexOf("limit"));
                    countSql = countSql + " where ("+("today".equals(searchDay) ? "to_days(t.create_time) = to_days(now()) " : "YEARWEEK(date_format(t.create_time,'%Y-%m-%d'),1)=YEARWEEK(now(),1)")+")";
                }
            }
            searchSql.setListSqlString(listSql);
            searchSql.setClusterSqlString(countSql);
        }
        return searchSql;
    }


    public static void main(String[] args) {

        String aa = "adasdhask order by as desc limt 5,5";
        String bb = aa.substring(aa.indexOf("order by"));
        System.out.println(bb);
    }
}
