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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport" id="BizYakOutboundReportResult">
        <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="farmerHouseholdCount"         column="farmer_household_count"/>
        <result property="farmerPopulationCount"        column="farmer_population_count"/>
        <result property="yakOutboundCount"             column="yak_outbound_count"/>
        <result property="cattleOutboundCount"          column="cattle_outbound_count"/>
        <result property="sheepOutboundCount"           column="sheep_outbound_count"/>
        <result property="goatOutboundCount"            column="goat_outbound_count"/>
        <result property="horseMuleOutboundCount"       column="horse_mule_outbound_count"/>
        <result property="donkeyOutboundCount"          column="donkey_outbound_count"/>
        <result property="selfConsumptionCattleCount"   column="self_consumption_cattle_count"/>
        <result property="selfConsumptionSheepCount"    column="self_consumption_sheep_count"/>
        <result property="selfConsumptionGoatCount"     column="self_consumption_goat_count"/>
        <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.farmer_household_count, r.farmer_population_count,
               r.yak_outbound_count, r.cattle_outbound_count,
               r.sheep_outbound_count, r.goat_outbound_count,
               r.horse_mule_outbound_count, r.donkey_outbound_count,
               r.self_consumption_cattle_count, r.self_consumption_sheep_count,
               r.self_consumption_goat_count,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_yak_outbound_report r
    </sql>

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

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

    <select id="selectBizYakOutboundReportList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport"
            resultMap="BizYakOutboundReportResult">
        <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="insertBizYakOutboundReport" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_yak_outbound_report (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name,
            farmer_household_count, farmer_population_count,
            yak_outbound_count, cattle_outbound_count,
            sheep_outbound_count, goat_outbound_count,
            horse_mule_outbound_count, donkey_outbound_count,
            self_consumption_cattle_count, self_consumption_sheep_count, self_consumption_goat_count,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{farmerHouseholdCount}, #{farmerPopulationCount},
            #{yakOutboundCount}, #{cattleOutboundCount},
            #{sheepOutboundCount}, #{goatOutboundCount},
            #{horseMuleOutboundCount}, #{donkeyOutboundCount},
            #{selfConsumptionCattleCount}, #{selfConsumptionSheepCount}, #{selfConsumptionGoatCount},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizYakOutboundReport" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport">
        update biz_yak_outbound_report set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            farmer_household_count = #{farmerHouseholdCount},
            farmer_population_count = #{farmerPopulationCount},
            yak_outbound_count = #{yakOutboundCount},
            cattle_outbound_count = #{cattleOutboundCount},
            sheep_outbound_count = #{sheepOutboundCount},
            goat_outbound_count = #{goatOutboundCount},
            horse_mule_outbound_count = #{horseMuleOutboundCount},
            donkey_outbound_count = #{donkeyOutboundCount},
            self_consumption_cattle_count = #{selfConsumptionCattleCount},
            self_consumption_sheep_count = #{selfConsumptionSheepCount},
            self_consumption_goat_count = #{selfConsumptionGoatCount},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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

    <delete id="deleteByYearMonth">
        delete from biz_yak_outbound_report
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
    </delete>
</mapper>
