<?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.screen.mapper.HomeScreenStatsMapper">

    <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="countSupplierForScreen" resultType="int">
        select count(*) from biz_supplier where del_flag = '0'
    </select>

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

    <select id="countTradeMarketForScreen" resultType="int">
        select count(*) from biz_trade_market
        where del_flag = '0' and operate_status = 1
    </select>

    <select id="selectOutboundCountByMonth"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenMonthCountDto">
        select month(status_change_date) as month, count(*) as 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;= #{yearEnd}
        <include refid="pastureInScope"/>
        group by month(status_change_date)
    </select>

    <select id="selectAppointmentAnnualByYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenAppointmentStatDto">
        select provider_type as providerType, count(*) as count
        from biz_service_appointment
        where year(appoint_date) = #{statYear}
          and status not in (2, 3)
        group by provider_type
    </select>

    <select id="selectAppointmentMonthlyByYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenAppointmentStatDto">
        select month(appoint_date) as month, provider_type as providerType, count(*) as count
        from biz_service_appointment
        where year(appoint_date) = #{statYear}
          and status not in (2, 3)
        group by month(appoint_date), provider_type
    </select>

    <select id="selectAppointmentMonthlyByYearMonthRange"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenAppointmentStatDto">
        select year(appoint_date) as statYear,
               month(appoint_date) as statMonth,
               provider_type as providerType,
               count(*) as count
        from biz_service_appointment
        where year(appoint_date) * 100 + month(appoint_date) &gt;= #{startYm}
          and year(appoint_date) * 100 + month(appoint_date) &lt;= #{endYm}
          and status not in (2, 3)
        group by year(appoint_date), month(appoint_date), provider_type
    </select>

    <select id="sumSubsidyAmountByTypeForYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenSubsidyTypeSumDto">
        select subsidy_type as subsidyType, coalesce(sum(subsidy_amount), 0) as amountSum
        from biz_subsidy_grant_record
        where grant_date &gt;= #{yearStart}
          and grant_date &lt;= #{yearEnd}
        group by subsidy_type
    </select>

    <select id="countSubsidyGrantInYear" resultType="int">
        select count(*) from biz_subsidy_grant_record
        where grant_date &gt;= #{yearStart}
          and grant_date &lt;= #{yearEnd}
    </select>

    <select id="countVideoCoursesPublishedInYear" resultType="int">
        select count(*) from biz_tech_resource
        where del_flag = '0' and publish_status = 1 and resource_type = '004008'
          and publish_time &gt;= #{yearStart}
          and publish_time &lt;= #{yearEnd}
    </select>

    <select id="countTrainingsPublishedInYear" resultType="int">
        select count(*) from biz_practical_training
        where del_flag = '0' and publish_status = 1
          and publish_time &gt;= #{yearStart}
          and publish_time &lt;= #{yearEnd}
    </select>

    <select id="selectTrainingTrendByTrainingYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenTrainingMonthDto">
        select month(training_time) as month,
               count(*) as activityCount,
               coalesce(sum(actual_enrolled_count), 0) as enrolledCount
        from biz_practical_training
        where del_flag = '0' and publish_status = 1
          and year(training_time) = #{statYear}
        group by month(training_time)
    </select>

    <select id="selectTrainingTrendByTrainingYearMonthRange"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenTrainingMonthDto">
        select year(training_time) as statYear,
               month(training_time) as statMonth,
               count(*) as activityCount,
               coalesce(sum(actual_enrolled_count), 0) as enrolledCount
        from biz_practical_training
        where del_flag = '0' and publish_status = 1
          and year(training_time) * 100 + month(training_time) &gt;= #{startYm}
          and year(training_time) * 100 + month(training_time) &lt;= #{endYm}
        group by year(training_time), month(training_time)
    </select>

    <select id="countPublishedTrainingsWithTrainingTimeInYearMonthRange" resultType="int">
        select count(*) from biz_practical_training
        where del_flag = '0' and publish_status = 1
          and year(training_time) * 100 + month(training_time) &gt;= #{startYm}
          and year(training_time) * 100 + month(training_time) &lt;= #{endYm}
    </select>

    <select id="countPublishedTrainingsWithTrainingTimeInYear" resultType="int">
        select count(*) from biz_practical_training
        where del_flag = '0' and publish_status = 1
          and year(training_time) = #{statYear}
    </select>

    <select id="selectDistinctOutboundYears" resultType="int">
        select distinct year(status_change_date)
        from biz_yak_asset
        where del_flag = '0' and asset_status = 4 and status_change_date is not null
        order by 1 desc
    </select>

    <select id="selectDistinctAppointmentYears" resultType="int">
        select distinct year(appoint_date) from biz_service_appointment
        where status not in (2, 3)
        order by 1 desc
    </select>

    <select id="selectDistinctSubsidyYears" resultType="int">
        select distinct year(grant_date) from biz_subsidy_grant_record
        order by 1 desc
    </select>

    <select id="selectDistinctStandardCreateYears" resultType="int">
        select distinct y from (
            select year(create_time) as y from biz_breeding_standard where publish_status = 1
            union
            select year(create_time) from biz_feeding_standard where publish_status = 1
            union
            select year(create_time) from biz_equipment_work_standard where publish_status = 1
            union
            select year(create_time) from biz_epidemic_treatment_standard where publish_status = 1
            union
            select year(create_time) from biz_growth_slaughter_standard where publish_status = 1
        ) t
        order by y desc
    </select>

    <select id="selectDistinctTechPublishYears" resultType="int">
        select distinct year(publish_time) from biz_tech_resource
        where del_flag = '0' and publish_status = 1 and resource_type = '004008'
          and publish_time is not null
        order by 1 desc
    </select>

    <select id="selectDistinctTrainingYears" resultType="int">
        select distinct year(training_time) from biz_practical_training
        where del_flag = '0' and publish_status = 1
        order by 1 desc
    </select>

    <select id="selectTownMapMarkers"
            resultType="com.ruoyi.web.modules.screen.domain.vo.HomeScreenTownMapMarkerVo">
        select d.dept_id as townDeptId,
               d.dept_name as townName,
               COALESCE(h.yak_total, 0) as yakInventoryCount,
               COALESCE(o.yak_outbound_count, 0) as yakOutboundCount
        from sys_dept d
        left join (
            select town_dept_id, sum(yak_total) as yak_total
            from biz_yak_herd_inventory
            <where>
                <if test="herdStatYear != null and herdStatMonth != null">
                    and stat_year = #{herdStatYear}
                    and stat_month = #{herdStatMonth}
                </if>
                <if test="herdStatYear == null or herdStatMonth == null">
                    and 1 = 0
                </if>
            </where>
            group by town_dept_id
        ) h on h.town_dept_id = d.dept_id
        left join (
            select town_dept_id, sum(yak_outbound_count) as yak_outbound_count
            from biz_yak_outbound_report
            <where>
                <if test="outboundStatYear != null and outboundStatMonth != null">
                    and stat_year = #{outboundStatYear}
                    and stat_month = #{outboundStatMonth}
                </if>
                <if test="outboundStatYear == null or outboundStatMonth == null">
                    and 1 = 0
                </if>
            </where>
            group by town_dept_id
        ) o on o.town_dept_id = d.dept_id
        where d.parent_id = 100
          and d.order_num &gt; 12
          and d.del_flag = '0'
          and d.status = '0'
        order by d.order_num asc, d.dept_id asc
    </select>

    <select id="sumHerdInventoryYakTotalByStatYear" resultType="int">
        select coalesce(sum(yak_total), 0)
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
    </select>

    <select id="sumOutboundReportYakCountByStatYear" resultType="int">
        select coalesce(sum(yak_outbound_count), 0)
        from biz_yak_outbound_report
        where stat_year = #{statYear}
    </select>

    <select id="sumOutboundReportFarmerHouseholdCountByStatYear" resultType="int">
        select coalesce(sum(farmer_household_count), 0)
        from biz_yak_outbound_report
        where stat_year = #{statYear}
    </select>

    <select id="selectLatestHerdInventoryStatPeriod"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenStatPeriodDto">
        select stat_year as statYear, stat_month as statMonth
        from biz_yak_herd_inventory
        order by stat_year desc, stat_month desc
        limit 1
    </select>

    <select id="selectLatestOutboundReportStatPeriod"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenStatPeriodDto">
        select stat_year as statYear, stat_month as statMonth
        from biz_yak_outbound_report
        order by stat_year desc, stat_month desc
        limit 1
    </select>

    <select id="sumHerdInventoryYakTotalByStatYearMonth" resultType="int">
        select coalesce(sum(yak_total), 0)
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </select>

    <select id="sumOutboundReportYakCountByStatYearMonth" resultType="int">
        select coalesce(sum(yak_outbound_count), 0)
        from biz_yak_outbound_report
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </select>

    <select id="sumOutboundReportFarmerHouseholdCountByStatYearMonth" resultType="int">
        select coalesce(sum(farmer_household_count), 0)
        from biz_yak_outbound_report
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </select>

    <select id="sumHerdInventoryGenderByStatYearMonth"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenHerdGenderSumDto">
        select coalesce(sum(yak_total), 0) as yakTotal,
               coalesce(sum(bull_total), 0) as bullTotal,
               coalesce(sum(cow_total), 0) as cowTotal
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </select>

    <select id="sumHerdInventoryCowAgeByStatYearMonth"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenHerdCowAgeSumDto">
        select coalesce(sum(cow_young_count), 0) as cowYoungCount,
               coalesce(sum(cow_adult_count), 0) as cowAdultCount
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </select>

    <select id="selectHerdInventoryBullCowByMonth"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenHerdGenderMonthDto">
        select stat_month as month,
               coalesce(sum(bull_total), 0) as bullCount,
               coalesce(sum(cow_total), 0) as cowCount
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
        group by stat_month
        order by stat_month
    </select>

    <select id="selectHerdInventoryBullCowByYearMonthRange"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenYearMonthBullCowDto">
        select stat_year as statYear,
               stat_month as statMonth,
               coalesce(sum(bull_total), 0) as bullCount,
               coalesce(sum(cow_total), 0) as cowCount
        from biz_yak_herd_inventory
        where stat_year * 100 + stat_month &gt;= #{startYm}
          and stat_year * 100 + stat_month &lt;= #{endYm}
        group by stat_year, stat_month
        order by stat_year, stat_month
    </select>

    <select id="selectOutboundReportYakCountByMonth"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenMonthCountDto">
        select stat_month as month,
               coalesce(sum(yak_outbound_count), 0) as count
        from biz_yak_outbound_report
        where stat_year = #{statYear}
        group by stat_month
        order by stat_month
    </select>

    <select id="selectOutboundReportYakCountByYearMonthRange"
            resultType="com.ruoyi.web.modules.screen.domain.dto.HomeScreenYearMonthCountDto">
        select stat_year as statYear,
               stat_month as statMonth,
               coalesce(sum(yak_outbound_count), 0) as count
        from biz_yak_outbound_report
        where stat_year * 100 + stat_month &gt;= #{startYm}
          and stat_year * 100 + stat_month &lt;= #{endYm}
        group by stat_year, stat_month
        order by stat_year, stat_month
    </select>
</mapper>
