<?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。
  唯一键：(stat_year, stat_month, village_dept_id)
-->
<mapper namespace="com.ruoyi.web.modules.industryservice.mapper.BizVaccinationDataMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizVaccinationData" id="BizVaccinationDataResult">
        <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="shouldVaccinateCount"   column="should_vaccinate_count"/>
        <result property="actualVaccinateCount"   column="actual_vaccinate_count"/>
        <result property="immunityRate"           column="immunity_rate"/>
        <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.should_vaccinate_count, r.actual_vaccinate_count, r.immunity_rate,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark
        from biz_vaccination_data r
    </sql>

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

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

    <select id="selectBizVaccinationDataList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizVaccinationData"
            resultMap="BizVaccinationDataResult">
        <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="insertBizVaccinationData" parameterType="com.ruoyi.web.modules.industryservice.domain.BizVaccinationData"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_vaccination_data (
            stat_year, stat_month, town_dept_id, town_name, village_dept_id, village_name,
            should_vaccinate_count, actual_vaccinate_count, immunity_rate,
            create_by, create_time, remark
        ) values (
            #{statYear}, #{statMonth}, #{townDeptId}, #{townName}, #{villageDeptId}, #{villageName},
            #{shouldVaccinateCount}, #{actualVaccinateCount}, #{immunityRate},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizVaccinationData" parameterType="com.ruoyi.web.modules.industryservice.domain.BizVaccinationData">
        update biz_vaccination_data set
            stat_year = #{statYear},
            stat_month = #{statMonth},
            town_dept_id = #{townDeptId},
            town_name = #{townName},
            village_dept_id = #{villageDeptId},
            village_name = #{villageName},
            should_vaccinate_count = #{shouldVaccinateCount},
            actual_vaccinate_count = #{actualVaccinateCount},
            immunity_rate = #{immunityRate},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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