<?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.bonus.system.basic.dao.SysOrgDao">

    <select id="selectOrgList" resultType="com.bonus.system.basic.domain.SysOrg">
        select * from (
        select o.*, GROUP_CONCAT(u.user_id) as orgHeadUserId, GROUP_CONCAT(u.user_name) as orgHeadUserName,
        a.user_id as attendanceUserId, a.user_name as attendanceUserName
        from sys_organization o
        left join sys_organization_head h on h.org_id = o.id and h.is_active = 1
        left join sys_user u on u.user_id = h.user_id
        left join sys_user a on a.user_id = o.attendance_user_id
        where o.is_active = 1
        <if test="bean.userId != null and bean.userId != ''">
            AND (u.user_id = #{bean.userId} or a.user_id = #{bean.userId})
        </if>
        GROUP BY o.id
        ) a
        where 1=1
        <if test="bean.orgName != null and bean.orgName != ''">
            AND a.org_name like concat('%', #{bean.orgName}, '%')
        </if>
        <if test="bean.orgHeadUserName != null and bean.orgHeadUserName != ''">
            AND a.orgHeadUserName like concat('%', #{bean.orgHeadUserName}, '%')
        </if>
        <if test='bean.orgList != null and bean.orgList.size() > 0'>
            and a.id in (
            <foreach collection="bean.orgList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>

    </select>

    <select id="selectOrgById" resultType="com.bonus.system.basic.domain.SysOrg">
        select *
        from (select o.*,
                     GROUP_CONCAT(u.user_id)   as orgHeadUserId,
                     GROUP_CONCAT(u.user_name) as orgHeadUserName,
                     a.user_id                 as attendanceUserId,
                     a.user_name               as attendanceUserName
              from sys_organization o
                       left join sys_organization_head h on h.org_id = o.id and h.is_active = 1
                       left join sys_user u on u.user_id = h.user_id
                       left join sys_user a on a.user_id = o.attendance_user_id
              where o.is_active = 1
              GROUP BY o.id) a
        where a.id = #{orgId}
    </select>

    <select id="checkOrgNameUnique" resultType="com.bonus.system.basic.domain.SysOrg">
        select *
        from sys_organization
        where org_name = #{orgName}
          and is_active = 1
          and parent_id = #{parentId} limit 1
    </select>

    <select id="hasChildByOrgId" resultType="java.lang.Integer">
        select count(1)
        from sys_organization
        where parent_id = #{orgId}
          and is_active = 1
    </select>

    <select id="checkOrgExistUser" resultType="java.lang.Integer">
        select count(1)
        from sys_user_org
        where org_id = #{orgId}
          and is_active = 1
    </select>

    <select id="orgPersonSelect" resultType="com.bonus.system.basic.domain.SysTree">
        select id, org_name as name, parent_id, true as disabled
        from sys_organization
        where is_active = 1
        union
        select CONCAT(su.user_id, '|', suo.org_id) as id,
               user_name                           as name,
               suo.org_id                          as parnet_id,
               false                               as disabled
        from sys_user su
                 left join sys_user_org suo on suo.user_id = su.user_id and suo.is_active = 1
        where su.is_active = 1
    </select>

    <select id="selectUserByName" resultType="java.util.Map">
        select DISTINCT su.user_id as userId,
                        user_name  as userName,
                        suo.org_id as orgId
        from sys_user su
                 left join sys_user_org suo on suo.user_id = su.user_id
        where user_name = #{name}
        GROUP BY su.user_id
        ORDER BY suo.create_time
    </select>

    <select id="getParentIdByName" resultType="java.lang.Long">
        select id
        from sys_organization
        where org_name = #{orgName}
          and is_active = '1'
    </select>
    <select id="getOrgList" resultType="com.bonus.system.basic.domain.SysOrgImport">
        SELECT org_name as `orgName`
        FROM sys_organization
        WHERE is_active = '1'
          and org_name is not null
    </select>
    <select id="getPersonList" resultType="com.bonus.system.basic.domain.SysOrgImport">
        SELECT user_name as `orgName`
        FROM sys_user
        WHERE is_active = '1'
          and user_name is not null
    </select>

    <select id="getOrgIdList" resultType="com.bonus.system.api.domain.MapVo">
        SELECT h.org_id as id
        FROM sys_organization_head h
                 LEFT JOIN sys_user u ON u.user_id = h.user_id
        WHERE h.is_active = 1
          AND u.user_id = #{userId}
        UNION
        SELECT o.id as id
        FROM sys_organization o
                 LEFT JOIN sys_user a ON a.user_id = o.attendance_user_id
        WHERE o.is_active = 1
          AND a.user_id = #{userId}
    </select>

    <select id="getOrgIdListAdministrators" resultType="com.bonus.system.api.domain.MapVo">
        SELECT o.id as id
        FROM sys_organization o
                 LEFT JOIN sys_user a ON a.user_id = o.attendance_user_id
        WHERE o.is_active = 1
          AND a.user_id = #{userId}
    </select>

    <select id="getOrgIdListManager" resultType="com.bonus.system.api.domain.MapVo">
        SELECT h.org_id as id
        FROM sys_organization_head h
                 LEFT JOIN sys_user u ON u.user_id = h.user_id
        WHERE h.is_active = 1
          AND u.user_id = #{userId}
    </select>

    <select id="selectOrgListPerson" resultType="com.bonus.system.basic.domain.SysOrg">
        select * from (SELECT o.*, GROUP_CONCAT(u.user_id) as orgHeadUserId, GROUP_CONCAT(u.user_name) as orgHeadUserName,
        a.user_id as attendanceUserId, a.user_name as attendanceUserName FROM (
        SELECT o.*  FROM sys_user u
        LEFT JOIN sys_user_org uo ON u.user_id = uo.user_id
        LEFT JOIN sys_organization o ON o.id =uo.org_id
        WHERE u.user_id = #{bean.userId}
        ) o
        left join sys_organization_head h on h.org_id = o.id and h.is_active = 1
        left join sys_user u on u.user_id = h.user_id
        left join sys_user a on a.user_id = o.attendance_user_id
        where o.is_active = 1
        GROUP BY o.id
        )a
        where 1=1
        <if test="bean.orgName != null and bean.orgName != ''">
            AND a.org_name like concat('%', #{bean.orgName}, '%')
        </if>
        <if test="bean.orgHeadUserName != null and bean.orgHeadUserName != ''">
            AND a.orgHeadUserName like concat('%', #{bean.orgHeadUserName}, '%')
        </if>

    </select>

    <insert id="insertOrg" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into sys_organization(org_name,
        <if test="attendanceUserId != null and attendanceUserId != '' and attendanceUserId != 'null'">
            attendance_user_id,
        </if>
        parent_id, is_province, is_department,
        province, address, lon, lat, reviewer_status, create_user_id)
        values (#{orgName},
        <if test="attendanceUserId != null and attendanceUserId != '' and attendanceUserId != 'null'">
            #{attendanceUserId},
        </if>
        #{parentId}, #{isProvince}, #{isDepartment}, #{province},
        #{address}, #{lon}, #{lat}, 1, #{createUserId})
    </insert>

    <insert id="insertOrgHead">
        insert into sys_organization_head(org_id, user_id)
        values
        <foreach item="params" collection="list" separator=",">
            (#{orgId}, #{params})
        </foreach>
    </insert>

    <update id="updateOrg">
        update sys_organization
        set org_name           = #{orgName},
            attendance_user_id = #{attendanceUserId},
            parent_id          = #{parentId},
            is_province        = #{isProvince},
            is_department      = #{isDepartment},
            province           = #{province},
            address            = #{address},
            lon                = #{lon},
            lat                = #{lat},
            update_user_id     = #{updateUserId}
        where id = #{id}
    </update>

    <delete id="deleteOrgById">
        update sys_organization
        set is_active      = 0,
            update_user_id = #{updateUserId}
        where id = #{orgId};
        update sys_organization_head
        set is_active = 0
        where org_id = #{orgId}
    </delete>

    <delete id="deleteOrgHead">
        update sys_organization_head
        set is_active = 0
        where org_id = #{orgId}
    </delete>
</mapper>