<?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.bonus.canteen.core.marketing.mapper.MktEffectiveMapper">

    <resultMap type="com.bonus.canteen.core.marketing.vo.MktEffectiveUserVO" id="mktEffectiveUserVO">
        <result property="effId" column="eff_id"/>
        <result property="userType" column="user_type"/>
        <collection property="orgIdList" ofType="Long">
            <result property="orgId" column="org_id"/>
        </collection>
        <collection property="psnTypeList" ofType="Integer">
            <result property="psnType" column="psn_type"/>
        </collection>
    </resultMap>

    <!-- 获取指定类型的生效范围详情 -->
    <select id="selectAllUserEff" resultMap="mktEffectiveUserVO">
        select me.eff_id,
               me.user_type,
               med.org_id,
               med.psn_type
        from mkt_effective me
                 left join mkt_effective_detail med on me.eff_id = med.eff_id
        where me.eff_type = #{effType}
          and me.del_flag = #{delFlag}
    </select>

    <select id="getUserEffIdByCustId" resultType="java.lang.Long">
        select med.eff_id
        from cust_info ci
                 left join cust_org co on ci.org_id = co.org_id
                 left join mkt_effective_detail med on co.org_id = med.org_id
                 left join mkt_effective me on me.eff_id = med.eff_id
        where me.del_flag = #{delFlag} and ci.cust_id = #{custId}
    </select>

    <!-- 获取生效范围名称，根据范围id列表 -->
    <select id="getEffNameStrByIdList" resultType="java.lang.String">
        SELECT
            GROUP_CONCAT(eff_name)
        FROM
            mkt_effective
        WHERE
            del_flag = 2
            AND eff_id in
            <foreach collection="effIdList" item="effId" separator="," open="(" close=")">
                #{effId}
            </foreach>
    </select>

</mapper>
