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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizYakHerdInventory" id="BizYakHerdInventoryResult">
        <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="villageCount"           column="village_count"/>
        <result property="herdsmanHouseholdCount" column="herdsman_household_count"/>
        <result property="breedingBullCount"      column="breeding_bull_count"/>
        <result property="bullCastratedCount"     column="bull_castrated_count"/>
        <result property="bullTotal"              column="bull_total"/>
        <result property="cowYoungCount"          column="cow_young_count"/>
        <result property="cowAdultCount"          column="cow_adult_count"/>
        <result property="cowTotal"               column="cow_total"/>
        <result property="milkingCowCount"        column="milking_cow_count"/>
        <result property="fertileCowCount"        column="fertile_cow_count"/>
        <result property="dairyTotal"             column="dairy_total"/>
        <result property="sheepTotal"             column="sheep_total"/>
        <result property="milkingSheepCount"      column="milking_sheep_count"/>
        <result property="insuredYakCount"        column="insured_yak_count"/>
        <result property="yakTotal"               column="yak_total"/>
        <result property="grandTotal"             column="grand_total"/>
        <result property="yakSheepTotal"          column="yak_sheep_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 h.id, h.stat_year, h.stat_month, h.town_dept_id, h.town_name,
               h.village_dept_id, h.village_name,
               h.village_count, h.herdsman_household_count,
               h.breeding_bull_count, h.bull_castrated_count, h.bull_total,
               h.cow_young_count, h.cow_adult_count, h.cow_total,
               h.milking_cow_count, h.fertile_cow_count, h.dairy_total,
               h.sheep_total, h.milking_sheep_count, h.insured_yak_count,
               h.yak_total, h.grand_total, h.yak_sheep_total,
               h.create_by, h.create_time, h.update_by, h.update_time, h.remark
        from biz_yak_herd_inventory h
    </sql>

    <select id="selectBizYakHerdInventoryById" parameterType="long" resultMap="BizYakHerdInventoryResult">
        <include refid="selectVo"/>
        where h.id = #{id}
    </select>

    <select id="selectByYearMonthAndTown" resultMap="BizYakHerdInventoryResult">
        <include refid="selectVo"/>
        where h.stat_year = #{statYear}
          and h.stat_month = #{statMonth}
          and h.town_dept_id = #{townDeptId}
        limit 1
    </select>

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

    <select id="countByYearMonthAndTownExcludeId" resultType="int">
        select count(1)
        from biz_yak_herd_inventory
        where stat_year = #{statYear}
          and stat_month = #{statMonth}
          and town_dept_id = #{townDeptId}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

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

    <select id="selectBizYakHerdInventoryList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakHerdInventory"
            resultMap="BizYakHerdInventoryResult">
        <include refid="selectVo"/>
        left join sys_dept d on h.town_dept_id = d.dept_id
        left join sys_dept v on h.village_dept_id = v.dept_id
        <where>
            <if test="statYear != null">
                and h.stat_year = #{statYear}
            </if>
            <if test="statMonth != null">
                and h.stat_month = #{statMonth}
            </if>
            <if test="townDeptId != null">
                and h.town_dept_id = #{townDeptId}
            </if>
            <if test="villageDeptId != null">
                and h.village_dept_id = #{villageDeptId}
            </if>
        </where>
        order by h.stat_year desc, h.stat_month desc, d.order_num asc, v.order_num asc, h.id desc
    </select>

    <insert id="insertBizYakHerdInventory" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakHerdInventory"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_yak_herd_inventory (
            stat_year, stat_month, town_dept_id, town_name,
            village_dept_id, village_name,
            village_count, herdsman_household_count,
            breeding_bull_count, bull_castrated_count, bull_total,
            cow_young_count, cow_adult_count, cow_total,
            milking_cow_count, fertile_cow_count, dairy_total,
            sheep_total, milking_sheep_count, insured_yak_count,
            yak_total, grand_total, yak_sheep_total,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName},
            #{villageDeptId}, #{villageName},
            #{villageCount}, #{herdsmanHouseholdCount},
            #{breedingBullCount}, #{bullCastratedCount}, #{bullTotal},
            #{cowYoungCount}, #{cowAdultCount}, #{cowTotal},
            #{milkingCowCount}, #{fertileCowCount}, #{dairyTotal},
            #{sheepTotal}, #{milkingSheepCount}, #{insuredYakCount},
            #{yakTotal}, #{grandTotal}, #{yakSheepTotal},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizYakHerdInventory" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakHerdInventory">
        update biz_yak_herd_inventory set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            village_count = #{villageCount},
            herdsman_household_count = #{herdsmanHouseholdCount},
            breeding_bull_count = #{breedingBullCount},
            bull_castrated_count = #{bullCastratedCount},
            bull_total = #{bullTotal},
            cow_young_count = #{cowYoungCount},
            cow_adult_count = #{cowAdultCount},
            cow_total = #{cowTotal},
            milking_cow_count = #{milkingCowCount},
            fertile_cow_count = #{fertileCowCount},
            dairy_total = #{dairyTotal},
            sheep_total = #{sheepTotal},
            milking_sheep_count = #{milkingSheepCount},
            insured_yak_count = #{insuredYakCount},
            yak_total = #{yakTotal},
            grand_total = #{grandTotal},
            yak_sheep_total = #{yakSheepTotal},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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

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