<?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.CzrkJzdzMapper">

    <resultMap type="com.boman.domain.CzrkJzdz" id="CzrkJzdzResult">
        <result property="id" column="id"/>
        <result property="czrkId" column="czrk_id"/>
        <result property="idCard" column="id_card"/>
        <result property="userName" column="user_name"/>
        <result property="sort" column="sort"/>
        <result property="deptId" column="dept_id"/>
        <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="townId" column="town_id"/>
        <result property="town" column="town"/>
        <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="gridId" column="grid_id"/>
        <result property="gridName" column="grid_name"/>

        <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="status" column="status"/>
    </resultMap>

    <sql id="selectCzrkJzdzVo">
        select id,
               czrk_id,
               id_card,
               user_name,
               sort,
               dept_id,
               province_id,
               province,
               city_id,
               city,
               region_id,
               region,
               town_id,
               town,
               village_id,
               village,
               villager_group_id,
               villager_group,
               grid_id,
               grid_name,
               now_in,
               create_by,
               create_time,
               update_by,
               update_time,
               status
        from czrk_jzdz
    </sql>

    <select id="selectCzrkJzdzList" resultMap="CzrkJzdzResult">
        <include refid="selectCzrkJzdzVo"/>
        <where>
            <if test="czrkId != null ">and czrk_id = #{czrkId}</if>
            <if test="idCard != null  and idCard != ''">and id_card = #{idCard}</if>
            <if test="userName != null  and userName != ''">and user_name like concat( #{userName}, '%')</if>
            <if test="sort != null ">and sort = #{sort}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="provinceId != null  and provinceId != ''">and province_id = #{provinceId}</if>
            <if test="province != null  and province != ''">and province = #{province}</if>
            <if test="cityId != null  and cityId != ''">and city_id = #{cityId}</if>
            <if test="city != null  and city != ''">and city = #{city}</if>
            <if test="regionId != null  and regionId != ''">and region_id = #{regionId}</if>
            <if test="region != null  and region != ''">and region = #{region}</if>
            <if test="townId != null  and townId != ''">and town_id = #{townId}</if>
            <if test="town != null  and town != ''">and town = #{town}</if>
            <if test="villageId != null  and villageId != ''">and village_id = #{villageId}</if>
            <if test="village != null  and village != ''">and village = #{village}</if>
            <if test="nowIn != null  and nowIn != ''">and now_in = #{nowIn}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="deptIdList != null and deptIdList.size() > 0">
                and dept_id in
                <foreach item="deptId" collection="deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
        order by create_time DESC
    </select>

    <select id="listIdCard" resultType="string">
        select id_card from czrk_jzdz
        <where>
            <if test="czrkId != null ">and czrk_id = #{czrkId}</if>
            <if test="idCard != null  and idCard != ''">and id_card = #{idCard}</if>
            <if test="userName != null  and userName != ''">and user_name like concat( #{userName}, '%')</if>
            <if test="sort != null ">and sort = #{sort}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="provinceId != null  and provinceId != ''">and province_id = #{provinceId}</if>
            <if test="province != null  and province != ''">and province = #{province}</if>
            <if test="cityId != null  and cityId != ''">and city_id = #{cityId}</if>
            <if test="city != null  and city != ''">and city = #{city}</if>
            <if test="regionId != null  and regionId != ''">and region_id = #{regionId}</if>
            <if test="region != null  and region != ''">and region = #{region}</if>
            <if test="townId != null  and townId != ''">and town_id = #{townId}</if>
            <if test="town != null  and town != ''">and town = #{town}</if>
            <if test="villageId != null  and villageId != ''">and village_id = #{villageId}</if>
            <if test="village != null  and village != ''">and village = #{village}</if>
            <if test="nowIn != null  and nowIn != ''">and now_in = #{nowIn}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectCzrkJzdzById" parameterType="Long" resultMap="CzrkJzdzResult">
        <include refid="selectCzrkJzdzVo"/>
        where id = #{id}
    </select>

    <select id="listByIdCard" resultMap="CzrkJzdzResult">
        <include refid="selectCzrkJzdzVo"/>
        where id_card in
        <foreach item="idCard" collection="idCardList" open="(" separator="," close=")">
            #{idCard}
        </foreach>
    </select>

    <select id="listByCzrkIds" resultMap="CzrkJzdzResult">
        <include refid="selectCzrkJzdzVo"/>
        where czrk_id in
        <foreach item="czrkId" collection="czrkIds" open="(" separator="," close=")">
            #{czrkId}
        </foreach>
    </select>

    <select id="listByCzrkId" resultMap="CzrkJzdzResult">
        <include refid="selectCzrkJzdzVo"/>
        where czrk_id = #{czrkId} and status = 'Y'
    </select>

    <insert id="insertCzrkJzdz" useGeneratedKeys="true" keyProperty="id">
        insert into czrk_jzdz
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="czrkId != null">czrk_id,</if>
            <if test="idCard != null">id_card,</if>
            <if test="userName != null">user_name,</if>
            <if test="sort != null">sort,</if>
            <if test="deptId != null">dept_id,</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="villagerGroupId != null">villager_group_id,</if>
            <if test="villagerGroup != null">villager_group,</if>
            <if test="gridId != null">grid_id,</if>
            <if test="gridName != null">grid_name,</if>

            <if test="townId != null">town_id,</if>
            <if test="town != null">town,</if>
            <if test="villageId != null">village_id,</if>
            <if test="village != null">village,</if>
            <if test="nowIn != null">now_in,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="czrkId != null">#{czrkId},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="userName != null">#{userName},</if>
            <if test="sort != null">#{sort},</if>
            <if test="deptId != null">#{deptId},</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="villagerGroupId != null">#{villagerGroupId},</if>
            <if test="villagerGroup != null">#{villagerGroup},</if>
            <if test="gridId != null">#{gridId},</if>
            <if test="gridName != null">#{gridName},</if>

            <if test="townId != null">#{townId},</if>
            <if test="town != null">#{town},</if>
            <if test="villageId != null">#{villageId},</if>
            <if test="village != null">#{village},</if>
            <if test="nowIn != null">#{nowIn},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateCzrkJzdz">
        update czrk_jzdz
        <trim prefix="SET" suffixOverrides=",">
            <if test="czrkId != null">czrk_id = #{czrkId},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</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="townId != null">town_id = #{townId},</if>
            <if test="town != null">town = #{town},</if>
            <if test="villageId != null">village_id = #{villageId},</if>
            <if test="village != null">village = #{village},</if>
            <if test="nowIn != null">now_in = #{nowIn},</if>
            <if test="villagerGroupId != null">villager_group_id = #{villagerGroupId},</if>
            <if test="villagerGroup != ''">villager_group = #{villagerGroup},</if>
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="gridName != null">grid_name = #{gridName},</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 = sysdate(),</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateByIdCard">
        update czrk_jzdz
        <trim prefix="SET" suffixOverrides=",">
            <if test="czrkId != null">czrk_id = #{czrkId},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</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="townId != null">town_id = #{townId},</if>
            <if test="town != null">town = #{town},</if>
            <if test="villageId != null">village_id = #{villageId},</if>
            <if test="village != null">village = #{village},</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 = sysdate(),</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id_card = #{idCard}
    </update>

    <update id="updateByCzrkId">
        update czrk_jzdz
        <trim prefix="SET" suffixOverrides=",">
            <if test="czrkId != null">czrk_id = #{czrkId},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</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="townId != null">town_id = #{townId},</if>
            <if test="town != null">town = #{town},</if>
            <if test="villageId != null">village_id = #{villageId},</if>
            <if test="village != null">village = #{village},</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 = sysdate(),</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where czrk_id = #{czrkId}
    </update>

    <update id="updateHouseholdRegister">
        update czrk_jzdz
        set province_id = #{hzCzrkJzdz.provinceId},
        province = #{hzCzrkJzdz.province},
        city_id = #{hzCzrkJzdz.cityId},
        city = #{hzCzrkJzdz.city},
        region_id = #{hzCzrkJzdz.regionId},
        region = #{hzCzrkJzdz.region},
        town_id = #{hzCzrkJzdz.townId},
        town = #{hzCzrkJzdz.town},
        village_id = #{hzCzrkJzdz.villageId},
        village = #{hzCzrkJzdz.village},
        now_in = #{hzCzrkJzdz.nowIn}
        where id_card in
        <foreach item="idCard" collection="idCardList" open="(" separator="," close=")">
            #{idCard}
        </foreach>
    </update>

    <delete id="deleteCzrkJzdzById" parameterType="Long">
        delete
        from czrk_jzdz
        where id = #{id}
    </delete>

    <delete id="deleteByCzrkId" parameterType="Long">
        delete
        from czrk_jzdz
        where czrk_id = #{czrkId}
    </delete>

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

    <select id="listCzrkIdByTypeAreaIdUserName" resultType="java.lang.Long">
        SELECT jzdz.czrk_id
        FROM czrk
        LEFT JOIN czrk_jzdz jzdz ON czrk.id = jzdz.czrk_id
        where czrk.is_del = 'N'
        <if test="userName != null  and userName != ''">
            and jzdz.user_name like concat( #{userName}, '%')
        </if>
        <choose>
            <when test="type == 1">and jzdz.province_id = #{areaId}</when>
            <when test="type == 2">and jzdz.city_id = #{areaId}</when>
            <when test="type == 3">and jzdz.region_id = #{areaId}</when>
            <when test="type == 4">and jzdz.village_towns_id = #{areaId}</when>
            <when test="type == 5">and jzdz.village_id = #{areaId}</when>
        </choose>
        GROUP BY czrk.id
    </select>

    <select id="selectCzrkJzdzListPage" parameterType="Integer" resultMap="CzrkJzdzResult">
        select *
        from czrk_jzdz
        where village_id = ''
          and now_in != ''  limit #{startNum}
            , #{endNum}
    </select>

    <select id="selectCzrkJzdzListByDeptId" resultMap="CzrkJzdzResult">
        select id, dept_id, create_time, status from czrk_jzdz
        where 1=1
        <if test="deptIdList != null  and deptIdList.size() > 0">
            and dept_id in
            <foreach item="deptId" collection="deptIdList" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
    </select>
    <select id="selectCzrkJzdzListByRegionId" resultMap="CzrkJzdzResult">
        SELECT MAX(ANY_VALUE(id))          as id,
               ANY_VALUE(dept_id)          as dept_id,
               ANY_VALUE(region_id)        as region_id,
               ANY_VALUE(region)           as region,
               ANY_VALUE(town_id)          as town_id,
               ANY_VALUE(town)             as town,
               ANY_VALUE(village_id)       as village_id,
               ANY_VALUE(village)          as village,
               ANY_VALUE(create_time)      as create_time,
               MAX(ANY_VALUE(update_time)) as update_time,
               ANY_VALUE(STATUS)           as STATUS
        FROM czrk_jzdz
        where region_id = #{regionId}
        GROUP BY id_card


    </select>
    <select id="getCzrkJzdzByTime" resultMap="CzrkJzdzResult">
        select ANY_VALUE(id)          as id,
               ANY_VALUE(czrk_id)     as czrk_id,
               ANY_VALUE(id_card)     as id_card,
               ANY_VALUE(user_name)   as user_name,
               ANY_VALUE(sort)        as sort,
               ANY_VALUE(dept_id)     as dept_id,
               ANY_VALUE(province_id) as province_id,
               ANY_VALUE(province)    as province,
               ANY_VALUE(city_id)     as city_id,
               ANY_VALUE(city)        as city,
               ANY_VALUE(region_id)   as region_id,
               ANY_VALUE(region)      as region,
               ANY_VALUE(town_id)     as town_id,
               ANY_VALUE(town)        as town,
               ANY_VALUE(village_id)  as village_id,
               ANY_VALUE(village)     as village,
               ANY_VALUE(now_in)      as now_in,
               ANY_VALUE(create_by)   as create_by,
               ANY_VALUE(create_time) as create_time,
               ANY_VALUE(update_by)   as update_by,
               ANY_VALUE(update_time) as update_time,
               ANY_VALUE(status)      as status
        from czrk_jzdz
        where 1 = 1
          and status = #{status}
          and create_time &gt;= #{todayStartStr}
          and create_time &lt;= #{todayEndStr}
          and region_id = #{regionId}
        group by id_card
    </select>
</mapper>