<?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.modules.industryservice.mapper.BreedingIndustryStatsMapper">

    <sql id="pastureInScope">
        <if test="allowedPastureIds != null and allowedPastureIds.size() > 0">
            and pasture_id in
            <foreach collection="allowedPastureIds" item="pid" open="(" separator="," close=")">
                #{pid}
            </foreach>
        </if>
    </sql>

    <select id="selectActivePastureIds" resultType="long">
        select id from biz_pasture
        where del_flag = '0'
    </select>

    <select id="countPastureInScope" resultType="int">
        select count(*) from biz_pasture
        where del_flag = '0'
        <if test="allowedPastureIds != null and allowedPastureIds.size() > 0">
            and id in
            <foreach collection="allowedPastureIds" item="pid" open="(" separator="," close=")">
                #{pid}
            </foreach>
        </if>
    </select>

    <select id="countInventory" resultType="int">
        select count(*) from biz_yak_asset
        where del_flag = '0' and asset_status = 1 and pasture_id is not null
        <include refid="pastureInScope"/>
    </select>

    <select id="countBull" resultType="int">
        select count(*) from biz_yak_asset
        where del_flag = '0' and asset_status = 1 and pasture_id is not null
          and trim(gender) = '公'
        <include refid="pastureInScope"/>
    </select>

    <select id="countCow" resultType="int">
        select count(*) from biz_yak_asset
        where del_flag = '0' and asset_status = 1 and pasture_id is not null
          and trim(gender) = '母'
        <include refid="pastureInScope"/>
    </select>

    <select id="countAnnualOutbound" resultType="int">
        select count(*) from biz_yak_asset
        where del_flag = '0' and asset_status = 4 and pasture_id is not null
          and status_change_date is not null
          and status_change_date &gt;= #{yearStart}
          and status_change_date &lt;= #{statDateEnd}
        <include refid="pastureInScope"/>
    </select>

    <select id="countWarningYak" resultType="int">
        select count(distinct w.yak_no)
        from biz_yak_disease_warning w
        inner join biz_yak_asset y on y.yak_no = w.yak_no and y.del_flag = '0'
        where w.alert_time &gt;= #{yearStart}
          and w.alert_time &lt;= #{statDateEnd}
          and y.pasture_id is not null
        <if test="allowedPastureIds != null and allowedPastureIds.size() > 0">
            and y.pasture_id in
            <foreach collection="allowedPastureIds" item="pid" open="(" separator="," close=")">
                #{pid}
            </foreach>
        </if>
    </select>

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.dto.BreedingIndustryYakSnapshot" id="YakSnapshotResult">
        <result property="pastureId" column="pasture_id"/>
        <result property="assetStatus" column="asset_status"/>
        <result property="statusChangeDate" column="status_change_date"/>
        <result property="gender" column="gender"/>
        <result property="ageMonths" column="age_months"/>
        <result property="birthDate" column="birth_date"/>
    </resultMap>

    <select id="selectYakSnapshotsInScope" resultMap="YakSnapshotResult">
        select pasture_id, asset_status, status_change_date, gender, age_months, birth_date
        from biz_yak_asset
        where del_flag = '0' and pasture_id is not null
        <include refid="pastureInScope"/>
    </select>

    <select id="countGrassland" resultType="int">
        select count(*) from biz_grassland where del_flag = '0'
    </select>

    <select id="sumGrasslandAreaMu" resultType="java.math.BigDecimal">
        select coalesce(sum(area_mu), 0) from biz_grassland
        where del_flag = '0' and area_mu is not null
    </select>

    <select id="countAvailableGrassland" resultType="int">
        select count(*) from biz_grassland
        where del_flag = '0' and degradation_level in (1, 2)
    </select>

    <select id="countInUseGrassland" resultType="int">
        select count(*) from biz_grassland
        where del_flag = '0'
          and (
            utilization_type in (2, 3)
            or (
              utilization_type = 1
              and (
                grazing_ban_start_date is null
                or grazing_ban_end_date is null
                or #{statDate} &lt; grazing_ban_start_date
                or #{statDate} &gt; grazing_ban_end_date
              )
            )
          )
    </select>

    <select id="selectDegradationGroupCounts"
            resultType="com.ruoyi.web.modules.industryservice.domain.vo.BreedingIndustryDegradationStatVo">
        select degradation_level as degradationLevel, count(*) as count
        from biz_grassland
        where del_flag = '0'
        group by degradation_level
        order by degradation_level
    </select>
</mapper>
