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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizLivestockProductOutput" id="BizLivestockProductOutputResult">
        <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="meatOutput" column="meat_output"/>
        <result property="milkOutput" column="milk_output"/>
        <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.meat_output, r.milk_output,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_livestock_product_output r
    </sql>

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

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

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

    <select id="selectBizLivestockProductOutputList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockProductOutput"
            resultMap="BizLivestockProductOutputResult">
        <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="insertBizLivestockProductOutput" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockProductOutput"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_livestock_product_output (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name,
            meat_output, milk_output, create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{meatOutput}, #{milkOutput}, #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizLivestockProductOutput" parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockProductOutput">
        update biz_livestock_product_output set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            meat_output = #{meatOutput},
            milk_output = #{milkOutput},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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