<?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.tuoheng.mapper.TenantMapper">
    <sql id="Base_Column_List">
        id,create_time,update_time,create_user,update_user,user_id,remark,`code`,`name`,
        enabled,province_code,province_name,
        city_code,city_name,district_code,district_name,customer,
        customer_phone,adress,lng,lat
    </sql>
    <insert id="insertTenant" parameterType="com.tuoheng.model.po.TenantPo" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO t_tenant (user_id, remark, `code`, `name`, province_code, province_name, city_code, city_name,
                              district_code, district_name, customer, customer_phone, adress, lng, lat)
        VALUES (#{userId}, #{remark}, #{code}, #{name}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName},
                #{districtCode}, #{districtName}, #{customer}, #{customerPhone}, #{adress}, #{lng}, #{lat})
    </insert>
    <update id="updateById" parameterType="com.tuoheng.model.po.TenantPo">
        update t_tenant
        <set>
            <if test="name != null and name !=''">
                name = #{name},
            </if>
            <if test="enabled != null">
                enabled = #{enabled},
            </if>
            province_code=#{provinceCode},
            province_name=#{provinceName},
            city_code =#{cityCode},
            city_name=#{cityName},
            district_code =#{districtCode},
            district_name =#{districtName},
            customer =#{customer},
            customer_phone =#{customerPhone},
            adress =#{adress},
            lng =#{lng},
            lat =#{lat}
        </set>
        where id = #{id}
    </update>
    <select id="getByCode" resultType="com.tuoheng.model.dto.TTenant">
        select *
        from tuoheng_oidc.t_tenant
        where code = #{code}
          and enabled = 1
    </select>
    <select id="findList" resultType="com.tuoheng.model.vo.TenantVo">
        SELECT t.id, t.user_id userId, t.remark, t.code tenantCode,
        t.name tenantName,t.customer,t.customer_phone
        ,t.province_code,t.province_name,t.city_code,t.city_name,t.district_code,t.district_name,u.username
        username,t.update_time updateTime,t.create_time createTime,u.is_able status
        FROM t_tenant t,users u
        WHERE t.enabled = 1 and t.user_id =u.id
        <if test="query.tenantName != null and query.tenantName != ''">
            and t.name LIKE concat('%',#{query.tenantName},'%')
        </if>
        <if test="query.username != null and query.username != ''">
            and u.username LIKE concat('%',#{query.username},'%')
        </if>
        <if test="query.status != null">
            and u.is_able =#{query.status}
        </if>
        ORDER BY t.create_time desc
    </select>
    <select id="selectById" resultType="com.tuoheng.model.po.TenantPo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_tenant
        WHERE id = #{id}
        and enabled = 1
    </select>
</mapper>
