<?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.ruoyi.ibs.tabs.mapper.CustTabListInfoMapper">
    
    <resultMap type="CustTabListInfo" id="CustTabListInfoResult">
        <result property="id"    column="id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="tabName"    column="tab_name"    />
        <result property="belongOrg3"    column="belong_org3"    />
        <result property="tabEnum"    column="tab_enum"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="remark"    column="remark"    />
        <result property="custType" column="cust_type" />
        <result property="computType" column="comput_type" />
        <result property="endDate" column="end_date" />
        <result property="authType" column="auth_type" />
        <result property="authUser" column="auth_user" />
        <result property="createDept" column="create_dept" />
        <result property="updateName" column="update_name" />
        <result property="treeList" column="tree_list" />
        <result property="tabId" column="tab_id"/>
        <result property="isStart" column="is_start"/>
    </resultMap>

    <sql id="selectCustTabListInfoVo">
        select ctli.id, ctli.parent_id, ctli.tab_name, ctli.belong_org3, ctli.tab_enum, ctli.create_time,
               ctli.update_by, ctli.update_time, ctli.create_by, ctli.remark, ctli.cust_type, ctli.comput_type,
               ctli.end_date, ctli.auth_type, ctli.auth_user,ctli.tab_id,ctli.is_start
               from cust_tab_list_info ctli
    </sql>

    <select id="selectTabPotListInfoByTabType" parameterType="CustTabListInfo" resultType="CustTabGroupVo">
        select     ctli.id,ctli.tab_id, ctli.tab_name,group_concat(ctli2.tab_name SEPARATOR '|') as children
            from cust_tab_list_info ctli
        left join  cust_tab_list_info ctli2 on ctli.id= ctli2.parent_id
        <where>
            <if test="custType != null  and custType != ''"> and ctli.cust_type = #{custType}</if>
            <if test="tabType != null  and tabType != ''"> and ctli.tab_type = #{tabType}</if>
            <if test="loginId != null  and loginId != ''"> and ( ctli.auth_type = '1'  and ctli.create_by = #{loginId}  or ctli.auth_type = '3' or
                ctli.auth_type = '2'  and ctli.auth_user like concat('%', #{loginId}, '%') )</if>
            and ctli.is_delete = 0
            and ctli.parent_id = #{parentId}
            and ctli.tab_enum = '0'
        </where>
        group by ctli.id,ctli.tab_name,ctli.tab_id
    </select>



    <select id="selectCustTabListInfoList" parameterType="CustTabListInfo" resultMap="CustTabListInfoResult">
        select ctli.id, ctli.parent_id, ctli.tab_name, ctli.belong_org3, ctli.tab_enum, ctli.create_time,
        ctli.update_by, ctli.update_time, ctli.create_by, ctli.remark, ctli.cust_type, ctli.comput_type,
        ctli.end_date, ctli.auth_type, ctli.auth_user, concat_ws('-',su.nick_name,su.user_name) as update_name,ctli.pot_sql,ctli.tree_list,ctli.tab_id,ctli.is_start
        from cust_tab_list_info ctli
        left join sys_user su on su.user_name = ctli.create_by
        <where>
            ctli.is_delete = 0
            <if test="parentId != null and parentId != ''"> and ctli.parent_id = #{parentId}</if>
            <if test="tabName != null  and tabName != ''"> and ctli.tab_name like concat('%', #{tabName}, '%')</if>
            <if test="tabNameBy != null  and tabNameBy != ''"> and ctli.tab_name= #{tabNameBy} </if>
            <if test="belongOrg3 != null  and belongOrg3 != ''"> and ctli.belong_org3 = #{belongOrg3}</if>
            <if test="custType != null  and custType != ''"> and ctli.cust_type = #{custType}</if>
            <if test="computType != null  and computType != ''"> and ctli.comput_type = #{computType}</if>
            <if test="authType != null  and authType != ''"> and ctli.auth_type = #{authType}</if>
            <if test="tabEnum != null  and tabEnum != ''"> and ctli.tab_enum = #{tabEnum}</if>
            <if test="endDate != null  and endDate != ''"> and ctli.end_date  &lt;= #{endDate}</if>
            <if test="updateName != null  and updateName != ''"> and su.nick_name like concat('%', #{updateName}, '%')</if>
            <if test="tabId != null  and tabId != ''"> and ctli.tab_id  = #{tabId}</if>
            <if test="id != null and id !=''"> and ctli.id = #{id}</if>
            <if test="tabThresholdType!=null and tabThresholdType!='' " > and ctli.tab_threshold_type = #{tabThresholdType}</if>
            <if test="loginId != null  and loginId != ''">
                and (
                ctli.auth_type = '1'  and ctli.create_by = #{loginId}
                or ctli.auth_type = '3' and  left(ctli.create_by,3) =  left(#{loginId},3)
                or ctli.auth_type = '2'  and ctli.auth_user like concat('%', #{loginId}, '%')
                or ctli.create_by = #{loginId}
                or ctli.update_by = #{loginId} )</if>
        </where>
<!--        ${params.dataScope}-->
    </select>
    
    <select id="selectCustTabListInfoById" parameterType="Long" resultMap="CustTabListInfoResult">
        <include refid="selectCustTabListInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectCustTabListInfoHis" resultType="com.ruoyi.ibs.tabs.domain.vo.CustTabEditHisVo">
       select ctli.create_time as update_time,concat_ws('-',su.nick_name,su.user_name)  as user_name, sr.role_name
       from (
        select  create_by, create_time
            from cust_tab_list_info
                where  tab_id = #{tabId}  and belong_org3 = #{belongOrg3}
                    group by create_by, create_time ) ctli
        left join sys_user su on su.user_name = ctli.create_by
        left join sys_user_role ssr on su.user_id = ssr.user_id
        left join sys_role sr on ssr.role_id = sr.role_id
    </select>


    <insert id="insertCustTabListInfo" parameterType="CustTabListInfo" useGeneratedKeys="true" keyProperty="id">
        insert into cust_tab_list_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="tabName != null">tab_name,</if>
            <if test="tabId != null">tab_id,</if>
            <if test="belongOrg3 != null">belong_org3,</if>
            <if test="tabEnum != null">tab_enum,</if>
            <if test="custType != null">cust_type,</if>
            <if test="computType != null">comput_type,</if>
            <if test="endDate != null">end_date,</if>
            <if test="authType != null">auth_type,</if>
            <if test="authUser != null">auth_user,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="potRelation !=null">pot_relation,</if>
            <if test="createDept != null">create_dept,</if>
            <if test="remark !=null">remark,</if>
            <if test="potSql !=null " > pot_sql,</if>
            <if test="treeList !=null " > tree_list,</if>
            <if test="tabThresholdType !=null " > tab_threshold_type,</if>
            update_time,
            create_time,
            <if test="createBy != null">create_by,</if>
            is_delete,
            is_start,

         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="tabName != null">#{tabName},</if>
            <if test="tabId != null">#{tabId},</if>
            <if test="tabType != null">#{tabType},</if>
            <if test="belongOrg3 != null">#{belongOrg3},</if>
            <if test="tabEnum != null">#{tabEnum},</if>
            <if test="custType != null">#{custType},</if>
            <if test="computType != null">#{computType},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="authType != null">#{authType},</if>
            <if test="authUser != null">#{authUser},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="potRelation !=null">#{potRelation},</if>
            <if test="createDept != null">#{createDept},</if>
            <if test="remark !=null">#{remark},</if>
            <if test="potSql !=null " >#{potSql},</if>
            <if test="treeList !=null " >#{treeList},</if>
            <if test="tabThresholdType!=null" > #{tabThresholdType},</if>
            sysdate(),
            sysdate(),
            <if test="createBy != null">#{createBy},</if>
            '0',
            '0',
         </trim>
    </insert>

    <update id="updateCustTabListInfo" parameterType="CustTabListInfo">
        update cust_tab_list_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="tabName != null">tab_name = #{tabName},</if>
            <if test="belongOrg3 != null">belong_org3 = #{belongOrg3},</if>
            <if test="tabEnum != null">tab_enum = #{tabEnum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isStart != null">is_start = #{isStart},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteCustTabListInfoById" >
        update cust_tab_list_info set is_delete = '1' ,update_time = sysdate(),update_by = #{loginId}
          where id = #{id} or parent_id = #{id}
    </update>

    <delete id="deleteCustTabListInfoByIds" parameterType="String">
        delete from cust_tab_list_info where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>