<?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.MemberBasicInfoMapper">

    <resultMap type="com.jsowell.pile.domain.MemberBasicInfo" id="MemberBasicInfoResult">
        <result property="id"    column="id"    />
        <result property="memberId" column="member_id"/>
        <result property="openId" column="open_id"/>
        <result property="buyerId" column="buyer_id"/>
        <result property="licensePlateNumber" column="license_plate_number"/>
        <result property="nickName"    column="nick_name"    />
        <result property="logicCard"    column="logic_card"/>
        <result property="physicsCard"  column="physics_card"/>
        <result property="status"    column="status"    />
        <result property="avatarUrl"    column="avatar_url"    />
        <result property="mobileNumber"    column="mobile_number"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMemberBasicInfoVo">
        select
        <include refid="Base_Column_List"/>
        from member_basic_info
    </sql>

    <sql id="Base_Column_List">
        <!--@mbg.generated PileBillingTemplate-->
        id, member_id, open_id, buyer_id, license_plate_number, nick_name,logic_card, physics_card, status, avatar_url,
        mobile_number, merchant_id, remark,
        create_time, create_by, update_time, update_by, del_flag
    </sql>

    <select id="selectMemberBasicInfoList" parameterType="com.jsowell.pile.domain.MemberBasicInfo" resultMap="MemberBasicInfoResult">
        <include refid="selectMemberBasicInfoVo"/>
        <where>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="logicCard != null and logicCard != ''"> and logic_card = #{logicCard}</if>
            <if test="physicsCard != null and physicsCard != ''"> and physics_card = #{physicsCard}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="avatarUrl != null  and avatarUrl != ''"> and avatar_url = #{avatarUrl}</if>
            <if test="mobileNumber != null  and mobileNumber != ''"> and mobile_number = #{mobileNumber}</if>
            <if test="merchantId != null "> and merchant_id = #{merchantId}</if>
        </where>
    </select>

    <select id="selectMemberBasicInfoById" parameterType="Integer" resultMap="MemberBasicInfoResult">
        <include refid="selectMemberBasicInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertMemberBasicInfo" parameterType="com.jsowell.pile.domain.MemberBasicInfo" useGeneratedKeys="true" keyProperty="id">
        insert into member_basic_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="memberId != null">member_id,</if>
            <if test="openId != null">open_id,</if>
            <if test="buyerId != null">buyer_id,</if>
            <if test="licensePlateNumber != null">license_plate_number,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="logicCard != null">logic_card,</if>
            <if test="physicsCard != null">physics_card,</if>
            <if test="status != null">status,</if>
            <if test="avatarUrl != null">avatar_url,</if>
            <if test="mobileNumber != null">mobile_number,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="memberId != null">#{memberId},</if>
            <if test="openId != null">#{openId},</if>
            <if test="buyerId != null">#{buyerId},</if>
            <if test="licensePlateNumber != null">#{licensePlateNumber},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="logicCard != null">#{logicCard},</if>
            <if test="physicsCard != null">#{physicsCard},</if>
            <if test="status != null">#{status},</if>
            <if test="avatarUrl != null">#{avatarUrl},</if>
            <if test="mobileNumber != null">#{mobileNumber},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateMemberBasicInfo" parameterType="com.jsowell.pile.domain.MemberBasicInfo">
        update member_basic_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="buyerId != null">buyer_id = #{buyerId},</if>
            <if test="licensePlateNumber != null">license_plate_number = #{licensePlateNumber},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="logicCard != null">logic_card = #{logicCard},</if>
            <if test="physicsCard != null">physics_card = #{physicsCard},</if>
            <if test="status != null">status = #{status},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="mobileNumber != null">mobile_number = #{mobileNumber},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

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

    <select id="selectInfoByPhysicsCard" resultType="com.jsowell.pile.vo.uniapp.customer.MemberVO">
        SELECT
        t1.member_id as memberId,
        t1.STATUS as status,
        t1.nick_name as nickName,
        t1.mobile_number as mobileNumber,
        t1.password,
        t1.avatar_url,
        t1.logic_card,
        t1.physics_card,
        t1.merchant_id,
        t1.remark,
        t1.create_time,
        t1.create_by,
        t1.update_time,
        t1.update_by,
        t1.del_flag,
        t2.principal_balance as principalBalance,
        t2.gift_balance as giftBalance
        FROM
        member_basic_info t1
        JOIN member_wallet_info t2 ON t1.member_id = t2.member_id
        where t1.del_flag = '0'
        and physics_card = #{physicsCard,jdbcType=VARCHAR}
    </select>

    <select id="selectInfoByMobileNumberAndMerchantId" resultMap="MemberBasicInfoResult">
        select
            <include refid="Base_Column_List"/>
        from member_basic_info
        where del_flag = '0'
        and mobile_number = #{mobileNumber,jdbcType=VARCHAR}
        <if test="firstLevelMerchantId != null and firstLevelMerchantId != ''" >
            and merchant_id = #{firstLevelMerchantId,jdbcType=VARCHAR}
        </if>
        order by create_time DESC, update_time DESC
    </select>

    <select id="selectInfoByMemberId" resultMap="MemberBasicInfoResult">
        SELECT
        <include refid="Base_Column_List"/>
        FROM member_basic_info
        WHERE del_flag = '0'
        and member_id = #{memberId,jdbcType=VARCHAR}
    </select>

    <update id="updateMemberBalance">
        update member_wallet_info
        <trim prefix="SET" suffixOverrides=",">
            version = version + 1,
            <if test="newPrincipalBalance != null">principal_balance = #{newPrincipalBalance},</if>
            <if test="newGiftBalance != null">gift_balance = #{newGiftBalance},</if>
        </trim>
        where member_id = #{memberId,jdbcType=VARCHAR}
        and wallet_code = #{walletCode,jdbcType=VARCHAR}
        and version = #{version,jdbcType=INTEGER}
    </update>

    <select id="queryMemberInfoByMemberId" resultType="com.jsowell.pile.vo.uniapp.customer.MemberVO">
        SELECT
            t1.member_id as memberId,
            t1.STATUS as status,
            t1.nick_name as nickName,
            t1.avatar_url as avatarUrl,
            t1.mobile_number as mobileNumber,
            t1.merchant_id as merchantId,
            t1.create_time as createTime
            <!--t2.principal_balance as principalBalance,
            t2.gift_balance as giftBalance-->
        FROM
            member_basic_info t1
            <!--left JOIN member_wallet_info t2 ON t1.member_id = t2.member_id and t2.del_flag = '0'-->
        where t1.del_flag = '0'
          and t1.member_id = #{memberId,jdbcType=VARCHAR}
    </select>

    <select id="selectMemberList" resultType="com.jsowell.pile.vo.uniapp.customer.MemberVO">
        SELECT
            t1.merchant_id as merchantId,
            t3.merchant_name as merchantName,
            t1.member_id as memberId,
            t1.STATUS as status,
            t1.nick_name as nickName,
            t1.mobile_number as mobileNumber,
            <!--t2.principal_balance as principalBalance,-->
            <!--t2.gift_balance as giftBalance,-->
            t1.avatar_url as avatarUrl
        FROM
            member_basic_info t1
        <!--JOIN member_wallet_info t2 ON t1.member_id = t2.member_id and t2.del_flag = '0'-->
        JOIN pile_merchant_info t3 ON t1.merchant_id = t3.id and t3.del_flag = '0'
        <if test="dto.vinCode != null">
            join member_plate_number_relation t4 on t4.member_id = t1.member_id and t4.del_flag = '0'
        </if>
        where
            t1.del_flag = '0'
        <if test="dto.mobileNumber != null and dto.mobileNumber != ''">and t1.mobile_number like '%${dto.mobileNumber}%'</if>
        <if test="dto.nickName != null and dto.nickName != ''">and t1.nick_name like '%${dto.nickName}%'</if>
        <if test="dto.memberId != null and dto.memberId != ''">and t1.member_id like '%${dto.memberId}%'</if>
        <if test="dto.merchantId != null and dto.merchantId != ''">and t1.merchant_id = #{dto.merchantId,jdbcType=VARCHAR}</if>
        <if test="dto.merchantDeptIds != null and dto.merchantDeptIds.size() != 0">
            and t3.dept_id in
            <foreach collection="dto.merchantDeptIds" item="deptId" open="(" separator="," close=")">
                #{deptId,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="dto.vinCode != null">
            and t4.vin_code like '%${dto.vinCode}%'
        </if>
        <if test="dto.firstMerchantIds != null and dto.firstMerchantIds.size() != 0">
            and t1.merchant_id in
            <foreach collection="dto.firstMerchantIds" item="firstMerchantId" open="(" separator="," close=")">
                #{firstMerchantId,jdbcType=VARCHAR}
            </foreach>
        </if>
        GROUP BY (t1.member_id)
    </select>

    <select id="getMerchantListByAuth" resultType="com.jsowell.pile.vo.base.MerchantInfoVO">
        SELECT DISTINCT
            ( t1.merchant_id ) as merchantId,
            t2.merchant_name as merchantName
        FROM
            member_basic_info t1
                JOIN pile_merchant_info t2 ON t1.merchant_id = t2.id and t1.del_flag = '0'
        <where>
        <if test="deptIds != null and deptIds.size() != 0">
            t2.dept_id in
            <foreach collection="deptIds" item="deptid" open="(" separator="," close=")">
                #{deptid,jdbcType=VARCHAR}
            </foreach>
        </if>
        </where>
    </select>

    <select id="queryMerchantVipList" resultType="com.jsowell.pile.vo.uniapp.customer.MerchantVipVO">
        select
            t1.member_id as memberId,
            t1.merchant_id as merchantId,
            t3.merchant_name as merchantName,
            t2.mobile_number as mobileNumber,
            t2.nick_name as nickName,
            t2.avatar_url as avatarUrl,
            t1.wallet_code as walletCode,
            t1.principal_balance as principalBalance,
            t1.gift_balance as giftBalance
        from member_wallet_info t1
        JOIN member_basic_info t2 on t2.member_id = t1.member_id and t2.del_flag = '0'
        JOIN pile_merchant_info t3 ON t1.merchant_id = t3.id and t3.del_flag = '0'
        where t1.del_flag = '0'
          AND t1.merchant_id in
        <foreach collection="merchantIdList" item="merchantId" open="(" separator="," close=")">
            #{merchantId,jdbcType=VARCHAR}
        </foreach>
        <if test="dto.mobileNumber != null and dto.mobileNumber != ''">
            and t2.mobile_number like '%${dto.mobileNumber}%'
        </if>
        <if test="dto.memberId != null and dto.memberId != ''">and t2.member_id like '%${dto.memberId}%'</if>
    </select>

    <select id="getMemberInfoByPlateNumber" resultType="com.jsowell.pile.vo.uniapp.customer.MemberVO">
        SELECT
            t2.member_id AS memberId,
            t2.merchant_id AS merchantId,
            t1.license_plate_number AS plateNumber,
            t2.STATUS,
            t2.mobile_number AS mobileNumber
        FROM
            member_plate_number_relation t1
            JOIN member_basic_info t2 ON t1.member_id = t2.member_id
        WHERE
            t1.license_plate_number = #{plateNumber,jdbcType=VARCHAR}
          AND t1.del_flag = '0'
          AND t2.STATUS = '1'
    </select>
</mapper>