<?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.cool.store.mapper.HyPartnerUserInfoMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.HyPartnerUserInfoDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="partner_id" jdbcType="VARCHAR" property="partnerId"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="user_channel_id" jdbcType="BIGINT" property="userChannelId"/>
        <result column="crm_create_time" jdbcType="TIMESTAMP" property="crmCreateTime"/>
        <result column="openid" jdbcType="VARCHAR" property="openid"/>
        <result column="downstream_system_salting" jdbcType="VARCHAR" property="downstreamSystemSalting"/>
        <result column="downstream_system_password" jdbcType="VARCHAR" property="downstreamSystemPassword"/>
        <result column="downstream_system_secondary_password" jdbcType="VARCHAR"
                property="downstreamSystemSecondaryPassword"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, partner_id, mobile, create_time, update_time, user_channel_id, crm_create_time, openid,
        downstream_system_salting, downstream_system_password,downstream_system_secondary_password
    </sql>
    <select id="selectByPartnerId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from xfsg_partner_user_info
        where partner_id = #{partnerId}
    </select>

    <select id="selectByMobile" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from xfsg_partner_user_info
        where mobile = #{mobile}
    </select>

    <select id="selectByOpenid" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from xfsg_partner_user_info
        where openid = #{openid}
    </select>

    <select id="selectByPartnerIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from xfsg_partner_user_info
        <where>
            <if test="partnerIdList!=null and partnerIdList.size>0">
                <foreach collection="partnerIdList" open="and partner_id in (" close=")" separator="," item="partnerId">
                    #{partnerId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectLastCrmCreateTime" resultType="java.lang.String">
        SELECT crm_create_time as crmCreateTime FROM xfsg_partner_user_info ORDER BY crm_create_time desc limit 1
    </select>
    <select id="selectPasswordIsNull" resultType="com.cool.store.entity.HyPartnerUserInfoDO">
        SELECT * FROM `xfsg_partner_user_info` a LEFT JOIN xfsg_qualifications_info b on a.partner_id=b.partner_id WHERE b.id is not null and a.downstream_system_password is null
    </select>

    <insert id="insertSelective" keyColumn="id" keyProperty="record.id" useGeneratedKeys="true">
        insert into xfsg_partner_user_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record.partnerId != null">
                partner_id,
            </if>
            <if test="record.mobile != null">
                mobile,
            </if>
            <if test="record.createTime != null">
                create_time,
            </if>
            <if test="record.updateTime != null">
                update_time,
            </if>
            <if test="record.userChannelId!=null">
                user_channel_id,
            </if>
            <if test="record.ecWantShopArea!=null">
                ec_want_shop_area,
            </if>
            <if test="record.crmCreateTime!=null">
                crm_create_time,
            </if>
            <if test="record.openid!=null">
                openid,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record.partnerId != null">
                #{record.partnerId},
            </if>
            <if test="record.mobile != null">
                #{record.mobile},
            </if>
            <if test="record.createTime != null">
                #{record.createTime},
            </if>
            <if test="record.updateTime != null">
                #{record.updateTime},
            </if>
            <if test="record.userChannelId != null">
                #{record.userChannelId},
            </if>
            <if test="record.ecWantShopArea != null">
                #{record.ecWantShopArea},
            </if>
            <if test="record.crmCreateTime != null">
                #{record.crmCreateTime},
            </if>
            <if test="record.openid != null">
                #{record.openid},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update xfsg_partner_user_info
        <set>
            <if test="record.partnerId != null">
                partner_id = #{record.partnerId},
            </if>
            <if test="record.mobile != null">
                mobile = #{record.mobile},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime},
            </if>
            <if test="record.ecWantShopArea != null">
                ec_want_shop_area = #{record.ecWantShopArea},
            </if>
            <if test="record.openid != null">
                openid = #{record.openid},
            </if>
        </set>
        where id = #{record.id}
    </update>

    <update id="updateByPartnerId">
        update xfsg_partner_user_info
        <set>
            <if test="record.mobile != null">
                mobile = #{record.mobile},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime},
            </if>
        </set>
        where partner_id = #{record.partnerId}
    </update>
    <update id="updatePasswordByPartnerId">
        update xfsg_partner_user_info
        <set>
            <if test="record.downstreamSystemSalting != null">
                downstream_system_salting = #{record.downstreamSystemSalting},
            </if>
            <if test="record.downstreamSystemPassword != null">
                downstream_system_password = #{record.downstreamSystemPassword},
            </if>
            <if test="record.downstreamSystemSecondaryPassword != null">
                downstream_system_secondary_password = #{record.downstreamSystemSecondaryPassword},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime},
            </if>
        </set>
        where partner_id = #{record.partnerId}
    </update>
    <update id="dataHandle">
        <foreach collection="list" item="item" separator=";">
            update xfsg_partner_user_info
            <set>
                <if test="item.downstreamSystemSalting != null">
                    downstream_system_salting = #{item.downstreamSystemSalting},
                </if>
                <if test="item.downstreamSystemPassword != null">
                    downstream_system_password = #{item.downstreamSystemPassword},
                </if>
                <if test="item.downstreamSystemSecondaryPassword != null">
                    downstream_system_secondary_password = #{item.downstreamSystemSecondaryPassword},
                </if>
                update_time = now()

            </set>
            where id = #{item.id}

        </foreach>
    </update>

    <select id="selectLastBindRecord" resultType="com.cool.store.dto.wechat.ServiceAccountOpenIdDTO">
        select
        b.partner_id as partnerId,
        b.union_id as unionId,
        b.service_account_open_id as serviceAccountOpenId,
        MAX(b.update_time) as lastUpdateTime
        from xfsg_partner_user_info  a
        left join xfsg_partner_user_wechat_bind b
        on a.partner_id = b.partner_id
        where b.partner_id is not null
        and service_account_open_id is not null
        <if test="mobileList !=null and mobileList.size>0">
            <foreach collection="mobileList" open="and mobile in (" close=")" separator="," item="mobile">
                #{mobile}
            </foreach>
        </if>
        GROUP BY b.partner_id
    </select>

</mapper>