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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        check_id, check_no, org_id, staff_id, check_date, wh_id, make_staff, make_time, flg_valid, remarks, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user, flg_lock, check_status,annex_paths
    </sql>
    <!-- 盘点查询一览 -->
    <sql id="Base_Column_List_Respons">
        ck.check_id, ck.check_no, ck.org_id, ck.staff_id, ck.check_date, ck.wh_id, ck.make_staff, ck.make_time, ck.flg_valid
    , ck.remarks, ck.flg_lock,ck.check_status
    ,sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n} ) AS "checkStatusName"
    ,staff.staff_name AS "checkStaff"
    ,org.org_name AS "checkOrg"
    ,mstaff.staff_name AS "makeStaffName"
    ,staff.staff_name AS "staffName"
    ,wh.wh_name AS "whName"
    ,org.org_name AS "orgName"
    ,ck.annex_paths
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Check">
        <id column="check_id" property="checkId"/>
                <result column="check_no" property="checkNo"/>
                <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
                <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
            <result column="check_date" property="checkDate" typeHandler="TimestampTypeHandler"/>
                <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
                <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="remarks" property="remarks"/>
                <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"/>
                <result column="flg_lock" property="flgLock"/>
                <result column="check_status" property="checkStatus"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.CheckResponse">
        <id column="check_id" property="checkId"/>
        <result column="check_no" property="checkNo"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="check_date" property="checkDate" typeHandler="TimestampTypeHandler"/>
        <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
        <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
        <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="remarks" property="remarks"/>
        <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"/>
        <result column="flg_lock" property="flgLock"/>
        <result column="check_status" property="checkStatus"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="checkNo != null and checkNo != ''">
                AND check_no = #{checkNo}
            </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="checkDate != null">
                AND check_date = #{checkDate}
            </if>
            <if test="whId != null and whId != ''">
                AND wh_id = #{whId}
            </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="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </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>
            <if test="flgLock != null">
                AND flg_lock = #{flgLock}
            </if>
            <if test="checkStatus != null and checkStatus != ''">
                AND check_status = #{checkStatus}
            </if>
        </where>
    </sql>

    <!-- 盘点一览查询条件 -->
    <sql id="Condition_Respons">
        <where>
            <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="checkNo != null and checkNo != ''">
                AND ck.check_no LIKE concat('%',my_ex.likequery(#{checkNo}),'%')
            </if>

            <if test="coilingStateList != null and coilingStateList.size()>0">
                AND ck.check_status =any(#{coilingStateList,typeHandler=StringListTypeHandler})
            </if>
            <if test="orgId != null and orgId != ''">
                AND ck.org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND ck.staff_id = #{staffId}
            </if>
            <if test="checkDate != null">
                AND ck.check_date = #{checkDate}
            </if>
            <if test="whId != null and whId != ''">
                AND ck.wh_id = #{whId}
            </if>
            <if test="flgValid != null">
                AND ck.flg_valid = #{flgValid}
            </if>
            <if test="remarks != null and remarks != ''">
                AND ck.remarks = #{remarks}
            </if>
            <if test="cpId != null">
                AND ck.cp_id = #{cpId}
            </if>
            <if test="flgLock != null">
                AND ck.flg_lock = #{flgLock}
            </if>
            <if test="checkStatus != null and checkStatus != ''">
                AND ck.check_status = #{checkStatus}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND ck.flg_valid  =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="orgIdList != null and orgIdList.size>0">
                AND ck.org_id = any(#{orgIdList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size>0">
                AND ck.staff_id = any(#{staffIdList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="checkStatusList != null and checkStatusList.size() > 0">
                AND ck.check_status = any(#{checkStatusList, typeHandler=UuidListTypeHandler})
            </if>
            <if test="searchText !=null">
                AND   ck.check_no LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND ck.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND ck.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键checkId批量操作 -->
        WHERE check_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_psi_check，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Respons"/>
        FROM dkic_b.t_psi_check AS ck
        lEFT JOIN dkic_b.t_mst_staff AS staff ON  staff.staff_id=ck.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=ck.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=ck.make_staff
        LEFT JOIN sys.t_data_kind AS tdk  ON tdk.kind_code = ck.check_status
        lEFT JOIN dkic_b.t_mst_warehouse AS wh ON wh.wh_id=ck.wh_id
        <include refid="Condition_Respons"/>
        order by ck.make_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_psi_check的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Respons"/>
        FROM dkic_b.t_psi_check AS ck
        lEFT JOIN dkic_b.t_mst_staff AS staff ON  staff.staff_id=ck.staff_id
        lEFT JOIN dkic_b.t_mst_org AS org ON org.org_id=ck.org_id
        lEFT JOIN dkic_b.t_mst_staff AS mstaff ON mstaff.staff_id=ck.make_staff
        LEFT JOIN sys.t_data_kind AS tdk  ON tdk.kind_code = ck.check_status
        lEFT JOIN dkic_b.t_mst_warehouse AS wh ON wh.wh_id=ck.wh_id
        WHERE ck.check_id = #{id}::uuid
    </select>

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

    <!-- 根据主键锁定表t_psi_check的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_check
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into t_psi_check
        (
        <trim suffixOverrides=",">
            check_no,
            org_id,
            staff_id,
            check_date,
            wh_id,
            make_staff,
            make_time,
            remarks,
            cp_id,
            op_app_code,
            flg_lock,
            check_status,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.checkNo},
                #{item.orgId}::uuid,
                #{item.staffId}::uuid,
                #{item.checkDate},
                #{item.whId}::uuid,
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
                #{item.flgLock},
                #{item.checkStatus},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
