<?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.report.member.mapper.CuMemberPerkMapper">

    <!-- 补贴配置列表 -->
    <select id="queryList" resultType="com.hzs.common.domain.report.member.ext.CuMemberPerkExt">
        select cmp.*,
        cm.member_code,
        cm.member_name,
        bv.vertex_name,
        cmt.team_name
        from CU_MEMBER_PERK cmp
        inner join cu_member cm
        on cm.pk_id = cmp.pk_member
        left join bd_vertex bv
        on bv.pk_id = cm.pk_vertex
        left join cu_member_team cmt
        on cmt.pk_id = cm.pk_id
        where cmp.del_flag = 0
        and cmp.pk_country = #{pkCountry}
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.memberName != null and param.memberName != ''">
            and cm.member_name like #{param.memberName} || '%'
        </if>
        <if test="param.pkVertex != null">
            and cm.pk_vertex = #{param.pkVertex}
        </if>
        <if test="param.pkTeamCode != null">
            and cm.pk_team_code = #{param.pkTeamCode}
        </if>
        <if test="param.status != null">
            <choose>
                <when test="param.status == 1">
                    and cmp.calculate_start_date > sysdate
                </when>
                <when test="param.status == 2">
                    and sysdate >= cmp.calculate_start_date
                    and cmp.calculate_end_date >= sysdate
                </when>
                <when test="param.status == 3">
                    and cmp.status = #{param.status}
                    and sysdate > cmp.calculate_end_date
                </when>
                <when test="param.status == 4">
                    and cmp.status = #{param.status}
                </when>
            </choose>
        </if>
        <if test="param.creationTime != null">
            and cmp.start_date >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= cmp.start_date
        </if>
        order by cmp.start_date desc
    </select>

    <!-- 补贴配置详情 -->
    <select id="queryDetail" resultType="com.hzs.common.domain.report.member.ext.CuMemberPerkExt">
        select cmp.*, cm.member_code
        from CU_MEMBER_PERK cmp
                 left join cu_member cm
                           on cm.pk_id = cmp.pk_member
        where cmp.pk_id = #{pkId}
    </select>

</mapper>
