<?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.CreditUserMapper">
    
    <resultMap type="com.ruoyi.system.domain.CreditUser" id="CreditUserResult">
        <result property="id"    column="id"    />
        <result property="userName"    column="user_name"    />
        <result property="creditScore"    column="credit_score"    />
        <result property="creditLevel"    column="credit_level"    />
        <result property="gender"    column="gender"    />
        <result property="age"    column="age"    />
        <result property="idCard"    column="id_card"    />
        <result property="phoneNum"    column="phone_num"    />
        <result property="provinceId"    column="province_id"    />
        <result property="province"    column="province"    />
        <result property="cityId"    column="city_id"    />
        <result property="city"    column="city"    />
        <result property="regionId"    column="region_id"    />
        <result property="region"    column="region"    />
        <result property="villageTownsId"    column="village_towns_id"    />
        <result property="villageTowns"    column="village_towns"    />
        <result property="villageId"    column="village_id"    />
        <result property="village"    column="village"    />
        <result property="villagerGroupId"    column="villager_group_id"    />
        <result property="villagerGroup"    column="villager_group"    />
        <result property="nowIn"    column="now_in"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="deleteReason"    column="delete_reason"    />
    </resultMap>

    <sql id="selectCreditUserVo">
        select id, user_name, credit_score, credit_level, gender, age, id_card, phone_num, province_id, province, city_id, city, region_id, region, village_towns_id, village_towns, village_id, village, villager_group_id, villager_group, now_in, create_by, create_time, update_by, update_time, is_del, delete_reason from credit_user
    </sql>

    <select id="selectCreditUserList" parameterType="CreditUser" resultMap="CreditUserResult">
        <include refid="selectCreditUserVo"/>
        <where>  
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="creditLevel != null  and creditLevel != ''"> and credit_level = #{creditLevel}</if>
            <if test="gender != null  and gender != ''"> and gender = #{gender}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="idCard != null  and idCard != ''"> and id_card like concat( #{idCard}, '%')</if>
            <if test="phoneNum != null  and phoneNum != ''"> and phone_num = #{phoneNum}</if>
        </where>
    </select>
    
    <select id="selectCreditUserById" parameterType="Long" resultMap="CreditUserResult">
        <include refid="selectCreditUserVo"/>
        where id = #{id}
    </select>

    <select id="selectCreditUser" parameterType="CreditUser" resultMap="CreditUserResult">
        select id,user_name,credit_score,credit_level from credit_user
        <where>
            <if test="idCard != null  and idCard != ''"> and id_card = #{idCard}</if>
            <if test="phoneNum != null  and phoneNum != ''"> and phone_num = #{phoneNum}</if>
        </where>
    </select>
    <select id="selectCreditUserByIdCard" resultMap="CreditUserResult">
        <include refid="selectCreditUserVo"/>
        where id_card = #{idCard} and is_del = 'N' limit 1
    </select>
    <select id="getAllCreditUser" resultMap="CreditUserResult">
        <include refid="selectCreditUserVo"/>
        where 1=1
        and DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%s')
        and DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%s')
    </select>

    <insert id="insertCreditUser" parameterType="CreditUser" useGeneratedKeys="true" keyProperty="id">
        insert into credit_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null">user_name,</if>
            <if test="creditScore != null">credit_score,</if>
            <if test="creditLevel != null">credit_level,</if>
            <if test="gender != null">gender,</if>
            <if test="age != null">age,</if>
            <if test="idCard != null">id_card,</if>
            <if test="phoneNum != null">phone_num,</if>
            <if test="provinceId != null">province_id,</if>
            <if test="province != null">province,</if>
            <if test="cityId != null">city_id,</if>
            <if test="city != null">city,</if>
            <if test="regionId != null">region_id,</if>
            <if test="region != null">region,</if>
            <if test="villageTownsId != null">village_towns_id,</if>
            <if test="villageTowns != null">village_towns,</if>
            <if test="villageId != null">village_id,</if>
            <if test="village != null">village,</if>
            <if test="villagerGroupId != null">villager_group_id,</if>
            <if test="villagerGroup != null">villager_group,</if>
            <if test="nowIn != null">now_in,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="isDel != null">is_del,</if>
            <if test="deleteReason != null">delete_reason,</if>
            create_time,update_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null">#{userName},</if>
            <if test="creditScore != null">#{creditScore},</if>
            <if test="creditLevel != null">#{creditLevel},</if>
            <if test="gender != null">#{gender},</if>
            <if test="age != null">#{age},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="phoneNum != null">#{phoneNum},</if>
            <if test="provinceId != null">#{provinceId},</if>
            <if test="province != null">#{province},</if>
            <if test="cityId != null">#{cityId},</if>
            <if test="city != null">#{city},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="region != null">#{region},</if>
            <if test="villageTownsId != null">#{villageTownsId},</if>
            <if test="villageTowns != null">#{villageTowns},</if>
            <if test="villageId != null">#{villageId},</if>
            <if test="village != null">#{village},</if>
            <if test="villagerGroupId != null">#{villagerGroupId},</if>
            <if test="villagerGroup != null">#{villagerGroup},</if>
            <if test="nowIn != null">#{nowIn},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="deleteReason != null">#{deleteReason},</if>
            sysdate(),sysdate()
         </trim>
    </insert>

    <update id="updateCreditUser" parameterType="CreditUser">
        update credit_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null">user_name = #{userName},</if>
            <if test="creditScore != null">credit_score = #{creditScore},</if>
            <if test="creditLevel != null">credit_level = #{creditLevel},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="age != null">age = #{age},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="phoneNum != null">phone_num = #{phoneNum},</if>
            <if test="provinceId != null">province_id = #{provinceId},</if>
            <if test="province != null">province = #{province},</if>
            <if test="cityId != null">city_id = #{cityId},</if>
            <if test="city != null">city = #{city},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="region != null">region = #{region},</if>
            <if test="villageTownsId != null">village_towns_id = #{villageTownsId},</if>
            <if test="villageTowns != null">village_towns = #{villageTowns},</if>
            <if test="villageId != null">village_id = #{villageId},</if>
            <if test="village != null">village = #{village},</if>
            <if test="villagerGroupId != null">villager_group_id = #{villagerGroupId},</if>
            <if test="villagerGroup != null">villager_group = #{villagerGroup},</if>
            <if test="nowIn != null">now_in = #{nowIn},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="deleteReason != null">delete_reason = #{deleteReason},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCreditUserById" parameterType="Long">
        delete from credit_user where id = #{id}
    </delete>

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