<?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.yifu.cloud.plus.v1.yifu.archives.mapper.TCutsomerDataPermissonMapper">

    <resultMap id="tCutsomerDataPermissonMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TCutsomerDataPermisson">
        <id property="id" column="ID"/>
        <result property="isOwner" column="IS_OWNER"/>
        <result property="contractId" column="CONTRACT_ID"/>
        <result property="customerId" column="CUSTOMER_ID"/>
        <result property="permissonSrc" column="PERMISSON_SRC"/>
        <result property="permissonLevel" column="PERMISSON_LEVEL"/>
        <result property="permissionUser" column="PERMISSION_USER"/>
        <result property="bePermissonUser" column="BE_PERMISSON_USER"/>
        <result property="bePermissionTime" column="BE_PERMISSION_TIME"/>
        <result property="remark" column="REMARK"/>
        <result property="type" column="type"/>
        <result property="settleDomainId" column="SETTLE_DOMAIN_ID"/>
    </resultMap>
    <resultMap id="tCutsomerDataPermissonMapVo" type="com.yifu.cloud.plus.v1.yifu.archives.vo.TCutsomerDataPermissonVo">
        <id property="id" column="ID"/>
        <result property="isOwner" column="IS_OWNER"/>
        <result property="contractId" column="CONTRACT_ID"/>
        <result property="customerId" column="CUSTOMER_ID"/>
        <result property="permissonSrc" column="PERMISSON_SRC"/>
        <result property="permissonLevel" column="PERMISSON_LEVEL"/>
        <result property="permissionUser" column="PERMISSION_USER"/>
        <result property="bePermissonUser" column="BE_PERMISSON_USER"/>
        <result property="bePermissionTime" column="BE_PERMISSION_TIME"/>
        <result property="remark" column="REMARK"/>
        <result property="settleDomainId" column="SETTLE_DOMAIN_ID"/>
        <result property="type" column="type"/>

        <result property="name" column="name"/>
        <result property="permissionUserName" column="nickname"/>
    </resultMap>
    <sql id="base_column_list">
        a.ID,
        a.IS_OWNER,
        a.CONTRACT_ID,
        a.CUSTOMER_ID,
        a.PERMISSON_SRC,
        a.PERMISSON_LEVEL,
        a.PERMISSION_USER,
        a.BE_PERMISSON_USER,
        a.BE_PERMISSION_TIME,
        a.REMARK,
        a.SETTLE_DOMAIN_ID,
        a.type
    </sql>
    <sql id="where_getTCutsomerDataPermissonPage">
            <if test="tCutsomerDataPermisson.bePermissonUser != null and tCutsomerDataPermisson.bePermissonUser.trim() != ''">
                AND a.BE_PERMISSON_USER = #{tCutsomerDataPermisson.bePermissonUser}
            </if>
             <if test="tCutsomerDataPermisson.type != null and tCutsomerDataPermisson.type.trim() != ''">
                AND a.type = #{tCutsomerDataPermisson.type}
            </if>
            <if test="tCutsomerDataPermisson.name != null and tCutsomerDataPermisson.name.trim() != ''">
               AND a.name like '%${tCutsomerDataPermisson.name}%'
            </if>
            <if test="tCutsomerDataPermisson.id != null and tCutsomerDataPermisson.id.trim() != ''">
                AND a.ID = #{tCutsomerDataPermisson.id}
            </if>
            <if test="tCutsomerDataPermisson.isOwner != null and tCutsomerDataPermisson.isOwner.trim() != ''">
                AND a.IS_OWNER = #{tCutsomerDataPermisson.isOwner}
            </if>
            <if test="tCutsomerDataPermisson.permissonSrc != null and tCutsomerDataPermisson.permissonSrc.trim() != ''">
                AND a.PERMISSON_SRC = #{tCutsomerDataPermisson.permissonSrc}
            </if>
            <if test="tCutsomerDataPermisson.permissonLevel != null and tCutsomerDataPermisson.permissonLevel.trim() != ''">
                AND a.PERMISSON_LEVEL = #{tCutsomerDataPermisson.permissonLevel}
            </if>
            <if test="tCutsomerDataPermisson.permissionUser != null and tCutsomerDataPermisson.permissionUser.trim() != ''">
                AND a.PERMISSION_USER = #{tCutsomerDataPermisson.permissionUser}
            </if>
            <if test="tCutsomerDataPermisson.bePermissionTime != null and tCutsomerDataPermisson.bePermissionTime.trim() != ''">
                AND a.BE_PERMISSION_TIME = #{tCutsomerDataPermisson.bePermissionTime}
            </if>
            <if test="tCutsomerDataPermisson.remark != null and tCutsomerDataPermisson.remark.trim() != ''">
                AND a.REMARK = #{tCutsomerDataPermisson.remark}
            </if>
    </sql>
    <select id="getTCutsomerDataPermissonPageCount" resultType="java.lang.Long">
        SELECT
         count(1)
        FROM  view_cutsomer_permisson a
        where 1=1
        <include refid="where_getTCutsomerDataPermissonPage"/>

    </select>
    <!--查询-->
    <select id="getTCutsomerDataPermissonPage" resultMap="tCutsomerDataPermissonMapVo">
        SELECT
        <include refid="base_column_list"/>
        ,a.name
        ,u.nickname
        from view_cutsomer_permisson a
        left join yifu_cloud.sys_user u on a.PERMISSION_USER = u.user_id
        where 1=1
        <include refid="where_getTCutsomerDataPermissonPage"/>
        ORDER BY a.BE_PERMISSION_TIME DESC
        <if test="null != page and null != size">
            limit #{page},#{size}
        </if>
    </select>
    <select id="getOwnerIdByIdAndType" resultType="java.lang.String">
        SELECT BE_PERMISSON_USER FROM t_cutsomer_data_permisson
        WHERE IS_OWNER = '0'
        <if test="0==type">
            AND CUSTOMER_ID = '${domainId}'
        </if>
        <if test="1==type">
            AND CONTRACT_ID = '${domainId}'
        </if>
        <if test="2==type">
            AND SETTLE_DOMAIN_ID = '${domainId}'
        </if>
        ORDER BY BE_PERMISSION_TIME ASC
        limit 1
    </select>
    <!-- 获取当前登录人拥有的所有项目权限 -->
    <select id="selectAllSettleDomainPermissionByUserId" resultMap="tCutsomerDataPermissonMap">
        SELECT
        <include refid="base_column_list"/>
        FROM t_cutsomer_data_permisson
        WHERE 1=1
        AND SETTLE_DOMAIN_ID IS NOT NULL
        AND BE_PERMISSON_USER = '${userId}'
    </select>
    <select id="getCustomerServiceByid" resultType="java.lang.String">
      SELECT
	    GROUP_CONCAT(distinct BE_PERMISSON_USER )
      FROM
	    t_cutsomer_data_permisson
      WHERE
	    IS_CUSTOMER_SERVICE = '0'
	    AND SETTLE_DOMAIN_ID = '${id}'
    </select>
    <select id="getUserByid" resultType="java.lang.String">
        SELECT
	    GROUP_CONCAT(distinct BE_PERMISSON_USER )
      FROM
	    t_cutsomer_data_permisson
      WHERE
	     SETTLE_DOMAIN_ID = '${settleId}'
    </select>


</mapper>
