<?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。
  表：biz_large_livestock_farmer（物理删除）。
  唯一键查询：selectByUnique / countByUniqueExcludeId → (stat_year, stat_month, village_dept_id, enterprise_name)
-->
<mapper namespace="com.ruoyi.web.modules.industryservice.mapper.BizLargeLivestockFarmerMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer" id="BizLargeLivestockFarmerResult">
        <id property="id" column="id"/>
        <result property="statYear" column="stat_year"/>
        <result property="statMonth" column="stat_month"/>
        <result property="townDeptId" column="town_dept_id"/>
        <result property="townName" column="town_name"/>
        <result property="villageDeptId" column="village_dept_id"/>
        <result property="villageName" column="village_name"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="legalRepresentative" column="legal_representative"/>
        <result property="enterpriseType" column="enterprise_type"/>
        <result property="registeredCapital" column="registered_capital"/>
        <result property="establishDate" column="establish_date"/>
        <result property="livestockLaborCount" column="livestock_labor_count"/>
        <result property="stockSubtotal" column="stock_subtotal"/>
        <result property="cattleYearEndStock" column="cattle_year_end_stock"/>
        <result property="cattleBreedingFemaleStock" column="cattle_breeding_female_stock"/>
        <result property="sheepYearEndStock" column="sheep_year_end_stock"/>
        <result property="sheepBreedingFemaleStock" column="sheep_breeding_female_stock"/>
        <result property="cattleOutbound" column="cattle_outbound"/>
        <result property="sheepOutbound" column="sheep_outbound"/>
        <result property="grasslandAreaSubtotal" column="grassland_area_subtotal"/>
        <result property="grassBalanceArea" column="grass_balance_area"/>
        <result property="transferGrassArea" column="transfer_grass_area"/>
        <result property="approvedCarryingCapacity" column="approved_carrying_capacity"/>
        <result property="yearEndStockSheepUnit" column="year_end_stock_sheep_unit"/>
        <result property="isGrassBalanced" column="is_grass_balanced"/>
        <result property="income" column="income"/>
        <result property="expense" column="expense"/>
        <result property="profit" column="profit"/>
        <result property="subsidyYear" column="subsidy_year"/>
        <result property="hasSubsidy" column="has_subsidy"/>
        <result property="subsidyAmount" column="subsidy_amount"/>
        <result property="hasTrademark" column="has_trademark"/>
        <result property="hasQualityCert" column="has_quality_cert"/>
        <result property="salesChannel" column="sales_channel"/>
        <result property="drivenPopulation" column="driven_population"/>
        <result property="wagePaid" column="wage_paid"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select r.id, r.stat_year, r.stat_month, r.town_dept_id, r.town_name, r.village_dept_id, r.village_name,
               r.enterprise_name, r.legal_representative, r.enterprise_type, r.registered_capital, r.establish_date,
               r.livestock_labor_count, r.stock_subtotal, r.cattle_year_end_stock, r.cattle_breeding_female_stock,
               r.sheep_year_end_stock, r.sheep_breeding_female_stock, r.cattle_outbound, r.sheep_outbound,
               r.grassland_area_subtotal, r.grass_balance_area, r.transfer_grass_area,
               r.approved_carrying_capacity, r.year_end_stock_sheep_unit, r.is_grass_balanced,
               r.income, r.expense, r.profit, r.subsidy_year, r.has_subsidy, r.subsidy_amount,
               r.has_trademark, r.has_quality_cert, r.sales_channel, r.driven_population, r.wage_paid,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_large_livestock_farmer r
    </sql>

    <select id="selectBizLargeLivestockFarmerById" parameterType="long" resultMap="BizLargeLivestockFarmerResult">
        <include refid="selectVo"/>
        where r.id = #{id}
    </select>

    <select id="countByUniqueExcludeId" resultType="int">
        select count(1) from biz_large_livestock_farmer
        where stat_year = #{statYear} and stat_month = #{statMonth}
          and village_dept_id = #{villageDeptId} and enterprise_name = #{enterpriseName}
        <if test="excludeId != null">and id != #{excludeId}</if>
    </select>

    <select id="selectByUnique" resultMap="BizLargeLivestockFarmerResult">
        <include refid="selectVo"/>
        where r.stat_year = #{statYear} and r.stat_month = #{statMonth}
          and r.village_dept_id = #{villageDeptId} and r.enterprise_name = #{enterpriseName}
        limit 1
    </select>

    <select id="selectBizLargeLivestockFarmerList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer"
            resultMap="BizLargeLivestockFarmerResult">
        <include refid="selectVo"/>
        left join sys_dept d on r.town_dept_id = d.dept_id
        <where>
            <if test="statYear != null">and r.stat_year = #{statYear}</if>
            <if test="statMonth != null">and r.stat_month = #{statMonth}</if>
            <if test="townDeptId != null">and r.town_dept_id = #{townDeptId}</if>
            <if test="villageDeptId != null">and r.village_dept_id = #{villageDeptId}</if>
            <if test="enterpriseName != null and enterpriseName != ''">and r.enterprise_name like concat('%', #{enterpriseName}, '%')</if>
        </where>
        order by r.stat_year desc, r.stat_month desc, d.order_num asc, r.village_dept_id asc, r.id desc
    </select>

    <insert id="insertBizLargeLivestockFarmer" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_large_livestock_farmer (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name, enterprise_name,
            legal_representative, enterprise_type, registered_capital, establish_date, livestock_labor_count,
            stock_subtotal, cattle_year_end_stock, cattle_breeding_female_stock, sheep_year_end_stock,
            sheep_breeding_female_stock, cattle_outbound, sheep_outbound, grassland_area_subtotal,
            grass_balance_area, transfer_grass_area, approved_carrying_capacity, year_end_stock_sheep_unit,
            is_grass_balanced, income, expense, profit, subsidy_year, has_subsidy, subsidy_amount,
            has_trademark, has_quality_cert, sales_channel, driven_population, wage_paid,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName}, #{enterpriseName},
            #{legalRepresentative}, #{enterpriseType}, #{registeredCapital}, #{establishDate}, #{livestockLaborCount},
            #{stockSubtotal}, #{cattleYearEndStock}, #{cattleBreedingFemaleStock}, #{sheepYearEndStock},
            #{sheepBreedingFemaleStock}, #{cattleOutbound}, #{sheepOutbound}, #{grasslandAreaSubtotal},
            #{grassBalanceArea}, #{transferGrassArea}, #{approvedCarryingCapacity}, #{yearEndStockSheepUnit},
            #{isGrassBalanced}, #{income}, #{expense}, #{profit}, #{subsidyYear}, #{hasSubsidy}, #{subsidyAmount},
            #{hasTrademark}, #{hasQualityCert}, #{salesChannel}, #{drivenPopulation}, #{wagePaid},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizLargeLivestockFarmer" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer">
        update biz_large_livestock_farmer set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            enterprise_name = #{enterpriseName},
            legal_representative = #{legalRepresentative},
            enterprise_type = #{enterpriseType},
            registered_capital = #{registeredCapital},
            establish_date = #{establishDate},
            livestock_labor_count = #{livestockLaborCount},
            stock_subtotal = #{stockSubtotal},
            cattle_year_end_stock = #{cattleYearEndStock},
            cattle_breeding_female_stock = #{cattleBreedingFemaleStock},
            sheep_year_end_stock = #{sheepYearEndStock},
            sheep_breeding_female_stock = #{sheepBreedingFemaleStock},
            cattle_outbound = #{cattleOutbound},
            sheep_outbound = #{sheepOutbound},
            grassland_area_subtotal = #{grasslandAreaSubtotal},
            grass_balance_area = #{grassBalanceArea},
            transfer_grass_area = #{transferGrassArea},
            approved_carrying_capacity = #{approvedCarryingCapacity},
            year_end_stock_sheep_unit = #{yearEndStockSheepUnit},
            is_grass_balanced = #{isGrassBalanced},
            income = #{income},
            expense = #{expense},
            profit = #{profit},
            subsidy_year = #{subsidyYear},
            has_subsidy = #{hasSubsidy},
            subsidy_amount = #{subsidyAmount},
            has_trademark = #{hasTrademark},
            has_quality_cert = #{hasQualityCert},
            sales_channel = #{salesChannel},
            driven_population = #{drivenPopulation},
            wage_paid = #{wagePaid},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

    <delete id="deleteBizLargeLivestockFarmerByIds">
        delete from biz_large_livestock_farmer where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
    </delete>
</mapper>
