<?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.zcxk.dao.TenantMapper">

    <sql id="Base_Column_List">
        t.id as "id"
        ,t.TENANT_NAME as "tenantName"
        ,u.id as "userId"
        ,u.USERNAME as "userName"
        ,u.PHONE as "phone"
        ,t.`CODE` as "code"
        ,t.`TENANT_STATE` as "tenantState"
        ,t.`tenant_type` as "tenantType"
        ,t.LOGO as "logo"
        ,t.WEB_URL as "webUrl"
        ,t.DATE_CREATE as "dateCreate"
        ,dict.DICT_VALUE as "tenantTypeValue"
    </sql>

    <sql id="tenantJoins">
        inner join uims_user u on t.code=u.TENANT_ID and u.USER_TYPE='-999' and u.status =1
        left join (
			select d.DICT_CODE,d.DICT_VALUE
			from uims_dict p
			left join uims_dict d on d.PARENT_DICT_ID=p.id and d.status =1
			where p.DICT_CODE = 'SYSTEM_TENANT_TYPE'and p.status =1
        )dict on dict .DICT_CODE= t.TENANT_TYPE
    </sql>
    <select id="selectById" resultType="com.zcxk.dto.TenantDto">
        select
        <include refid="Base_Column_List"/>
        from uims_tenant t
        <include refid="tenantJoins"/>
        where t.ID = #{id} and t.status = 1
    </select>
    <select id="selectByCode" resultType="com.zcxk.dto.TenantDto">
        select
        <include refid="Base_Column_List"/>
        from uims_tenant t
        <include refid="tenantJoins"/>
        where t.code = #{code} and t.status = 1
    </select>

    <!-- 查询全部 -->
    <select id="selectAll" resultType="com.zcxk.dto.TenantDto">
        select
        <include refid="Base_Column_List"/>
        from uims_tenant t
        <include refid="tenantJoins"/>
        where t.status = 1
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.dto.TenantDto">
        select
        <include refid="Base_Column_List"/>
        from uims_tenant t
        <include refid="tenantJoins"/>
        <where>
            t.status = 1
            <if test="tenantName != null and tenantName != ''">
                and TENANT_NAME like concat('%',#{tenantName},'%')
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_tenant(TENANT_NAME,tenant_type ,CODE ,LOGO ,WEB_URL ,START_DATE ,END_DATE ,TENANT_STATE ,REMARK ,STATUS ,DATE_CREATE ,CREATE_BY ,DATE_UPDATE ,UPDATE_BY)
        values ( #{tenantName},#{tenantType}, #{code}, #{logo}, #{webUrl}, #{startDate}, #{endDate}, #{tenantState}, #{remark}, #{status},#{dateCreate},#{createBy},#{dateUpdate},#{updateBy})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_tenant(ID ,TENANT_NAME ,CODE ,LOGO ,WEB_URL ,START_DATE ,END_DATE ,TENANT_STATE ,REMARK ,STATUS ,DATE_CREATE ,CREATE_BY ,DATE_UPDATE ,UPDATE_BY)
        values
        <foreach collection="tenants" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantName}, #{item.code}, #{item.logo}, #{item.webUrl}, #{item.startDate},#{item.endDate},
            #{item.tenantState}, #{item.remark}, #{item.status}, #{item.dateCreate}, #{item.createBy}, #{item.dateUpdate}, #{item.updateBy}  )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_tenant
        <set>
            <if test="tenantName != null and tenantName != ''">
                TENANT_NAME = #{tenantName},
            </if>
            <if test="tenantType != null and tenantType != ''">
                tenant_type = #{tenantType},
            </if>
            <if test="code != null and code != ''">
                CODE = #{code},
            </if>
<!--            <if test="logo != null and logo != ''">-->
                LOGO = #{logo},
<!--            </if>-->
            <if test="webUrl != null and webUrl != ''">
                WEB_URL = #{webUrl},
            </if>
            <if test="startDate != null">
                START_DATE = #{startDate},
            </if>
            <if test="endDate != null ">
                END_DATE = #{tenantState},
            </if>
            <if test="tenantState != null">
                TENANT_STATE = #{tenantState},
            </if>
            <if test="remark != null and remark != ''">
                REMARK = #{remark},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
            <if test="updateBy != null and remark != ''">
                UPDATE_BY = #{updateBy},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        update  uims_tenant set status = 0 where ID = #{id}
    </delete>

    <delete id="deleteUserByTenantId">
        update uims_user u
        left join uims_tenant t on u.TENANT_ID=t.code
        set u.status = 1
        where t.id=#{id}
    </delete>
    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_tenant and status =1
    </select>
    <select id="countIds" resultType="java.lang.Integer">
        select id from uims_tenant
        where TENANT_NAME =#{tenantName} and status =1
    </select>

    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.zcxk.dto.TenantDto">
        select
        <include refid="Base_Column_List"/>
        from uims_tenant t
        <include refid="tenantJoins"/>
        <where>
            t.status = 1
            <if test="tenantDto.tenantName != null and tenantDto.tenantName != ''">
                and t.TENANT_NAME like concat('%',#{tenantDto.tenantName},'%')
            </if>
        </where>
    </select>

    <!-- 生成租户菜单 -->
    <insert id="createTenantMenu">
        insert into uims_tenant_menu (TENANT_ID ,MENU_ID ,MENU_NAME ,MENU_IMAGE ,MENU_SEQ ,LINK_PATH ,STATUS
        ,DATE_CREATE ,CREATE_BY ,DATE_UPDATE ,UPDATE_BY,REMARK ,ENGLISH_NAME ,MENU_DYNAMIC_IMAGE)
        select
            #{tenantId},
            id,
            name,
            menu_image,
            seq,
            LINK_PATH,
            1,
            now(),
            #{userName},
            now(),
            #{userName},
            remark,
            ENGLISH_NAME,
            MENU_DYNAMIC_IMAGE
        from uims_menu
        where id in (
        <foreach collection="menuIds" item="item" index="index" separator=",">
         #{item}
        </foreach>
        )
    </insert>
    <!-- 通过租户id查询租户菜单id -->
    <select id="selectTenantMenuIds" resultType="java.lang.Integer">
        select MENU_ID from uims_tenant_menu where TENANT_ID = #{tenantId} and status =1
    </select>

    <delete id="deleteTenantMenuIds">
    delete from uims_tenant_menu
    where TENANT_ID=#{tenantId}
    <if test="menuIds != null">
        and  MENU_ID in (
            <foreach collection="menuIds" item="item" index="index" separator=",">
                #{item}
            </foreach>
        )
    </if>
    </delete>
</mapper>