<?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="cn.iocoder.yudao.module.company.mapper.CompanyMapper">
    <select id="selectListPage" resultType="cn.iocoder.yudao.module.company.entity.Company">
        SELECT
        bc.*
        FROM
        base_company bc
        WHERE deleted = '0'
        <if test="map.registDateArray.length>0">
            AND (bc.regist_date BETWEEN #{map.registDateArray[0]} AND #{map.registDateArray[1]})
        </if>
        <if test="map.corpName!='' and map.corpName!=null">
            AND (bc.corp_name LIKE CONCAT('%',#{map.corpName},'%'))
        </if>
        <if test="map.orgCard!='' and map.orgCard!=null">
            AND (bc.org_card LIKE CONCAT('%',#{map.orgCard},'%'))
        </if>
        <if test="map.legalName!='' and map.legalName!=null">
            AND (bc.legal_name LIKE CONCAT('%',#{map.legalName},'%'))
        </if>
        <if test="map.legalCard!='' and map.legalCard!=null">
            AND (bc.legal_card LIKE CONCAT('%',#{map.legalCard},'%'))
        </if>
        <if test="map.contactName!='' and map.contactName!=null">
            AND (bc.contact_name LIKE CONCAT('%',#{map.contactName},'%'))
        </if>
        <if test="map.mobilePhone!='' and map.mobilePhone!=null">
            AND (bc.mobile_phone LIKE CONCAT('%',#{map.mobilePhone},'%'))
        </if>
        ORDER BY bc.create_time DESC
    </select>

    <resultMap id="QueryResultMap" type="cn.iocoder.yudao.module.company.vo.CompanyRespVO">
        <id property="id" column="c_id" jdbcType="VARCHAR"/>
        <association property="userDTO" javaType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
            <id property="id" column="u_id" jdbcType="BIGINT"/>
        </association>
    </resultMap>
    <select id="getCompanyAndManager" resultMap="QueryResultMap">
        select c.id as c_id,
               u.id as u_id
        from base_company c
                 inner join system_users u on c.login_account = u.username
    </select>

    <select id="getCompanyPageByServer" resultType="cn.iocoder.yudao.module.company.entity.Company">
        select bc.*
        <if test="map.lgt != null">
            , ROUND(ST_Distance_Sphere (POINT (#{map.lgt}, #{map.lat}),POINT (bc.lgt, bc.lat ))) AS distance
        </if>
        from base_company bc
                 left join system_tenant st on bc.tenant_id = st.id
        where FIND_IN_SET(#{map.serviceCodes}, bc.service_codes) > 0
          and bc.deleted = 0
        <if test="map.tenantId != null">
            and bc.tenant_id = #{map.tenantId}
        </if>
        <if test="map.lgt != null">
            order by distance
        </if>
    </select>
</mapper>
