<?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.sale.MultiOwnerMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        multi_id, order_id, owner_type, owner_id, allocation_ratio, flg_valid, cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.sale.MultiOwner">
        <id column="multi_id" property="multiId"/>
            <result column="order_id" property="orderId" typeHandler="UuidTypeHandler"/>
            <result column="owner_type" property="ownerType"/>
            <result column="owner_id" property="ownerId" typeHandler="UuidTypeHandler"/>
            <result column="allocation_ratio" property="allocationRatio"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.sale.MultiOwnerResponse">
        <id column="multi_id" property="multiId"/>
        <result column="order_id" property="orderId" typeHandler="UuidTypeHandler"/>
        <result column="owner_type" property="ownerType"/>
        <result column="owner_id" property="ownerId" typeHandler="UuidTypeHandler"/>
        <result column="allocation_ratio" property="allocationRatio"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="staff_id" property="staffId"/>
        <result column="staff_name" property="staffName"/>
        <result column="org_id" property="orgId"/>
        <result column="org_name" property="orgName"/>

    </resultMap>
    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="orderId != null and orderId != ''">
                AND order_id = #{orderId}
            </if>
            <if test="ownerType != null and ownerType != ''">
                AND owner_type = #{ownerType}
            </if>
            <if test="ownerId != null and ownerId != ''">
                AND owner_id = #{ownerId}
            </if>
            <if test="allocationRatio != null">
                AND allocation_ratio = #{allocationRatio}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_psi_multi_owner，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_multi_owner
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_psi_multi_owner的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_multi_owner
        WHERE multi_id = #{id}::uuid
    </select>

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_multi_owner
        (
        <trim suffixOverrides=",">
            order_id,
            owner_type,
            owner_id,
            allocation_ratio,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.orderId}::uuid,
                #{item.ownerType},
                #{item.ownerId}::uuid,
                #{item.allocationRatio},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <delete id="deleteByOrderId">
        DELETE FROM dkic_b.t_psi_multi_owner WHERE order_id = #{orderId}::uuid
    </delete>

    <!-- 查询表t_psi_multi_owner，（条件查询+分页）列表 -->
    <select id="selectByType" resultMap="BaseResultMapResponse">
        SELECT tpmo.multi_id,
               tpmo.order_id,
               tpmo.owner_type,
               tpmo.owner_id,
               tpmo.allocation_ratio,
               tpmo.flg_valid,
               tpmo.cp_id,
               tms.staff_id,
               tms.staff_name,
               tmo.org_id,
               tmo.org_name
        FROM dkic_b.t_psi_multi_owner tpmo
        left join dkic_b.t_mst_staff tms on tms.staff_id = tpmo.owner_id
        left join dkic_b.t_mst_org tmo  on tmo.org_id = tpmo.owner_id
        where tpmo.order_id = #{orderId}::uuid AND tpmo.owner_type = ANY ( #{ownerTypes, typeHandler=StringListTypeHandler} )
    </select>
</mapper>
