package com.sunxung.factoring.entity;

import com.sunxung.factoring.component.util.SpringUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.service.sys.OrgService;

/**
 * 查询辅助类
 */
public class BaseSearch {

    /** 组织机构 */
    private Org org;
    /**
     * @see LevelType
     */
    private String level = LevelType.all.name();
    private String fastSearch;
    /** 页码（首页从1开始） */
    private int page = 1;
    /** 页大小 */
    private int rows = 30;
    /** 排序字段 */
    private String sidx = "gmt_create";
    /** 排序类型 */
    private String sord = "desc";

    public void initOrg() {
        if (org == null) {
            return;
        }
        setOrg(getOrgService().get(org.getId()));
        if (LevelType.self.name().equals(level)) {
            org.setInternalCode(null);
        } else {
            org.setId(null);
        }
    }

    /** 页码（首页从1开始） */
    public int getPage() {
        return page > 1 ? (page) : 1;
    }

    public void setPage(int page) {
        this.page = page;
    }

    /** 页大小 */
    public int getRows() {
        return rows > 1 ? (rows) : 10;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    /** 排序字段 */
    public String getSidx() {
        if (sidx == null || sidx.trim().equals("")) {
            sidx = "gmt_create";
        }
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    /** 排序类型 */
    public String getSord() {
        if (sord == null || sord.trim().equals("")) {
            sord = "desc";
        }
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public String getLevel() {
        return StringUtil.isEmpty(this.level) ? LevelType.all.name() : level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getFastSearch() {
        return fastSearch;
    }

    public void setFastSearch(String fastSearch) {
        this.fastSearch = fastSearch;
    }

    public Org getOrg() {
        return org;
    }

    public void setOrg(Org org) {
        this.org = org;
    }

    public static enum LevelType {
        /** 全部 */
        all,
        /** 本级 */
        self
    }

    private static OrgService orgService;

    private static OrgService getOrgService() {
        if (orgService == null) {
            orgService = SpringUtil.getBeanByType(OrgService.class);
        }
        return orgService;
    }
}
