<?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.zhyc.module.base.mapper.DaSheepfoldMapper">

    <resultMap type="com.zhyc.module.base.domain.DaSheepfold" id="DaSheepfoldResult">
        <result property="id" column="id"/>
        <result property="ranchId" column="ranch_id"/>
        <result property="sheepfoldName" column="sheepfold_name"/>
        <result property="sheepfoldTypeId" column="sheepfold_type_id"/>
        <result property="sheepfoldNo" column="sheepfold_no"/>
        <result property="rowNo" column="row_no"/>
        <result property="columns" column="columns"/>
        <result property="comment" column="comment"/>
    </resultMap>

    <sql id="selectDaSheepfoldVo">
        select id,
               ranch_id,
               sheepfold_name,
               sheepfold_type_id,
               sheepfold_no,
               row_no,
               columns,
               comment
        from da_sheepfold

    </sql>

    <select id="selectDaSheepfoldList" parameterType="DaSheepfold" resultMap="DaSheepfoldResult">
        <include refid="selectDaSheepfoldVo"/>
        <where>
            <if test="ranchId != null ">and ranch_id = #{ranchId}</if>
            <if test="sheepfoldTypeId != null ">and sheepfold_type_id = #{sheepfoldTypeId}</if>
        </where>

        order by
            SUBSTRING_INDEX(row_no, '-', 1),
            CAST(columns AS UNSIGNED)
    </select>

    <select id="selectDaSheepfoldById" parameterType="Long" resultMap="DaSheepfoldResult">
        <include refid="selectDaSheepfoldVo"/>
        where id = #{id}
    </select>

    <insert id="insertDaSheepfold" parameterType="DaSheepfold" useGeneratedKeys="true" keyProperty="id">
        insert into da_sheepfold
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ranchId != null">ranch_id,</if>
            <if test="sheepfoldName != null">sheepfold_name,</if>
            <if test="sheepfoldTypeId != null">sheepfold_type_id,</if>
            <if test="sheepfoldNo != null">sheepfold_no,</if>
            <if test="rowNo != null">row_no,</if>
            <if test="columns != null">columns,</if>
            <if test="comment != null">comment,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ranchId != null">#{ranchId},</if>
            <if test="sheepfoldName != null">#{sheepfoldName},</if>
            <if test="sheepfoldTypeId != null">#{sheepfoldTypeId},</if>
            <if test="sheepfoldNo != null">#{sheepfoldNo},</if>
            <if test="rowNo != null">#{rowNo},</if>
            <if test="columns != null">#{columns},</if>
            <if test="comment != null">#{comment},</if>
        </trim>
    </insert>

    <update id="updateDaSheepfold" parameterType="DaSheepfold">
        update da_sheepfold
        <trim prefix="SET" suffixOverrides=",">
            <if test="ranchId != null">ranch_id = #{ranchId},</if>
            <if test="sheepfoldName != null">sheepfold_name = #{sheepfoldName},</if>
            <if test="sheepfoldTypeId != null">sheepfold_type_id = #{sheepfoldTypeId},</if>
            <if test="sheepfoldNo != null">sheepfold_no = #{sheepfoldNo},</if>
            <if test="rowNo != null">row_no = #{rowNo},</if>
            <if test="columns != null">columns = #{columns},</if>
            <if test="comment != null">comment = #{comment},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDaSheepfoldById" parameterType="Long">
        delete
        from da_sheepfold
        where id = #{id}
    </delete>

    <delete id="deleteDaSheepfoldByIds" parameterType="String">
        delete from da_sheepfold where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCount" parameterType="DaSheepfold" resultType="int">
        SELECT COUNT(*)
        FROM da_sheepfold
        WHERE ranch_id = #{ranchId}
          AND sheepfold_type_id = #{sheepfoldTypeId}
          AND sheepfold_no = #{sheepfoldNo}
    </select>
</mapper>