package org.hzhub.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.hzhub.erp.domain.entity.CustomerGeneral;
import org.hzhub.erp.domain.vo.CustomerVO;

import java.util.List;

/**
 * 客户档案 Mapper（SCLTGENERAL 表）
 */
@Mapper
public interface CustomerMapper extends BaseMapper<CustomerGeneral> {

    /**
     * 分页查询客户列表
     */
    @Select("<script>" +
            "SELECT TOP ${pageSize} * FROM (" +
            "  SELECT ROW_NUMBER() OVER (ORDER BY CLTCODE) AS rn, " +
            "    CLTCODE AS customerCode, " +
            "    CLTNAME AS customerName, " +
            "    COMPANY_ID AS companyCode, " +
            "    COMPANY_NAME AS companyName, " +
            "    BRAND AS brand, " +
            "    BRANDNAME AS brandName, " +
            "    LINKMAN AS contactName, " +
            "    AREAID AS salesAreaCode, " +
            "    AREANAME AS salesAreaName, " +
            "    SALESID_T AS salesPersonCode, " +
            "    SALESNAME_T AS salesPersonName, " +
            "    SALEDOCID AS saleDocCode, " +
            "    SALEDOCNAME AS saleDocName, " +
            "    CLTPRICENO AS pricePlanCode, " +
            "    CLTPRICENAME AS pricePlanName, " +
            "    CLTTYPE AS customerType, " +
            "    STREET AS address, " +
            "    TEL1 AS phone, " +
            "    EMAIL AS email, " +
            "    SDORGID AS sdOrgCode, " +
            "    SDORGNAME AS sdOrgName, " +
            "    province, city, ISSTOP AS isStop " +
            "  FROM SCLTGENERAL " +
            "  WHERE 1=1 " +
            "  <if test='keyword != null and keyword != \"\"'>" +
            "    AND (CLTCODE LIKE '%' + #{keyword} + '%' " +
            "      OR CLTNAME LIKE '%' + #{keyword} + '%' " +
            "      OR LINKMAN LIKE '%' + #{keyword} + '%' " +
            "      OR AREANAME LIKE '%' + #{keyword} + '%' " +
            "      OR SALESNAME_T LIKE '%' + #{keyword} + '%') " +
            "  </if>" +
            "  <if test='companyCode != null and companyCode != \"\"'>" +
            "    AND COMPANY_ID = #{companyCode} " +
            "  </if>" +
            "  <if test='salesAreaCode != null and salesAreaCode != \"\"'>" +
            "    AND AREAID = #{salesAreaCode} " +
            "  </if>" +
            "  <if test='brand != null and brand != \"\"'>" +
            "    AND BRAND = #{brand} " +
            "  </if>" +
            ") t WHERE rn &gt; (${pageNum} - 1) * ${pageSize} ORDER BY rn" +
            "</script>")
    List<CustomerVO> selectCustomerPage(@Param("pageNum") int pageNum,
                                        @Param("pageSize") int pageSize,
                                        @Param("keyword") String keyword,
                                        @Param("companyCode") String companyCode,
                                        @Param("salesAreaCode") String salesAreaCode,
                                        @Param("brand") String brand);

    /**
     * 查询客户总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM SCLTGENERAL " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "  AND (CLTCODE LIKE '%' + #{keyword} + '%' " +
            "    OR CLTNAME LIKE '%' + #{keyword} + '%' " +
            "    OR LINKMAN LIKE '%' + #{keyword} + '%' " +
            "    OR AREANAME LIKE '%' + #{keyword} + '%' " +
            "    OR SALESNAME_T LIKE '%' + #{keyword} + '%') " +
            "</if>" +
            "<if test='companyCode != null and companyCode != \"\"'>" +
            "  AND COMPANY_ID = #{companyCode} " +
            "</if>" +
            "<if test='salesAreaCode != null and salesAreaCode != \"\"'>" +
            "  AND AREAID = #{salesAreaCode} " +
            "</if>" +
            "<if test='brand != null and brand != \"\"'>" +
            "  AND BRAND = #{brand} " +
            "</if>" +
            "</script>")
    long selectCustomerCount(@Param("keyword") String keyword,
                             @Param("companyCode") String companyCode,
                             @Param("salesAreaCode") String salesAreaCode,
                             @Param("brand") String brand);

    /**
     * 根据客户编号获取详情
     */
    @Select("SELECT " +
            "  CLTCODE AS customerCode, " +
            "  CLTNAME AS customerName, " +
            "  COMPANY_ID AS companyCode, " +
            "  COMPANY_NAME AS companyName, " +
            "  BRAND AS brand, " +
            "  BRANDNAME AS brandName, " +
            "  LINKMAN AS contactName, " +
            "  AREAID AS salesAreaCode, " +
            "  AREANAME AS salesAreaName, " +
            "  SALESID_T AS salesPersonCode, " +
            "  SALESNAME_T AS salesPersonName, " +
            "  SALEDOCID AS saleDocCode, " +
            "  SALEDOCNAME AS saleDocName, " +
            "  CLTPRICENO AS pricePlanCode, " +
            "  CLTPRICENAME AS pricePlanName, " +
            "  CLTTYPE AS customerType, " +
            "  STREET AS address, " +
            "  TEL1 AS phone, " +
            "  EMAIL AS email, " +
            "  SDORGID AS sdOrgCode, " +
            "  SDORGNAME AS sdOrgName, " +
            "  province, city, ISSTOP AS isStop " +
            "FROM SCLTGENERAL " +
            "WHERE CLTCODE = #{customerCode}")
    CustomerVO selectCustomerDetail(@Param("customerCode") String customerCode);

