<?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.hzs.member.empty.mapper.CuMemberEmptyProductMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.empty.CuMemberEmptyProduct">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_MEMBER_EMPTY" property="pkMemberEmpty"/>
        <result column="PK_MEMBER_EMPTY_DETAIL" property="pkMemberEmptyDetail"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="SPECS_NAME_ID" property="specsNameId"/>
    </resultMap>

    <!-- 批量删除会员空单产品 -->
    <update id="deleteBatch">
        update cu_member_empty_product cmep
        set cmep.del_flag = 0,
        cmep.pk_modified = #{userId},
        cmep.modified_time = sysdate
        where cmep.del_flag = 0
        and cmep.pk_member_empty = #{pkMemberEmpty}
        and cmep.pk_id in
        (select cmep.pk_id
        from cu_member_empty_product cmep
        where cmep.del_flag = 0
        and cmep.pk_member_empty = #{pkMemberEmpty}
        <if test="emptyProductList != null and emptyProductList.size > 0">
            and (
            <foreach collection="emptyProductList" item="item" separator=" or ">
                (
                cmep.pk_member_empty_detail = #{item.pkMemberEmptyDetail}
                and cmep.pk_product = #{item.pkProduct}
                and cmep.specs_name_id = #{item.specsNameId}
                )
            </foreach>
            )
        </if>
        )
    </update>

    <!-- 批量插入会员空单产品 -->
    <insert id="insertOrUpdateBatch">
        merge into CU_MEMBER_EMPTY_PRODUCT cmep
        using (
        <foreach collection="emptyProductList" item="item" separator=" union ">
            select
            #{item.pkMemberEmpty} PK_MEMBER_EMPTY,
            #{item.pkMemberEmptyDetail} PK_MEMBER_EMPTY_DETAIL,
            #{item.pkProduct} PK_PRODUCT,
            #{item.quantity} QUANTITY,
            #{item.specsName} SPECS_NAME,
            #{item.specsNameId} SPECS_NAME_ID,
            #{item.pkCountry} PK_COUNTRY
            from dual
        </foreach>
        ) tmp
        on (
        cmep.DEL_FLAG = 0
        and tmp.PK_MEMBER_EMPTY = cmep.PK_MEMBER_EMPTY
        and tmp.PK_MEMBER_EMPTY_DETAIL = cmep.PK_MEMBER_EMPTY_DETAIL
        and tmp.PK_PRODUCT = cmep.PK_PRODUCT
        and tmp.SPECS_NAME_ID = cmep.SPECS_NAME_ID
        )
        WHEN MATCHED THEN
        update set
        cmep.QUANTITY = tmp.QUANTITY,
        cmep.SPECS_NAME = tmp.SPECS_NAME,
        cmep.PK_MODIFIED = #{userId},
        cmep.MODIFIED_TIME = sysdate
        WHEN NOT MATCHED THEN
        insert (
        pk_id,
        PK_MEMBER_EMPTY,
        PK_MEMBER_EMPTY_DETAIL,
        PK_PRODUCT,
        QUANTITY,
        SPECS_NAME,
        SPECS_NAME_ID,
        PK_COUNTRY,
        PK_CREATOR
        )
        values (
        CU_MEMBER_EMPTY_PRODUCT_SEQ.Nextval,
        tmp.PK_MEMBER_EMPTY,
        tmp.PK_MEMBER_EMPTY_DETAIL,
        tmp.PK_PRODUCT,
        tmp.QUANTITY,
        tmp.SPECS_NAME,
        tmp.SPECS_NAME_ID,
        tmp.PK_COUNTRY,
        #{userId}
        )
    </insert>

    <!-- 查询空单明细产品信息列表 -->
    <select id="queryProductList" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyProductExt">
        select cmep.*, bp.product_name, bp.product_code
        from cu_member_empty_product cmep
        left join bd_product bp
        on bp.pk_id = cmep.pk_product
        and bp.del_flag = 0
        where cmep.del_flag = 0
        and cmep.pk_member_empty_detail in
        <foreach collection="emptyDetailIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by cmep.pk_member_empty_detail desc
    </select>

</mapper>
