<?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="cn.superdesk.uniorder.dao.mapper.organisation.SysCliqueMapperEx">


    <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysClique">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into sys_clique
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="pid != null">
                pid,
            </if>
            <if test="higherPid != null">
                higher_pid,
            </if>
            <if test="levels != null">
                levels,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="code != null">
                code,
            </if>
            <if test="enabled != null">
                enabled,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="pid != null">
                #{pid,jdbcType=BIGINT},
            </if>
            <if test="higherPid != null">
                #{higherPid,jdbcType=BIGINT},
            </if>
            <if test="levels != null">
                #{levels,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="code != null">
                #{code,jdbcType=VARCHAR},
            </if>
            <if test="enabled != null">
                #{enabled,jdbcType=TINYINT},
            </if>
        </trim>
    </insert>

    <resultMap id="SysCliqueTreeModelResultMap" type="cn.superdesk.uniorder.dto.vo._function.tree.SysCliqueTreeModel">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="pid" jdbcType="BIGINT" property="pid" />
        <result column="higher_pid" jdbcType="BIGINT" property="higherPid" />
        <result column="levels" jdbcType="INTEGER" property="levels" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="code" jdbcType="VARCHAR" property="code" />
        <result column="enabled" jdbcType="TINYINT" property="enabled" />
    </resultMap>

    <select id="findByTree002" resultMap="SysCliqueTreeModelResultMap">
        SELECT
            sc.id,
            sc.pid,
            sc.higher_pid,
            sc.levels,
            <!--
            sc.name
            -->
            CONCAT(sc.name,'#集团分公司Id(',sc.id,')levels(',sc.levels,')') as name,
            sc.code,
            sc.enabled
        FROM
            sys_clique sc
        <where>
            <!-- 效用;1-有效的;2-无效的;3-不限; -->
            <choose>
                <when test="efficacy != null and efficacy == 1">
                    <!--
                    and sc.efficacy = #{efficacy,jdbcType=TINYINT}
                    -->
                    and sc.enabled = 1
                </when>
                <when test="efficacy != null and efficacy == 2">
                    and sc.enabled = 0
                </when>
                <when test="efficacy != null and efficacy == 3">

                </when>
                <otherwise>

                </otherwise>
            </choose>
        </where>
        ORDER BY sc.levels ASC ,sc.pid ASC,sc.id ASC
    </select>

    <select id="findByTree003" resultMap="SysCliqueTreeModelResultMap">


        SELECT
            sco.org_id AS id,
            sco.clique_id AS pid,
            sco.higher_pid,
            5 AS levels,
            <!--
            so.organization_name as name,
            -->
            CONCAT(so.organization_name,'#项目(',sco.org_id,')levels(',5,')') AS name,
            so.organization_no AS code,
            CASE so.deleted WHEN 1 THEN 0 WHEN 0 THEN 1 END as enabled
        FROM
            sys_clique_org sco
            LEFT JOIN sys_organization so ON sco.org_id = so.organization_id
        <where>
            <!-- 效用;1-有效的;2-无效的;3-不限; -->
            <choose>
                <when test="efficacy != null and efficacy == 1">
                    and so.deleted = 0
                </when>
                <when test="efficacy != null and efficacy == 2">
                    and so.deleted = 1
                </when>
                <when test="efficacy != null and efficacy == 3">

                </when>
                <otherwise>

                </otherwise>
            </choose>
        </where>
        ORDER BY sco.clique_id ASC,sco.org_id ASC
    </select>

    <select id="findByTree004" resultMap="SysCliqueTreeModelResultMap">

        SELECT
            se.enterprise_id AS id,
            sco.org_id AS pid,
            sco.higher_pid,
            6 AS levels,
            <!--
            se.enterprise_name AS name,
            -->
            CONCAT(se.enterprise_name,'#企业Id(',se.enterprise_id,')levels(',6,')') AS name,
            se.enterprise_no AS code,
            se.enabled
        FROM
            sys_clique_org sco
            LEFT JOIN sys_enterprise se ON sco.org_id = se.organization_id
        <where>
            <!-- 效用;1-有效的;2-无效的;3-不限; -->
            <choose>
                <when test="efficacy != null and efficacy == 1">

                    and se.enabled = 1
                    and se.deleted = 0
                </when>
                <when test="efficacy != null and efficacy == 2">
                    and se.enabled = 0
                    and se.deleted = 1
                </when>
                <when test="efficacy != null and efficacy == 3">

                </when>
                <otherwise>

                </otherwise>
            </choose>
        </where>
        ORDER BY sco.org_id ASC,se.enterprise_id ASC
    </select>

    <!-- 有问题 三个表的id没办法 -->
    <select id="findByTree" resultMap="SysCliqueTreeModelResultMap">
        SELECT * FROM (

            SELECT
                sc.id,
                sc.pid,
                sc.higher_pid,
                sc.levels,
                <!--
                sc.name
                -->
                CONCAT(sc.name,'#集团分公司Id(',sc.id,')levels(',sc.levels,')') as name,
                sc.code,
                sc.enabled
            FROM
                sys_clique sc
            WHERE
                sc.enabled = 1

            UNION

            SELECT
                sco.org_id AS id,
                sco.clique_id AS pid,
                sco.higher_pid,
                5 AS levels,
                <!--
                so.organization_name as name,
                -->
                CONCAT(so.organization_name,'#项目(',sco.org_id,')levels(',5,')') AS name,
                so.organization_no AS code,
                CASE so.deleted WHEN 1 THEN 0 WHEN 0 THEN 1 END as enabled
            FROM
                sys_clique_org sco
                LEFT JOIN sys_organization so ON sco.org_id = so.organization_id
            WHERE
                so.deleted = 0

            UNION

            SELECT
                se.enterprise_id AS id,
                sco.org_id AS pid,
                sco.higher_pid,
                6 AS levels,
                <!--
                se.enterprise_name AS name,
                -->
                CONCAT(se.enterprise_name,'#企业Id(',se.enterprise_id,')levels(',6,')') AS name,
                se.enterprise_no AS code,
                se.enabled
            FROM
                sys_clique_org sco
                LEFT JOIN sys_enterprise se ON sco.org_id = se.organization_id
            WHERE
                se.enabled =1
                and se.deleted=0

    ) as u

    ORDER BY u.levels ASC ,u.pid ASC,u.id ASC
    </select>
</mapper>