    /**
     * 获取所有销区列表
     */
    @Select("SELECT DISTINCT AREAID AS salesAreaCode, AREANAME AS salesAreaName " +
            "FROM SCLTGENERAL " +
            "WHERE AREAID IS NOT NULL AND AREANAME IS NOT NULL " +
            "ORDER BY AREAID")
    List<CustomerVO> selectSalesAreas();

    /**
     * 获取所有品牌列表
     */
    @Select("SELECT DISTINCT BRAND AS brand, BRANDNAME AS brandName " +
            "FROM SCLTGENERAL " +
            "WHERE BRAND IS NOT NULL AND BRANDNAME IS NOT NULL " +
            "ORDER BY BRAND")
    List<CustomerVO> selectBrands();

    /**
     * 客户选择列表（用于CRM选择器，支持多公司过滤）
     */
    @Select("<script>" +
            "SELECT TOP ${pageSize} * FROM (" +
            "  SELECT ROW_NUMBER() OVER (ORDER BY CLTCODE) AS rn, " +
            "    CLTCODE AS customerCode, " +
            "    CLTNAME AS customerName, " +
            "    COMPANY_ID AS companyCode, " +
            "    COMPANY_NAME AS companyName, " +
            "    LINKMAN AS contactName, " +
            "    TEL1 AS phone, " +
            "    province, city, ISSTOP AS isStop " +
            "  FROM SCLTGENERAL " +
            "  WHERE 1=1 " +
            "  <if test='keyword != null and keyword != \"\"'>" +
            "    AND (CLTCODE LIKE '%' + #{keyword} + '%' " +
            "      OR CLTNAME LIKE '%' + #{keyword} + '%' " +
            "      OR LINKMAN LIKE '%' + #{keyword} + '%') " +
            "  </if>" +
            "  <if test='companyIds != null and companyIds != \"\"'>" +
            "    AND COMPANY_ID IN " +
            "    <foreach collection='companyIdList' item='id' open='(' separator=',' close=')'>" +
            "      #{id}" +
            "    </foreach>" +
            "  </if>" +
            ") t WHERE rn &gt; (${pageNum} - 1) * ${pageSize} ORDER BY rn" +
            "</script>")
    List<CustomerVO> selectCustomerSelectPage(@Param("pageNum") int pageNum,
                                               @Param("pageSize") int pageSize,
                                               @Param("keyword") String keyword,
                                               @Param("companyIds") String companyIds,
                                               @Param("companyIdList") List<String> companyIdList);

    /**
     * 客户选择总数（用于CRM选择器，支持多公司过滤）
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM SCLTGENERAL " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "  AND (CLTCODE LIKE '%' + #{keyword} + '%' " +
            "    OR CLTNAME LIKE '%' + #{keyword} + '%' " +
            "    OR LINKMAN LIKE '%' + #{keyword} + '%') " +
            "</if>" +
            "<if test='companyIds != null and companyIds != \"\"'>" +
            "  AND COMPANY_ID IN " +
            "  <foreach collection='companyIdList' item='id' open='(' separator=',' close=')'>" +
            "    #{id}" +
            "  </foreach>" +
            "</if>" +
            "</script>")
    long selectCustomerSelectCount(@Param("keyword") String keyword,
                                    @Param("companyIds") String companyIds,
                                    @Param("companyIdList") List<String> companyIdList);
}
