<?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.miaxis.app.user.mapper.UserInfoMapper">

    <resultMap type="UserInfo" id="UserInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="sex"    column="sex"    />
        <result property="sfzmlx"    column="sfzmlx"    />
        <result property="idCard"    column="id_card"    />
        <result property="crdate"    column="crdate"    />
        <result property="birthday"    column="birthday"    />
        <result property="mobile"    column="mobile"    />
        <result property="password"    column="password"    />
        <result property="wechar"    column="wechar"    />
        <result property="openid"    column="openid"    />
        <result property="inscode"    column="inscode"    />
        <result property="createTime"    column="create_time"    />
        <result property="schoolClassTypeId"    column="school_class_type_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="pxcx"    column="pxcx"    />
        <result property="coachnum"    column="coachnum"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectUserInfoVo">
        select id, name, sex, sfzmlx, id_card, crdate, birthday, mobile,  wechar, openid, inscode, create_time, school_class_type_id, update_time, pxcx, coachnum, status from user_info
    </sql>

    <select id="selectUserInfoList" parameterType="UserInfo" resultMap="UserInfoResult">
        <include refid="selectUserInfoVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="sex != null "> and sex = #{sex}</if>
            <if test="sfzmlx != null "> and sfzmlx = #{sfzmlx}</if>
            <if test="idCard != null  and idCard != ''"> and id_card = #{idCard}</if>
            <if test="crdate != null "> and crdate = #{crdate}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="password != null  and password != ''"> and password = #{password}</if>
            <if test="wechar != null  and wechar != ''"> and wechar = #{wechar}</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="inscode != null  and inscode != ''"> and inscode = #{inscode}</if>
            <if test="schoolClassTypeId != null "> and school_class_type_id = #{schoolClassTypeId}</if>
            <if test="pxcx != null  and pxcx != ''"> and pxcx = #{pxcx}</if>
            <if test="coachnum != null  and coachnum != ''"> and coachnum = #{coachnum}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectUserInfoById" parameterType="Long" resultMap="UserInfoResult">
        <include refid="selectUserInfoVo"/>
        where id = #{id}
    </select>
    <select id="selectUserInfoByMobile"  resultMap="UserInfoResult">
        <include refid="selectUserInfoVo"/>
        where mobile = #{mobile}
    </select>

    <insert id="insertUserInfo" parameterType="UserInfo">
        insert into user_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="sex != null">sex,</if>
            <if test="sfzmlx != null">sfzmlx,</if>
            <if test="idCard != null">id_card,</if>
            <if test="crdate != null">crdate,</if>
            <if test="birthday != null">birthday,</if>
            <if test="mobile != null">mobile,</if>
            <if test="password != null">password,</if>
            <if test="wechar != null">wechar,</if>
            <if test="openid != null">openid,</if>
            <if test="inscode != null">inscode,</if>
            <if test="createTime != null">create_time,</if>
            <if test="schoolClassTypeId != null">school_class_type_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="pxcx != null">pxcx,</if>
            <if test="coachnum != null">coachnum,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="sex != null">#{sex},</if>
            <if test="sfzmlx != null">#{sfzmlx},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="crdate != null">#{crdate},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="password != null">#{password},</if>
            <if test="wechar != null">#{wechar},</if>
            <if test="openid != null">#{openid},</if>
            <if test="inscode != null">#{inscode},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="schoolClassTypeId != null">#{schoolClassTypeId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="pxcx != null">#{pxcx},</if>
            <if test="coachnum != null">#{coachnum},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateUserInfo" parameterType="UserInfo">
        update user_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="sfzmlx != null">sfzmlx = #{sfzmlx},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="crdate != null">crdate = #{crdate},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="password != null">password = #{password},</if>
            <if test="wechar != null">wechar = #{wechar},</if>
            <if test="openid != null">openid = #{openid},</if>
            <if test="inscode != null">inscode = #{inscode},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="schoolClassTypeId != null">school_class_type_id = #{schoolClassTypeId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="pxcx != null">pxcx = #{pxcx},</if>
            <if test="coachnum != null">coachnum = #{coachnum},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserInfoById" parameterType="Long">
        delete from user_info where id = #{id}
    </delete>

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

    <select id="getMyRegistrationInformation" resultType="UserVo">
        SELECT
        u.name,
        u.mobile,
        DATE_FORMAT(u.create_time,'%Y-%m-%d %H:%i:%s') AS createDate,
        s.name as schoolName,
        sct.class_name as className,
        sct.price as classPrice
        FROM
        user_info u
        LEFT JOIN school_info s ON s.inscode = u.inscode
        LEFT JOIN school_info_class_type sct ON sct.id = u.school_class_type_id
        where u.id = #{userId}
    </select>

</mapper>
