<?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.produce.manage_sheep.mapper.ScTransitionInfoMapper">
    <resultMap type="com.zhyc.module.produce.manage_sheep.domain.ScTransitionInfo" id="ScTransitionInfoResult">
        <result property="id" column="id"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="eventType" column="event_type"/>
        <result property="varietyId" column="variety_id"/>
        <result property="varietyName" column="varietyName"/>
        <result property="transTo" column="trans_to"/>
        <result property="transFrom" column="trans_from"/>
        <result property="transType" column="trans_type"/>
        <result property="transTypeText" column="transTypeText"/>
        <result property="transitionDate" column="transition_date"/>
        <result property="technician" column="technician"/>
        <result property="status" column="status"/>
        <result property="comment" column="comment"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>

    <sql id="selectScTransitionInfoVo">
        SELECT t.*,
               bv.variety        AS varietyName,
               bs.manage_tags    AS manageTags,
               t.user_id,
               t.dept_id,
               sf.sheepfold_name AS sheepfoldName,
               t.event_type      AS eventType,
               t.transition_date AS transitionDate,
               dr.ranch          AS currentRanchName,
               CASE t.trans_type
                   WHEN 0 THEN '内部调拨'
                   WHEN 1 THEN '内部销售'
                   WHEN 2 THEN '育肥调拨'
                   ELSE '未知'
                   END           AS transTypeText,
               CASE t.status
                   WHEN 0 THEN '待审批'
                   WHEN 1 THEN '已通过'
                   WHEN 2 THEN '已驳回'
                   ELSE '未知状态'
                   END           AS statusText
        FROM sc_transition_info t
                 LEFT JOIN bas_sheep bs ON t.sheep_id = bs.id
                 LEFT JOIN bas_sheep_variety bv ON bs.variety_id = bv.id
                 LEFT JOIN da_sheepfold sf ON t.sheepfold_id = sf.id
                 LEFT JOIN da_ranch dr ON bs.ranch_id = dr.id
    </sql>

    <select id="selectScTransitionInfoList" resultMap="ScTransitionInfoResult">
        <include refid="selectScTransitionInfoVo"/>
        <where>
            <if test="sc.ids != null and sc.ids.length > 0">
                and t.id IN
                <foreach collection="sc.ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="sc.ids == null or sc.ids.length == 0">
                <if test="manageTagsList != null and manageTagsList.size() > 0">
                    <foreach collection="manageTagsList" item="tag" separator="or" open="and (" close=")">
                        bs.manage_tags like concat('%', #{tag}, '%')
                    </foreach>
                </if>
                <if test="sc.sheepId != null">and t.sheep_id = #{sc.sheepId}</if>
                <if test="sc.transType != null">and t.trans_type = #{sc.transType}</if>
                <if test="sc.varietyId != null">and bs.variety_id = #{sc.varietyId}</if>
                <if test="sc.transTo != null and sc.transTo != ''">and t.trans_to = #{sc.transTo}</if>
                <if test="sc.transFrom != null and sc.transFrom != ''">and t.trans_from = #{sc.transFrom}</if>
                <if test="sc.status != null">and t.status = #{sc.status}</if>
                <if test="sc.params != null and sc.params.beginTransitionDate != null and sc.params.beginTransitionDate != '' and sc.params.endTransitionDate != null and sc.params.endTransitionDate != ''">
                    and t.transition_date between #{sc.params.beginTransitionDate} and #{sc.params.endTransitionDate}
                </if>
                <if test="sc.technician != null and sc.technician != ''">
                    and t.technician like concat('%', #{sc.technician}, '%')
                </if>
                <if test="sc.isDelete != null">
                    and bs.is_delete = #{sc.isDelete}
                </if>
                <if test="sc.sheepTypeId != null">
                    and bs.type_id = #{sc.sheepTypeId}
                </if>
                <if test="sc.currentRanchId != null">
                    and bs.ranch_id = #{sc.currentRanchId}
                </if>
            </if>
            ${sc.params.dataScope}
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectScTransitionInfoById" parameterType="Integer" resultMap="ScTransitionInfoResult">
        <include refid="selectScTransitionInfoVo"/>
        WHERE t.id = #{id}
    </select>

    <!--    <insert id="insertScTransitionInfo" parameterType="ScTransitionInfo" useGeneratedKeys="true" keyProperty="id">-->
    <!--        insert into sc_transition_info-->
    <!--        <trim prefix="(" suffix=")" suffixOverrides=",">-->
    <!--            <if test="sheepId != null">sheep_id,</if>-->
    <!--            <if test="varietyId != null">variety_id,</if>-->
    <!--            <if test="transTo != null and transTo != ''">trans_to,</if>-->
    <!--            <if test="transFrom != null and transFrom != ''">trans_from,</if>-->
    <!--            <if test="eventType != null and eventType != ''">event_type,</if>-->
    <!--            <if test="transType != null">trans_type,</if>-->
    <!--            <if test="transitionDate != null">transition_date,</if>-->
    <!--            <if test="technician != null and technician != ''">technician,</if>-->
    <!--            <if test="status != null">status,</if>-->
    <!--            <if test="comment != null">comment,</if>-->
    <!--            <if test="createBy != null">create_by,</if>-->
    <!--            <if test="createTime != null">create_time,</if>-->
    <!--            <if test="userId != null">user_id,</if>-->
    <!--            <if test="deptId != null">dept_id,</if>-->
    <!--        </trim>-->
    <!--        <trim prefix="values (" suffix=")" suffixOverrides=",">-->
    <!--            <if test="sheepId != null">#{sheepId},</if>-->
    <!--            <if test="varietyId != null">#{varietyId},</if>-->
    <!--            <if test="transTo != null and transTo != ''">#{transTo},</if>-->
    <!--            <if test="transFrom != null and transFrom != ''">#{transFrom},</if>-->
    <!--            <if test="eventType != null and eventType != ''">#{eventType},</if>-->
    <!--            <if test="transType != null">#{transType},</if>-->
    <!--            <if test="transitionDate != null">#{transitionDate},</if>-->
    <!--            <if test="technician != null and technician != ''">#{technician},</if>-->
    <!--            <if test="status != null">#{status},</if>-->
    <!--            <if test="comment != null">#{comment},</if>-->
    <!--            <if test="createBy != null">#{createBy},</if>-->
    <!--            <if test="createTime != null">#{createTime},</if>-->
    <!--            <if test="userId != null">#{userId},</if>-->
    <!--            <if test="deptId != null">#{deptId},</if>-->
    <!--        </trim>-->
    <!--    </insert>-->

    <insert id="insertScTransitionInfoBatch">
        INSERT INTO sc_transition_info (
        sheep_id,sheepfold_id, variety_id, trans_to, trans_from,
        event_type,trans_type, transition_date, technician, status, comment,
        create_by, create_time,user_id,dept_id
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.sheepId},#{item.sheepfoldId}, #{item.varietyId}, #{item.transTo}, #{item.transFrom},
            #{item.eventType},
            #{item.transType},#{item.transitionDate}, #{item.technician}, #{item.status}, #{item.comment},
            #{item.createBy}, now(), #{item.userId},
            #{item.deptId}
            )
        </foreach>
    </insert>

    <update id="updateScTransitionInfo" parameterType="ScTransitionInfo">
        update sc_transition_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="varietyId != null">variety_id = #{varietyId},</if>
            <if test="sheepfoldId != null">sheepfold_id = #{sheepfoldId},</if>
            <if test="transTo != null and transTo != ''">trans_to = #{transTo},</if>
            <if test="transFrom != null and transFrom != ''">trans_from = #{transFrom},</if>
            <if test="eventType != null and eventType != ''">event_type = #{eventType},</if>
            <if test="transType != null">trans_type = #{transType},</if>
            <if test="transitionDate != null">transition_date = #{transitionDate},</if>
            <if test="technician != null and technician != ''">technician = #{technician},</if>
            <if test="status != null">status = #{status},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScTransitionInfoById" parameterType="Integer">
        delete
        from sc_transition_info
        where id = #{id}
    </delete>

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

    <select id="searchEarNumbers" resultType="java.lang.String">
        SELECT DISTINCT bs.manage_tags
        FROM bas_sheep bs
        WHERE bs.manage_tags LIKE CONCAT('%', #{query}, '%')
          AND bs.is_delete = 0
        ORDER BY bs.manage_tags
    </select>
</mapper>