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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizGrasslandUsageRecord" id="BizGrasslandUsageRecordResult">
        <id     property="id"                column="id"/>
        <result property="grasslandId"       column="grassland_id"/>
        <result property="pastureId"         column="pasture_id"/>
        <result property="usageAreaMu"       column="usage_area_mu"/>
        <result property="useStartDate"      column="use_start_date"/>
        <result property="useEndDate"        column="use_end_date"/>
        <result property="actualUseDays"     column="actual_use_days"/>
        <result property="grazingHeadCount"  column="grazing_head_count"/>
        <result property="grazingMethod"     column="grazing_method"/>
        <result property="delFlag"           column="del_flag"/>
        <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"/>
        <result property="grasslandName"     column="grassland_name"/>
        <result property="pastureName"       column="pasture_name"/>
    </resultMap>

    <sql id="selectVo">
        select r.id, r.grassland_id, r.pasture_id, r.usage_area_mu,
               r.use_start_date, r.use_end_date, r.actual_use_days,
               r.grazing_head_count, r.grazing_method, r.del_flag,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark,
               g.grassland_name, p.pasture_name
        from biz_grassland_usage_record r
        left join biz_grassland g on r.grassland_id = g.id
        left join biz_pasture p on r.pasture_id = p.id
    </sql>

    <select id="selectBizGrasslandUsageRecordById" parameterType="long" resultMap="BizGrasslandUsageRecordResult">
        <include refid="selectVo"/>
        where r.id = #{id} and r.del_flag = '0'
    </select>

    <select id="selectBizGrasslandUsageRecordList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizGrasslandUsageRecord" resultMap="BizGrasslandUsageRecordResult">
        <include refid="selectVo"/>
        <where>
            r.del_flag = '0'
            <if test="keyword != null and keyword != ''">
                and g.grassland_name like concat('%', #{keyword}, '%')
            </if>
            <if test="startDate != null and startDate != ''">
                and date_format(r.create_time, '%Y-%m-%d') &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and date_format(r.create_time, '%Y-%m-%d') &lt;= #{endDate}
            </if>
        </where>
        order by r.create_time desc, r.id desc
    </select>

    <insert id="insertBizGrasslandUsageRecord" parameterType="com.ruoyi.web.modules.industryservice.domain.BizGrasslandUsageRecord" useGeneratedKeys="true" keyProperty="id">
        insert into biz_grassland_usage_record (
            grassland_id, pasture_id, usage_area_mu,
            use_start_date, use_end_date, actual_use_days,
            grazing_head_count, grazing_method, del_flag,
            create_by, create_time, remark
        ) values (
            #{grasslandId}, #{pastureId}, #{usageAreaMu},
            #{useStartDate}, #{useEndDate}, #{actualUseDays},
            #{grazingHeadCount}, #{grazingMethod}, #{delFlag},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizGrasslandUsageRecord" parameterType="com.ruoyi.web.modules.industryservice.domain.BizGrasslandUsageRecord">
        update biz_grassland_usage_record set
            grassland_id = #{grasslandId},
            pasture_id = #{pastureId},
            usage_area_mu = #{usageAreaMu},
            use_start_date = #{useStartDate},
            use_end_date = #{useEndDate},
            actual_use_days = #{actualUseDays},
            grazing_head_count = #{grazingHeadCount},
            grazing_method = #{grazingMethod},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizGrasslandUsageRecordByIds">
        update biz_grassland_usage_record set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0' and id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
