<?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.zhyc.module.base.mapper.BasSheepGroupMappingMapper">
    
    <resultMap type="BasSheepGroupMapping" id="BasSheepGroupMappingResult">
        <result property="id"    column="id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="groupId"    column="group_id"    />
    </resultMap>

    <sql id="selectBasSheepGroupMappingVo">
        select id, sheep_id, group_id from bas_sheep_group_mapping
    </sql>

<!--    <select id="selectBasSheepGroupMappingList" parameterType="BasSheepGroupMapping" resultMap="BasSheepGroupMappingResult">-->
<!--        <include refid="selectBasSheepGroupMappingVo"/>-->
<!--        <where>  -->
<!--            <if test="sheepId != null "> and sheep_id = #{sheepId}</if>-->
<!--            <if test="groupId != null "> and group_id = #{groupId}</if>-->
<!--        </where>-->
<!--    </select>-->

    <select id="selectBasSheepGroupMappingList"
            parameterType="map"
            resultType="map"> <!-- 1. 返回 map 方便前端直接取值 -->

        SELECT
        m.id,
        m.sheep_id,
        m.group_id,
        bsg.group_name,
        s.id AS bs_sheep_id,
        s.bs_manage_tags,
        s.variety,
        s.gender,
        s.name,
        s.birthday,
        s.parity,
        s.month_age,
        s.breed,
        s.birth_weight,
        s.weaning_weight,
        s.current_weight,
        s.father_manage_tags,
        s.mother_manage_tags,
        s.family
        FROM bas_sheep_group_mapping m
        JOIN sheep_file s ON s.id = m.sheep_id
        JOIN bas_sheep_group bsg ON m.group_id = bsg.group_id
        <where>
            <if test="sheepId != null"> AND m.sheep_id = #{sheepId}</if>
            <if test="groupId != null"> AND m.group_id = #{groupId}</if>
            <if test="bsManageTags != null and bsManageTags.size > 0">
                AND s.bs_manage_tags IN
                <foreach collection="bsManageTags" item="tag" open="(" separator="," close=")">
                    #{tag}
                </foreach>
            </if>
        </where>
        ORDER BY m.id
    </select>

    
    <select id="selectBasSheepGroupMappingById" parameterType="Long" resultMap="BasSheepGroupMappingResult">
        <include refid="selectBasSheepGroupMappingVo"/>
        where id = #{id}
    </select>

    <insert id="insertBasSheepGroupMapping" parameterType="BasSheepGroupMapping" useGeneratedKeys="true" keyProperty="id">
        insert into bas_sheep_group_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="groupId != null">group_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="groupId != null">#{groupId},</if>
         </trim>
    </insert>

    <update id="updateBasSheepGroupMapping" parameterType="BasSheepGroupMapping">
        update bas_sheep_group_mapping
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBasSheepGroupMappingById" parameterType="Long">
        delete from bas_sheep_group_mapping where id = #{id}
    </delete>

    <delete id="deleteBasSheepGroupMappingByIds" parameterType="String">
        delete from bas_sheep_group_mapping where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <!-- 根据耳号查询羊只ID -->
    <select id="selectSheepIdsByEarTags" parameterType="list" resultType="map">
        SELECT id, manage_tags
        FROM bas_sheep
        WHERE manage_tags IN
        <foreach collection="earTags" item="tag" open="(" separator="," close=")">
            #{tag}
        </foreach>
    </select>

    <!-- 批量插入 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO bas_sheep_group_mapping (sheep_id, group_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.sheepId}, #{item.groupId})
        </foreach>
    </insert>


    <select id="selectListByGroupId"
            resultMap="BasSheepGroupMappingResult">
        SELECT id, sheep_id, group_id
        FROM bas_sheep_group_mapping
        WHERE group_id = #{groupId}
    </select>

    <!-- 树形分组 -->
    <select id="selectGroupTree"
            resultType="com.zhyc.module.base.domain.BasGroupTreeVo">
        SELECT group_id   AS groupId,
               group_name AS groupName,
               parent_id  AS parentId
        FROM bas_sheep_group
        ORDER BY parent_id, group_id
    </select>
</mapper>