<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.custom.mapper.CustomerMainMapper">


    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <resultMap id="CustomerMainMap" type="cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO">
        <id column="id" property="id"/>
        <id column="deptCode" property="deptCode"/>
        <collection select="getAttn" property="attnList" javaType="list"
                    column="deptCode"
                    ofType="cn.iocoder.yudao.module.custom.entity.CustomerMain">
        </collection>
    </resultMap>
    <select id="selectListPage" resultMap="CustomerMainMap">
        SELECT
        main.id AS id,
        main.user_id AS userId,
        main.type_code AS typeCode,
        main.dept_code AS deptCode,
        main.cus_name AS cusName,
        main.phone_number AS phoneNumber,
        main.birthday AS birthday,
        main.address AS address,
        main.sex AS sex,
        main.id_card AS idCard,
        main.id_card_image AS idCardImage,
        main.data_from AS dataFrom,
        main.near_do_time AS nearDoTime,
        main.near_do_content AS nearDoContent,
        main.inviter AS inviter,
        main.inviter_type AS inviterType,
        main.status AS status,
        group_concat(item.ser_content) AS serContents
        FROM
        base_customer_main main
        LEFT JOIN base_customer_item item ON main.id = item.cus_id AND item.deleted = 0
        <where>
            main.deleted = 0
            <if test="entity.cusName != null and entity.cusName != ''">
                AND main.cus_name LIKE concat('%',#{entity.cusName},'%')
            </if>
            <if test="entity.typeCode != null and entity.typeCode != ''">
                AND main.type_code = #{entity.typeCode}
            </if>
            <if test="entity.systemCode != null and entity.systemCode != ''">
                AND item.system_code = #{entity.systemCode}
            </if>
            <if test="entity.phoneNumber != null and entity.phoneNumber != ''">
                AND main.phone_number LIKE concat('%',#{entity.phoneNumber},'%')
            </if>
        </where>
        GROUP BY main.id
        ORDER BY main.create_time DESC
    </select>
    <select id="getAttn" resultType="cn.iocoder.yudao.module.custom.entity.CustomerMain">
        SELECT main.id              AS id,
               main.user_id         AS userId,
               main.type_code       AS typeCode,
               main.dept_code       AS deptCode,
               main.cus_name        AS cusName,
               main.phone_number    AS phoneNumber,
               main.birthday        AS birthday,
               main.address         AS address,
               main.sex             AS sex,
               main.id_card         AS idCard,
               main.id_card_image   AS idCardImage,
               main.data_from       AS dataFrom,
               main.near_do_time    AS nearDoTime,
               main.near_do_content AS nearDoContent,
               main.inviter         AS inviter,
               main.inviter_type    AS inviterType,
               main.status          AS status
        FROM base_customer_main main
        where main.dept_code = #{deptCode}
          and main.deleted = 0
          and main.type_code = '04'
        ORDER BY main.create_time DESC
    </select>
</mapper>