<?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.boman.web.core.mapper.FxryMapper">


    <insert id="add">
        INSERT INTO fxry(`id`, `name`, `sex`, `age`, `id_card`, `village_towns_id`, `village_towns`, `village_id`,
                         `village`, `group_id`, `group`, `detail_addr`, `id_card_hide`, `addr_outside_province`,
                         `intended_return_time`, `telephone`, `remark`, `wzhjdz`,
                        `create_time`, `create_by`, `update_time`, `update_by`)
        VALUES (#{id}, #{name}, #{sex}, #{age}, #{idCard}, #{villageTownsId}, #{villageTowns}, #{villageId},
            #{village}, #{groupId}, #{group}, #{detailAddr}, #{idCardHide}, #{addrOutsideProvince},
            #{intendedReturnTime}, #{telephone}, #{remark}, #{wzhjdz},
                SYSDATE(), #{createBy}, SYSDATE(), #{updateBy});
    </insert>

    <update id="edit">
        UPDATE fxry
            <set>
                <if test="name != null and name != ''">name = #{name},</if>
                <if test="sex != null and sex != ''">sex = #{sex},</if>
                <if test="age != null and age != ''">age = #{age},</if>
                <if test="idCard != null and idCard != ''">id_card = #{idCard},</if>
                <if test="villageTownsId != null and villageTownsId != ''">village_towns_id = #{villageTownsId},</if>
                <if test="villageTowns != null and villageTowns != ''">village_towns = #{villageTowns},</if>
                <if test="villageId != null and villageId != ''">village_id = #{villageId},</if>
                <if test="village != null and village != ''">village = #{village},</if>
                <if test="village != null and village != ''">village = #{village},</if>
                <if test="groupId != null and groupId != ''">group_id = #{groupId},</if>
                <if test="group != null and group != ''">`group` = #{group},</if>
                <if test="detailAddr != null and detailAddr != ''">detail_addr = #{detailAddr},</if>
                <if test="wzhjdz != null and wzhjdz != ''">wzhjdz = #{wzhjdz},</if>
                <if test="idCardHide != null and idCardHide != ''">id_card_hide = #{idCardHide},</if>
                <if test="addrOutsideProvince != null and addrOutsideProvince != ''">addr_outside_province = #{addrOutsideProvince},</if>
                <if test="intendedReturnTime != null and intendedReturnTime != ''">intended_return_time = #{intendedReturnTime},</if>
                <if test="intendedReturnTime != null and intendedReturnTime != ''">intended_return_time = #{intendedReturnTime},</if>
                <if test="telephone != null and telephone != ''">telephone = #{telephone},</if>
                <if test="remark != null and remark != ''">remark = #{remark},</if>
                <if test="delete != null and delete != ''">`delete` = #{delete},</if>
                <if test="updateTime != null and updateTime != ''">update_time = SYSDATE()</if>
                <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            </set>
        WHERE `id` = #{id};
    </update>

    <update id="delete">
        UPDATE fxry
        SET `delete`  = '是',
            update_time = SYSDATE(),
            update_by = #{updateBy}
        WHERE `id` = #{id};
    </update>

    <select id="list" resultType="com.boman.domain.Fxry">
        select id,
               `name`,
               `sex`,
               telephone,
               id_card idCardHide,
               wzhjdz,
               addr_outside_province addrOutsideProvince,
               intended_return_time intendedReturnTime,
               village_towns villageTowns
        from fxry
        where `delete` = '否'
        <if test="name != null and name != ''">
          and (`name` like concat('%', #{name}, '%') or `id_card` like concat('%', #{name}, '%'))
        </if>
        <if test="sex != null and sex != ''">and `sex` = #{sex}</if>
        <if test="idCard != null and idCard != ''">and `id_card` like concat('%', #{idCard}, '%')</if>
        <if test="telephone != null and telephone != ''">and telephone = #{telephone}</if>
        order by create_time desc
    </select>

    <select id="getById" resultType="com.boman.domain.Fxry">
        select id,
               `name`,
               sex,
               age,
               id_card idCard,
               village_towns_id villageTownsId,
               village_towns villageTowns,
               village_id villageId,
               village,
               group_id groupId,
               `group`,
               detail_addr detailAddr,
               addr_outside_province addrOutsideProvince,
               intended_return_time intendedReturnTime,
               telephone,
               remark
        from fxry
        where id = #{id} limit 1;
    </select>

    <select id="countByIdCard" resultType="int">
        select count(id)
        from fxry
        where id_card = #{idCard}
    </select>
</mapper>