<?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.pcloud.book.cultivate.dao.impl.CultivateRaysRecordDaoImpl">

    <resultMap type="com.pcloud.book.cultivate.entity.CultivateRaysRecord" id="CultivateRaysRecordMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="cultivateId" column="cultivate_id" jdbcType="INTEGER"/>
        <result property="changeType" column="change_type" jdbcType="INTEGER"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="gainsType" column="gains_type" jdbcType="INTEGER"/>
        <result property="hours" column="hours" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List" >
        id, cultivate_id, change_type, remark, gains_type, create_time, hours
    </sql>

    <!--查询单个-->
    <select id="getById" resultMap="CultivateRaysRecordMap">
        select
          <include refid="Base_Column_List"/>
        from book.cultivate_rays_record
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.cultivate_rays_record(cultivate_id, change_type, remark, gains_type, create_time, hours)
        values (#{cultivateId}, #{changeType}, #{remark}, #{gainsType}, now(), #{hours})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.cultivate_rays_record
        <set>
            <if test="cultivateId != null">
                cultivate_id = #{cultivateId},
            </if>
            <if test="changeType != null">
                change_type = #{changeType},
            </if>
            <if test="remark != null and remark != ''">
                remark = #{remark},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.cultivate_rays_record where id = #{id}
    </delete>

    <select id="getLastRecord" parameterType="map" resultMap="CultivateRaysRecordMap">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            cultivate_rays_record
        WHERE
            cultivate_id = #{cultivateId}
        AND change_type = #{changeType}
        ORDER BY
            create_time DESC
        LIMIT 1
    </select>

</mapper>