<?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">
<!-- 有无畜户数据填报 biz_livestock_ownership_household -->
<mapper namespace="com.ruoyi.web.modules.industryservice.mapper.BizLivestockOwnershipHouseholdMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold"
               id="BizLivestockOwnershipHouseholdResult">
        <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="withLivestockHouseholdCount"     column="with_livestock_household_count"/>
        <result property="withLivestockPopulationCount"    column="with_livestock_population_count"/>
        <result property="withLivestockLaborCount"         column="with_livestock_labor_count"/>
        <result property="withLivestockAnimalCount"        column="with_livestock_animal_count"/>
        <result property="withoutLivestockHouseholdCount"  column="without_livestock_household_count"/>
        <result property="withoutLivestockPopulationCount" column="without_livestock_population_count"/>
        <result property="withoutLivestockLaborCount"      column="without_livestock_labor_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.with_livestock_household_count, r.with_livestock_population_count,
               r.with_livestock_labor_count, r.with_livestock_animal_count,
               r.without_livestock_household_count, r.without_livestock_population_count,
               r.without_livestock_labor_count,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_livestock_ownership_household r
    </sql>

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

    <select id="countByYearMonthAndVillageExcludeId" resultType="int">
        select count(1)
        from biz_livestock_ownership_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="BizLivestockOwnershipHouseholdResult">
        <include refid="selectVo"/>
        where r.stat_year = #{statYear}
          and r.stat_month = #{statMonth}
          and r.village_dept_id = #{villageDeptId}
        limit 1
    </select>

    <select id="selectBizLivestockOwnershipHouseholdList"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold"
            resultMap="BizLivestockOwnershipHouseholdResult">
        <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="insertBizLivestockOwnershipHousehold"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_livestock_ownership_household (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name,
            with_livestock_household_count, with_livestock_population_count,
            with_livestock_labor_count, with_livestock_animal_count,
            without_livestock_household_count, without_livestock_population_count,
            without_livestock_labor_count,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{withLivestockHouseholdCount}, #{withLivestockPopulationCount},
            #{withLivestockLaborCount}, #{withLivestockAnimalCount},
            #{withoutLivestockHouseholdCount}, #{withoutLivestockPopulationCount},
            #{withoutLivestockLaborCount},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizLivestockOwnershipHousehold"
            parameterType="com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold">
        update biz_livestock_ownership_household set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            with_livestock_household_count = #{withLivestockHouseholdCount},
            with_livestock_population_count = #{withLivestockPopulationCount},
            with_livestock_labor_count = #{withLivestockLaborCount},
            with_livestock_animal_count = #{withLivestockAnimalCount},
            without_livestock_household_count = #{withoutLivestockHouseholdCount},
            without_livestock_population_count = #{withoutLivestockPopulationCount},
            without_livestock_labor_count = #{withoutLivestockLaborCount},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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