<?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="cn.wisenergy.mapper.UsersMapper">
    <resultMap id="userMap" type="cn.wisenergy.model.app.User">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="password" property="password"/>
        <result column="head_image" property="headImage"/>
        <result column="user_level" property="userLevel"/>
        <result column="cross_border_line" property="crossBorderLine"/>
        <result column="id_card_number" property="idCardNumber"/>
        <result column="fans_nickname" property="fansNickname"/>
        <result column="fans_id" property="fansId"/>
        <result column="invite_code" property="inviteCode"/>
        <result column="be_invited_code" property="beInvitedCode"/>
        <result column="customer_service_id" property="customerServiceId"/>
        <result column="frozen" property="frozen"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="table">
        user_info
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        user_id,password,head_image,user_level,cross_border_line,id_card_number,fans_nickname,fans_id, invite_code,
        be_invited_code,customer_service_id,create_time,update_time
    </sql>

    <sql id="vals">
        #{userId},#{password},#{headImage},#{userLevel},#{crossBorderLine},#{idCardNumber},#{fansNickname},#{fansId},#{inviteCode},
        #{beInvitedCode},#{customerServiceId},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="userId != null">user_id = #{userId},</if>
        <if test="password != null">password =#{password},</if>
        <if test="headImage != null">head_image =#{headImage},</if>
        <if test="userLevel != null">user_level =#{userLevel},</if>
        <if test="crossBorderLine != null">cross_border_line =#{crossBorderLine},</if>
        <if test="idCardNumber != null">id_card_number = #{idCardNumber},</if>
        <if test="fansNickname != null">fans_nickname =#{fansNickname},</if>
        <if test="fansId != null">fans_id =#{fansId},</if>
        <if test="inviteCode != null">invite_code =#{inviteCode},</if>
        <if test="beInvitedCode != null">be_invited_code = #{beInvitedCode},</if>
        <if test="customerServiceId != null">customer_service_id = #{customerServiceId},</if>
        update_time =now()
    </sql>

    <sql id="criteria">
        <if test="id != null">id = #{id}</if>
        <if test="userId != null">and user_id = #{userId}</if>
        <if test="password != null">and password =#{password}</if>
        <if test="headImage != null">and head_image =#{headImage}</if>
        <if test="userLevel != null">and user_level =#{userLevel}</if>
        <if test="crossBorderLine != null">and cross_border_line =#{crossBorderLine}</if>
        <if test="idCardNumber != null">and id_card_number = #{idCardNumber}</if>
        <if test="fansNickname != null">and fans_nickname =#{fansNickname}</if>
        <if test="fansId != null">and fans_id =#{fansId}</if>
        <if test="inviteCode != null">and invite_code =#{inviteCode}</if>
        <if test="beInvitedCode != null">and be_invited_code = #{beInvitedCode}</if>
        <if test="customerServiceId != null">and customer_service_id = #{customerServiceId}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>


    <insert id="add" parameterType="cn.wisenergy.model.app.User" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>

    <update id="edit" parameterType="cn.wisenergy.model.app.User">
        UPDATE
        <include refid="table"/>
        <set>
            <include refid="updateCondition"/>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <delete id="delById" parameterType="java.lang.Integer">
        delete from
        <include refid="table"/>
        where id = #{id}
    </delete>

    <select id="getList" resultMap="userMap" parameterType="map">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            be_invited_code=1
            <if test="createTime != null">
                and(YEAR(create_time) = YEAR(#{createTime})
                AND MONTH(create_time) = MONTH(#{createTime}))
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="getUserById" resultType="cn.wisenergy.model.app.User" parameterType="java.lang.Long">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            id=#{id}
        </where>
    </select>

    <select id="getByUserId" resultType="cn.wisenergy.model.app.User" parameterType="string">
        select
        id,user_id,password,head_image,user_level,cross_border_line,id_card_number,fans_nickname,fans_id, invite_code,
        be_invited_code,customer_service_id,create_time,update_time
        from
        user_info
        <where>
            user_id=#{userId}
        </where>
    </select>
    <select id="getById" resultType="java.lang.Integer" parameterType="string">
        select
        userId
        from
        user_info
        <where>
            user_id=#{userId}
        </where>
    </select>
    <select id="ByUserId" resultType="java.lang.Integer">
        select
        id
        from
        user_info
        <where>
            user_id=#{userId}
        </where>
    </select>

    <select id="findAllNotZeroUser" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            user_level !=0
        </where>
    </select>

    <!--查询用户-->
    <select id="findByName" resultType="cn.wisenergy.model.app.User">
        select * from User where user_id = #{userId}
    </select>

    <!--查询密码-->
    <select id="findPswByName" resultType="String">
        select password from user where user_id = #{userId}
    </select>

    <select id="getByBeInvitedCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            invite_code = #{beInvitedCode}
        </where>
    </select>

    <select id="getAllGoldUser" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            user_level >3
        </where>
    </select>

    <!--用户注册-->
    <insert id="insertbyint">
        insert into user_info(user_id,invite_code,be_invited_code,user_level,head_image,frozen) value
        (#{userId},#{inviteCode},#{beInvitedCode},#{userLevel},#{headImage},#{frozen})
    </insert>
    <insert id="save">
        insert into user_info(user_id,be_invited_code) value (#{userId},#{beInvitedCode})
    </insert>

    <select id="queryUsersByPhone" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="vals"/>
        from
        <include refid="table"/>
        <where>
            user_id=#{userId}
        </where>
    </select>
    <!--根据用户的推荐人邀请码比对推荐人的本人邀请码，查询推荐人的用户ID-->
    <select id="inviteCodeBeInvitedCode" resultType="java.lang.Integer">
        select
        user_id
        from
        <include refid="table"/>
        <where>
            invite_code=#{beInvitedCode}
        </where>
    </select>
    <!--根据用户的推荐人邀请码比对推荐人的本人邀请码，查询推荐人的用户ID-->
    <select id="beInvitedCode1" resultType="java.lang.Integer">
        select
        id
        from
        <include refid="table"/>
        <where>
            invite_code=#{beInvitedCode}
        </where>
    </select>

    <select id="InvitedCode2" resultType="cn.wisenergy.model.app.User" parameterType="string">
        select
        *
        from
        user_info
        <where>
            invite_code=#{inviteCode}
        </where>
    </select>

    <select id="getuserIdById" resultType="java.lang.Integer">
        select
        id
        from
        user_info
        <where>
            user_id=#{userId}
        </where>
    </select>
    <select id="BYQMById" resultType="String">
        select
        be_invited_code
        from
        <include refid="table"/>
        <where>
            user_id=#{userId}
        </where>
    </select>
    <update id="edit1">
        update
        user_info
        <set>
            <if test="userLevel != null">user_level =#{userLevel},</if>
            <if test="inviteCode != null">invite_code =#{inviteCode},</if>
            create_time =now(),
            update_time =now()
        </set>
        <where>
            user_id = #{userId}
        </where>
    </update>


    <!--分页查询所有用户信息 -->
    <select id="getUsersListByMap" resultType="cn.wisenergy.model.app.User" parameterType="java.util.Map">
        select id as id,
        user_id as userId,
        password as password,
        user_level as userLevel,
        cross_border_line as crossBorderLine,
        id_card_number as idCardNumber,
        fans_nickname as fansNickname,
        invite_code as inviteCode,
        be_invited_code as beInvitedCode,
        create_time as createTime,
        update_time as updateTime
        from user_info
        <trim prefix="where" prefixOverrides="and | or">
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="account != null and account!=''">
                and userId=#{userId}
            </if>
            <if test="password != null and password!=''">
                and password=#{password}
            </if>
            <if test="salt != null and salt!=''">
                and userLevel=#{userLevel}
            </if>

            <if test="sex != null">
                and idCardNumber=#{idCardNumber}
            </if>
            <if test="isAuthentication != null">
                and fansNickname=#{fansNickname}
            </if>
            <if test="name != null and name!=''">
                and inviteCode=#{inviteCode}
            </if>
            <if test="cardNo != null and cardNo!=''">
                and beInvitedCode=#{beInvitedCode}
            </if>
            <if test="null!=creatdTime">
                and creatdTime=#{creatdTime}
            </if>
            <if test="null!=updatedTime">
                and updatedTime=#{updatedTime}
            </if>
        </trim>
        <if test="beginPos != null and pageSize != null ">
            limit #{beginPos},#{pageSize}
        </if>
    </select>

    <!--    获取当前用户的所有直接推荐人-->
    <select id="getByInviteCode" resultType="cn.wisenergy.model.app.User" parameterType="string">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            be_invited_code=#{inviteCode}
            and user_level=#{userLevel}
        </where>
    </select>

    <select id="queryByInviteCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            invite_code=#{inviteCode}
        </where>
    </select>

    <select id="getByLevel" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            user_level=#{userLevel}
        </where>
    </select>

    <select id="getUserByIntiveCode" parameterType="string" resultType="string">
        select user_id
        from
        <include refid="table"/>
        where
        invite_code = #{beInvitedCode}
    </select>

    <select id="getUserIdByIntiveCode" resultType="string" parameterType="string">
        select user_id
        from
        <include refid="table"/>
        where
        invite_code = #{outerUserId}
    </select>

    <select id="getAerialDeliveryUserBeforOneMonth" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where
        be_invited_code = '1'
        and DATE_FORMAT(create_time,'%Y-%m-%d')
        BETWEEN #{before} AND #{now}
    </select>

    <select id="randOneGetUserByUserId" resultType="cn.wisenergy.model.app.User">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="table"/>
        WHERE id &lt; #{id}
        ORDER BY RAND() LIMIT 1
    </select>

    <select id="getListByIntvitedCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where be_invited_code = #{intiveCode}
    </select>

    <select id="getuserByBeInvitedCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where
        invite_code = #{be_invited_code}
    </select>

    <select id="getListUserByBeInvitedCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where
        be_invited_code = #{be_invited_code}
    </select>

    <select id="getUserByInviteCode" resultType="cn.wisenergy.model.app.User">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where
        invite_code = #{inviteCode}
    </select>

</mapper>
