<?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.bonus.detail.mapper.CuMemberAssessDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.detail.CuMemberAssessDetail">
        <id column="PK_ID" property="pkId" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_ASSESS" property="pkAssess" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_ORDER" property="pkOrder" />
        <result column="CHANGE_TYPE" property="changeType" />
        <result column="TRADE_TIME" property="tradeTime" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG,
        PK_COUNTRY,
        CREATION_TIME,
        MODIFIED_TIME,
        PK_ID, PK_ASSESS, PK_MEMBER, PK_ORDER, CHANGE_TYPE, TRADE_TIME, CREATOR, MODIFIED
    </sql>

    <insert id="insertCuMemberAssessDetailByPeriod">
        merge into cu_member_assess_detail a
        using (select ca.pk_id,ca.pk_member,(case when ca.assess_type=0 then 1 else 3 end) change_type,
        ca.assess_target trade_achieve,ca.pk_creator,ca.pk_country from ${tableName} ca
        where ca.assess_status=2 and ca.period = #{period} and pk_member in
        <foreach collection="cuMemberList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
        and assess_type = #{assessType}
        and ca.assess_target &lt;= ca.month_balance+ca.month_add) b
        on (a.pk_assess = b.pk_id and a.pk_member = b.pk_member and a.change_type=b.change_type)
        when not matched then
        insert (pk_assess,pk_member,change_type,trade_achieve, pk_creator,pk_country)
            values(b.pk_id,b.pk_member,b.change_type,b.trade_achieve, b.pk_creator,b.pk_country)
    </insert>

    <insert id="insertCuMemberAssesDetailByOrder">
        insert into cu_member_assess_detail (pk_assess,pk_member,PK_ORDER,change_type,trade_achieve,TRADE_TIME, pk_creator,pk_country)
        select nvl(ca.pk_id,0) PK_ASSESS,so.PK_MEMBER,so.pk_id pk_order,
        0 CHANGE_TYPE,order_achieve TRADE_ACHIEVE,
        pay_time TRADE_TIME,so.PK_MEMBER,so.pk_country from sa_order so
        left join  (select * from ${tableName} where period = #{period} and ASSESS_TYPE=0) ca
        on so.pk_member = ca.pk_member
        where so.del_flag=0 and so.order_status=1 and order_type in(3,12,14,22,26,31)
        and so.order_achieve >0
        and pay_time >=#{startDate, jdbcType=DATE}
        and pay_time &lt; #{endDate, jdbcType=DATE}
        union
        select nvl(ca.pk_id,0) PK_ASSESS,so.PK_MEMBER,so.pk_id pk_order,
               0 CHANGE_TYPE,order_achieve TRADE_ACHIEVE,
               pay_time TRADE_TIME,so.PK_MEMBER,so.pk_country from sa_t_order so
       left join  (select * from ${tableName} where period = #{period} and ASSESS_TYPE=0) ca
                    on so.pk_member = ca.pk_member
        where so.del_flag=0 and so.order_status=1 and order_type in (21)
          and so.order_achieve >0
          and pay_time >=#{startDate, jdbcType=DATE}
          and pay_time &lt; #{endDate, jdbcType=DATE}
        union
        select nvl(ca.pk_id,0) PK_ASSESS,so.PK_MEMBER,so.pk_id pk_order,
               2 CHANGE_TYPE,order_ass_achieve TRADE_ACHIEVE,
               pay_time TRADE_TIME,so.PK_MEMBER,so.pk_country from sa_order so
        left join  (select * from ${tableName} where period = #{period} and ASSESS_TYPE=1) ca
                   on so.pk_member = ca.pk_member
        where so.del_flag=0 and so.order_status=1 and order_type =13
          and pay_time >=#{startDate, jdbcType=DATE}
          and pay_time &lt; #{endDate, jdbcType=DATE}
    </insert>

    <update id="updateCuMemberAssessDetailByPeriod">
        merge into cu_member_assess_detail a
        using (select pk_id,pk_member,(case when assess_type=0 then 1 else 3 end)change_type,
        assess_target trade_achieve,pk_creator,pk_country from ${tableName}
        where assess_status=2 and period = #{period}
        and assess_target &lt;= month_balance+month_add) b
        on (a.pk_assess = b.pk_id and a.pk_member = b.pk_member and a.change_type=b.change_type)
        when matched then
        update set a.trade_achieve = b.trade_achieve
        when not matched then
        insert (pk_assess,pk_member,change_type,trade_achieve, pk_creator,pk_country)
            values(b.pk_id,b.pk_member,b.change_type,b.trade_achieve, b.pk_creator,b.pk_country)
    </update>

    <update id="updateCuMemberAssessDetailByRevoke">
        merge into cu_member_assess_detail a
        using(
        <foreach collection="revokeDetailList" item="item" open=" " separator=" union " close=" ">
            select #{item.changeType} change_type,#{item.pkMember} pk_member,#{item.pkAssess} pk_assess from dual
        </foreach>
        )b on (a.pk_assess=b.pk_assess and a.pk_member=b.pk_member and a.change_type=b.change_type)
        when matched then
        update set a.del_flag=1
    </update>
    <delete id="deleteCuMemberAssessDetailByPeriod">
        DELETE  FROM  cu_member_assess_detail  WHERE  pk_assess  IN  (
        SELECT  pk_id  FROM  ${tableName}  WHERE  period= #{period})
    </delete>

</mapper>
