<?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.diaoyun.zion.chinafrica.dao.TbCfUserInfoDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity" id="tbCfUserInfoMap">
        <result property="userId" column="user_id"/>
        <result property="userNo" column="user_no"/>
        <result property="userType" column="user_type"/>
        <result property="account" column="account"/>
        <result property="avatar" column="avatar"/>
        <result property="nick" column="nick"/>
        <result property="phone" column="phone"/>
        <result property="phoneFlag" column="phone_flag"/>
        <result property="password" column="password"/>
        <result property="lastLoginTime" column="last_login_time"/>
        <result property="lastLoginIp" column="last_login_ip"/>
        <result property="loginCount" column="login_count"/>
        <result property="email" column="email"/>
        <result property="facebook" column="facebook"/>
        <result property="createTime" column="create_time"/>
        <result property="sex" column="sex"/>
        <result property="defaultAddressId" column="default_address_id"/>
        <result property="invitedUserId" column="invited_user_id"/>
        <result property="invitedCount" column="invited_count"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="emailFlag" column="email_flag"/>
        <result property="isSend" column="is_send"/>
    </resultMap>

    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
		select
			`user_id`,
			`user_no`,
			`user_type`,
			`account`,
			`avatar`,
			`nick`,
			`phone`,
			`phone_flag`,
			`password`,
			`last_login_time`,
			`last_login_ip`,
			`login_count`,
			`email`,
			`facebook`,
			`create_time`,
			`sex`,
			`default_address_id`,
			`invited_user_id`,
			`invited_count`,
			`enable_flag`,
			`email_flag`,
			`is_send`
		from tb_cf_user_info
		where user_id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select
        `user_id`,
        `user_no`,
        `user_type`,
        `account`,
        `avatar`,
        `nick`,
        `phone`,
        `phone_flag`,
        `password`,
        `last_login_time`,
        `last_login_ip`,
        `login_count`,
        `email`,
        `facebook`,
        `create_time`,
        `sex`,
        `default_address_id`,
        `invited_user_id`,
        `invited_count`,
        `enable_flag`,
        `email_flag`,
        `is_send`
        from tb_cf_user_info
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by user_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>
    <select id="selectUserByPhone" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
       select * from tb_cf_user_info where phone is not null
   </select>
    <select id="queryTotal" resultType="int">
        select count(*) from tb_cf_user_info
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>
    <select id="checkUserByNick" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select * from tb_cf_user_info
        WHERE nick=#{name} or account=#{name}
    </select>
    <select id="checkUserByPhone" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select * from tb_cf_user_info
        WHERE phone=#{phone}
    </select>
    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
		insert into tb_cf_user_info(
			`user_id`,
			`user_no`,
			`user_type`,
			`account`,
			`avatar`,
			`nick`,
			`phone`,
			`phone_flag`,
			`password`,
			`last_login_time`,
			`last_login_ip`,
			`login_count`,
			`email`,
			`facebook`,
			`create_time`,
			`sex`,
			`default_address_id`,
			`invited_user_id`,
			`invited_count`,
			`enable_flag`,
			`email_flag`,
			`is_send`
			)
		values(
			#{userId},
			#{userNo},
			#{userType},
			#{account},
			#{avatar},
			#{nick},
			#{phone},
			#{phoneFlag},
			#{password},
			#{lastLoginTime},
			#{lastLoginIp},
			#{loginCount},
			#{email},
			#{facebook},
			#{createTime},
			#{sex},
			#{defaultAddressId},
			#{invitedUserId},
			#{invitedCount},
			#{enableFlag},
			#{emailFlag},
			#{isSend}
			)
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        update tb_cf_user_info
        <set>

            <if test="avatar != null">`avatar` = #{avatar},</if>
            <if test="nick != null">`nick` = #{nick},</if>
            <if test="phone != null">`phone` = #{phone},</if>
            <if test="phoneFlag != null">`phone_flag` = #{phoneFlag},</if>
            <!--<if test="password != null">`password` = #{password},</if>-->
            <if test="lastLoginTime != null">`last_login_time` = #{lastLoginTime},</if>
            <if test="lastLoginIp != null">`last_login_ip` = #{lastLoginIp},</if>
            <if test="loginCount != null">`login_count` = #{loginCount},</if>
            <if test="email != null">`email` = #{email},</if>
            <if test="facebook != null">`facebook` = #{facebook},</if>
            <if test="sex != null">`sex` = #{sex},</if>
            <if test="defaultAddressId != null">`default_address_id` = #{defaultAddressId},</if>
            <if test="invitedUserId != null">`invited_user_id` = #{invitedUserId},</if>
            <if test="invitedCount != null">`invited_count` = #{invitedCount},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag},</if>
            <if test="emailFlag != null">`email_flag` = #{emailFlag}</if>
            <if test="isSend != null">`is_send` = #{isSend}</if>
        </set>
        where user_id = #{userId}
    </update>

    <delete id="delete">
		delete from tb_cf_user_info where user_id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from tb_cf_user_info where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
    <select id="findEmails" resultType="String">
		select account from tb_cf_user_info where is_send=0 and  account like '%@%' order by rand() limit 1
    </select>

    <!-- 修改用户是否发送邮件-->
    <update id="updateIsSend">
        update tb_cf_user_info set is_send=1 where account=#{email}
    </update>

    <!--根据nickname获取用户信息-->
    <select id="getByNickname" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select * from tb_cf_user_info where nickname=#{nickname}
    </select>

    <!--根据userId 账号  email nick 查找用户-->
    <select id="findByAccount" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select * from tb_cf_user_info where enable_flag=1 and (user_id=#{userId} or account=#{account}
        or email =#{account} or nick =#{account})
    </select>
    <!--根据userId 账号  email nick 查找用户-->
    <select id="findAccount" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
        select * from tb_cf_user_info where enable_flag=1 and  account=#{account}

    </select>
    <!--根据userId 账号  email 查找用户-->
    <select id="findByEmail" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity">
         select * from tb_cf_user_info where enable_flag=1 and   account=#{email} or email =#{email}
    </select>
    <!--修改密码-->
    <update id="updatePassWord">
        update tb_cf_user_info set password=#{newPassword} where user_id=#{userId}
    </update>
    <!--提供给第三方的 ，根据userId，查找需要的信息-->
    <select id="queryUserByUserId" resultType="com.diaoyun.zion.chinafrica.vo.TbCfUserVo">
        select
         u.user_id,
         u.user_no,
         u.nick user_name,
         u.phone,
         u.last_login_time,
		u.enable_flag
        from tb_cf_user_info u
        WHERE 1=1 and  u.user_id=#{userId}
    </select>
</mapper>