<?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.dxhy.admin.sys.dao.SysTabulateDeptDao">

    <select id="getTabluteDept" resultType="com.dxhy.admin.sys.entity.SysTabulateEntity">
        SELECT
        case when tab.type = '2' then tabdept.tabulate_code else tab.tabulate_code end tabulateCode,
        tabdept.tabulate_name,
        tabdept.menu_id,
        tab.id,
        tab.type,
        tab.titlewidth,
        tab.selection,
        tab.show_overflow_tooltip,
        tab.titlefixed,
        tab.index_type,
        tab.default_status,
        tab.input_fixed,
        tab.icon_fixed,
        tabdept.order_num,
        tabdept.show_status,
        tab.sortable

        FROM sys_tabulate tab,
        sys_tabulate_dept tabdept
        where tabdept.tabulate_id = tab.id
        and tab.menu_id = #{menuId}
        <if test="company != null and company != '' and company != 'null'">
            and tabdept.company=#{company}
        </if>
        order by tab.order_num
    </select>


    <select id="getTabluteNotDept" resultType="com.dxhy.admin.sys.entity.SysTabulateEntity">
        SELECT tab.tabulate_code,
               tab.tabulate_name,
               tab.menu_id,
               tab.id,
               tab.type,
               tab.titlewidth,
               tab.selection,
               tab.show_overflow_tooltip,
               tab.titlefixed,
               tab.index_type,
               tab.default_status,
               tab.order_num,
               tab.input_fixed,
               tab.icon_fixed,
               tab.show_status,
               tab.sortable

        FROM sys_tabulate tab
        where tab.menu_id = #{menuId}
          and not exists(select tabulate_id
                         from sys_tabulate_dept dept
                         where dept.company = #{company}
                           and dept.menu_id =
                               #{menuId}
                           and tab.id = dept.tabulate_id)
        order by tab.order_num
    </select>

    <select id="getTabluteDeptpool" resultType="com.dxhy.admin.sys.entity.SysTabulateEntity">
        SELECT tab.company,
               tab.menu_id

        FROM sys_tabulate_dept tab
        where tab.company = #{company}

    </select>

    <select id="getTabluteDeptMneuId" resultType="Long">
        SELECT tab.menu_id

        FROM sys_tabulate_dept tab
        where tab.company = #{company}

        group by menu_id


    </select>

    <select id="getTabluteDeptPoolRefreshCache" resultType="com.dxhy.admin.sys.entity.SysTabulateEntity">
        SELECT
        case when tab.type = '2' then tabdept.tabulate_code else tab.tabulate_code end tabulateCode,
        tabdept.tabulate_name,
        tabdept.menu_id,
        tab.id,
        tab.type,
        tab.titlewidth,
        tab.selection,
        tab.show_overflow_tooltip,
        tab.titlefixed,
        tab.index_type,
        tab.default_status,
        tab.input_fixed,
        tab.icon_fixed,
        tabdept.order_num,
        tabdept.show_status,
        tab.sortable
        FROM sys_tabulate tab,
        sys_tabulate_dept tabdept
        where tabdept.tabulate_id = tab.id
        <if test="company != null and company != '' and company != 'null'">
            and tabdept.company=#{company}
        </if>
        order by tab.order_num
    </select>
    <update id="updateTabulateDept">
        update sys_tabulate_dept
        set tabulate_name = #{tabulateName},
            order_num     = #{orderNum},
            show_status   = #{showStatus},
            tabulate_code = #{tabulateCode}
        where id = #{id}
    </update>

    <select id="queryDeptCompany" parameterType="java.lang.Long" resultType="java.lang.String">
        select company
        from sys_tabulate_dept
        <if test="menuId != null">
            where menu_id = #{menuId}
        </if>
        group by company
    </select>
</mapper>
