<?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.jcs.api.mapper.OrganizationMapper">


    <select id="getOrganizationInfo" resultType="java.util.Map">
        SELECT
            ( SELECT COUNT( 1 ) FROM cb_organization_user cou WHERE cou.emergency_team_id = co.sequence_nbr ) AS
            value,
            co.emergency_team_name AS name
        FROM
            cb_organization co
        WHERE co.is_delete = 0
        ORDER BY
            co.sort desc
            LIMIT 5
    </select>
    <select id="getTeamLeader" resultType="Map">
        SELECT cou.member_name AS `value`, cou.telephone AS tel, '当值值长' AS `name` FROM cb_organization_user cou WHERE cou.duty_leader = true LIMIT 1
    </select>
    <select id="getOrganizationList" resultType="java.util.Map">
        SELECT
            ifnull(cou.consultation_nub, 0) as userId,
            ifnull(cou.emergency_team_id, '') as groupId,
            ifnull(co.emergency_team_name, '') as emergencyTeamName,
            ifnull(cou.member_name, '') as memberName ,
            ifnull(cou.post_name, '') as  postName,
            ifnull(cou.telephone, '')as  telephone,
            ifnull(co.duty, '') as duty ,
            ifnull(cou.consultation_nub, '') as consultationNub
        FROM
            cb_organization co
                LEFT JOIN cb_organization_user cou ON co.sequence_nbr = cou.emergency_team_id
            where  co.biz_org_code like concat(#{bizOrgCode}, '%') and cou.is_delete = 0
            ORDER BY co.sort desc ,cou.sort, cou.rec_date
    </select>
    <select id="getOrganizationGroup" resultType="java.util.Map">
        SELECT
            co.sequence_nbr as id,
            co.duty
        FROM
            cb_organization co
                LEFT JOIN cb_organization_user cou ON co.sequence_nbr = cou.emergency_team_id
        where  co.biz_org_code like concat(#{bizOrgCode}, '%')
        GROUP BY co.sequence_nbr

    </select>
    <select id="selectOrganization" resultType="com.yeejoin.amos.boot.module.jcs.api.dto.OrganizationExportDto">
        SELECT
            co.emergency_team_name AS emergencyTeamName,
            co.duty,
            co.group_leader AS groupLeader,
            co.group_leader_tel AS groupLeaderTel,
            co.deputy_group_leader AS deputyGroupLeader,
            co.deputy_group_leader_tel AS deputyGroupLeaderTel
        FROM
            cb_organization co
        WHERE
              co.is_delete = 0
    </select>
    <select id="selectOrganizationUserList" resultType="com.yeejoin.amos.boot.module.jcs.api.dto.OrganizationUserExportDto">
        SELECT
            cou.emergency_team_name AS emergencyTeamName,
            cou.member_name as memberName,
            cou.post_name AS postName,
            cou.telephone,
            cou.consultation_nub
        FROM
            cb_organization_user cou
         where  cou.is_delete = 0
    </select>
    <select id="selectMaxSort" resultType="java.lang.Integer">
        SELECT IFNULL( MAX(sort),0) FROM cb_organization
    </select>

    <select id="selectOrganizationUserMaxSort" resultType="java.lang.Integer">
        SELECT IFNULL( MAX(sort),0) FROM cb_organization_user
    </select>

</mapper>
