<?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="com.fzm.mall.customer.service.customer.mapper.GroupMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fzm.mall.customer.service.customer.model.Group">
        <id column="id" property="id" />
        <result column="customer_name" property="customerName" />
        <result column="user_name" property="userName" />
        <result column="customer_image" property="customerImage" />
        <result column="user_image" property="userImage" />
        <result column="master_id" property="masterId" />
        <result column="group_id" property="groupId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <collection property="userIdList" ofType="string">
            <result column="user_id" />
        </collection>
    </resultMap>

    <select id="listGroupAndUser" resultMap="BaseResultMap">
        select g.id, g.customer_name, g.user_name, g.customer_image, g.user_image, g.master_id, g.group_id, g.create_time, g.update_time, gu.user_id
        from customer_group g
        left join customer_group_user gu on g.group_id = gu.group_id
        where g.master_id = #{userId}
    </select>

    <select id="list" resultMap="BaseResultMap">
        select g.id, g.customer_name, g.user_name, g.customer_image, g.user_image, g.master_id, g.group_id, g.create_time, g.update_time, gu.user_id
        from customer_group g
        left join customer_group_user gu on g.group_id = gu.group_id
        <where>
            gu.user_id = #{userId}
            <if test="toId != null and toId != ''">
                and g.group_id = #{toId}
            </if>
        </where>

    </select>
</mapper>
