<?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.activity.pick.mapper.AcPickMemberLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.pick.AcPickMemberLog">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_PICK" property="pkPick"/>
        <result column="PICK_QUANTITY" property="pickQuantity"/>
        <result column="PICK_TIME" property="pickTime"/>
        <result column="PICK_ORDER" property="pickOrder"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="SOURCE" property="source"/>
    </resultMap>

    <!-- 查询提货日志 -->
    <select id="queryMemberLog" resultType="com.hzs.activity.pick.vo.AcPickMemberLogVO">
        select ap.pick_type,
               apml.pick_quantity,
               apml.pick_time,
               apml.pick_order
        from AC_PICK_MEMBER_LOG apml
                 inner join ac_pick ap
                            on ap.pk_id = apml.pk_pick
        where ap.pk_member = #{pkMember}
          and ap.del_flag = 0
          and apml.del_flag = 0
          and apml.source = 2
        order by apml.pick_time desc
    </select>

    <!-- 批量插入提货日志 -->
    <update id="insertBatch">
        merge into AC_PICK_MEMBER_LOG apml
        using (
        <foreach collection="list" item="item" separator=" union ">
            select ap.pk_id PK_PICK,
            ap.pk_member pk_member,
            #{item.orderCode} PICK_ORDER,
            ap.pk_country pk_country
            from ac_pick ap
            where ap.pick_type = #{item.pickType}
            and ap.pk_base_config = #{item.pkBaseConfig}
            and ap.pk_data_id = #{item.pkDataId}
            and ap.pk_member = #{item.pkMember}
        </foreach>
        ) tmp
        on (0 = 1)
        WHEN NOT MATCHED THEN
        insert
        (PK_ID,
        PK_MEMBER,
        PK_PICK,
        PICK_QUANTITY,
        PICK_TIME,
        PICK_ORDER,
        PK_COUNTRY,
        DEL_FLAG,
        SOURCE)
        values
        (ac_pick_member_log_seq.nextval,
        tmp.pk_member,
        tmp.pk_pick,
        #{quantity},
        sysdate,
        tmp.pick_order,
        tmp.pk_country,
        0,
        #{source})
    </update>


</mapper>
