<?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.yeejoin.amos.boot.module.tzs.flc.api.mapper.UnitPersonMapper">


    <select id="selectPersonListCount" resultType="Integer">
          select count(1) from (
            SELECT
            DISTINCT
        u.sequence_nbr AS sequenceNbr,
            u.*,
            replace(CONCAT(u.org_expand_attr6->'$.province',u.org_expand_attr6->'$.city',u.org_expand_attr6->'$.district',u.org_expand_attr6->'$.address'),'"','') fulladdress ,
            g.*,
            p.biz_org_name as parentName,
			TIMESTAMPDIFF(YEAR, g.birthday, CURDATE()) as ages ,
            case
            when  count(q.sequence_nbr) > 0 then '有'
            else '无' end hasLicence
            FROM
            cb_org_usr u
            LEFT JOIN
            (  SELECT
            v.`instance_id`,
            max(case v.field_code when 'person_type' then IFNULL(v.field_value_label,v.field_value) end) person_type,
            max(case v.field_code when 'sex' then IFNULL(v.field_value_label,v.field_value) end) sex,
            max(case v.field_code when 'birthday' then IFNULL(v.field_value_label,v.field_value) end) birthday,
            max(case v.field_code when 'licence_type' then IFNULL(v.field_value_label,v.field_value) end) licence_type,
            max(case v.field_code when 'licence_code' then IFNULL(v.field_value_label,v.field_value) end) licence_code,
            max(case v.field_code when 'education' then IFNULL(v.field_value_label,v.field_value) end) education,
            max(case v.field_code when 'major' then IFNULL(v.field_value_label,v.field_value) end) major,
            max(case v.field_code when 'phone' then IFNULL(v.field_value_label,v.field_value) end) phone,
            max(case v.field_code when 'email' then IFNULL(v.field_value_label,v.field_value) end) email
            FROM
            `cb_dynamic_form_instance` v
            WHERE
            v.group_code = 1106
            GROUP BY
            v.`instance_id`
            ) g
            on u.sequence_nbr = g.instance_id
            LEFT JOIN tz_flc_person_quality q on q.pserson_id = u.sequence_nbr
            LEFT JOIN cb_org_usr p on p.sequence_nbr = u.parent_id
            where
            u.biz_org_type = 'person'
            AND
            u.is_delete = 0
            <if test="map.parentId != null">
                AND u.parent_id in
                <foreach collection="map.parentId" item="item" open="(" close=")" separator=","> ${item} </foreach>
            </if>
            GROUP BY
            u.sequence_nbr ,
            u.biz_org_name ,
            u.biz_org_code
            order by u.rec_date desc
            )a where a.sequence_nbr is not null
            <if test="map.ages != null">
                <choose>
                    <when test="map.ages == 0">
                    AND a.ages  <![CDATA[ >= ]]> 60
                    </when>
                    <when test="map.ages == 1">
                        AND a.ages  <![CDATA[ <= ]]> 59 and  a.ages <![CDATA[ >= ]]> 50
                    </when>
                    <when test="map.ages == 2">
                        AND a.ages  <![CDATA[ <= ]]> 49 and  a.ages <![CDATA[ >= ]]> 40
                    </when>
                    <when test="map.ages == 3">
                        AND a.ages  <![CDATA[ <= ]]> 39 and  a.ages <![CDATA[ >= ]]> 30
                    </when>
                    <when test="map.ages == 4">
                        AND a.ages  <![CDATA[ <= ]]> 29 and  a.ages <![CDATA[ >= ]]> 20
                    </when>
                    <when test="map.ages == 5">
                        AND a.ages <![CDATA[ < ]]> 20
                    </when>
                </choose>
            </if>
            <if test="map.sql != null">
            <foreach collection="map.sql" item="item"> ${item} </foreach>
            </if>

            </select>


    <select id="selectPersonList" resultType="Map">
            select * from (
        SELECT
        DISTINCT
        u.sequence_nbr AS sequenceNbr,
        u.*,
        replace(CONCAT(u.org_expand_attr6->'$.province',u.org_expand_attr6->'$.city',u.org_expand_attr6->'$.district',u.org_expand_attr6->'$.address'),'"','') fulladdress ,
        g.*,
        p.biz_org_name as parentName,
        TIMESTAMPDIFF(YEAR, g.birthday, CURDATE()) as ages ,
        case
        when  count(q.sequence_nbr) > 0 then '有'
        else '无' end hasLicence
        FROM
        cb_org_usr u
        LEFT JOIN
        (  SELECT
        v.`instance_id`,
        max(case v.field_code when 'person_type' then IFNULL(v.field_value_label,v.field_value) end) person_type,
        max(case v.field_code when 'sex' then IFNULL(v.field_value_label,v.field_value) end) sex,
        max(case v.field_code when 'birthday' then IFNULL(v.field_value_label,v.field_value) end) birthday,
        max(case v.field_code when 'licence_type' then IFNULL(v.field_value_label,v.field_value) end) licence_type,
        max(case v.field_code when 'licence_code' then IFNULL(v.field_value_label,v.field_value) end) licence_code,
        max(case v.field_code when 'education' then IFNULL(v.field_value_label,v.field_value) end) education,
        max(case v.field_code when 'major' then IFNULL(v.field_value_label,v.field_value) end) major,
        max(case v.field_code when 'phone' then IFNULL(v.field_value_label,v.field_value) end) phone,
        max(case v.field_code when 'email' then IFNULL(v.field_value_label,v.field_value) end) email
        FROM
        `cb_dynamic_form_instance` v
        WHERE
        v.group_code = 1106
        GROUP BY
        v.`instance_id`
        ) g
        on u.sequence_nbr = g.instance_id
        LEFT JOIN tz_flc_person_quality q on q.pserson_id = u.sequence_nbr
        LEFT JOIN cb_org_usr p on p.sequence_nbr = u.parent_id
        where
        u.biz_org_type = 'person'
        AND
        u.is_delete = 0
        <if test="map.parentId != null">
            AND u.parent_id  in
            <foreach collection="map.parentId" item="item" open="(" close=")" separator=","> ${item} </foreach>
        </if>
        GROUP BY
        u.sequence_nbr ,
        u.biz_org_name ,
        u.biz_org_code
        order by u.rec_date desc
        )a where a.sequence_nbr is not null
        <if test="map.ages != null">
            <choose>
                <when test="map.ages == 0">
                    AND a.ages  <![CDATA[ >= ]]> 60
                </when>
                <when test="map.ages == 1">
                    AND a.ages  <![CDATA[ <= ]]> 59 and  a.ages <![CDATA[ >= ]]> 50
                </when>
                <when test="map.ages == 2">
                    AND a.ages  <![CDATA[ <= ]]> 49 and  a.ages <![CDATA[ >= ]]> 40
                </when>
                <when test="map.ages == 3">
                    AND a.ages  <![CDATA[ <= ]]> 39 and  a.ages <![CDATA[ >= ]]> 30
                </when>
                <when test="map.ages == 4">
                    AND a.ages  <![CDATA[ <= ]]> 29 and  a.ages <![CDATA[ >= ]]> 20
                </when>
                <when test="map.ages == 5">
                    AND a.ages <![CDATA[ < ]]> 20
                </when>
            </choose>
        </if>
        <if test="map.sql != null">
            <foreach collection="map.sql" item="item"> ${item} </foreach>
        </if>
        LIMIT #{map.pageNum}, #{map.pageSize}

    </select>





</mapper>
