<?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.TenantInfoMapper">

	 
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.name_ AS name
        , t.logo_ AS logo
        , t.province_code_ AS provinceCode
        , t.city_code_ AS cityCode
        , t.region_code_ AS regionCode
        , t.username_ AS username
        , t.user_id_ AS userId
        , t.phone_ AS phone
        , t.enable_flag_ AS enableFlag
        , t.update_time_ AS updateTime
        , t.create_time_ AS createTime
        , t.brief_Introduction_ AS briefIntroduction
        </sql>

    <insert id="insertInfo" parameterType="com.yonge.cooleshow.biz.dal.entity.TenantInfo">
        insert into tenant_info
        (name_,logo_,brief_Introduction_,province_code_,city_code_,region_code_,username_,phone_)
        values (#{param.name},#{param.logo},#{param.briefIntroduction},#{param.provinceCode},#{param.cityCode},#{param.regionCode},#{param.username},#{param.phone})
    </insert>


    <update id="updateInfo">
        update tenant_info
        set
        <if test="param.name != null and param.name != ''">
            name_ = #{param.name},
        </if>
        <if test="param.logo != null and param.logo != ''">
            logo_ = #{param.logo},
        </if>
        <if test="param.provinceCode != null and param.provinceCode != ''">
            province_code_ = #{param.provinceCode},
        </if>
        <if test="param.cityCode != null and param.cityCode != ''">
            city_code_ = #{param.cityCode},
        </if>
        <if test="param.regionCode != null and param.regionCode != ''">
            region_code_ = #{param.regionCode},
        </if>
        <if test="param.briefIntroduction != null and param.briefIntroduction != ''">
            brief_introduction_= #{param.briefIntroduction},
        </if>
        <if test="param.username != null and param.username != ''">
            username_ = #{param.username},
        </if>
        <if test="param.phone != null and param.phone != ''">
            phone_ = #{param.phone}
        </if>
        WHERE id_ = #{param.id}


    </update>
    <update id="updateFlag">
        update tenant_info
        set enable_flag_ = 0
        where id_ = #{id}
    </update>
    <update id="updateMethod">
        update teacher
        set settlement_from_ = 'TEACHER'
        where tenant_id_ = #{id}
    </update>

    <update id="updateStatusById">
      update  tenant_apply_record
      set status_ = 'PASS'
      where  id_ =#{id}
    </update>
    <update id="updateUnpassStatusById">
        update  tenant_apply_record
      set status_ = 'UNPASS'
      where  id_ =#{id}
    </update>
    <update id="updateIdPhone">
        update tenant_info set user_id_ = #{newId} and phone_ =#{phone}  where user_id_ = #{oldId}
    </update>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper$TenantInfo">
        SELECT
        <include refid="baseColumns"/>
        FROM tenant_info t
        <where>
            <if test="param.keyword != null and param.keyword  != ''">
                and (
                t.name_ like concat('%',#{param.keyword},'%')
                or t.username_ like concat('%',#{param.keyword},'%')
                or t.phone_ like concat('%',#{param.keyword},'%')
                )
            </if>
            <if test="param.provinceCode != null">
                and t.province_code_ = #{param.provinceCode}
            </if>
            <if test="param.cityCode != null">
                and t.city_code_ = #{param.cityCode}
            </if>
            <if test="param.regionCode != null">
                and t.region_code_ = #{param.regionCode}
            </if>
            <if test="param.enableFlag != null">
                and t.enable_flag_ = #{param.enableFlag}
            </if>
        </where>
        order by t.create_time_ desc
	</select>

    <select id="queryTeacherCounts" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherCounts">
        SELECT count(t.tenant_id_) as teacherCounts ,t.tenant_id_  as id FROM teacher t where t.tenant_id_  = #{id}
    </select>

    <select id="queryTeacherCount" resultType="java.lang.String">
        SELECT count(t.tenant_id_) FROM teacher t where t.tenant_id_ = #{id}
    </select>

    <select id="queryStudentCounts" resultType="com.yonge.cooleshow.biz.dal.entity.StudentCounts">
        SELECT count(s.tenant_id_) as studentCounts, s.tenant_id_  as id FROM student s where s.tenant_id_ =  #{id}
    </select>
    <select id="queryStudentCount" resultType="java.lang.String">
        SELECT count(s.tenant_id_) FROM student s where s.tenant_id_ = #{id}
    </select>
    <select id="selectApplyPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper$TenantInfo">
        SELECT
        id_ as id,
        name_ AS name,
        logo_ AS logo,
        brief_Introduction_ AS briefIntroduction,
        username_ AS username,
        phone_ AS phone,
        province_code_ AS provinceCode,
        city_code_ AS cityCode,
        region_code_ AS regionCode,
        status_ AS status
        FROM tenant_apply_record t
        where 1=1
        <if test="param.keyword != null and param.keyword.trim()  != ''">
            and (
            t.name_ like concat('%',#{param.keyword},'%')
            or t.username_ like concat('%',#{param.keyword},'%')
            or t.phone_ like concat('%',#{param.keyword},'%')
            )
        </if>
        <if test="param.provinceCode != null and param.provinceCode != ''
               and param.cityCode != null and param.cityCode != ''
               and param.regionCode != null and param.regionCode != ''">
            and
            t.province_code_ = #{param.provinceCode}
            and
            t.city_code_ = #{param.cityCode}
            and
            t.region_code_ = #{param.regionCode}
        </if>
    </select>

    <select id="selectHistoryPage"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper$TenantInfo">
        select
        id_ AS id,
        tenant_apply_record_id_ AS tenantApplyRecordId,
        name_ AS name,
        logo_ as logo,
        province_code_ AS provinceCode,
        city_code_ AS cityCode,
        region_code_ AS regionCode,
        username_ AS username,
        phone_ AS phone,
        status_ AS status,
        verify_user_id_ AS verifyUserId,
        reason_ AS reason,
        update_time_ AS updateTime,
        create_time_ AS createTime
        FROM
        tenant_entry_record
        WHERE
        1=1
        <if test="id != null">
          and id_ = #{id}
        </if>
    </select>
    <select id="queryNow" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper$TenantInfo">
        select
        id_ AS id,
        tenant_id_ AS tenantId,
        name_ AS name,
        logo_ as logo,
        province_code_ AS provinceCode,
        city_code_ AS cityCode,
        region_code_ AS regionCode,
        username_ AS username,
        phone_ AS phone,
        status_ AS status,
        reason_ AS reason
        from
        tenant_apply_record
        where
        1=1
        <if test="param.id != null">
        and
        id_ = #{param.id}
        </if>
    </select>
    <select id="selectInfoById" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper$TenantInfo">
        select
                  t.id_ AS id
                , t.name_ AS name
                , t.logo_ AS logo
                , t.province_code_ AS provinceCode
                , t.city_code_ AS cityCode
                , t.region_code_ AS regionCode
                , t.username_ AS userName
                , t.user_id_ AS userId
                , t.phone_ AS phone
                , t.update_time_ AS updateTime
                , t.create_time_ AS createTime
                , t.brief_Introduction_ AS briefIntroduction
        from tenant_info t
        where 1=1
        <if test="id != null">
           and id_ = #{id}
        </if>

    </select>


</mapper>