<?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.jsowell.pile.mapper.MemberGroupMapper">

    <resultMap type="com.jsowell.pile.domain.MemberGroup" id="MemberGroupResult">
        <result property="id"    column="id"    />
        <result property="groupName"    column="group_name"    />
        <result property="groupCode"    column="group_code"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="groupLevel"    column="group_level"    />
        <result property="groupType"    column="group_type"    />
        <result property="discount"    column="discount"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>
    
    <sql id="Base_Column_List">
        id, group_name, group_code, merchant_id, station_id, group_level, group_type, discount, create_by, create_time, update_by, update_time, del_flag
    </sql>

    <sql id="selectMemberGroupVo">
        select
        <include refid="Base_Column_List"/>
        from member_group
    </sql>

    <select id="selectMemberGroupList" parameterType="com.jsowell.pile.domain.MemberGroup" resultMap="MemberGroupResult">
        <include refid="selectMemberGroupVo"/>
        <where>
            <if test="groupName != null  and groupName != ''"> and group_name = #{groupName}</if>
            <if test="groupCode != null  and groupCode != ''"> and group_code = #{groupCode}</if>
            <if test="merchantId != null  and merchantId != ''"> and merchant_id = #{merchantId}</if>
            <if test="stationId != null  and stationId != ''"> and station_id = #{stationId}</if>
            <if test="groupLevel != null  and groupLevel != ''"> and group_level = #{groupLevel}</if>
            <if test="groupType != null  and groupType != ''"> and group_type = #{groupType}</if>
            <if test="discount != null "> and discount = #{discount}</if>
        </where>
    </select>

    <select id="selectMemberGroupById" parameterType="Long" resultMap="MemberGroupResult">
        <include refid="selectMemberGroupVo"/>
        where id = #{id}
    </select>

    <insert id="insertMemberGroup" parameterType="com.jsowell.pile.domain.MemberGroup">
        insert into member_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="groupName != null">group_name,</if>
            <if test="groupCode != null">group_code,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="groupLevel != null">group_level,</if>
            <if test="groupType != null">group_type,</if>
            <if test="discount != null">discount,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="groupName != null">#{groupName},</if>
            <if test="groupCode != null">#{groupCode},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="groupLevel != null">#{groupLevel},</if>
            <if test="groupType != null">#{groupType},</if>
            <if test="discount != null">#{discount},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateMemberGroup" parameterType="com.jsowell.pile.domain.MemberGroup">
        update member_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupName != null">group_name = #{groupName},</if>
            <if test="groupCode != null">group_code = #{groupCode},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="groupLevel != null">group_level = #{groupLevel},</if>
            <if test="groupType != null">group_type = #{groupType},</if>
            <if test="discount != null">discount = #{discount},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMemberGroupById" parameterType="Long">
        delete from member_group where id = #{id}
    </delete>

    <delete id="deleteMemberGroupByIds" parameterType="String">
        delete from member_group where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="queryMemberGroupList" resultType="com.jsowell.pile.vo.web.MemberGroupVO">
        select
            t1.id as id,
            t1.group_name as groupName,
            t1.group_code as groupCode,
            t1.merchant_id as merchantId,
            t1.station_id as stationId,
            t1.group_level as groupLevel,
            t1.group_type as groupType,
            t1.discount as discount,
            t2.merchant_name as merchantName,
            t3.station_name as stationName
        from member_group t1
        left join pile_merchant_info t2 on t1.merchant_id = t2.id
        left join pile_station_info t3 on t3.id = t1.station_id
        where t1.del_flag = '0'
        <if test="merchantIdList != null and merchantIdList.size() != 0">
            and t1.merchant_id in
            <foreach collection="merchantIdList" item="merchantId" open="(" separator="," close=")">
                #{merchantId,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="queryMemberDiscount" resultType="com.jsowell.pile.vo.web.MemberDiscountVO">
        SELECT
            t1.group_code AS groupCode,
            t1.group_type AS groupType,
            t1.discount AS discount,
            t1.merchant_id AS merchantId,
            t1.station_id AS stationId
        FROM
            member_group t1
                JOIN member_group_relation t2 ON t2.group_code = t1.group_code
                AND t2.del_flag = '0'
        WHERE
            t1.del_flag = '0'
          AND t1.merchant_id = #{merchantId,jdbcType=VARCHAR}
          AND t1.station_id = #{stationId,jdbcType=VARCHAR}
          AND t2.member_id = #{memberId,jdbcType=VARCHAR}
    </select>

    <select id="queryMemberList" resultType="com.jsowell.pile.vo.uniapp.customer.MemberVO">
        SELECT
            t1.merchant_id as merchantId,
            t1.member_id as memberId,
            t1.STATUS as status,
            t1.nick_name as nickName,
            t1.mobile_number as mobileNumber,
            t1.avatar_url as avatarUrl
        FROM
        member_basic_info t1
        JOIN member_group_relation t2 ON t2.member_id = t1.member_id and t2.del_flag = '0'
        where
            t2.group_code = #{groupCode,jdbcType=VARCHAR}
    </select>

    <select id="selectByGroupCode" resultMap="MemberGroupResult">
        select
            <include refid="Base_Column_List"/>
        from member_group
        where del_flag = '0'
        and group_code = #{groupCode,jdbcType=VARCHAR}
    </select>

    <select id="queryByStationId" resultType="com.jsowell.pile.vo.web.MemberGroupVO">
        select
            t1.id as id,
            t1.group_code as groupCode,
            t1.merchant_id as merchantId,
            t1.station_id as stationId,
            t1.group_level as groupLevel,
            t1.group_type as groupType,
            t1.discount as discount
        from member_group t1
        where t1.del_flag = '0'
        and find_in_set(#{stationId,jdbcType=VARCHAR}, t1.station_id)
    </select>

    <select id="queryByMerchantId" resultType="com.jsowell.pile.vo.web.MemberGroupVO">
        select
        t1.id as id,
        t1.group_code as groupCode,
        t1.merchant_id as merchantId,
        t1.station_id as stationId,
        t1.group_level as groupLevel,
        t1.group_type as groupType,
        t1.discount as discount
        from member_group t1
        where t1.del_flag = '0'
        and t1.merchant_id = #{merchantId,jdbcType=VARCHAR}
    </select>

    <select id="queryByGroupCodeAndMemberId" resultType="com.jsowell.pile.vo.web.MemberGroupVO">
        SELECT
            t1.group_code AS groupCode,
            t1.group_type AS groupType,
            t1.discount AS discount,
            t1.merchant_id AS merchantId,
            t1.station_id AS stationId
        FROM
            member_group t1
        JOIN member_group_relation t2 ON t2.group_code = t1.group_code AND t2.del_flag = '0'
        WHERE
            t1.del_flag = '0'
        and t1.group_code = #{groupCode,jdbcType=VARCHAR}
        and t2.member_id = #{memberId,jdbcType=VARCHAR}
    </select>

    <select id="queryMemberGroupCode" resultType="java.lang.String">
        SELECT
            t1.group_code AS groupCode
        FROM
            member_group t1
        JOIN member_group_relation t2 ON t2.group_code = t1.group_code AND t2.del_flag = '0'
        WHERE
            t1.del_flag = '0'
            and t2.member_id = #{memberId,jdbcType=VARCHAR}
            and t1.merchant_id = #{merchantId,jdbcType=VARCHAR}
            and find_in_set(#{stationId,jdbcType=VARCHAR}, t1.station_id)
    </select>

    <select id="queryStationList" resultType="com.jsowell.pile.vo.base.MemberGroupStationVO">
        SELECT
            t1.id as stationId,
            t1.station_name as stationName,
            t2.template_code as templateCode,
            t2.template_remark as templateRemark,
            t2.template_desc as templateDesc
        FROM
            pile_station_info t1
        JOIN pile_billing_template t2 ON t2.station_id = t1.id AND t2.del_flag = '0' AND t2.member_flag = '1'
        WHERE
            FIND_IN_SET(t1.id, (SELECT station_id FROM member_group WHERE group_code = #{groupCode,jdbcType=VARCHAR}))
    </select>
</mapper>