<?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.ruoyi.web.work.mapper.EnterpriseMapper">
    
    <select id="selectList" resultType="com.ruoyi.web.work.domain.Enterprise">
        select * from tb_enterprise
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="state != null "> and state = #{state}</if>
        </where>
    </select>
    <select id="indexList" resultType="com.ruoyi.web.work.domain.vo.EnterpriseListVo">
        SELECT
        e.id,
        e.name,
        e.logo,
        e.recommend,
        e.region_name,
        e.latitude,
        e.longitude,
        (SELECT COUNT( p.id ) FROM tb_position p WHERE	p.user_id = e.id AND p.state=0 AND p.audit=1 ) AS positions
        <if test="longitude !=null and longitude !=null ">,ROUND( ST_Distance_Sphere ( POINT ( #{longitude}, #{latitude} ), POINT ( e.longitude, e.latitude ))/ 1000, 2 ) AS distance</if>
        FROM
        tb_enterprise e
        WHERE e.state =1
            <if test="name != null  and name != ''"> and e.name like concat('%', #{name}, '%')</if>
            <if test="state != null "> and e.state = #{state}</if>
            <if test="regionId != null">and FIND_IN_SET(#{regionId},e.ancestors)</if>
            <if test="recommend != null "> AND e.recommend = #{recommend}</if>
            <if test="orderBy=='distance' and longitude !=null">ORDER BY distance ASC</if>
            <if test="orderBy=='id'">ORDER BY e.id DESC</if>
    </select>
</mapper>