<?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.huimv.env.admin.mapper.BasePigpenMapper">

    <select id="getMaxBuildNum" resultType="java.lang.Integer">
        SELECT COALESCE(MAX(other4),0) FROM `base_pigpen` WHERE parent_id = #{parentId}
    </select>

    <select id="listPigpen" resultType="com.huimv.env.admin.entity.dto.ListPigpenDto">
        select a.buildName 'buildName',a.pigpenId 'pigpenId',a.farmId 'farmId',a.lat 'lat',a.lng 'lng',count(t.id) 'terminalCount',a.gateway 'gatewayCount'  from

        (select b.id 'pigpenId', g.id 'id',b.build_name 'buildName',b.farm_id 'farmId',b.lat 'lat', b.lng 'lng',COUNT(g.id) gateway from base_pigpen b LEFT JOIN
        gateway g ON b.id=g.location_id where b.farm_id=#{farmId} and b.parent_id=0 GROUP BY b.id) a
				LEFT JOIN
				terminal t
        ON t.gateway_id=a.id
				GROUP BY a.id
    </select>
</mapper>
