package com.platform.utils;

import com.platform.xss.SQLFilter;
import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-03-14 23:15
 */
@Data
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    //当前页码
    private Integer page = 1;
    //每页条数
    private Integer limit = 10;

    public Query(Map<String, Object> params) {
        this.putAll(params);

        //分页参数
        this.page = params.get("page") == null ? 1 :  Integer.parseInt(params.get("page").toString());
        this.limit = params.get("limit") == null ? 10 :  Integer.parseInt(params.get("limit").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        if(params.get("sidx")!=null){
            String sidx = params.get("sidx").toString();
            String humpToLine = SQLFilter.humpToLine(sidx);
            this.put("sidx", SQLFilter.sqlInject(humpToLine));
        }
        if(params.get("order")!=null){
            String order = params.get("order").toString();
            this.put("order", SQLFilter.sqlInject(order));
        }
    }

}
