<?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.FreezeMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        freeze_id
        , freeze_no, cus_id, org_id, staff_id, freeze_status, freeze_date, unfreeze_date,auto_unfreeze_date,
        sys.f_remove_zero(sum_quantity) as sum_quantity, 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_Respons">
        fre.freeze_id
        ,fre.freeze_no,fre.cus_id,fre.org_id,fre.staff_id,fre.freeze_status
        ,sys.f_get_name_i18n(tdk.kind_name_i18n,
        #{i18n}
        )
        AS
        "freezeStatusName"
        ,
        fre
        .
        freeze_date,
        fre
        .
        auto_unfreeze_date ,
        sys
        .
        f_remove_zero
        (
        fre
        .
        sum_quantity
        )
        as
        sum_quantity,
        fre
        .
        remarks,
        fre
        .
        make_staff
        ,
        fre
        .
        make_time,
        fre
        .
        flg_valid,
        fre
        .
        cp_id,
        cus
        .
        cus_phone
        ,
        cus
        .
        cus_name,
        staff
        .
        staff_name,
        org
        .
        org_name,
        mstaff
        .
        staff_name
        AS
        "makeStaffName"
        ,
        fre
        .
        annex_paths
        ,
        fre
        .
        unfreeze_date
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.FreezeResponse">
        <id column="freeze_id" property="freezeId"/>
        <result column="freeze_no" property="freezeNo"/>
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="freeze_status" property="freezeStatus"/>
        <result column="freeze_date" property="freezeDate" typeHandler="TimestampTypeHandler"/>
        <result column="unfreeze_date" property="unfreezeDate" typeHandler="TimestampTypeHandler"/>
        <result column="auto_unfreeze_date" property="autoUnfreezeDate" typeHandler="TimestampTypeHandler"/>
        <result column="sum_quantity" property="sumQuantity"/>
        <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="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="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="makeStaffName" property="makeStaffName"/>

    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Freeze">
        <id column="freeze_id" property="freezeId"/>
        <result column="freeze_no" property="freezeNo"/>
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="freeze_status" property="freezeStatus"/>
        <result column="freeze_date" property="freezeDate" typeHandler="TimestampTypeHandler"/>
        <result column="unfreeze_date" property="unfreezeDate" typeHandler="TimestampTypeHandler"/>
        <result column="sum_quantity" property="sumQuantity"/>
        <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="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="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
        <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="staffIds != null and staffIds.size()>0">
                AND staff.staff_id = any(#{staffIds, typeHandler=uuidListTypeHandler})
            </if>
            <if test="orgIds != null and orgIds.size()>0">
                AND org.org_id = any(#{orgIds, typeHandler=uuidListTypeHandler})
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND cus.cus_phone like concat('%', my_ex.likequery(#{cusPhone}) , '%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND cus.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="searchText !=null">
                AND (fre.freeze_no LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                or cus.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                or cus.cus_name LIKE concat('%',my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="freezeDateSStart != null and freezeDateSEnd != null">
                AND fre.freeze_date &gt;= #{freezeDateSStart}::timestamp with time zone
                AND fre.freeze_date &lt; #{freezeDateSEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="autoUnfreezeDateSStart != null and autoUnfreezeDateSEnd != null">
                AND fre.auto_unfreeze_date &gt;= #{autoUnfreezeDateSStart}::timestamp with time zone
                AND fre.auto_unfreeze_date &lt; #{autoUnfreezeDateSEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND fre.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND fre.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </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="freezeNo != null and freezeNo != ''">
                AND fre.freeze_no LIKE concat('%',my_ex.likequery(#{freezeNo}),'%')
            </if>
            <if test="cusId != null and cusId != ''">
                AND fre.cus_id = #{cusId}
            </if>
            <if test="orgId != null and orgId != ''">
                AND fre.org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND fre.staff_id = #{staffId}
            </if>
            <if test="freezeStatus != null and freezeStatus != ''">
                AND fre.freeze_status = #{freezeStatus}
            </if>
            <if test="freezeDate != null">
                AND fre.freeze_date = #{freezeDate}
            </if>
            <if test="autoUnfreezeDate != null">
                AND fre.auto_unfreeze_date = #{autoUnfreezeDate}
            </if>
            <if test="remarks != null and remarks != ''">
                AND fre.remarks = #{remarks}
            </if>
            <if test="cpId != null">
                AND fre.cp_id = #{cpId}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND fre.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="orgIdList != null and orgIdList.size>0">
                AND fre.org_id = any(#{orgIdList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size>0">
                AND fre.staff_id = any(#{staffIdList, typeHandler=UuidListTypeHandler})
            </if>

            <if test="freezeStatusList != null and freezeStatusList.size() > 0">
                AND fre.freeze_status = any(#{freezeStatusList, typeHandler=StringListTypeHandler})
            </if>
        </where>
    </sql>

    <!-- 查询表t_psi_freeze，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Respons"/>
        FROM dkic_b.t_psi_freeze AS fre
        lEFT JOIN dkic_b.t_mst_customer AS cus ON cus.cus_id=fre.cus_id
        lEFT JOIN dkic_b.t_mst_staff AS staff ON staff.staff_id=fre.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=fre.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=fre.make_staff
        LEFT JOIN sys.t_data_kind AS tdk ON tdk.kind_code = fre.freeze_status
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_freeze，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_freeze AS fre
        lEFT JOIN dkic_b.t_mst_customer AS cus ON cus.cus_id=fre.cus_id
        lEFT JOIN dkic_b.t_mst_staff AS staff ON staff.staff_id=fre.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=fre.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=fre.make_staff
        LEFT JOIN sys.t_data_kind AS tdk ON tdk.kind_code = fre.freeze_status
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_psi_freeze的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Respons"/>
        FROM dkic_b.t_psi_freeze AS fre
        lEFT JOIN dkic_b.t_mst_customer AS cus ON cus.cus_id=fre.cus_id
        lEFT JOIN dkic_b.t_mst_staff AS staff ON staff.staff_id=fre.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=fre.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=fre.make_staff
        LEFT JOIN sys.t_data_kind AS tdk ON tdk.kind_code = fre.freeze_status
        WHERE fre.freeze_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_freeze的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_psi_freeze
        WHERE freeze_id = #{freezeId}
        for update
    </select>

</mapper>
