package org.hzhub.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.hzhub.erp.domain.entity.SalesOrganization;
import org.hzhub.erp.domain.vo.CustomerVO;

import java.util.List;

/**
 * 销售组织 Mapper（OSDORG 表）
 */
@Mapper
public interface SalesOrganizationMapper extends BaseMapper<SalesOrganization> {

    /**
     * 获取所有销区列表（从 OSDORG 表）
     * 销区通常是销售组织的第2层或第3层
     */
    @Select("SELECT DISTINCT ORGCODE AS salesAreaCode, ORGNAME AS salesAreaName " +
            "FROM OSDORG " +
            "WHERE ORGLEVEL = 3 " +  // 假设销区是第3层，可根据实际情况调整
            "  AND ORGCODE IS NOT NULL " +
            "  AND ORGNAME IS NOT NULL " +
            "  AND ISENABLE = 1 " +
            "ORDER BY ORGCODE")
    List<CustomerVO> selectSalesAreasFromOrg();

    /**
     * 获取所有销售组织层级
     */
    @Select("SELECT DISTINCT ORGLEVEL FROM OSDORG ORDER BY ORGLEVEL")
    List<Integer> selectOrgLevels();
}