<?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.ruoyi.system.mapper.TUserProfileMapper">

    <resultMap type="TUserProfile" id="TUserProfileResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
        <result property="sfzid" column="sfzid"/>
        <result property="telOne" column="tel_one"/>
        <result property="telTwo" column="tel_two"/>
        <result property="telThree" column="tel_three"/>
        <result property="birthday" column="birthday"/>
        <result property="age" column="age"/>
        <result property="marrStatus" column="marr_status"/>
        <result property="national" column="national"/>
        <result property="local" column="local"/>
        <result property="clientType" column="client_type"/>
        <result property="liveStatus" column="live_status"/>
        <result property="familyAddress" column="family_address"/>
        <result property="detailAddress" column="detail_address"/>
        <result property="houseAddress" column="house_address"/>
        <result property="houseDetailAddress" column="house_detail_address"/>
        <result property="serviceid" column="serviceid"/>
        <result property="oldmenStatus" column="oldmen_status"/>
        <result property="changeContent" column="change_content"/>
        <result property="loseMonth" column="lose_month"/>
        <result property="treatMonth" column="treat_month"/>
        <result property="isTreat" column="is_treat"/>
        <result property="serviceType" column="service_type"/>
        <result property="treateSure" column="treate_sure"/>
        <result property="moneyCome" column="money_come"/>
        <result property="moneyMonth" column="money_month"/>
        <result property="cardCode" column="card_code"/>
        <result property="slowIllness" column="slow_illness"/>
        <result property="bloodType" column="blood_type"/>
        <result property="shiStatus" column="shi_status"/>
        <result property="disablityStatus" column="disablity_status"/>
        <result property="disablityEntry" column="disablity_entry"/>
        <result property="educaLevel" column="educa_level"/>
        <result property="special" column="special"/>
        <result property="title" column="title"/>
        <result property="unity" column="unity"/>
        <result property="natureHeart" column="nature_heart"/>
        <result property="eatPoint" column="eat_point"/>
        <result property="speciality" column="speciality"/>
        <result property="hobby" column="hobby"/>
        <result property="photo" column="photo"/>
        <result property="otherPhoto" column="other_photo"/>
        <result property="remarks" column="remarks"/>
        <result property="createTime" column="createtime"/>
    </resultMap>

    <resultMap id="TUserProfileTEmergentPeopleResult" type="TUserProfile" extends="TUserProfileResult">
        <collection property="tEmergentPeopleList" notNullColumn="sub_id" javaType="java.util.List"
                    resultMap="TEmergentPeopleResult"/>
    </resultMap>

    <resultMap type="TEmergentPeople" id="TEmergentPeopleResult">
        <result property="id" column="sub_id"/>
        <result property="profileId" column="sub_profile_id"/>
        <result property="name" column="sub_name"/>
        <result property="telno" column="sub_telno"/>
        <result property="relation" column="sub_relation"/>
        <result property="isConn" column="sub_is_conn"/>
    </resultMap>

    <sql id="selectTUserProfileVo">
        select id,
               name,
               gender,
               sfzid,
               tel_one,
               tel_two,
               tel_three,
               birthday,
               age,
               marr_status, national, local, client_type, live_status, family_address, detail_address, house_address, house_detail_address, serviceid, oldmen_status, change_content, lose_month, treat_month, is_treat, service_type, treate_sure, money_come, money_month, card_code, slow_illness, blood_type, shi_status, disablity_status, disablity_entry, educa_level, special, title, unity, nature_heart, eat_point, speciality, hobby, photo, other_photo, remarks, createtime
        from t_user_profile
    </sql>

    <select id="selectTUserProfileList" parameterType="TUserProfile" resultMap="TUserProfileResult">
        <include refid="selectTUserProfileVo"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="gender != null ">and gender = #{gender}</if>
            <if test="sfzid != null  and sfzid != ''">and sfzid = #{sfzid}</if>
            <if test="telOne != null  and telOne != ''">and tel_one = #{telOne}</if>
            <if test="telTwo != null  and telTwo != ''">and tel_two = #{telTwo}</if>
            <if test="telThree != null  and telThree != ''">and tel_three = #{telThree}</if>
            <if test="birthday != null  and birthday != ''">and birthday = #{birthday}</if>
            <if test="age != null ">and age = #{age}</if>
            <if test="marrStatus != null ">and marr_status = #{marrStatus}</if>
            <if test="national != null ">and national = #{national}</if>
            <if test="local != null ">and local = #{local}</if>
            <if test="clientType != null ">and client_type = #{clientType}</if>
            <if test="liveStatus != null ">and live_status = #{liveStatus}</if>
            <if test="familyAddress != null  and familyAddress != ''">and family_address = #{familyAddress}</if>
            <if test="detailAddress != null  and detailAddress != ''">and detail_address = #{detailAddress}</if>
            <if test="houseAddress != null  and houseAddress != ''">and house_address = #{houseAddress}</if>
            <if test="houseDetailAddress != null  and houseDetailAddress != ''">and house_detail_address =
                #{houseDetailAddress}
            </if>
            <if test="serviceid != null ">and serviceid = #{serviceid}</if>
            <if test="oldmenStatus != null  and oldmenStatus != ''">and oldmen_status = #{oldmenStatus}</if>
            <if test="changeContent != null  and changeContent != ''">and change_content = #{changeContent}</if>
            <if test="loseMonth != null  and loseMonth != ''">and lose_month = #{loseMonth}</if>
            <if test="treatMonth != null  and treatMonth != ''">and treat_month = #{treatMonth}</if>
            <if test="isTreat != null ">and is_treat = #{isTreat}</if>
            <if test="serviceType != null ">and service_type = #{serviceType}</if>
            <if test="treateSure != null ">and treate_sure = #{treateSure}</if>
            <if test="moneyCome != null ">and money_come = #{moneyCome}</if>
            <if test="moneyMonth != null  and moneyMonth != ''">and money_month = #{moneyMonth}</if>
            <if test="cardCode != null  and cardCode != ''">and card_code = #{cardCode}</if>
            <if test="slowIllness != null  and slowIllness != ''">and slow_illness = #{slowIllness}</if>
            <if test="bloodType != null ">and blood_type = #{bloodType}</if>
            <if test="shiStatus != null ">and shi_status = #{shiStatus}</if>
            <if test="disablityStatus != null ">and disablity_status = #{disablityStatus}</if>
            <if test="disablityEntry != null ">and disablity_entry = #{disablityEntry}</if>
            <if test="educaLevel != null ">and educa_level = #{educaLevel}</if>
            <if test="special != null  and special != ''">and special = #{special}</if>
            <if test="title != null  and title != ''">and title = #{title}</if>
            <if test="unity != null  and unity != ''">and unity = #{unity}</if>
            <if test="natureHeart != null  and natureHeart != ''">and nature_heart = #{natureHeart}</if>
            <if test="eatPoint != null  and eatPoint != ''">and eat_point = #{eatPoint}</if>
            <if test="speciality != null  and speciality != ''">and speciality = #{speciality}</if>
            <if test="hobby != null  and hobby != ''">and hobby = #{hobby}</if>
            <if test="photo != null  and photo != ''">and photo = #{photo}</if>
            <if test="otherPhoto != null  and otherPhoto != ''">and other_photo = #{otherPhoto}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
            <if test="createTime != null ">and createtime = #{createTime}</if>
        </where>
    </select>

    <select id="selectTUserProfileById" parameterType="Integer" resultMap="TUserProfileTEmergentPeopleResult">
        select a.id,
               a.name,
               a.gender,
               a.sfzid,
               a.tel_one,
               a.tel_two,
               a.tel_three,
               a.birthday,
               a.age,
               a.marr_status,
               a.national,
               a.local,
               a.client_type,
               a.live_status,
               a.family_address,
               a.detail_address,
               a.house_address,
               a.house_detail_address,
               a.serviceid,
               a.oldmen_status,
               a.change_content,
               a.lose_month,
               a.treat_month,
               a.is_treat,
               a.service_type,
               a.treate_sure,
               a.money_come,
               a.money_month,
               a.card_code,
               a.slow_illness,
               a.blood_type,
               a.shi_status,
               a.disablity_status,
               a.disablity_entry,
               a.educa_level,
               a.special,
               a.title,
               a.unity,
               a.nature_heart,
               a.eat_point,
               a.speciality,
               a.hobby,
               a.photo,
               a.other_photo,
               a.remarks,
               a.createtime,
               b.id         as sub_id,
               b.profile_id as sub_profile_id,
               b.name       as sub_name,
               b.telno      as sub_telno,
               b.relation   as sub_relation,
               b.is_conn    as sub_is_conn
        from t_user_profile a
                 left join t_emergent_people b on b.profile_id = a.id
        where a.id = #{id}
    </select>
    <select id="selectTUserProfileListByDevice" resultType="com.ruoyi.system.domain.dto.TUserProfileDto">
        select id, name, gender, sfzid, tel_one, age, family_address from t_user_profile
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="gender != null ">and gender = #{gender}</if>
            <if test="sfzid != null  and sfzid != ''">and sfzid = #{sfzid}</if>
            <if test="telOne != null  and telOne != ''">and tel_one = #{telOne}</if>
            <if test="age != null ">and age = #{age}</if>
            <if test="familyAddress != null  and familyAddress != ''">and family_address = #{familyAddress}</if>
        </where>
    </select>
    <select id="selectTUserProfileBySfzId" resultType="com.ruoyi.system.domain.TUserProfile">
        select *
        from t_user_profile
        where sfzid = #{sfzid}
    </select>
    <select id="selectTUserProfileBySfzIdV2" resultType="com.ruoyi.system.domain.TUserProfile">
        <include refid="selectTUserProfileVo"/>
        where sfzid = #{sfzid}
    </select>
    <select id="selectuserAgeDto" resultType="com.ruoyi.system.domain.dto.UserAgeDto">
        SELECT CASE
                   WHEN age &lt; 20
                       THEN
                       '10'
                   WHEN age &gt;= 20
                       AND age &lt; 30 THEN
                       '20'
                   WHEN age &gt;= 30
                       AND age &lt; 40 THEN
                       '30'
                   WHEN age &gt;= 40
                       AND age &lt; 50 THEN
                       '40'
                   WHEN age &gt;= 50
                       AND age &lt; 60 THEN
                       '50'
                   WHEN age &gt;= 60
                       AND age &lt; 70 THEN
                       '60'
                   WHEN age &gt;= 70
                       AND age &lt; 80 THEN
                       '70'
                   WHEN age &gt;= 80
                       AND age &lt; 90 THEN
                       '80'
                   WHEN age &gt; 90 THEN
                       '100以上'
                   END AS age,
               count(*) AS count
        FROM
            t_user_profile
        GROUP BY
            CASE
            WHEN age &lt; 20
            THEN
            '10'
            WHEN age &gt;= 20
            AND age &lt; 30 THEN
            '20'
            WHEN age &gt;= 30
            AND age &lt; 40 THEN
            '30'
            WHEN age &gt;= 40
            AND age &lt; 50 THEN
            '40'
            WHEN age &gt;= 50
            AND age &lt; 60 THEN
            '50'
            WHEN age &gt;= 60
            AND age &lt; 70 THEN
            '60'
            WHEN age &gt;= 70
            AND age &lt; 80 THEN
            '70'
            WHEN age &gt;= 80
            AND age &lt; 90 THEN
            '80'
            WHEN age &gt; 90 THEN
            '100以上'
        END;
    </select>
    <select id="selectuserAgeCount" resultType="java.lang.Integer">
        select IFNULL(count(*), 0)
        from t_user_profile
    </select>
    <select id="selectAddUser" resultType="com.ruoyi.system.domain.dto.AddUserDto">
        SELECT DATE_FORMAT(createtime, '%Y-%m') AS month, COUNT(*) AS count
        FROM t_user_profile
        GROUP BY month;
    </select>

    <insert id="insertTUserProfile" parameterType="TUserProfile" useGeneratedKeys="true" keyProperty="id">
        insert into t_user_profile
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="gender != null">gender,</if>
            <if test="sfzid != null">sfzid,</if>
            <if test="telOne != null">tel_one,</if>
            <if test="telTwo != null">tel_two,</if>
            <if test="telThree != null">tel_three,</if>
            <if test="birthday != null">birthday,</if>
            <if test="age != null">age,</if>
            <if test="marrStatus != null">marr_status,</if>
            <if test="national != null">national,</if>
            <if test="local != null">local,</if>
            <if test="clientType != null">client_type,</if>
            <if test="liveStatus != null">live_status,</if>
            <if test="familyAddress != null">family_address,</if>
            <if test="detailAddress != null">detail_address,</if>
            <if test="houseAddress != null">house_address,</if>
            <if test="houseDetailAddress != null">house_detail_address,</if>
            <if test="serviceid != null">serviceid,</if>
            <if test="oldmenStatus != null">oldmen_status,</if>
            <if test="changeContent != null">change_content,</if>
            <if test="loseMonth != null">lose_month,</if>
            <if test="treatMonth != null">treat_month,</if>
            <if test="isTreat != null">is_treat,</if>
            <if test="serviceType != null">service_type,</if>
            <if test="treateSure != null">treate_sure,</if>
            <if test="moneyCome != null">money_come,</if>
            <if test="moneyMonth != null">money_month,</if>
            <if test="cardCode != null">card_code,</if>
            <if test="slowIllness != null">slow_illness,</if>
            <if test="bloodType != null">blood_type,</if>
            <if test="shiStatus != null">shi_status,</if>
            <if test="disablityStatus != null">disablity_status,</if>
            <if test="disablityEntry != null">disablity_entry,</if>
            <if test="educaLevel != null">educa_level,</if>
            <if test="special != null">special,</if>
            <if test="title != null">title,</if>
            <if test="unity != null">unity,</if>
            <if test="natureHeart != null">nature_heart,</if>
            <if test="eatPoint != null">eat_point,</if>
            <if test="speciality != null">speciality,</if>
            <if test="hobby != null">hobby,</if>
            <if test="photo != null">photo,</if>
            <if test="otherPhoto != null">other_photo,</if>
            <if test="remarks != null">remarks,</if>
            <if test="createTime != null">createtime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="gender != null">#{gender},</if>
            <if test="sfzid != null">#{sfzid},</if>
            <if test="telOne != null">#{telOne},</if>
            <if test="telTwo != null">#{telTwo},</if>
            <if test="telThree != null">#{telThree},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="age != null">#{age},</if>
            <if test="marrStatus != null">#{marrStatus},</if>
            <if test="national != null">#{national},</if>
            <if test="local != null">#{local},</if>
            <if test="clientType != null">#{clientType},</if>
            <if test="liveStatus != null">#{liveStatus},</if>
            <if test="familyAddress != null">#{familyAddress},</if>
            <if test="detailAddress != null">#{detailAddress},</if>
            <if test="houseAddress != null">#{houseAddress},</if>
            <if test="houseDetailAddress != null">#{houseDetailAddress},</if>
            <if test="serviceid != null">#{serviceid},</if>
            <if test="oldmenStatus != null">#{oldmenStatus},</if>
            <if test="changeContent != null">#{changeContent},</if>
            <if test="loseMonth != null">#{loseMonth},</if>
            <if test="treatMonth != null">#{treatMonth},</if>
            <if test="isTreat != null">#{isTreat},</if>
            <if test="serviceType != null">#{serviceType},</if>
            <if test="treateSure != null">#{treateSure},</if>
            <if test="moneyCome != null">#{moneyCome},</if>
            <if test="moneyMonth != null">#{moneyMonth},</if>
            <if test="cardCode != null">#{cardCode},</if>
            <if test="slowIllness != null">#{slowIllness},</if>
            <if test="bloodType != null">#{bloodType},</if>
            <if test="shiStatus != null">#{shiStatus},</if>
            <if test="disablityStatus != null">#{disablityStatus},</if>
            <if test="disablityEntry != null">#{disablityEntry},</if>
            <if test="educaLevel != null">#{educaLevel},</if>
            <if test="special != null">#{special},</if>
            <if test="title != null">#{title},</if>
            <if test="unity != null">#{unity},</if>
            <if test="natureHeart != null">#{natureHeart},</if>
            <if test="eatPoint != null">#{eatPoint},</if>
            <if test="speciality != null">#{speciality},</if>
            <if test="hobby != null">#{hobby},</if>
            <if test="photo != null">#{photo},</if>
            <if test="otherPhoto != null">#{otherPhoto},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateTUserProfile" parameterType="TUserProfile">
        update t_user_profile
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="sfzid != null">sfzid = #{sfzid},</if>
            <if test="telOne != null">tel_one = #{telOne},</if>
            <if test="telTwo != null">tel_two = #{telTwo},</if>
            <if test="telThree != null">tel_three = #{telThree},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="age != null">age = #{age},</if>
            <if test="marrStatus != null">marr_status = #{marrStatus},</if>
            <if test="national != null">national = #{national},</if>
            <if test="local != null">local = #{local},</if>
            <if test="clientType != null">client_type = #{clientType},</if>
            <if test="liveStatus != null">live_status = #{liveStatus},</if>
            <if test="familyAddress != null">family_address = #{familyAddress},</if>
            <if test="detailAddress != null">detail_address = #{detailAddress},</if>
            <if test="houseAddress != null">house_address = #{houseAddress},</if>
            <if test="houseDetailAddress != null">house_detail_address = #{houseDetailAddress},</if>
            <if test="serviceid != null">serviceid = #{serviceid},</if>
            <if test="oldmenStatus != null">oldmen_status = #{oldmenStatus},</if>
            <if test="changeContent != null">change_content = #{changeContent},</if>
            <if test="loseMonth != null">lose_month = #{loseMonth},</if>
            <if test="treatMonth != null">treat_month = #{treatMonth},</if>
            <if test="isTreat != null">is_treat = #{isTreat},</if>
            <if test="serviceType != null">service_type = #{serviceType},</if>
            <if test="treateSure != null">treate_sure = #{treateSure},</if>
            <if test="moneyCome != null">money_come = #{moneyCome},</if>
            <if test="moneyMonth != null">money_month = #{moneyMonth},</if>
            <if test="cardCode != null">card_code = #{cardCode},</if>
            <if test="slowIllness != null">slow_illness = #{slowIllness},</if>
            <if test="bloodType != null">blood_type = #{bloodType},</if>
            <if test="shiStatus != null">shi_status = #{shiStatus},</if>
            <if test="disablityStatus != null">disablity_status = #{disablityStatus},</if>
            <if test="disablityEntry != null">disablity_entry = #{disablityEntry},</if>
            <if test="educaLevel != null">educa_level = #{educaLevel},</if>
            <if test="special != null">special = #{special},</if>
            <if test="title != null">title = #{title},</if>
            <if test="unity != null">unity = #{unity},</if>
            <if test="natureHeart != null">nature_heart = #{natureHeart},</if>
            <if test="eatPoint != null">eat_point = #{eatPoint},</if>
            <if test="speciality != null">speciality = #{speciality},</if>
            <if test="hobby != null">hobby = #{hobby},</if>
            <if test="photo != null">photo = #{photo},</if>
            <if test="otherPhoto != null">other_photo = #{otherPhoto},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="createTime != null">createtime = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTUserProfileById" parameterType="Integer">
        delete
        from t_user_profile
        where id = #{id}
    </delete>

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

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

    <delete id="deleteTEmergentPeopleByProfileId" parameterType="Integer">
        delete
        from t_emergent_people
        where profile_id = #{profileId}
    </delete>

    <insert id="batchTEmergentPeople">
        insert into t_emergent_people( id, profile_id, name, telno, relation, is_conn) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.profileId}, #{item.name}, #{item.telno}, #{item.relation}, #{item.isConn})
        </foreach>
    </insert>
</mapper>