<?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.yonge.cooleshow.biz.dal.mapper.UserTenantBindRecordMapper">



    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ AS id
        , t.user_id_ AS userId
        , t.user_type_ AS userType
        , t.tenant_id_ AS tenantId
        , t.bind_status_ AS bindStatus
        , t.bind_time_ AS bindTime
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.UserTenantBindRecordWrapper$UserTenantBindRecord">
        SELECT
        <include refid="baseColumns"/>
        ,su.username_ userName
        ,su.phone_ phone
        ,su.gender_ gender
<!--        ,su.lock_flag_ lockFlag-->
        <if test="param.userType == 'TEACHER'">
            ,if(su.lock_flag_ = 0 and tc.lock_flag_= 0,0,1) as lockFlag
            ,tc.settlement_from_ settlementFrom
            ,group_concat(tg.name_) tenantGroupName
            ,group_concat(tg.id_) tenantGroupId
        </if>
        <if test="param.userType == 'STUDENT'">
            ,tg.name_ tenantGroupName
            ,tg.id_ tenantGroupId
            ,st.subject_id_ subjectId
            ,if(su.lock_flag_ = 0 and st.lock_flag_= 0,0,1) as lockFlag
        </if>
        FROM user_tenant_bind_record t
        LEFT JOIN sys_user su ON t.user_id_ = su.id_
        <if test="param.userType == 'TEACHER'">
            LEFT JOIN teacher tc on t.user_id_ = tc.user_id_
            LEFT JOIN tenant_group tg on t.user_id_ = tg.admin_id_
            <if test="param.tenantId != null">
                and tg.tenant_id_ = #{param.tenantId}
            </if>
        </if>
        <if test="param.userType == 'STUDENT'">
            LEFT JOIN student st on t.user_id_ = st.user_id_
            LEFT JOIN tenant_group tg on st.tenant_group_id_ = tg.id_
            <if test="param.tenantId != null">
                and tg.tenant_id_ = #{param.tenantId}
            </if>
        </if>
        <where>
            <if test="param.keyword != null and param.keyword.trim() != ''">
                AND (t.user_id_ like concat('%',#{param.keyword},'%')
                or su.username_ like concat('%',#{param.keyword},'%')
                or su.phone_ like concat('%',#{param.keyword},'%')
                )
            </if>
            <if test="param.tenantId != null">
                AND t.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.userType != null and param.userType.trim() != ''">
                AND t.user_type_ = #{param.userType}
            </if>
            <if test="param.tenantGroupId != null">
<!--                <if test="param.userType == 'TEACHER'">-->
<!--                    AND tg.id_ =#{param.tenantGroupId}-->
<!--                </if>-->
                <if test="param.userType == 'STUDENT'">
                    AND st.tenant_group_id_ = #{param.tenantGroupId}
                </if>
            </if>
            <if test="param.userType == 'TEACHER' and param.settlementFrom != null">
                AND tc.settlement_from_ = #{param.settlementFrom}
            </if>
            <if test="param.bindStatus != null">
                AND t.bind_status_ = #{param.bindStatus}
            </if>
            <if test="param.lockFlag != null">
                AND su.lock_flag_ = #{param.lockFlag}
            </if>
            <if test="param.bindStartTime != null">
                AND t.bind_time_ >= #{param.bindStartTime}
            </if>
            <if test="param.bindEndTime != null">
                AND #{param.bindEndTime} >= t.bind_time_
            </if>
            <if test="param.userType == 'STUDENT' and param.subjectId != null">
                AND st.subject_id_ = #{param.subjectId}
            </if>
        </where>
        <if test="param.userType == 'TEACHER'">
            group by t.user_id_
        </if>
        <if test="param.tenantGroupId != null and param.userType == 'TEACHER'">
            HAVING find_in_set(#{param.tenantGroupId},tenantGroupId)
        </if>
        order by field(t.bind_status_,1,0) ,t.bind_time_ desc
    </select>

    <insert id="add">
        INSERT INTO user_tenant_bind_record(user_id_, user_type_, tenant_id_, bind_status_, bind_time_)
        VALUES (#{record.userId}, #{record.userType}, #{record.tenantId}, #{record.bindStatus}, #{record.bindTime})
        ON DUPLICATE KEY UPDATE bind_status_ = VALUES(bind_status_),
                                bind_time_   = VALUES(bind_time_)
    </insert>
</mapper>