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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizPasture" id="BizPastureResult">
        <id     property="id"              column="id"/>
        <result property="externalId"      column="external_id"/>
        <result property="pastureName"     column="pasture_name"/>
        <result property="farmType"        column="farm_type"/>
        <result property="introduction"    column="introduction"/>
        <result property="latitude"        column="latitude"/>
        <result property="longitude"       column="longitude"/>
        <result property="county"          column="county"/>
        <result property="town"            column="town"/>
        <result property="floorArea"       column="floor_area"/>
        <result property="scaleBreeding"   column="scale_breeding"/>
        <result property="breedSpecies"    column="breed_species"/>
        <result property="detailAddress"   column="detail_address"/>
        <result property="personInCharge"  column="person_in_charge"/>
        <result property="contactPhone"    column="contact_phone"/>
        <result property="dataSource"      column="data_source"/>
        <result property="lastSyncTime"    column="last_sync_time"/>
        <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"/>
    </resultMap>

    <sql id="selectVo">
        select id, external_id, pasture_name, farm_type, introduction, latitude, longitude,
               county, town, floor_area, scale_breeding, breed_species,
               detail_address, person_in_charge, contact_phone,
               data_source, last_sync_time, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_pasture
    </sql>

    <select id="selectBizPastureById" parameterType="long" resultMap="BizPastureResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizPastureByIdAny" parameterType="long" resultMap="BizPastureResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectBizPastureByExactName" resultMap="BizPastureResult">
        <include refid="selectVo"/>
        where del_flag = '0' and pasture_name = #{pastureName}
        limit 1
    </select>

    <select id="selectBizPastureByExternalId" resultMap="BizPastureResult">
        <include refid="selectVo"/>
        where external_id = #{externalId}
        limit 1
    </select>

    <select id="selectBizPastureList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizPasture" resultMap="BizPastureResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="keyword != null and keyword != ''">
                and pasture_name like concat('%', #{keyword}, '%')
            </if>
            <if test="farmType != null and farmType != ''">
                and farm_type = #{farmType}
            </if>
        </where>
        order by create_time desc, id desc
    </select>

    <insert id="insertBizPasture" parameterType="com.ruoyi.web.modules.industryservice.domain.BizPasture" useGeneratedKeys="true" keyProperty="id">
        insert into biz_pasture (
            external_id, pasture_name, farm_type, introduction, latitude, longitude,
            county, town, floor_area, scale_breeding, breed_species,
            detail_address, person_in_charge, contact_phone,
            data_source, last_sync_time, del_flag,
            create_by, create_time, remark
        ) values (
            #{externalId}, #{pastureName}, #{farmType}, #{introduction}, #{latitude}, #{longitude},
            #{county}, #{town}, #{floorArea}, #{scaleBreeding}, #{breedSpecies},
            #{detailAddress}, #{personInCharge}, #{contactPhone},
            #{dataSource}, #{lastSyncTime}, #{delFlag},
            #{createBy}, #{createTime, jdbcType=TIMESTAMP}, #{remark}
        )
    </insert>

    <update id="updateBizPastureFromSync" parameterType="com.ruoyi.web.modules.industryservice.domain.BizPasture">
        update biz_pasture set
            external_id = #{externalId},
            pasture_name = #{pastureName},
            farm_type = #{farmType},
            introduction = #{introduction},
            latitude = #{latitude},
            longitude = #{longitude},
            county = #{county},
            town = #{town},
            floor_area = #{floorArea},
            scale_breeding = #{scaleBreeding},
            breed_species = #{breedSpecies},
            detail_address = #{detailAddress},
            person_in_charge = #{personInCharge},
            contact_phone = #{contactPhone},
            data_source = #{dataSource},
            last_sync_time = #{lastSyncTime},
            del_flag = #{delFlag},
            update_by = #{updateBy},
            update_time = #{updateTime, jdbcType=TIMESTAMP},
            remark = #{remark}
        where id = #{id}
    </update>
</mapper>
