<?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.szxc.mapper.SzxcJmInfoMapper">

    <resultMap type="SzxcJmInfo" id="SzxcJmInfoResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="name"    column="name"    />
        <result property="oldName"    column="old_name"    />
        <result property="nationality"    column="nationality"    />
        <result property="cardId"    column="card_id"    />
        <result property="bornDate"    column="born_date"    />
        <result property="age"    column="age"    />
        <result property="sex"    column="sex"    />
        <result property="jg"    column="jg"    />
        <result property="relation"    column="relation"    />
        <result property="jgNature"    column="jg_nature"    />
        <result property="longLive"    column="long_live"    />
        <result property="hjAddress"    column="hj_address"    />
        <result property="phone"    column="phone"    />
        <result property="currentAddress"    column="current_address"    />
        <result property="brqk"    column="brqk"    />
        <result property="hy"    column="hy"    />
        <result property="xueli"    column="xueli"    />
        <result property="zzmm"    column="zzmm"    />
        <result property="zjxy"    column="zjxy"    />
        <result property="jkzk"    column="jkzk"    />
        <result property="ills"    column="ills"    />
        <result property="head"    column="head"    />
        <result property="acountNo"    column="acount_no"    />
        <result property="hcStatus"    column="hc_status"    />
        <result property="off"    column="off"    />
        <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="userId"    column="user_id"    />
    </resultMap>

    <sql id="selectSzxcJmInfoVo">
        select id, dept_id, dept_name, name, old_name, nationality, card_id, born_date, age, sex, jg, relation, jg_nature, long_live, hj_address, phone, current_address, brqk, hy, xueli, zzmm, zjxy, jkzk, ills, head, acount_no, hc_status,off, create_by, create_time, update_by, update_time, user_id from szxc_jm_info
    </sql>

    <select id="selectSzxcJmInfoList" parameterType="SzxcJmInfo" resultMap="SzxcJmInfoResult">
        <include refid="selectSzxcJmInfoVo"/>  as j
        <where>
            <!--<if test="deptId != null "> and dept_id = #{deptId}</if>-->
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="oldName != null  and oldName != ''"> and old_name like concat('%', #{oldName}, '%')</if>
            <if test="nationality != null  and nationality != ''"> and nationality = #{nationality}</if>
            <if test="cardId != null  and cardId != ''"> and card_id = #{cardId}</if>
            <if test="bornDate != null "> and born_date = #{bornDate}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="jg != null  and jg != ''"> and jg = #{jg}</if>
            <if test="relation != null  and relation != ''"> and relation = #{relation}</if>
            <if test="jgNature != null  and jgNature != ''"> and jg_nature = #{jgNature}</if>
            <if test="longLive != null  and longLive != ''"> and long_live = #{longLive}</if>
            <if test="hjAddress != null  and hjAddress != ''"> and hj_address = #{hjAddress}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="currentAddress != null  and currentAddress != ''"> and current_address = #{currentAddress}</if>
            <if test="brqk != null  and brqk != ''"> and brqk = #{brqk}</if>
            <if test="hy != null  and hy != ''"> and hy = #{hy}</if>
            <if test="xueli != null  and xueli != ''"> and xueli = #{xueli}</if>
            <if test="zzmm != null  and zzmm != ''"> and zzmm = #{zzmm}</if>
            <if test="zjxy != null  and zjxy != ''"> and zjxy = #{zjxy}</if>
            <if test="jkzk != null  and jkzk != ''"> and jkzk = #{jkzk}</if>
            <if test="ills != null  and ills != ''"> and ills = #{ills}</if>
            <if test="head != null  and head != ''"> and head = #{head}</if>
            <if test="acountNo != null "> and acount_no = #{acountNo}</if>
            <if test="hcStatus != null  and hcStatus != ''"> and hc_status = #{hcStatus}</if>
            <if test="off != null  and off != ''"> and off = #{off}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <!-- <if test="deptId != null "> and dept_id = #{deptId}</if>-->
            <!--<if test="deptId != null and deptId != 0">
                AND (j.dept_id = #{deptId} OR j.dept_id IN ( SELECT s.dept_id FROM sys_dept s WHERE find_in_set(#{deptId}, ancestors) ))
            </if>-->
            <!-- 数据范围过滤 -->
            <if test="params.dataScope != null and params.dataScope !=''">
                ${params.dataScope}
            </if>
        </where>
    </select>

    <select id="selectSzxcJmInfoById" parameterType="Long" resultMap="SzxcJmInfoResult">
        <include refid="selectSzxcJmInfoVo"/>
        where id = #{id}
    </select>

    <select id="getDataBySex"  resultType="java.util.Map">
        SELECT count(*) headCount,
        count(IF(sex=0,TRUE,NULL)) man,
        count(IF(sex=1,TRUE,NULL)) women,
        j.dept_name
        FROM `szxc_jm_info` j
        GROUP BY  dept_id;
    </select>

    <select id="getDataByAge"  resultType="java.util.Map">
        SELECT dept_name,
      <![CDATA[  COUNT(CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 0
        and ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) <= 12 THEN '0-12岁' END) '0-12岁',
        COUNT(CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 13
        and ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) <= 17 THEN '13-17岁' END) '13-17岁',
        COUNT(CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 18
        and ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) <= 22 THEN '18-22岁' END) '18-22岁',
        COUNT( CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 23
        and ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) <= 30 THEN '23-30岁' END) '23-30岁',
        COUNT(CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 31
        and ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) <= 59 THEN '31-59岁' END ) '31-59岁',
        COUNT( CASE WHEN ROUND(DATEDIFF(CURDATE(), born_date)/365.2422) >= 60 THEN '60岁以上' END) '60岁以上' ]]>
        from szxc_jm_info  GROUP BY dept_id
    </select>

    <select id="getDataByTag"  resultType="java.util.Map">
      SELECT dept_name,
		    count(IF(t.tag_name='党员',TRUE,NULL)) '党员',
		    count(IF(t.tag_name='老人',TRUE,NULL)) '老人',
		    count(IF(t.tag_name='重点人群',TRUE,NULL)) '重点人群',
		    count(IF(t.tag_name='退役军人',TRUE,NULL)) '退役军人',
		    count(IF(t.tag_name='退休人员',TRUE,NULL)) '退休人员',
		    count(IF(t.tag_name='残疾人',TRUE,NULL)) '残疾人',
		    count(IF(t.tag_name='计划生育',TRUE,NULL)) '计划生育',
		    count(IF(t.tag_name='劳动保障',TRUE,NULL)) '劳动保障',
		    count(IF(t.tag_name='奖励',TRUE,NULL)) '奖励',
		    count(IF(t.tag_name='统战',TRUE,NULL)) '统战',
		    count(IF(t.tag_name='公益岗',TRUE,NULL)) '公益岗',
		    count(IF(t.tag_name='统战',TRUE,NULL)) '统战',
		    count(IF(t.tag_name='脱享户',TRUE,NULL)) '脱享户',
		    count(IF(t.tag_name='边缘户',TRUE,NULL)) '边缘户',
		    count(IF(t.tag_name='居民保险',TRUE,NULL)) '居民保险',
		    count(IF(t.tag_name='其他',TRUE,NULL)) '其他'
		from szxc_jm_info j
		left join szxc_jm_tag jt on  j.id=jt.jm_id
		left join szxc_person_tag t on  t.id=jt.tag_id
      GROUP BY j.dept_id
    </select>

    <insert id="insertSzxcJmInfo" parameterType="SzxcJmInfo" useGeneratedKeys="true" keyProperty="id">
        insert into szxc_jm_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="name != null">name,</if>
            <if test="oldName != null">old_name,</if>
            <if test="nationality != null">nationality,</if>
            <if test="cardId != null">card_id,</if>
            <if test="bornDate != null">born_date,</if>
            <if test="age != null">age,</if>
            <if test="sex != null">sex,</if>
            <if test="jg != null">jg,</if>
            <if test="relation != null">relation,</if>
            <if test="jgNature != null">jg_nature,</if>
            <if test="longLive != null">long_live,</if>
            <if test="hjAddress != null">hj_address,</if>
            <if test="phone != null">phone,</if>
            <if test="currentAddress != null">current_address,</if>
            <if test="brqk != null">brqk,</if>
            <if test="hy != null">hy,</if>
            <if test="xueli != null">xueli,</if>
            <if test="zzmm != null">zzmm,</if>
            <if test="zjxy != null">zjxy,</if>
            <if test="jkzk != null">jkzk,</if>
            <if test="ills != null">ills,</if>
            <if test="head != null and head != ''">head,</if>
            <if test="acountNo != null">acount_no,</if>
            <if test="hcStatus != null">hc_status,</if>
            <if test="off != null and off != ''">off,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="userId != null">user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="name != null">#{name},</if>
            <if test="oldName != null">#{oldName},</if>
            <if test="nationality != null">#{nationality},</if>
            <if test="cardId != null">#{cardId},</if>
            <if test="bornDate != null">#{bornDate},</if>
            <if test="age != null">#{age},</if>
            <if test="sex != null">#{sex},</if>
            <if test="jg != null">#{jg},</if>
            <if test="relation != null">#{relation},</if>
            <if test="jgNature != null">#{jgNature},</if>
            <if test="longLive != null">#{longLive},</if>
            <if test="hjAddress != null">#{hjAddress},</if>
            <if test="phone != null">#{phone},</if>
            <if test="currentAddress != null">#{currentAddress},</if>
            <if test="brqk != null">#{brqk},</if>
            <if test="hy != null">#{hy},</if>
            <if test="xueli != null">#{xueli},</if>
            <if test="zzmm != null">#{zzmm},</if>
            <if test="zjxy != null">#{zjxy},</if>
            <if test="jkzk != null">#{jkzk},</if>
            <if test="ills != null">#{ills},</if>
            <if test="head != null and head != ''">#{head},</if>
            <if test="acountNo != null">#{acountNo},</if>
            <if test="hcStatus != null">#{hcStatus},</if>
            <if test="off != null and off != ''">#{off},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="userId != null">#{userId},</if>
        </trim>
    </insert>

    <update id="updateSzxcJmInfo" parameterType="SzxcJmInfo">
        update szxc_jm_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="name != null">name = #{name},</if>
            <if test="oldName != null">old_name = #{oldName},</if>
            <if test="nationality != null">nationality = #{nationality},</if>
            <if test="cardId != null">card_id = #{cardId},</if>
            <if test="bornDate != null">born_date = #{bornDate},</if>
            <if test="age != null">age = #{age},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="jg != null">jg = #{jg},</if>
            <if test="relation != null">relation = #{relation},</if>
            <if test="jgNature != null">jg_nature = #{jgNature},</if>
            <if test="longLive != null">long_live = #{longLive},</if>
            <if test="hjAddress != null">hj_address = #{hjAddress},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="currentAddress != null">current_address = #{currentAddress},</if>
            <if test="brqk != null">brqk = #{brqk},</if>
            <if test="hy != null">hy = #{hy},</if>
            <if test="xueli != null">xueli = #{xueli},</if>
            <if test="zzmm != null">zzmm = #{zzmm},</if>
            <if test="zjxy != null">zjxy = #{zjxy},</if>
            <if test="jkzk != null">jkzk = #{jkzk},</if>
            <if test="ills != null">ills = #{ills},</if>
            <if test="head != null and head != ''">head = #{head},</if>
            <if test="acountNo != null">acount_no = #{acountNo},</if>
            <if test="hcStatus != null">hc_status = #{hcStatus},</if>
            <if test="off != null and off != ''">off = #{off},</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="userId != null">user_id = #{userId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSzxcJmInfoById" parameterType="Long">
        delete from szxc_jm_info where id = #{id}
    </delete>

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