<?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.zg.project.wisdom.mapper.MoveRecordMapper">

    <!-- ===================== resultMap ===================== -->
    <resultMap type="com.zg.project.wisdom.domain.MoveRecord" id="MoveRecordResult">
        <result property="id" column="id"/>

        <!-- 关键字段 -->
        <result property="rkInfoId" column="rk_info_id"/>
        <result property="newRkInfoId" column="new_rk_info_id"/>
        <result property="entityId" column="entity_id"/>

        <!-- 数量 -->
        <result property="realQty" column="real_qty"/>

        <!-- from -->
        <result property="fromCangku" column="from_cangku"/>
        <result property="fromPcode" column="from_pcode"/>
        <result property="fromTrayCode" column="from_tray_code"/>

        <!-- to -->
        <result property="toCangku" column="to_cangku"/>
        <result property="toPcode" column="to_pcode"/>
        <result property="toTrayCode" column="to_tray_code"/>

        <!-- 业务 -->
        <result property="moveReason" column="move_reason"/>
        <result property="movedBy" column="moved_by"/>
        <result property="movedByName" column="moved_by_name"/>
        <result property="movedAt" column="moved_at"/>

        <!-- 通用字段 -->
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="isDelete" column="is_delete"/>

        <!-- 扩展展示字段 -->
        <result property="fromCangkuName" column="from_cangku_name"/>
        <result property="toCangkuName" column="to_cangku_name"/>
        <result property="xmNo" column="xm_no"/>
        <result property="xmMs" column="xm_ms"/>
        <result property="wlMs" column="wl_ms"/>
        <result property="gysMc" column="gys_mc"/>
    </resultMap>

    <!-- ===================== 基础查询字段 ===================== -->
    <sql id="selectMoveRecordVo">
        SELECT
            id,
            rk_info_id,
            new_rk_info_id,
            entity_id,
            real_qty,
            from_cangku,
            from_pcode,
            from_tray_code,
            to_cangku,
            to_pcode,
            to_tray_code,
            move_reason,
            moved_by,
            moved_at,
            create_by,
            create_time,
            update_by,
            update_time,
            is_delete
        FROM move_record
    </sql>

    <!-- ===================== 新增 ===================== -->
    <insert id="insertMoveRecord"
            parameterType="com.zg.project.wisdom.domain.MoveRecord"
            useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO move_record (
            rk_info_id,
            new_rk_info_id,
            entity_id,
            real_qty,
            from_cangku,
            from_pcode,
            from_tray_code,
            to_cangku,
            to_pcode,
            to_tray_code,
            move_reason,
            moved_by,
            moved_at,
            create_by,
            create_time,
            update_by,
            update_time,
            is_delete
        ) VALUES (
                     #{rkInfoId},
                     #{newRkInfoId},
                     #{entityId},
                     #{realQty},
                     #{fromCangku},
                     #{fromPcode},
                     #{fromTrayCode},
                     #{toCangku},
                     #{toPcode},
                     #{toTrayCode},
                     #{moveReason},
                     #{movedBy},
                     #{movedAt},
                     #{createBy},
                     #{createTime},
                     #{updateBy},
                     #{updateTime},
                     #{isDelete}
                 )
    </insert>

    <!-- ===================== 分页列表 ===================== -->
    <select id="selectMoveRecordList" resultMap="MoveRecordResult">
        SELECT
        mr.*,
        su.user_name AS moved_by_name,
        ri.xm_no,
        ri.xm_ms,
        ri.wl_ms,
        ri.gys_mc,
        wi1.warehouse_name AS from_cangku_name,
        wi2.warehouse_name AS to_cangku_name
        FROM move_record mr
        LEFT JOIN sys_user su ON mr.moved_by = su.user_id
        LEFT JOIN rk_info ri ON mr.rk_info_id = ri.id
        LEFT JOIN warehouse_info wi1 ON mr.from_cangku = wi1.warehouse_code
        LEFT JOIN warehouse_info wi2 ON mr.to_cangku = wi2.warehouse_code
        <where>
            mr.is_delete = 0
            <if test="entityId != null and entityId != ''">
                AND mr.entity_id = #{entityId}
            </if>
            <if test="toCangku != null and toCangku != ''">
                AND mr.to_cangku = #{toCangku}
            </if>
            <if test="fromCangku != null and fromCangku != ''">
                AND mr.from_cangku = #{fromCangku}
            </if>
            <if test="xmNo != null and xmNo != ''">
                AND ri.xm_no = #{xmNo}
            </if>
            <if test="moveReason != null and moveReason != ''">
                AND mr.move_reason LIKE concat('%', #{moveReason}, '%')
            </if>
        </where>
        ORDER BY mr.create_time DESC
    </select>

    <!-- ===================== 查询详情 ===================== -->
    <select id="selectMoveRecordById"
            parameterType="Long"
            resultMap="MoveRecordResult">
        <include refid="selectMoveRecordVo"/>
        WHERE id = #{id}
    </select>

    <!-- ===================== 修改 ===================== -->
    <update id="updateMoveRecord"
            parameterType="com.zg.project.wisdom.domain.MoveRecord">
        UPDATE move_record
        <set>
            <if test="rkInfoId != null">rk_info_id = #{rkInfoId},</if>
            <if test="newRkInfoId != null">new_rk_info_id = #{newRkInfoId},</if>
            <if test="entityId != null">entity_id = #{entityId},</if>
            <if test="realQty != null">real_qty = #{realQty},</if>
            <if test="fromCangku != null">from_cangku = #{fromCangku},</if>
            <if test="fromPcode != null">from_pcode = #{fromPcode},</if>
            <if test="fromTrayCode != null">from_tray_code = #{fromTrayCode},</if>
            <if test="toCangku != null">to_cangku = #{toCangku},</if>
            <if test="toPcode != null">to_pcode = #{toPcode},</if>
            <if test="toTrayCode != null">to_tray_code = #{toTrayCode},</if>
            <if test="moveReason != null">move_reason = #{moveReason},</if>
            <if test="movedBy != null">moved_by = #{movedBy},</if>
            <if test="movedAt != null">moved_at = #{movedAt},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete}</if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- ===================== 删除 ===================== -->
    <delete id="deleteMoveRecordById" parameterType="Long">
        DELETE FROM move_record WHERE id = #{id}
    </delete>

    <delete id="deleteMoveRecordByIds" parameterType="Long">
        DELETE FROM move_record
        WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
