<?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.yzt.airconditionersystem.mapper.CompanyMapper">

    <resultMap id="BaseResultMap" type="com.yzt.airconditionersystem.bean.Company">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="operatorId" column="operator_id" jdbcType="VARCHAR"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="district" column="district" jdbcType="VARCHAR"/>
        <result property="detailedAddress" column="detailed_address" jdbcType="VARCHAR"/>
        <result property="contact" column="contact" jdbcType="VARCHAR"/>
        <result property="phone" column="phone" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="operatorName" column="operatorName" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        operator_id,
        name,
        username,
        district,
        detailed_address,
        contact,
        phone,
        type
    </sql>
    <select id="listByNameAndDistrictAndOperatorName" resultMap="BaseResultMap">
        select
            c.id,
            c.operator_id,
            c.name,
            c.username,
            c.district,
            c.detailed_address,
            c.contact,
            c.phone,
            c.type,
            o.name AS operatorName
        from company c JOIN company o
        ON
           c.operator_id = o.id
        <where>
            <if test="name != null and name != ''">
                c.name like "%"#{name,jdbcType=VARCHAR}"%"
            </if>
            <if test="district != null and district != ''">
                AND c.district like "%"#{district,jdbcType=VARCHAR}"%"
            </if>
            <if test="operatorName != null and operatorName != ''">
                AND o.name like "%"#{operatorName,jdbcType=VARCHAR}"%"
            </if>
            <if test="type != null and type != ''">
                AND c.type = #{type,jdbcType=INTEGER}
            </if>
        </where>
        group by c.id
    </select>
    <select id="listByNameAndDistrictAndContact" resultMap="BaseResultMap">
        select
        c.id,
        c.operator_id,
        c.name,
        c.username,
        c.district,
        c.detailed_address,
        c.contact,
        c.phone,
        c.type,
        o.name AS operatorName
        from company c JOIN company o
        ON
        c.operator_id = o.id
        <where>
            <if test="name != null and name != ''">
                c.name like "%"#{name,jdbcType=VARCHAR}"%"
            </if>
            <if test="district != null and district != ''">
                AND c.district like "%"#{district,jdbcType=VARCHAR}"%"
            </if>
            <if test="contact != null and contact != ''">
                AND c.contact like "%"#{contact,jdbcType=VARCHAR}"%"
            </if>
            <if test="type != null and type != ''">
                AND c.type = #{type,jdbcType=INTEGER}
            </if>
        </where>
        group by c.id
    </select>

    <select id="selectAll" resultType="com.yzt.airconditionersystem.bean.Company">
        select
            *
        FROM
            company
    </select>
</mapper>
