<?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.BizFarmerHouseholdMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold" id="BizFarmerHouseholdResult">
        <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="householdCount"              column="household_count"/>
        <result property="populationCount"             column="population_count"/>
        <result property="pastureContractArea"         column="pasture_contract_area"/>
        <result property="grazingBanArea"              column="grazing_ban_area"/>
        <result property="grassBalanceArea"            column="grass_balance_area"/>
        <result property="approvedCarryingCapacity"    column="approved_carrying_capacity"/>
        <result property="yearEndStockSheepUnit"       column="year_end_stock_sheep_unit"/>
        <result property="isPureHerder"                column="is_pure_herder"/>
        <result property="isNoLivestock"               column="is_no_livestock"/>
        <result property="isExtinctHousehold"          column="is_extinct_household"/>
        <result property="isOverloadHerder"            column="is_overload_herder"/>
        <result property="needCap"                     column="need_cap"/>
        <result property="needFloor"                   column="need_floor"/>
        <result property="grazingBanSubsidy"           column="grazing_ban_subsidy"/>
        <result property="grassBalanceReward"          column="grass_balance_reward"/>
        <result property="subsidyRewardSubtotal"       column="subsidy_reward_subtotal"/>
        <result property="afterCapFloorSubsidy"        column="after_cap_floor_subsidy"/>
        <result property="villageSupervisorSubsidy"    column="village_supervisor_subsidy"/>
        <result property="subsidyTotal"                column="subsidy_total"/>
        <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.household_count, r.population_count,
               r.pasture_contract_area, r.grazing_ban_area, r.grass_balance_area,
               r.approved_carrying_capacity, r.year_end_stock_sheep_unit,
               r.is_pure_herder, r.is_no_livestock, r.is_extinct_household,
               r.is_overload_herder, r.need_cap, r.need_floor,
               r.grazing_ban_subsidy, r.grass_balance_reward, r.subsidy_reward_subtotal,
               r.after_cap_floor_subsidy, r.village_supervisor_subsidy, r.subsidy_total,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_farmer_household r
    </sql>

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

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

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

    <select id="selectBizFarmerHouseholdList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold"
            resultMap="BizFarmerHouseholdResult">
        <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>
        </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="insertBizFarmerHousehold" parameterType="com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_farmer_household (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name,
            household_count, population_count,
            pasture_contract_area, grazing_ban_area, grass_balance_area,
            approved_carrying_capacity, year_end_stock_sheep_unit,
            is_pure_herder, is_no_livestock, is_extinct_household,
            is_overload_herder, need_cap, need_floor,
            grazing_ban_subsidy, grass_balance_reward, subsidy_reward_subtotal,
            after_cap_floor_subsidy, village_supervisor_subsidy, subsidy_total,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{householdCount}, #{populationCount},
            #{pastureContractArea}, #{grazingBanArea}, #{grassBalanceArea},
            #{approvedCarryingCapacity}, #{yearEndStockSheepUnit},
            #{isPureHerder}, #{isNoLivestock}, #{isExtinctHousehold},
            #{isOverloadHerder}, #{needCap}, #{needFloor},
            #{grazingBanSubsidy}, #{grassBalanceReward}, #{subsidyRewardSubtotal},
            #{afterCapFloorSubsidy}, #{villageSupervisorSubsidy}, #{subsidyTotal},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizFarmerHousehold" parameterType="com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold">
        update biz_farmer_household set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            household_count = #{householdCount},
            population_count = #{populationCount},
            pasture_contract_area = #{pastureContractArea},
            grazing_ban_area = #{grazingBanArea},
            grass_balance_area = #{grassBalanceArea},
            approved_carrying_capacity = #{approvedCarryingCapacity},
            year_end_stock_sheep_unit = #{yearEndStockSheepUnit},
            is_pure_herder = #{isPureHerder},
            is_no_livestock = #{isNoLivestock},
            is_extinct_household = #{isExtinctHousehold},
            is_overload_herder = #{isOverloadHerder},
            need_cap = #{needCap},
            need_floor = #{needFloor},
            grazing_ban_subsidy = #{grazingBanSubsidy},
            grass_balance_reward = #{grassBalanceReward},
            subsidy_reward_subtotal = #{subsidyRewardSubtotal},
            after_cap_floor_subsidy = #{afterCapFloorSubsidy},
            village_supervisor_subsidy = #{villageSupervisorSubsidy},
            subsidy_total = #{subsidyTotal},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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