<?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.dk.mdm.mapper.ivt.TransferMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        transfer_id, transfer_no, transfer_type, org_id, staff_id, transfer_date, o_wh_id, wh_id, o_non_std_code, non_std_code, remarks, make_staff, make_time, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
        ,annex_paths
    </sql>

    <!-- 查询一览列 -->
    <sql id="Base_Column_List_Response">
        trf.transfer_id, trf.transfer_no, trf.transfer_type, trf.org_id, trf.staff_id, trf.transfer_date, trf.o_wh_id
        , trf.wh_id, trf.o_non_std_code, trf.non_std_code, trf.remarks, trf.make_staff, trf.make_time
        , trf.flg_valid, trf.cp_id
         ,sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n} ) AS "transferStatusName"
        ,staff.staff_name AS "transferStaff"
        ,org.org_name AS "transferOrg"
        ,mstaff.staff_name AS "makeStaffName"
        ,trf.annex_paths
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Transfer">
        <id column="transfer_id" property="transferId"/>
                <result column="transfer_no" property="transferNo"/>
                <result column="transfer_type" property="transferType"/>
                <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
                <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
            <result column="transfer_date" property="transferDate" typeHandler="TimestampTypeHandler"/>
                <result column="o_wh_id" property="oWhId" typeHandler="UuidTypeHandler"/>
                <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
                <result column="o_non_std_code" property="oNonStdCode"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="remarks" property="remarks"/>
                <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
        <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
            <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
            <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
    </resultMap>

    <!-- 查询一览映射结果 -->
    <resultMap id="BaseResultMap_Response" type="com.dk.mdm.model.response.ivt.TransferResponse">
        <id column="transfer_id" property="transferId"/>
        <result column="transfer_no" property="transferNo"/>
        <result column="transfer_type" property="transferType"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="transfer_date" property="transferDate" typeHandler="TimestampTypeHandler"/>
        <result column="o_wh_id" property="oWhId" typeHandler="UuidTypeHandler"/>
        <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
        <result column="o_non_std_code" property="oNonStdCode"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="remarks" property="remarks"/>
        <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
        <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="oWhName" property="oWhName"/>
        <result column="whName" property="whName"/>
        <result column="orgName" property="orgName"/>
        <result column="staffName" property="staffName"/>
        <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="transferNo != null and transferNo != ''">
                AND transfer_no = #{transferNo}
            </if>
            <if test="transferType != null and transferType != ''">
                AND transfer_type = #{transferType}
            </if>
            <if test="orgId != null and orgId != ''">
                AND org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}
            </if>
            <if test="transferDate != null">
                AND transfer_date = #{transferDate}
            </if>
            <if test="oWhId != null and oWhId != ''">
                AND o_wh_id = #{oWhId}
            </if>
            <if test="whId != null and whId != ''">
                AND wh_id = #{whId}
            </if>
            <if test="oNonStdCode != null and oNonStdCode != ''">
                AND o_non_std_code = #{oNonStdCode}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND non_std_code = #{nonStdCode}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND make_time = #{makeTime}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND op_db_user = #{opDbUser}
            </if>
        </where>
    </sql>

    <!-- 一览条件列 -->
    <sql id="Condition_Response">
        <where>
            <if test="oWhName != null and oWhName != ''">
                AND  otmw.wh_name  LIKE concat('%',my_ex.likequery(#{oWhName}),'%')
            </if>
            <if test="whName != null and whName != ''">
                AND  tmw.wh_name  LIKE concat('%',my_ex.likequery(#{whName}),'%')
            </if>
            <if test="staffName != null and staffName != ''">
                AND  staff.staff_name  LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="orgName != null and orgName != ''">
                AND  org.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND trf.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND trf.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="transferDateWStart != null and transferDateWEnd != null">
                AND trf.transfer_date &gt;= #{transferDateWStart}::timestamp with time zone
                AND trf.transfer_date &lt; #{transferDateWEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="transferTypeList != null and transferTypeList.size()>0">
                AND trf.transfer_type =any(#{transferTypeList,typeHandler=StringListTypeHandler})
            </if>
            <if test="transferNo != null and transferNo != ''">
                AND trf.transfer_no  LIKE concat('%',my_ex.likequery(#{transferNo}),'%')
            </if>
            <if test="transferDate != null">
                AND trf.transfer_date = #{transferDate}
            </if>
            <if test="oWhId != null and oWhId != ''">
                AND o_wh_id = #{oWhId}
            </if>
            <if test="whId != null and whId != ''">
                AND wh_id = #{whId}
            </if>
            <if test="oNonStdCode != null and oNonStdCode != ''">
                AND o_non_std_code LIKE concat('%',my_ex.likequery(#{oNonStdCode}),'%')
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND non_std_code LIKE concat('%',my_ex.likequery(#{nonStdCode}),'%')
            </if>
            <if test="remarks != null and remarks != ''">
                AND trf.remarks  LIKE concat('%',my_ex.likequery(#{remarks}),'%')
            </if>
            <if test="cpId != null">
                AND trf.cp_id = #{cpId}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND trf.flg_valid  =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="orgIdList != null and orgIdList.size>0">
                AND trf.org_id = any(#{orgIdList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size>0">
                AND trf.staff_id = any(#{staffIdList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="searchText !=null">
                AND   trf.transfer_no LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>
        </where>
    </sql>

    <!-- 查询表t_psi_transfer，（条件查询+分页）列表 库存调整 -->
    <select id="selectByCond" resultMap="BaseResultMap_Response">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,otmw.wh_name as "oWhName"
        ,tmw.wh_name as "whName"
        FROM dkic_b.t_psi_transfer AS trf
        left join   dkic_b.t_mst_warehouse as otmw on otmw.wh_id = trf.o_wh_id
        left join   dkic_b.t_mst_warehouse as tmw on tmw.wh_id = trf.wh_id
        lEFT JOIN dkic_b.t_mst_staff AS staff ON  staff.staff_id=trf.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=trf.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=trf.make_staff
        LEFT JOIN sys.t_data_kind AS tdk  ON tdk.kind_code = trf.transfer_type
        <include refid="Condition_Response"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_transfer，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_transfer
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_psi_transfer的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap_Response">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,otmw.wh_name as "oWhName"
        ,tmw.wh_name as "whName"
        ,staff.staff_name AS "staffName"
        ,org.org_name AS "orgName"
        FROM dkic_b.t_psi_transfer AS trf
        left join   dkic_b.t_mst_warehouse as otmw on otmw.wh_id = trf.o_wh_id
        left join   dkic_b.t_mst_warehouse as tmw on tmw.wh_id = trf.wh_id
        lEFT JOIN dkic_b.t_mst_staff AS staff ON  staff.staff_id=trf.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=trf.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=trf.make_staff
        LEFT JOIN sys.t_data_kind AS tdk  ON tdk.kind_code = trf.transfer_type
        WHERE trf.transfer_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_transfer的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_transfer
        WHERE transfer_id = #{id}::uuid
        for update
    </select>

</mapper>
